Enable Console Completion only on Tab (#1558)

This commit is contained in:
Florian Märkl 2019-05-19 16:24:06 +02:00 committed by GitHub
parent 7e8eb9c393
commit d32e3fa20f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -52,6 +52,7 @@ ConsoleWidget::ConsoleWidget(MainWindow *main, QAction *action) :
actions.append(actionWrapLines);
// Completion
completionActive = false;
completer = new QCompleter(&completionModel, this);
completer->setMaxVisibleItems(20);
completer->setCaseSensitivity(Qt::CaseInsensitive);
@ -80,6 +81,11 @@ ConsoleWidget::ConsoleWidget(MainWindow *main, QAction *action) :
connect(historyDownShortcut, SIGNAL(activated()), this, SLOT(historyNext()));
historyDownShortcut->setContext(Qt::WidgetShortcut);
QShortcut *completionShortcut = new QShortcut(QKeySequence(Qt::Key_Tab), ui->inputLineEdit);
connect(completionShortcut, &QShortcut::activated, this, &ConsoleWidget::triggerCompletion);
connect(ui->inputLineEdit, &QLineEdit::editingFinished, this, &ConsoleWidget::disableCompletion);
connect(Config(), SIGNAL(fontsUpdated()), this, SLOT(setupFont()));
completer->popup()->installEventFilter(this);
@ -246,8 +252,33 @@ void ConsoleWidget::historyPrev()
}
}
void ConsoleWidget::triggerCompletion()
{
if (completionActive) {
return;
}
completionActive = true;
updateCompletion();
completer->complete();
}
void ConsoleWidget::disableCompletion()
{
if (!completionActive) {
return;
}
completionActive = false;
updateCompletion();
completer->popup()->hide();
}
void ConsoleWidget::updateCompletion()
{
if (!completionActive) {
completionModel.setStringList({});
return;
}
auto current = ui->inputLineEdit->text();
auto completions = Core()->autocomplete(current, R_LINE_PROMPT_DEFAULT);
int lastSpace = current.lastIndexOf(' ');
@ -262,6 +293,7 @@ void ConsoleWidget::updateCompletion()
void ConsoleWidget::clear()
{
disableCompletion();
ui->inputLineEdit->clear();
invalidateHistoryPosition();

View File

@ -56,6 +56,8 @@ private slots:
void historyNext();
void historyPrev();
void triggerCompletion();
void disableCompletion();
void updateCompletion();
void clear();
@ -77,6 +79,7 @@ private:
int maxHistoryEntries;
int lastHistoryPosition;
QStringList history;
bool completionActive;
QStringListModel completionModel;
QCompleter *completer;
QShortcut *historyUpShortcut;