From kde-commits Fri Jun 17 08:18:10 2016 From: Simon St James Date: Fri, 17 Jun 2016 08:18:10 +0000 To: kde-commits Subject: [ktexteditor] src/vimode: Move all the completion-related stuff into new Completer class. Message-Id: X-MARC-Message: https://marc.info/?l=kde-commits&m=146615202422733 Git commit 3b1a97db473b4e4855ea5f9ead4bf1ba0eebe0bb by Simon St James. Committed on 17/06/2016 at 08:14. Pushed by sstjames into branch 'master'. Move all the completion-related stuff into new Completer class. M +197 -191 src/vimode/emulatedcommandbar.cpp M +35 -24 src/vimode/emulatedcommandbar.h http://commits.kde.org/ktexteditor/3b1a97db473b4e4855ea5f9ead4bf1ba0eebe0bb diff --git a/src/vimode/emulatedcommandbar.cpp b/src/vimode/emulatedcommand= bar.cpp index 7f9a9fa..a868873 100644 --- a/src/vimode/emulatedcommandbar.cpp +++ b/src/vimode/emulatedcommandbar.cpp @@ -300,8 +300,7 @@ QString withSearchConfigRemoved(const QString &original= SearchText, const bool is EmulatedCommandBar::EmulatedCommandBar(InputModeManager *viInputModeManage= r, QWidget *parent) : KateViewBarWidget(false, parent) , m_viInputModeManager(viInputModeManager) - , m_view(viInputModeManager->view()) -{ + , m_view(viInputModeManager->view()){ QHBoxLayout *layout =3D new QHBoxLayout(); layout->setMargin(0); centralWidget()->setLayout(layout); @@ -332,23 +331,17 @@ EmulatedCommandBar::EmulatedCommandBar(InputModeManag= er *viInputModeManager, QWi m_interactiveSedReplaceMode.reset(new InteractiveSedReplaceMode(this, = m_matchHighligher.data())); layout->addWidget(m_interactiveSedReplaceMode->label()); = + m_completerTmp.reset(new Completer(this, m_view, m_edit)); + m_searchMode.reset(new SearchMode(this, m_matchHighligher.data(), m_vi= ew, m_edit)); m_searchMode->setViInputModeManager(viInputModeManager); = - m_commandMode.reset(new CommandMode(this, m_matchHighligher.data(), m_= view, m_edit, m_interactiveSedReplaceMode.data())); + + m_commandMode.reset(new CommandMode(this, m_matchHighligher.data(), m_= view, m_edit, m_interactiveSedReplaceMode.data(), m_completerTmp.data())); = m_edit->installEventFilter(this); connect(m_edit, SIGNAL(textChanged(QString)), this, SLOT(editTextChang= ed(QString))); = - m_completer =3D new QCompleter(QStringList(), m_edit); - // Can't find a way to stop the QCompleter from auto-completing when a= ttached to a QLineEdit, - // so don't actually set it as the QLineEdit's completer. - m_completer->setWidget(m_edit); - m_completer->setObjectName(QStringLiteral("completer")); - m_completionModel =3D new QStringListModel(this); - m_completer->setModel(m_completionModel); - m_completer->setCaseSensitivity(Qt::CaseInsensitive); - m_completer->popup()->installEventFilter(this); = m_exitStatusMessageDisplayHideTimer =3D new QTimer(this); m_exitStatusMessageDisplayHideTimer->setSingleShot(true); @@ -369,7 +362,7 @@ EmulatedCommandBar::~EmulatedCommandBar() = void EmulatedCommandBar::init(EmulatedCommandBar::Mode mode, const QString= &initialText) { - m_currentCompletionType =3D None; + m_completerTmp->m_currentCompletionType =3D None; m_mode =3D mode; m_isActive =3D true; m_wasAborted =3D true; @@ -407,7 +400,7 @@ void EmulatedCommandBar::setCommandResponseMessageTimeo= ut(long int commandRespon void EmulatedCommandBar::closed() { m_matchHighligher->updateMatchHighlight(KTextEditor::Range::invalid()); - m_completer->popup()->hide(); + m_completerTmp->m_completer->popup()->hide(); m_isActive =3D false; m_interactiveSedReplaceMode->deactivate(); = @@ -432,67 +425,69 @@ void EmulatedCommandBar::updateMatchHighlightAttrib() = bool EmulatedCommandBar::completerHandledKeypress ( const QKeyEvent* keyEv= ent ) { + if (!m_edit->isVisible()) + return false; if (keyEvent->modifiers() =3D=3D Qt::ControlModifier && (keyEvent->key= () =3D=3D Qt::Key_C || keyEvent->key() =3D=3D Qt::Key_BracketLeft)) { - if (m_currentCompletionType !=3D None && m_completer->popup()->isV= isible()) + if (m_completerTmp->m_currentCompletionType !=3D None && m_complet= erTmp->m_completer->popup()->isVisible()) { - abortCompletionAndResetToPreCompletion(); + m_completerTmp->abortCompletionAndResetToPreCompletion(); return true; } } if (keyEvent->modifiers() =3D=3D Qt::ControlModifier && keyEvent->key(= ) =3D=3D Qt::Key_Space) { - CompletionStartParams completionStartParams =3D activateWordFromDo= cumentCompletion(); - startCompletion(completionStartParams); + CompletionStartParams completionStartParams =3D m_completerTmp->ac= tivateWordFromDocumentCompletion(); + m_completerTmp->startCompletion(completionStartParams); return true; } if ((keyEvent->modifiers() =3D=3D Qt::ControlModifier && keyEvent->key= () =3D=3D Qt::Key_P) || keyEvent->key() =3D=3D Qt::Key_Down) { - if (!m_completer->popup()->isVisible()) { + if (!m_completerTmp->m_completer->popup()->isVisible()) { CompletionStartParams completionStartParams; if (m_mode =3D=3D Command) { completionStartParams =3D m_commandMode->completionInvoked= (CompletionInvocation::ExtraContext); } else { completionStartParams =3D m_searchMode->completionInvoked(= CompletionInvocation::ExtraContext); } - startCompletion(completionStartParams); - if (m_currentCompletionType !=3D None) { - setCompletionIndex(0); + m_completerTmp->startCompletion(completionStartParams); + if (m_completerTmp->m_currentCompletionType !=3D None) { + m_completerTmp->setCompletionIndex(0); } } else { // Descend to next row, wrapping around if necessary. - if (m_completer->currentRow() + 1 =3D=3D m_completer->completi= onCount()) { - setCompletionIndex(0); + if (m_completerTmp->m_completer->currentRow() + 1 =3D=3D m_com= pleterTmp->m_completer->completionCount()) { + m_completerTmp->setCompletionIndex(0); } else { - setCompletionIndex(m_completer->currentRow() + 1); + m_completerTmp->setCompletionIndex(m_completerTmp->m_compl= eter->currentRow() + 1); } } return true; } if ((keyEvent->modifiers() =3D=3D Qt::ControlModifier && keyEvent->key= () =3D=3D Qt::Key_N) || keyEvent->key() =3D=3D Qt::Key_Up) { - if (!m_completer->popup()->isVisible()) { + if (!m_completerTmp->m_completer->popup()->isVisible()) { CompletionStartParams completionStartParams; if (m_mode =3D=3D Command) { completionStartParams =3D m_commandMode->completionInvoked= (CompletionInvocation::NormalContext); } else { completionStartParams =3D m_searchMode->completionInvoked(= CompletionInvocation::NormalContext); } - startCompletion(completionStartParams); - setCompletionIndex(m_completer->completionCount() - 1); + m_completerTmp->startCompletion(completionStartParams); + m_completerTmp->setCompletionIndex(m_completerTmp->m_completer= ->completionCount() - 1); } else { // Ascend to previous row, wrapping around if necessary. - if (m_completer->currentRow() =3D=3D 0) { - setCompletionIndex(m_completer->completionCount() - 1); + if (m_completerTmp->m_completer->currentRow() =3D=3D 0) { + m_completerTmp->setCompletionIndex(m_completerTmp->m_compl= eter->completionCount() - 1); } else { - setCompletionIndex(m_completer->currentRow() - 1); + m_completerTmp->setCompletionIndex(m_completerTmp->m_compl= eter->currentRow() - 1); } } return true; } if (keyEvent->key() =3D=3D Qt::Key_Enter || keyEvent->key() =3D=3D Qt:= :Key_Return) { - if (m_completer->popup()->isVisible() && m_currentCompletionType = =3D=3D EmulatedCommandBar::WordFromDocument) { - deactivateCompletion(); + if (m_completerTmp->m_completer->popup()->isVisible() && m_complet= erTmp->m_currentCompletionType =3D=3D EmulatedCommandBar::WordFromDocument)= { + m_completerTmp->deactivateCompletion(); } else { m_wasAborted =3D false; - deactivateCompletion(); + m_completerTmp->deactivateCompletion(); if (m_mode =3D=3D Command) { m_commandMode->completionChosen(); } else { @@ -563,7 +558,7 @@ void EmulatedCommandBar::insertRegisterContents(const Q= KeyEvent *keyEvent) = bool EmulatedCommandBar::eventFilter(QObject *object, QEvent *event) { - Q_ASSERT(object =3D=3D m_edit || object =3D=3D m_completer->popup()); + Q_ASSERT(object =3D=3D m_edit || object =3D=3D m_completerTmp->m_compl= eter->popup()); if (m_suspendEditEventFiltering) { return false; } @@ -610,124 +605,6 @@ bool EmulatedCommandBar::deleteNonWordCharsToLeftOfCu= rsor() return deletionsMade; } = -QString EmulatedCommandBar::wordBeforeCursor() -{ - const int wordBeforeCursorBegin =3D this->wordBeforeCursorBegin(); - return m_edit->text().mid(wordBeforeCursorBegin, m_edit->cursorPositio= n() - wordBeforeCursorBegin); -} - -int EmulatedCommandBar::wordBeforeCursorBegin() -{ - int wordBeforeCursorBegin =3D m_edit->cursorPosition() - 1; - while (wordBeforeCursorBegin >=3D 0 && (m_edit->text()[wordBeforeCurso= rBegin].isLetterOrNumber() || m_edit->text()[wordBeforeCursorBegin] =3D=3D = QLatin1Char('_'))) { - wordBeforeCursorBegin--; - } - wordBeforeCursorBegin++; - return wordBeforeCursorBegin; -} - - -void EmulatedCommandBar::replaceWordBeforeCursorWith(const QString &newWor= d) -{ - const int wordBeforeCursorStart =3D m_edit->cursorPosition() - wordBef= oreCursor().length(); - const QString newText =3D m_edit->text().left(m_edit->cursorPosition()= - wordBeforeCursor().length()) + - newWord + - m_edit->text().mid(m_edit->cursorPosition()); - m_edit->setText(newText); - m_edit->setCursorPosition(wordBeforeCursorStart + newWord.length()); -} - -EmulatedCommandBar::CompletionStartParams EmulatedCommandBar::activateWord= FromDocumentCompletion() -{ - m_currentCompletionType =3D WordFromDocument; - QRegExp wordRegEx(QLatin1String("\\w{1,}")); - QStringList foundWords; - // Narrow the range of lines we search around the cursor so that we do= n't die on huge files. - const int startLine =3D qMax(0, m_view->cursorPosition().line() - 4096= ); - const int endLine =3D qMin(m_view->document()->lines(), m_view->cursor= Position().line() + 4096); - for (int lineNum =3D startLine; lineNum < endLine; lineNum++) { - const QString line =3D m_view->document()->line(lineNum); - int wordSearchBeginPos =3D 0; - while (wordRegEx.indexIn(line, wordSearchBeginPos) !=3D -1) { - const QString foundWord =3D wordRegEx.cap(0); - foundWords << foundWord; - wordSearchBeginPos =3D wordRegEx.indexIn(line, wordSearchBegin= Pos) + wordRegEx.matchedLength(); - } - } - foundWords =3D QSet::fromList(foundWords).toList(); - qSort(foundWords.begin(), foundWords.end(), caseInsensitiveLessThan); - CompletionStartParams completionStartParams; - completionStartParams.shouldStart =3D true; - completionStartParams.completions =3D foundWords; - completionStartParams.wordStartPos =3D wordBeforeCursorBegin(); - return completionStartParams; -} - -void EmulatedCommandBar::startCompletion ( const EmulatedCommandBar::Compl= etionStartParams& completionStartParams ) -{ - if (completionStartParams.shouldStart) - { - m_completionModel->setStringList(completionStartParams.completions= ); - const QString completionPrefix =3D m_edit->text().mid(completionSt= artParams.wordStartPos, m_edit->cursorPosition() - completionStartParams.wo= rdStartPos); - m_completer->setCompletionPrefix(completionPrefix); - m_completer->complete(); - m_currentCompletionStartParams =3D completionStartParams; - } -} - -void EmulatedCommandBar::deactivateCompletion() -{ - m_completer->popup()->hide(); - m_currentCompletionType =3D None; -} - -void EmulatedCommandBar::abortCompletionAndResetToPreCompletion() -{ - deactivateCompletion(); - m_isNextTextChangeDueToCompletionChange =3D true; - m_edit->setText(m_textToRevertToIfCompletionAborted); - m_edit->setCursorPosition(m_cursorPosToRevertToIfCompletionAborted); - m_isNextTextChangeDueToCompletionChange =3D false; -} - -void EmulatedCommandBar::updateCompletionPrefix() -{ - const QString completionPrefix =3D m_edit->text().mid(m_currentComplet= ionStartParams.wordStartPos, m_edit->cursorPosition() - m_currentCompletion= StartParams.wordStartPos); - m_completer->setCompletionPrefix(completionPrefix); - // Seem to need a call to complete() else the size of the popup box is= not altered appropriately. - m_completer->complete(); -} - -void EmulatedCommandBar::currentCompletionChanged() -{ - const QString newCompletion =3D m_completer->currentCompletion(); - if (newCompletion.isEmpty()) { - return; - } - QString transformedCompletion =3D newCompletion; - if (m_currentCompletionStartParams.completionTransform) - { - transformedCompletion =3D m_currentCompletionStartParams.completio= nTransform(newCompletion); - } - - m_isNextTextChangeDueToCompletionChange =3D true; - m_edit->setSelection(m_currentCompletionStartParams.wordStartPos, m_ed= it->cursorPosition() - m_currentCompletionStartParams.wordStartPos); - m_edit->insert(transformedCompletion); - m_isNextTextChangeDueToCompletionChange =3D false; -} - -void EmulatedCommandBar::setCompletionIndex(int index) -{ - const QModelIndex modelIndex =3D m_completer->popup()->model()->index(= index, 0); - // Need to set both of these, for some reason. - m_completer->popup()->setCurrentIndex(modelIndex); - m_completer->setCurrentRow(index); - - m_completer->popup()->scrollTo(modelIndex); - - currentCompletionChanged(); -} - bool EmulatedCommandBar::handleKeyPress(const QKeyEvent *keyEvent) { if (m_waitingForRegister) { @@ -739,23 +616,15 @@ bool EmulatedCommandBar::handleKeyPress(const QKeyEve= nt *keyEvent) if (handled) return true; } + const bool completerHandled =3D completerHandledKeypress(keyEvent); + if (completerHandled) + return true; + if (keyEvent->modifiers() =3D=3D Qt::ControlModifier && (keyEvent->key= () =3D=3D Qt::Key_C || keyEvent->key() =3D=3D Qt::Key_BracketLeft)) { - const bool handled =3D completerHandledKeypress(keyEvent); - if (handled) - return true; - if (m_currentCompletionType =3D=3D None || !m_completer->popup()->= isVisible()) { - emit hideMe(); - } else { - abortCompletionAndResetToPreCompletion(); - } + emit hideMe(); return true; } - if (!m_interactiveSedReplaceMode->isActive()) - { - const bool handled =3D completerHandledKeypress(keyEvent); - if (handled) - return true; - } + // Is this a built-in Emulated Command Bar keypress e.g. insert from r= egister, ctrl-h, etc? const bool barHandled =3D barHandledKeypress(keyEvent); if (barHandled) @@ -844,31 +713,15 @@ void EmulatedCommandBar::moveCursorTo(const KTextEdit= or::Cursor &cursorPos) void EmulatedCommandBar::editTextChanged(const QString &newText) { Q_ASSERT(!m_interactiveSedReplaceMode->isActive()); - if (!m_isNextTextChangeDueToCompletionChange) { - m_textToRevertToIfCompletionAborted =3D newText; - m_cursorPosToRevertToIfCompletionAborted =3D m_edit->cursorPositio= n(); - } if (m_mode =3D=3D SearchForward || m_mode =3D=3D SearchBackward) { m_searchMode->editTextChanged(newText); } - - if (m_currentCompletionType =3D=3D None) + else { - if (m_mode =3D=3D Command) - { - m_commandMode->editTextChanged(newText, m_isNextTextChangeDueT= oCompletionChange); - } + m_commandMode->editTextChanged(newText); } = - // If we edit the text after having selected a completion, this means = we implicitly accept it, - // and so we should dismiss it. - if (!m_isNextTextChangeDueToCompletionChange && m_completer->popup()->= currentIndex().row() !=3D -1) { - deactivateCompletion(); - } - - if (m_currentCompletionType !=3D None && !m_isNextTextChangeDueToCompl= etionChange) { - updateCompletionPrefix(); - } + m_completerTmp->editTextChanged(newText); } = void EmulatedCommandBar::startHideExitStatusMessageTimer() @@ -923,7 +776,7 @@ void EmulatedCommandBar::ActiveMode::closeWithStatusMes= sage(const QString& exitS = void EmulatedCommandBar::ActiveMode::startCompletion ( const EmulatedComma= ndBar::CompletionStartParams& completionStartParams ) { - m_emulatedCommandBar->startCompletion(completionStartParams); + m_emulatedCommandBar->m_completerTmp->startCompletion(completionStartP= arams); } = EmulatedCommandBar::InteractiveSedReplaceMode::InteractiveSedReplaceMode(E= mulatedCommandBar* emulatedCommandBar, MatchHighlighter* matchHighlighter) @@ -934,6 +787,156 @@ EmulatedCommandBar::InteractiveSedReplaceMode::Intera= ctiveSedReplaceMode(Emulate m_interactiveSedReplaceLabel->setObjectName(QStringLiteral("interactiv= esedreplace")); } = +EmulatedCommandBar::Completer::Completer ( EmulatedCommandBar* emulatedCom= mandBar, KTextEditor::ViewPrivate* view, QLineEdit* edit ) + : m_edit(edit), + m_view(view) +{ + m_completer =3D new QCompleter(QStringList(), edit); + // Can't find a way to stop the QCompleter from auto-completing when a= ttached to a QLineEdit, + // so don't actually set it as the QLineEdit's completer. + m_completer->setWidget(edit); + m_completer->setObjectName(QStringLiteral("completer")); + m_completionModel =3D new QStringListModel(emulatedCommandBar); + m_completer->setModel(m_completionModel); + m_completer->setCaseSensitivity(Qt::CaseInsensitive); + m_completer->popup()->installEventFilter(emulatedCommandBar); +} + +void EmulatedCommandBar::Completer::startCompletion ( const EmulatedComman= dBar::CompletionStartParams& completionStartParams ) +{ + if (completionStartParams.shouldStart) + { + m_completionModel->setStringList(completionStartParams.completions= ); + const QString completionPrefix =3D m_edit->text().mid(completionSt= artParams.wordStartPos, m_edit->cursorPosition() - completionStartParams.wo= rdStartPos); + m_completer->setCompletionPrefix(completionPrefix); + m_completer->complete(); + m_currentCompletionStartParams =3D completionStartParams; + } +} + +void EmulatedCommandBar::Completer::deactivateCompletion() +{ + m_completer->popup()->hide(); + m_currentCompletionType =3D None; +} + +bool EmulatedCommandBar::Completer::isCompletionActive() const +{ + return m_currentCompletionType !=3D None; +} + +bool EmulatedCommandBar::Completer::isNextTextChangeDueToCompletionChange(= ) const +{ + return m_isNextTextChangeDueToCompletionChange; +} + +void EmulatedCommandBar::Completer::editTextChanged ( const QString& newTe= xt ) +{ + if (!m_isNextTextChangeDueToCompletionChange) { + m_textToRevertToIfCompletionAborted =3D newText; + m_cursorPosToRevertToIfCompletionAborted =3D m_edit->cursorPositio= n(); + } + // If we edit the text after having selected a completion, this means = we implicitly accept it, + // and so we should dismiss it. + if (!m_isNextTextChangeDueToCompletionChange && m_completer->popup()->= currentIndex().row() !=3D -1) { + deactivateCompletion(); + } + + if (m_currentCompletionType !=3D None && !m_isNextTextChangeDueToCompl= etionChange) { + updateCompletionPrefix(); + } +} + +void EmulatedCommandBar::Completer::setCompletionIndex ( int index ) +{ + const QModelIndex modelIndex =3D m_completer->popup()->model()->index(= index, 0); + // Need to set both of these, for some reason. + m_completer->popup()->setCurrentIndex(modelIndex); + m_completer->setCurrentRow(index); + + m_completer->popup()->scrollTo(modelIndex); + + currentCompletionChanged(); +} + +void EmulatedCommandBar::Completer::currentCompletionChanged() +{ + const QString newCompletion =3D m_completer->currentCompletion(); + if (newCompletion.isEmpty()) { + return; + } + QString transformedCompletion =3D newCompletion; + if (m_currentCompletionStartParams.completionTransform) + { + transformedCompletion =3D m_currentCompletionStartParams.completio= nTransform(newCompletion); + } + + m_isNextTextChangeDueToCompletionChange =3D true; + m_edit->setSelection(m_currentCompletionStartParams.wordStartPos, m_ed= it->cursorPosition() - m_currentCompletionStartParams.wordStartPos); + m_edit->insert(transformedCompletion); + m_isNextTextChangeDueToCompletionChange =3D false; + +} + + +void EmulatedCommandBar::Completer::updateCompletionPrefix() +{ + const QString completionPrefix =3D m_edit->text().mid(m_currentComplet= ionStartParams.wordStartPos, m_edit->cursorPosition() - m_currentCompletion= StartParams.wordStartPos); + m_completer->setCompletionPrefix(completionPrefix); + // Seem to need a call to complete() else the size of the popup box is= not altered appropriately. + m_completer->complete(); +} + +EmulatedCommandBar::CompletionStartParams EmulatedCommandBar::Completer::a= ctivateWordFromDocumentCompletion() +{ + m_currentCompletionType =3D WordFromDocument; + QRegExp wordRegEx(QLatin1String("\\w{1,}")); + QStringList foundWords; + // Narrow the range of lines we search around the cursor so that we do= n't die on huge files. + const int startLine =3D qMax(0, m_view->cursorPosition().line() - 4096= ); + const int endLine =3D qMin(m_view->document()->lines(), m_view->cursor= Position().line() + 4096); + for (int lineNum =3D startLine; lineNum < endLine; lineNum++) { + const QString line =3D m_view->document()->line(lineNum); int word= SearchBeginPos =3D 0; + while (wordRegEx.indexIn(line, wordSearchBeginPos) !=3D -1) { + const QString foundWord =3D wordRegEx.cap(0); + foundWords << foundWord; + wordSearchBeginPos =3D wordRegEx.indexIn(line, wordSearchBegin= Pos) + wordRegEx.matchedLength(); + } + } + foundWords =3D QSet::fromList(foundWords).toList(); + qSort(foundWords.begin(), foundWords.end(), caseInsensitiveLessThan); + CompletionStartParams completionStartParams; + completionStartParams.shouldStart =3D true; + completionStartParams.completions =3D foundWords; + completionStartParams.wordStartPos =3D wordBeforeCursorBegin(); + return completionStartParams; +} + +QString EmulatedCommandBar::Completer::wordBeforeCursor() +{ + const int wordBeforeCursorBegin =3D this->wordBeforeCursorBegin(); + return m_edit->text().mid(wordBeforeCursorBegin, m_edit->cursorPositio= n() - wordBeforeCursorBegin); +} + +int EmulatedCommandBar::Completer::wordBeforeCursorBegin() +{ + int wordBeforeCursorBegin =3D m_edit->cursorPosition() - 1; + while (wordBeforeCursorBegin >=3D 0 && (m_edit->text()[wordBeforeCurso= rBegin].isLetterOrNumber() || m_edit->text()[wordBeforeCursorBegin] =3D=3D = QLatin1Char('_'))) { + wordBeforeCursorBegin--; + } + wordBeforeCursorBegin++; + return wordBeforeCursorBegin; +} + +void EmulatedCommandBar::Completer::abortCompletionAndResetToPreCompletion= () +{ + deactivateCompletion(); + m_isNextTextChangeDueToCompletionChange =3D true; + m_edit->setText(m_textToRevertToIfCompletionAborted); + m_edit->setCursorPosition(m_cursorPosToRevertToIfCompletionAborted); + m_isNextTextChangeDueToCompletionChange =3D false; +} + void EmulatedCommandBar::InteractiveSedReplaceMode::activate(QSharedPointe= r interactiveSedReplace) { Q_ASSERT_X(interactiveSedReplace->currentMatch().isValid(), "startInte= ractiveSearchAndReplace", "KateCommands shouldn't initiate an interactive s= ed replace with no initial match"); @@ -1156,11 +1159,12 @@ void EmulatedCommandBar::SearchMode::setBarBackgrou= nd ( EmulatedCommandBar::Sear m_edit->setPalette(barBackground); } = -EmulatedCommandBar::CommandMode::CommandMode ( EmulatedCommandBar* emulate= dCommandBar, EmulatedCommandBar::MatchHighlighter* matchHighlighter, KTextE= ditor::ViewPrivate* view, QLineEdit* edit, InteractiveSedReplaceMode *inte= ractiveSedReplaceMode) +EmulatedCommandBar::CommandMode::CommandMode ( EmulatedCommandBar* emulate= dCommandBar, EmulatedCommandBar::MatchHighlighter* matchHighlighter, KTextE= ditor::ViewPrivate* view, QLineEdit* edit, InteractiveSedReplaceMode *inte= ractiveSedReplaceMode, Completer* completer) : ActiveMode ( emulatedCommandBar, matchHighlighter ), m_edit(edit), m_view(view), - m_interactiveSedReplaceMode(interactiveSedReplaceMode) + m_interactiveSedReplaceMode(interactiveSedReplaceMode), + m_completer(completer) { QList cmds; = @@ -1210,11 +1214,13 @@ bool EmulatedCommandBar::CommandMode::handleKeyPres= s ( const QKeyEvent* keyEvent return false; } = -void EmulatedCommandBar::CommandMode::editTextChanged ( const QString& new= Text, bool isNextTextChangeDueToCompletionChange ) +void EmulatedCommandBar::CommandMode::editTextChanged ( const QString& new= Text ) { Q_UNUSED(newText); // We read the current text from m_edit. + if (m_completer->isCompletionActive()) + return; // Command completion doesn't need to be manually invoked. - if (!withoutRangeExpression().isEmpty() && !isNextTextChangeDueToCompl= etionChange) { + if (!withoutRangeExpression().isEmpty() && !m_completer->isNextTextCha= ngeDueToCompletionChange()) { // ... However, command completion mode should not be automaticall= y invoked if this is not the current leading // word in the text edit (it gets annoying if completion pops up a= fter ":s/se" etc). const bool commandBeforeCursorIsLeading =3D (commandBeforeCursorBe= gin() =3D=3D rangeExpression().length()); diff --git a/src/vimode/emulatedcommandbar.h b/src/vimode/emulatedcommandba= r.h index e712506..aa145bf 100644 --- a/src/vimode/emulatedcommandbar.h +++ b/src/vimode/emulatedcommandbar.h @@ -107,6 +107,37 @@ private: QStringList completions; std::function completionTransform; }; + bool completerHandledKeypress(const QKeyEvent* keyEvent); + class Completer + { + public: + Completer(EmulatedCommandBar* emulatedCommandBar, KTextEditor::Vie= wPrivate* view, QLineEdit* edit); + void startCompletion(const CompletionStartParams& completionStartP= arams); + void deactivateCompletion(); + bool isCompletionActive() const; + bool isNextTextChangeDueToCompletionChange() const; + void editTextChanged(const QString &newText); + void setCompletionIndex(int index); + void currentCompletionChanged(); + void updateCompletionPrefix(); + CompletionStartParams activateWordFromDocumentCompletion(); + QString wordBeforeCursor(); + int wordBeforeCursorBegin(); + void abortCompletionAndResetToPreCompletion(); + QCompleter *m_completer; + QStringListModel *m_completionModel; + QString m_textToRevertToIfCompletionAborted; + int m_cursorPosToRevertToIfCompletionAborted; + bool m_isNextTextChangeDueToCompletionChange =3D false; + CompletionStartParams m_currentCompletionStartParams; + CompletionType m_currentCompletionType =3D None; + private: + QLineEdit *m_edit; + KTextEditor::ViewPrivate *m_view; + }; + enum class CompletionInvocation { ExtraContext, NormalContext }; // TO= DO - make member of upcoming Completer class. + QScopedPointer m_completerTmp; + class ActiveMode { public: @@ -125,7 +156,7 @@ private: void closeWithStatusMessage(const QString& exitStatusMessage); void setCompletionMode(CompletionType completionType) { - m_emulatedCommandBar->m_currentCompletionType =3D completionTy= pe; + m_emulatedCommandBar->m_completerTmp->m_currentCompletionType = =3D completionType; }; // TODO - ultimately remove this - eventually, the upcoming Com= pletion class will store the mode, and it will be one of None, WordUnderCur= sor, and ModeSpecific. void startCompletion(const CompletionStartParams& completionStartP= arams); EmulatedCommandBar *m_emulatedCommandBar; @@ -157,7 +188,6 @@ private: QLabel *m_interactiveSedReplaceLabel; }; = - enum class CompletionInvocation { ExtraContext, NormalContext }; // TO= DO - make member of upcoming Completer class. = class SearchMode : public ActiveMode { @@ -194,13 +224,13 @@ private: class CommandMode : public ActiveMode { public: - CommandMode(EmulatedCommandBar* emulatedCommandBar, MatchHighlight= er* matchHighlighter, KTextEditor::ViewPrivate* view, QLineEdit* edit, Int= eractiveSedReplaceMode *interactiveSedReplaceMode); + CommandMode(EmulatedCommandBar* emulatedCommandBar, MatchHighlight= er* matchHighlighter, KTextEditor::ViewPrivate* view, QLineEdit* edit, Int= eractiveSedReplaceMode *interactiveSedReplaceMode, Completer* completer); virtual ~CommandMode() { } void setViInputModeManager(InputModeManager *viInputModeManager); virtual bool handleKeyPress ( const QKeyEvent* keyEvent ); - void editTextChanged(const QString &newText, bool isNextTextChange= DueToCompletionChange); + void editTextChanged(const QString &newText); QString executeCommand(const QString &commandToExecute); CompletionStartParams completionInvoked(CompletionInvocation invoc= ationType); void completionChosen(); @@ -241,6 +271,7 @@ private: InputModeManager *m_viInputModeManager =3D nullptr; KTextEditor::ViewPrivate *m_view; InteractiveSedReplaceMode *m_interactiveSedReplaceMode; + Completer *m_completer; KCompletion m_cmdCompletion; QHash m_cmdDict; KTextEditor::Command *queryCommand(const QString &cmd) const; @@ -251,33 +282,13 @@ private: = void moveCursorTo(const KTextEditor::Cursor &cursorPos); = - QCompleter *m_completer; - QStringListModel *m_completionModel; - bool m_isNextTextChangeDueToCompletionChange =3D false; - CompletionType m_currentCompletionType =3D None; - void updateCompletionPrefix(); - void currentCompletionChanged(); - bool m_completionActive; - QString m_textToRevertToIfCompletionAborted; - int m_cursorPosToRevertToIfCompletionAborted; - CompletionStartParams m_currentCompletionStartParams; - - bool completerHandledKeypress(const QKeyEvent* keyEvent); bool barHandledKeypress(const QKeyEvent* keyEvent); void insertRegisterContents(const QKeyEvent *keyEvent); bool eventFilter(QObject *object, QEvent *event) Q_DECL_OVERRIDE; void deleteSpacesToLeftOfCursor(); void deleteWordCharsToLeftOfCursor(); bool deleteNonWordCharsToLeftOfCursor(); - QString wordBeforeCursor(); - int wordBeforeCursorBegin(); - void replaceWordBeforeCursorWith(const QString &newWord); = - CompletionStartParams activateWordFromDocumentCompletion(); - void startCompletion(const CompletionStartParams& completionStartParam= s); - void deactivateCompletion(); - void abortCompletionAndResetToPreCompletion(); - void setCompletionIndex(int index); = void closed() Q_DECL_OVERRIDE; void closeWithStatusMessage(const QString& exitStatusMessage);