-
Notifications
You must be signed in to change notification settings - Fork 1
/
clangcompleter.cpp
84 lines (74 loc) · 2.27 KB
/
clangcompleter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "clangcompleter.h"
#include "ui_clangcompleter.h"
#include <iostream>
#include <QCompleter>
#include <QProcess>
#include <QDirModel>
ClangCompleter::ClangCompleter(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ClangCompleter)
{
ui->setupUi(this);
QCompleter *completer = new QCompleter(this);
completer->setModel(new QDirModel(completer));
ui->clangBinary->setCompleter(completer);
}
ClangCompleter::~ClangCompleter()
{
delete ui;
}
void ClangCompleter::on_clangBinary_textEdited(const QString &arg1)
{
}
QStringList ClangCompleter::getCompletions(QString arg) {
bool completeValue = false;
QString origArg = arg;
QString value;
if (arg == "")
arg = "#";
if (arg.endsWith("=")) {
arg += ",";
completeValue = true;
} else if (arg.contains("=")) {
QStringList parts = arg.split("=");
origArg = parts.front() + "=";
parts.pop_front();
arg = origArg + "," + parts.front();
}
QProcess pingProcess;
QString exec = ui->clangBinary->text();
QStringList params;
std::cerr << "Completing '" << arg.toStdString() << "'\n";
params << QString("--autocomplete=" + arg);
pingProcess.execute(exec, params);
pingProcess.start(exec, params);
pingProcess.waitForFinished();
QString output(pingProcess.readAllStandardOutput());
QStringList result = output.split("\n");
if (completeValue)
for (QString &completion : result)
completion = origArg + completion;
return result;
}
void ClangCompleter::on_flags_textEdited(const QString &arg1)
{
QStringList args = arg1.split(" ");
if (args.empty()) {
return;
}
QString toComplete = args.back();
args.pop_back();
QString prefix = args.join(" ");
QStringList wordList = getCompletions(toComplete);
for (QString &completion : wordList) {
QStringList list = completion.split("\t");
//std::cerr << "'" << prefix.toStdString() << "'" << std::endl;
completion = prefix + (args.empty() ? "" : " ") + list.front();
}
QCompleter *completer = new QCompleter(wordList, this);
completer->setCaseSensitivity(Qt::CaseInsensitive);
//completer->setCompletionMode(QCompleter::CompletionMode::UnfilteredPopupCompletion);
completer->setFilterMode(Qt::MatchStartsWith);
ui->flags->setCompleter(completer);
ui->flags->completer()->complete();
}