move processing of completion to separate function

This commit is contained in:
Axel Kohlmeyer
2023-09-01 02:52:26 -04:00
parent 9690d2dc02
commit b478888ede
2 changed files with 28 additions and 22 deletions

View File

@ -309,28 +309,7 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
return;
}
if (event->key() == Qt::Key_Backtab) {
if (command_completer) {
auto cursor = textCursor();
auto line = cursor.block().text().trimmed();
if (line.isEmpty()) return;
auto words = split_line(line.toStdString());
cursor.select(QTextCursor::WordUnderCursor);
// if on first word, try to complete command
if ((words.size() > 0) && (words[0] == cursor.selectedText().toStdString())) {
// no completion on comment lines
if (words[0][0] == '#') return;
command_completer->setCompletionPrefix(words[0].c_str());
auto popup = command_completer->popup();
QRect cr = cursorRect();
cr.setWidth(popup->sizeHintForColumn(0) +
popup->verticalScrollBar()->sizeHint().width());
popup->setAlternatingRowColors(true);
command_completer->setCurrentRow(0);
command_completer->complete(cr);
}
}
runCompletion();
return;
}
QPlainTextEdit::keyPressEvent(event);
@ -479,6 +458,32 @@ void CodeEditor::reformatCurrentLine()
}
}
void CodeEditor::runCompletion()
{
if (command_completer) {
auto cursor = textCursor();
auto line = cursor.block().text().trimmed();
if (line.isEmpty()) return;
auto words = split_line(line.toStdString());
cursor.select(QTextCursor::WordUnderCursor);
// if on first word, try to complete command
if ((words.size() > 0) && (words[0] == cursor.selectedText().toStdString())) {
// no completion on comment lines
if (words[0][0] == '#') return;
command_completer->setCompletionPrefix(words[0].c_str());
auto popup = command_completer->popup();
QRect cr = cursorRect();
cr.setWidth(popup->sizeHintForColumn(0) +
popup->verticalScrollBar()->sizeHint().width());
popup->setAlternatingRowColors(true);
command_completer->complete(cr);
}
}
}
void CodeEditor::insertCompletedCommand(const QString &completion)
{
if (command_completer->widget() != this) return;

View File

@ -53,6 +53,7 @@ private slots:
void find_help(QString &page, QString &help);
void open_help();
void reformatCurrentLine();
void runCompletion();
void insertCompletedCommand(const QString &completion);
private: