Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
clang-autocomplete-qt-demo/clangcompleter.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
84 lines (74 sloc)
2.27 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} |