[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: [ktexteditor] src/vimode: Move all the completion-related stuff into new Completer class.
From: Simon St James <kdedevel () etotheipiplusone ! com>
Date: 2016-06-17 8:18:10
Message-ID: E1bDoyg-0002ZN-5p () scm ! kde ! org
[Download RAW message or body]
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/emulatedcommandbar.cpp
index 7f9a9fa..a868873 100644
--- a/src/vimode/emulatedcommandbar.cpp
+++ b/src/vimode/emulatedcommandbar.cpp
@@ -300,8 +300,7 @@ QString withSearchConfigRemoved(const QString \
&originalSearchText, const bool is \
EmulatedCommandBar::EmulatedCommandBar(InputModeManager *viInputModeManager, QWidget \
*parent) : KateViewBarWidget(false, parent)
, m_viInputModeManager(viInputModeManager)
- , m_view(viInputModeManager->view())
-{
+ , m_view(viInputModeManager->view()){
QHBoxLayout *layout = new QHBoxLayout();
layout->setMargin(0);
centralWidget()->setLayout(layout);
@@ -332,23 +331,17 @@ EmulatedCommandBar::EmulatedCommandBar(InputModeManager \
*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_view, \
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(editTextChanged(QString)));
- m_completer = new QCompleter(QStringList(), m_edit);
- // Can't find a way to stop the QCompleter from auto-completing when attached 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 = new QStringListModel(this);
- m_completer->setModel(m_completionModel);
- m_completer->setCaseSensitivity(Qt::CaseInsensitive);
- m_completer->popup()->installEventFilter(this);
m_exitStatusMessageDisplayHideTimer = new QTimer(this);
m_exitStatusMessageDisplayHideTimer->setSingleShot(true);
@@ -369,7 +362,7 @@ EmulatedCommandBar::~EmulatedCommandBar()
void EmulatedCommandBar::init(EmulatedCommandBar::Mode mode, const QString \
&initialText) {
- m_currentCompletionType = None;
+ m_completerTmp->m_currentCompletionType = None;
m_mode = mode;
m_isActive = true;
m_wasAborted = true;
@@ -407,7 +400,7 @@ void EmulatedCommandBar::setCommandResponseMessageTimeout(long \
int commandRespon void EmulatedCommandBar::closed()
{
m_matchHighligher->updateMatchHighlight(KTextEditor::Range::invalid());
- m_completer->popup()->hide();
+ m_completerTmp->m_completer->popup()->hide();
m_isActive = false;
m_interactiveSedReplaceMode->deactivate();
@@ -432,67 +425,69 @@ void EmulatedCommandBar::updateMatchHighlightAttrib()
bool EmulatedCommandBar::completerHandledKeypress ( const QKeyEvent* keyEvent )
{
+ if (!m_edit->isVisible())
+ return false;
if (keyEvent->modifiers() == Qt::ControlModifier && (keyEvent->key() == \
Qt::Key_C || keyEvent->key() == Qt::Key_BracketLeft)) {
- if (m_currentCompletionType != None && m_completer->popup()->isVisible())
+ if (m_completerTmp->m_currentCompletionType != None && \
m_completerTmp->m_completer->popup()->isVisible()) {
- abortCompletionAndResetToPreCompletion();
+ m_completerTmp->abortCompletionAndResetToPreCompletion();
return true;
}
}
if (keyEvent->modifiers() == Qt::ControlModifier && keyEvent->key() == \
Qt::Key_Space) {
- CompletionStartParams completionStartParams = \
activateWordFromDocumentCompletion();
- startCompletion(completionStartParams);
+ CompletionStartParams completionStartParams = \
m_completerTmp->activateWordFromDocumentCompletion(); + \
m_completerTmp->startCompletion(completionStartParams); return true;
}
if ((keyEvent->modifiers() == Qt::ControlModifier && keyEvent->key() == \
Qt::Key_P) || keyEvent->key() == Qt::Key_Down) {
- if (!m_completer->popup()->isVisible()) {
+ if (!m_completerTmp->m_completer->popup()->isVisible()) {
CompletionStartParams completionStartParams;
if (m_mode == Command) {
completionStartParams = \
m_commandMode->completionInvoked(CompletionInvocation::ExtraContext); } else {
completionStartParams = \
m_searchMode->completionInvoked(CompletionInvocation::ExtraContext); }
- startCompletion(completionStartParams);
- if (m_currentCompletionType != None) {
- setCompletionIndex(0);
+ m_completerTmp->startCompletion(completionStartParams);
+ if (m_completerTmp->m_currentCompletionType != None) {
+ m_completerTmp->setCompletionIndex(0);
}
} else {
// Descend to next row, wrapping around if necessary.
- if (m_completer->currentRow() + 1 == m_completer->completionCount()) {
- setCompletionIndex(0);
+ if (m_completerTmp->m_completer->currentRow() + 1 == \
m_completerTmp->m_completer->completionCount()) { + \
m_completerTmp->setCompletionIndex(0); } else {
- setCompletionIndex(m_completer->currentRow() + 1);
+ m_completerTmp->setCompletionIndex(m_completerTmp->m_completer->currentRow() \
+ 1); }
}
return true;
}
if ((keyEvent->modifiers() == Qt::ControlModifier && keyEvent->key() == \
Qt::Key_N) || keyEvent->key() == Qt::Key_Up) {
- if (!m_completer->popup()->isVisible()) {
+ if (!m_completerTmp->m_completer->popup()->isVisible()) {
CompletionStartParams completionStartParams;
if (m_mode == Command) {
completionStartParams = \
m_commandMode->completionInvoked(CompletionInvocation::NormalContext); } else {
completionStartParams = \
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() == 0) {
- setCompletionIndex(m_completer->completionCount() - 1);
+ if (m_completerTmp->m_completer->currentRow() == 0) {
+ m_completerTmp->setCompletionIndex(m_completerTmp->m_completer->completionCount() \
- 1); } else {
- setCompletionIndex(m_completer->currentRow() - 1);
+ m_completerTmp->setCompletionIndex(m_completerTmp->m_completer->currentRow() \
- 1); }
}
return true;
}
if (keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return) {
- if (m_completer->popup()->isVisible() && m_currentCompletionType == \
EmulatedCommandBar::WordFromDocument) {
- deactivateCompletion();
+ if (m_completerTmp->m_completer->popup()->isVisible() && \
m_completerTmp->m_currentCompletionType == EmulatedCommandBar::WordFromDocument) { + \
m_completerTmp->deactivateCompletion(); } else {
m_wasAborted = false;
- deactivateCompletion();
+ m_completerTmp->deactivateCompletion();
if (m_mode == Command) {
m_commandMode->completionChosen();
} else {
@@ -563,7 +558,7 @@ void EmulatedCommandBar::insertRegisterContents(const QKeyEvent \
*keyEvent)
bool EmulatedCommandBar::eventFilter(QObject *object, QEvent *event)
{
- Q_ASSERT(object == m_edit || object == m_completer->popup());
+ Q_ASSERT(object == m_edit || object == m_completerTmp->m_completer->popup());
if (m_suspendEditEventFiltering) {
return false;
}
@@ -610,124 +605,6 @@ bool EmulatedCommandBar::deleteNonWordCharsToLeftOfCursor()
return deletionsMade;
}
-QString EmulatedCommandBar::wordBeforeCursor()
-{
- const int wordBeforeCursorBegin = this->wordBeforeCursorBegin();
- return m_edit->text().mid(wordBeforeCursorBegin, m_edit->cursorPosition() - \
wordBeforeCursorBegin);
-}
-
-int EmulatedCommandBar::wordBeforeCursorBegin()
-{
- int wordBeforeCursorBegin = m_edit->cursorPosition() - 1;
- while (wordBeforeCursorBegin >= 0 && \
(m_edit->text()[wordBeforeCursorBegin].isLetterOrNumber() || \
m_edit->text()[wordBeforeCursorBegin] == QLatin1Char('_'))) {
- wordBeforeCursorBegin--;
- }
- wordBeforeCursorBegin++;
- return wordBeforeCursorBegin;
-}
-
-
-void EmulatedCommandBar::replaceWordBeforeCursorWith(const QString &newWord)
-{
- const int wordBeforeCursorStart = m_edit->cursorPosition() - \
wordBeforeCursor().length();
- const QString newText = 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::activateWordFromDocumentCompletion()
-{
- m_currentCompletionType = WordFromDocument;
- QRegExp wordRegEx(QLatin1String("\\w{1,}"));
- QStringList foundWords;
- // Narrow the range of lines we search around the cursor so that we don't die on \
huge files.
- const int startLine = qMax(0, m_view->cursorPosition().line() - 4096);
- const int endLine = qMin(m_view->document()->lines(), \
m_view->cursorPosition().line() + 4096);
- for (int lineNum = startLine; lineNum < endLine; lineNum++) {
- const QString line = m_view->document()->line(lineNum);
- int wordSearchBeginPos = 0;
- while (wordRegEx.indexIn(line, wordSearchBeginPos) != -1) {
- const QString foundWord = wordRegEx.cap(0);
- foundWords << foundWord;
- wordSearchBeginPos = wordRegEx.indexIn(line, wordSearchBeginPos) + \
wordRegEx.matchedLength();
- }
- }
- foundWords = QSet<QString>::fromList(foundWords).toList();
- qSort(foundWords.begin(), foundWords.end(), caseInsensitiveLessThan);
- CompletionStartParams completionStartParams;
- completionStartParams.shouldStart = true;
- completionStartParams.completions = foundWords;
- completionStartParams.wordStartPos = wordBeforeCursorBegin();
- return completionStartParams;
-}
-
-void EmulatedCommandBar::startCompletion ( const \
EmulatedCommandBar::CompletionStartParams& completionStartParams )
-{
- if (completionStartParams.shouldStart)
- {
- m_completionModel->setStringList(completionStartParams.completions);
- const QString completionPrefix = \
m_edit->text().mid(completionStartParams.wordStartPos, m_edit->cursorPosition() - \
completionStartParams.wordStartPos);
- m_completer->setCompletionPrefix(completionPrefix);
- m_completer->complete();
- m_currentCompletionStartParams = completionStartParams;
- }
-}
-
-void EmulatedCommandBar::deactivateCompletion()
-{
- m_completer->popup()->hide();
- m_currentCompletionType = None;
-}
-
-void EmulatedCommandBar::abortCompletionAndResetToPreCompletion()
-{
- deactivateCompletion();
- m_isNextTextChangeDueToCompletionChange = true;
- m_edit->setText(m_textToRevertToIfCompletionAborted);
- m_edit->setCursorPosition(m_cursorPosToRevertToIfCompletionAborted);
- m_isNextTextChangeDueToCompletionChange = false;
-}
-
-void EmulatedCommandBar::updateCompletionPrefix()
-{
- const QString completionPrefix = \
m_edit->text().mid(m_currentCompletionStartParams.wordStartPos, \
m_edit->cursorPosition() - \
m_currentCompletionStartParams.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 = m_completer->currentCompletion();
- if (newCompletion.isEmpty()) {
- return;
- }
- QString transformedCompletion = newCompletion;
- if (m_currentCompletionStartParams.completionTransform)
- {
- transformedCompletion = \
m_currentCompletionStartParams.completionTransform(newCompletion);
- }
-
- m_isNextTextChangeDueToCompletionChange = true;
- m_edit->setSelection(m_currentCompletionStartParams.wordStartPos, \
m_edit->cursorPosition() - \
m_currentCompletionStartParams.wordStartPos);
- m_edit->insert(transformedCompletion);
- m_isNextTextChangeDueToCompletionChange = false;
-}
-
-void EmulatedCommandBar::setCompletionIndex(int index)
-{
- const QModelIndex modelIndex = 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 QKeyEvent \
*keyEvent) if (handled)
return true;
}
+ const bool completerHandled = completerHandledKeypress(keyEvent);
+ if (completerHandled)
+ return true;
+
if (keyEvent->modifiers() == Qt::ControlModifier && (keyEvent->key() == \
Qt::Key_C || keyEvent->key() == Qt::Key_BracketLeft)) {
- const bool handled = completerHandledKeypress(keyEvent);
- if (handled)
- return true;
- if (m_currentCompletionType == None || !m_completer->popup()->isVisible()) {
- emit hideMe();
- } else {
- abortCompletionAndResetToPreCompletion();
- }
+ emit hideMe();
return true;
}
- if (!m_interactiveSedReplaceMode->isActive())
- {
- const bool handled = completerHandledKeypress(keyEvent);
- if (handled)
- return true;
- }
+
// Is this a built-in Emulated Command Bar keypress e.g. insert from register, \
ctrl-h, etc? const bool barHandled = barHandledKeypress(keyEvent);
if (barHandled)
@@ -844,31 +713,15 @@ void EmulatedCommandBar::moveCursorTo(const KTextEditor::Cursor \
&cursorPos) void EmulatedCommandBar::editTextChanged(const QString &newText)
{
Q_ASSERT(!m_interactiveSedReplaceMode->isActive());
- if (!m_isNextTextChangeDueToCompletionChange) {
- m_textToRevertToIfCompletionAborted = newText;
- m_cursorPosToRevertToIfCompletionAborted = m_edit->cursorPosition();
- }
if (m_mode == SearchForward || m_mode == SearchBackward) {
m_searchMode->editTextChanged(newText);
}
-
- if (m_currentCompletionType == None)
+ else
{
- if (m_mode == Command)
- {
- m_commandMode->editTextChanged(newText, \
m_isNextTextChangeDueToCompletionChange);
- }
+ 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() != -1) {
- deactivateCompletion();
- }
-
- if (m_currentCompletionType != None && !m_isNextTextChangeDueToCompletionChange) \
{
- updateCompletionPrefix();
- }
+ m_completerTmp->editTextChanged(newText);
}
void EmulatedCommandBar::startHideExitStatusMessageTimer()
@@ -923,7 +776,7 @@ void EmulatedCommandBar::ActiveMode::closeWithStatusMessage(const \
QString& exitS
void EmulatedCommandBar::ActiveMode::startCompletion ( const \
EmulatedCommandBar::CompletionStartParams& completionStartParams ) {
- m_emulatedCommandBar->startCompletion(completionStartParams);
+ m_emulatedCommandBar->m_completerTmp->startCompletion(completionStartParams);
}
EmulatedCommandBar::InteractiveSedReplaceMode::InteractiveSedReplaceMode(EmulatedCommandBar* \
emulatedCommandBar, MatchHighlighter* matchHighlighter) @@ -934,6 +787,156 @@ \
EmulatedCommandBar::InteractiveSedReplaceMode::InteractiveSedReplaceMode(Emulate
m_interactiveSedReplaceLabel->setObjectName(QStringLiteral("interactivesedreplace"));
}
+EmulatedCommandBar::Completer::Completer ( EmulatedCommandBar* emulatedCommandBar, \
KTextEditor::ViewPrivate* view, QLineEdit* edit ) + : m_edit(edit),
+ m_view(view)
+{
+ m_completer = new QCompleter(QStringList(), edit);
+ // Can't find a way to stop the QCompleter from auto-completing when attached 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 = new QStringListModel(emulatedCommandBar);
+ m_completer->setModel(m_completionModel);
+ m_completer->setCaseSensitivity(Qt::CaseInsensitive);
+ m_completer->popup()->installEventFilter(emulatedCommandBar);
+}
+
+void EmulatedCommandBar::Completer::startCompletion ( const \
EmulatedCommandBar::CompletionStartParams& completionStartParams ) +{
+ if (completionStartParams.shouldStart)
+ {
+ m_completionModel->setStringList(completionStartParams.completions);
+ const QString completionPrefix = \
m_edit->text().mid(completionStartParams.wordStartPos, m_edit->cursorPosition() - \
completionStartParams.wordStartPos); + \
m_completer->setCompletionPrefix(completionPrefix); + m_completer->complete();
+ m_currentCompletionStartParams = completionStartParams;
+ }
+}
+
+void EmulatedCommandBar::Completer::deactivateCompletion()
+{
+ m_completer->popup()->hide();
+ m_currentCompletionType = None;
+}
+
+bool EmulatedCommandBar::Completer::isCompletionActive() const
+{
+ return m_currentCompletionType != None;
+}
+
+bool EmulatedCommandBar::Completer::isNextTextChangeDueToCompletionChange() const
+{
+ return m_isNextTextChangeDueToCompletionChange;
+}
+
+void EmulatedCommandBar::Completer::editTextChanged ( const QString& newText )
+{
+ if (!m_isNextTextChangeDueToCompletionChange) {
+ m_textToRevertToIfCompletionAborted = newText;
+ m_cursorPosToRevertToIfCompletionAborted = m_edit->cursorPosition();
+ }
+ // 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() != -1) { + deactivateCompletion();
+ }
+
+ if (m_currentCompletionType != None && !m_isNextTextChangeDueToCompletionChange) \
{ + updateCompletionPrefix();
+ }
+}
+
+void EmulatedCommandBar::Completer::setCompletionIndex ( int index )
+{
+ const QModelIndex modelIndex = 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 = m_completer->currentCompletion();
+ if (newCompletion.isEmpty()) {
+ return;
+ }
+ QString transformedCompletion = newCompletion;
+ if (m_currentCompletionStartParams.completionTransform)
+ {
+ transformedCompletion = \
m_currentCompletionStartParams.completionTransform(newCompletion); + }
+
+ m_isNextTextChangeDueToCompletionChange = true;
+ m_edit->setSelection(m_currentCompletionStartParams.wordStartPos, \
m_edit->cursorPosition() - m_currentCompletionStartParams.wordStartPos); + \
m_edit->insert(transformedCompletion); + m_isNextTextChangeDueToCompletionChange = \
false; +
+}
+
+
+void EmulatedCommandBar::Completer::updateCompletionPrefix()
+{
+ const QString completionPrefix = \
m_edit->text().mid(m_currentCompletionStartParams.wordStartPos, \
m_edit->cursorPosition() - m_currentCompletionStartParams.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::activateWordFromDocumentCompletion() +{
+ m_currentCompletionType = WordFromDocument;
+ QRegExp wordRegEx(QLatin1String("\\w{1,}"));
+ QStringList foundWords;
+ // Narrow the range of lines we search around the cursor so that we don't die on \
huge files. + const int startLine = qMax(0, m_view->cursorPosition().line() - \
4096); + const int endLine = qMin(m_view->document()->lines(), \
m_view->cursorPosition().line() + 4096); + for (int lineNum = startLine; lineNum < \
endLine; lineNum++) { + const QString line = \
m_view->document()->line(lineNum); int wordSearchBeginPos = 0; + while \
(wordRegEx.indexIn(line, wordSearchBeginPos) != -1) { + const QString \
foundWord = wordRegEx.cap(0); + foundWords << foundWord;
+ wordSearchBeginPos = wordRegEx.indexIn(line, wordSearchBeginPos) + \
wordRegEx.matchedLength(); + }
+ }
+ foundWords = QSet<QString>::fromList(foundWords).toList();
+ qSort(foundWords.begin(), foundWords.end(), caseInsensitiveLessThan);
+ CompletionStartParams completionStartParams;
+ completionStartParams.shouldStart = true;
+ completionStartParams.completions = foundWords;
+ completionStartParams.wordStartPos = wordBeforeCursorBegin();
+ return completionStartParams;
+}
+
+QString EmulatedCommandBar::Completer::wordBeforeCursor()
+{
+ const int wordBeforeCursorBegin = this->wordBeforeCursorBegin();
+ return m_edit->text().mid(wordBeforeCursorBegin, m_edit->cursorPosition() - \
wordBeforeCursorBegin); +}
+
+int EmulatedCommandBar::Completer::wordBeforeCursorBegin()
+{
+ int wordBeforeCursorBegin = m_edit->cursorPosition() - 1;
+ while (wordBeforeCursorBegin >= 0 && \
(m_edit->text()[wordBeforeCursorBegin].isLetterOrNumber() || \
m_edit->text()[wordBeforeCursorBegin] == QLatin1Char('_'))) { + \
wordBeforeCursorBegin--; + }
+ wordBeforeCursorBegin++;
+ return wordBeforeCursorBegin;
+}
+
+void EmulatedCommandBar::Completer::abortCompletionAndResetToPreCompletion()
+{
+ deactivateCompletion();
+ m_isNextTextChangeDueToCompletionChange = true;
+ m_edit->setText(m_textToRevertToIfCompletionAborted);
+ m_edit->setCursorPosition(m_cursorPosToRevertToIfCompletionAborted);
+ m_isNextTextChangeDueToCompletionChange = false;
+}
+
void EmulatedCommandBar::InteractiveSedReplaceMode::activate(QSharedPointer<SedReplace::InteractiveSedReplacer> \
interactiveSedReplace) {
Q_ASSERT_X(interactiveSedReplace->currentMatch().isValid(), \
"startInteractiveSearchAndReplace", "KateCommands shouldn't initiate an interactive \
sed replace with no initial match"); @@ -1156,11 +1159,12 @@ void \
EmulatedCommandBar::SearchMode::setBarBackground ( EmulatedCommandBar::Sear \
m_edit->setPalette(barBackground); }
-EmulatedCommandBar::CommandMode::CommandMode ( EmulatedCommandBar* \
emulatedCommandBar, EmulatedCommandBar::MatchHighlighter* matchHighlighter, \
KTextEditor::ViewPrivate* view, QLineEdit* edit, InteractiveSedReplaceMode \
*interactiveSedReplaceMode) +EmulatedCommandBar::CommandMode::CommandMode ( \
EmulatedCommandBar* emulatedCommandBar, EmulatedCommandBar::MatchHighlighter* \
matchHighlighter, KTextEditor::ViewPrivate* view, QLineEdit* edit, \
InteractiveSedReplaceMode *interactiveSedReplaceMode, Completer* completer) : \
ActiveMode ( emulatedCommandBar, matchHighlighter ), m_edit(edit),
m_view(view),
- m_interactiveSedReplaceMode(interactiveSedReplaceMode)
+ m_interactiveSedReplaceMode(interactiveSedReplaceMode),
+ m_completer(completer)
{
QList<KTextEditor::Command *> cmds;
@@ -1210,11 +1214,13 @@ bool EmulatedCommandBar::CommandMode::handleKeyPress ( const \
QKeyEvent* keyEvent return false;
}
-void EmulatedCommandBar::CommandMode::editTextChanged ( const QString& newText, bool \
isNextTextChangeDueToCompletionChange ) +void \
EmulatedCommandBar::CommandMode::editTextChanged ( const QString& newText ) {
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() && \
!isNextTextChangeDueToCompletionChange) { + if \
(!withoutRangeExpression().isEmpty() && \
!m_completer->isNextTextChangeDueToCompletionChange()) {
// ... However, command completion mode should not be automatically invoked \
if this is not the current leading
// word in the text edit (it gets annoying if completion pops up after \
":s/se" etc).
const bool commandBeforeCursorIsLeading = (commandBeforeCursorBegin() == \
rangeExpression().length());
diff --git a/src/vimode/emulatedcommandbar.h b/src/vimode/emulatedcommandbar.h
index e712506..aa145bf 100644
--- a/src/vimode/emulatedcommandbar.h
+++ b/src/vimode/emulatedcommandbar.h
@@ -107,6 +107,37 @@ private:
QStringList completions;
std::function<QString(const QString&)> completionTransform;
};
+ bool completerHandledKeypress(const QKeyEvent* keyEvent);
+ class Completer
+ {
+ public:
+ Completer(EmulatedCommandBar* emulatedCommandBar, KTextEditor::ViewPrivate* \
view, QLineEdit* edit); + void startCompletion(const CompletionStartParams& \
completionStartParams); + 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 = false;
+ CompletionStartParams m_currentCompletionStartParams;
+ CompletionType m_currentCompletionType = None;
+ private:
+ QLineEdit *m_edit;
+ KTextEditor::ViewPrivate *m_view;
+ };
+ enum class CompletionInvocation { ExtraContext, NormalContext }; // TODO - make \
member of upcoming Completer class. + QScopedPointer<Completer> m_completerTmp;
+
class ActiveMode
{
public:
@@ -125,7 +156,7 @@ private:
void closeWithStatusMessage(const QString& exitStatusMessage);
void setCompletionMode(CompletionType completionType)
{
- m_emulatedCommandBar->m_currentCompletionType = completionType;
+ m_emulatedCommandBar->m_completerTmp->m_currentCompletionType = \
completionType; }; // TODO - ultimately remove this - eventually, the upcoming \
Completion class will store the mode, and it will be one of None, WordUnderCursor, \
and ModeSpecific.
void startCompletion(const CompletionStartParams& completionStartParams);
EmulatedCommandBar *m_emulatedCommandBar;
@@ -157,7 +188,6 @@ private:
QLabel *m_interactiveSedReplaceLabel;
};
- enum class CompletionInvocation { ExtraContext, NormalContext }; // TODO - make \
member of upcoming Completer class.
class SearchMode : public ActiveMode
{
@@ -194,13 +224,13 @@ private:
class CommandMode : public ActiveMode
{
public:
- CommandMode(EmulatedCommandBar* emulatedCommandBar, MatchHighlighter* \
matchHighlighter, KTextEditor::ViewPrivate* view, QLineEdit* edit, \
InteractiveSedReplaceMode *interactiveSedReplaceMode); + \
CommandMode(EmulatedCommandBar* emulatedCommandBar, MatchHighlighter* \
matchHighlighter, KTextEditor::ViewPrivate* view, QLineEdit* edit, \
InteractiveSedReplaceMode *interactiveSedReplaceMode, Completer* completer); virtual \
~CommandMode() {
}
void setViInputModeManager(InputModeManager *viInputModeManager);
virtual bool handleKeyPress ( const QKeyEvent* keyEvent );
- void editTextChanged(const QString &newText, bool \
isNextTextChangeDueToCompletionChange); + void editTextChanged(const QString \
&newText); QString executeCommand(const QString &commandToExecute);
CompletionStartParams completionInvoked(CompletionInvocation \
invocationType); void completionChosen();
@@ -241,6 +271,7 @@ private:
InputModeManager *m_viInputModeManager = nullptr;
KTextEditor::ViewPrivate *m_view;
InteractiveSedReplaceMode *m_interactiveSedReplaceMode;
+ Completer *m_completer;
KCompletion m_cmdCompletion;
QHash<QString, KTextEditor::Command *> 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 = false;
- CompletionType m_currentCompletionType = 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& completionStartParams);
- void deactivateCompletion();
- void abortCompletionAndResetToPreCompletion();
- void setCompletionIndex(int index);
void closed() Q_DECL_OVERRIDE;
void closeWithStatusMessage(const QString& exitStatusMessage);
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic