[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: [ktexteditor] src: Move ActiveMode and Completer into their own source files - couldn't do this piec
From: Simon St James <kdedevel () etotheipiplusone ! com>
Date: 2016-06-17 8:18:10
Message-ID: E1bDoyg-0002ZN-Ht () scm ! kde ! org
[Download RAW message or body]
Git commit cbaa10dd605e33726335a04eea93f900b401f7ac by Simon St James.
Committed on 17/06/2016 at 08:14.
Pushed by sstjames into branch 'master'.
Move ActiveMode and Completer into their own source files - couldn't do this \
piecemeal, alas, so it took a while to disentangle it.
M +1 -0 src/CMakeLists.txt
A +49 -0 src/vimode/emulatedcommandbar/activemode.cpp [License: UNKNOWN] *
A +53 -0 src/vimode/emulatedcommandbar/activemode.h [License: UNKNOWN] *
M +236 -0 src/vimode/emulatedcommandbar/completer.cpp
M +49 -0 src/vimode/emulatedcommandbar/completer.h
M +4 -264 src/vimode/emulatedcommandbar/emulatedcommandbar.cpp
M +3 -73 src/vimode/emulatedcommandbar/emulatedcommandbar.h
The files marked with a * at the end have a non valid license. Please read: \
http://techbase.kde.org/Policies/Licensing_Policy and use the headers which are \
listed at that page.
http://commits.kde.org/ktexteditor/cbaa10dd605e33726335a04eea93f900b401f7ac
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 49900cd..b50de81 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -278,6 +278,7 @@ if (BUILD_VIMODE)
vimode/emulatedcommandbar/emulatedcommandbar.cpp
vimode/emulatedcommandbar/matchhighlighter.cpp
vimode/emulatedcommandbar/completer.cpp
+ vimode/emulatedcommandbar/activemode.cpp
vimode/commandrangeexpressionparser.cpp
vimode/keymapper.cpp
vimode/marks.cpp
diff --git a/src/vimode/emulatedcommandbar/activemode.cpp \
b/src/vimode/emulatedcommandbar/activemode.cpp new file mode 100644
index 0000000..3f2a11e
--- /dev/null
+++ b/src/vimode/emulatedcommandbar/activemode.cpp
@@ -0,0 +1,49 @@
+#include "activemode.h"
+#include "completer.h"
+#include "emulatedcommandbar.h"
+#include "matchhighlighter.h"
+
+using namespace KateVi;
+
+CompletionStartParams ActiveMode::completionInvoked(Completer::CompletionInvocation \
invocationType) +{
+ Q_UNUSED(invocationType);
+ return CompletionStartParams();
+};
+
+ActiveMode::~ActiveMode()
+{
+
+}
+
+void ActiveMode::hideAllWidgetsExcept(QWidget* widgetToKeepVisible)
+{
+ m_emulatedCommandBar->hideAllWidgetsExcept(widgetToKeepVisible);
+}
+
+void ActiveMode::moveCursorTo(const KTextEditor::Cursor &cursorPos)
+{
+ m_emulatedCommandBar->moveCursorTo(cursorPos);
+}
+
+void ActiveMode::updateMatchHighlight(const KTextEditor::Range& matchRange)
+{
+ m_matchHighligher->updateMatchHighlight(matchRange);
+}
+
+void ActiveMode::close( bool wasAborted )
+{
+ m_emulatedCommandBar->m_wasAborted = wasAborted;
+ m_emulatedCommandBar->hideMe();
+}
+
+void ActiveMode::closeWithStatusMessage(const QString& exitStatusMessage)
+{
+ m_emulatedCommandBar->closeWithStatusMessage(exitStatusMessage);
+}
+
+void ActiveMode::startCompletion ( const CompletionStartParams& \
completionStartParams ) +{
+ m_emulatedCommandBar->m_completer->startCompletion(completionStartParams);
+}
+
diff --git a/src/vimode/emulatedcommandbar/activemode.h \
b/src/vimode/emulatedcommandbar/activemode.h new file mode 100644
index 0000000..8e32959
--- /dev/null
+++ b/src/vimode/emulatedcommandbar/activemode.h
@@ -0,0 +1,53 @@
+#ifndef KATEVI_EMULATED_COMMAND_BAR_ACTIVEMODE_H
+#define KATEVI_EMULATED_COMMAND_BAR_ACTIVEMODE_H
+
+#include "completer.h"
+
+class QKeyEvent;
+class QString;
+
+namespace KTextEditor
+{
+ class Cursor;
+ class Range;
+}
+
+namespace KateVi
+{
+class EmulatedCommandBar;
+class CompletionStartParams;
+class MatchHighlighter;
+
+class ActiveMode
+{
+public:
+ ActiveMode(EmulatedCommandBar* emulatedCommandBar, MatchHighlighter* \
matchHighlighter) + : m_emulatedCommandBar(emulatedCommandBar),
+ m_matchHighligher(matchHighlighter)
+ {
+ }
+ virtual ~ActiveMode() = 0;
+ virtual bool handleKeyPress(const QKeyEvent *keyEvent) = 0;
+ virtual void editTextChanged(const QString &newText)
+ {
+ Q_UNUSED(newText);
+ }
+ virtual KateVi::CompletionStartParams \
completionInvoked(Completer::CompletionInvocation invocationType); + virtual void \
completionChosen() + {
+ }
+ virtual void deactivate(bool wasAborted) = 0;
+protected:
+ // Helper methods.
+ void hideAllWidgetsExcept(QWidget* widgetToKeepVisible);
+ void moveCursorTo(const KTextEditor::Cursor &cursorPos);
+ void updateMatchHighlight(const KTextEditor::Range &matchRange);
+ void close(bool wasAborted);
+ void closeWithStatusMessage(const QString& exitStatusMessage);
+ void startCompletion(const CompletionStartParams& completionStartParams);
+ EmulatedCommandBar *m_emulatedCommandBar;
+private:
+ MatchHighlighter *m_matchHighligher;
+};
+}
+#endif
diff --git a/src/vimode/emulatedcommandbar/completer.cpp \
b/src/vimode/emulatedcommandbar/completer.cpp index e69de29..b95c6c9 100644
--- a/src/vimode/emulatedcommandbar/completer.cpp
+++ b/src/vimode/emulatedcommandbar/completer.cpp
@@ -0,0 +1,236 @@
+#include "completer.h"
+#include "emulatedcommandbar.h"
+
+using namespace KateVi;
+
+#include "kateview.h"
+
+#include <QCompleter>
+#include <QLineEdit>
+#include <QStringListModel>
+#include <QAbstractItemView>
+
+namespace
+{
+bool caseInsensitiveLessThan(const QString &s1, const QString &s2)
+{
+ return s1.toLower() < s2.toLower();
+}
+}
+
+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 Completer::startCompletion ( const CompletionStartParams& completionStartParams \
) +{
+ if (completionStartParams.completionType != CompletionStartParams::None)
+ {
+ 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;
+ m_currentCompletionType = completionStartParams.completionType;
+ }
+}
+
+void Completer::deactivateCompletion()
+{
+ m_completer->popup()->hide();
+ m_currentCompletionType = CompletionStartParams::None;
+}
+
+bool Completer::isCompletionActive() const
+{
+ return m_currentCompletionType != CompletionStartParams::None;
+}
+
+bool Completer::isNextTextChangeDueToCompletionChange() const
+{
+ return m_isNextTextChangeDueToCompletionChange;
+}
+
+bool Completer::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 != CompletionStartParams::None && \
m_completer->popup()->isVisible()) + {
+ abortCompletionAndResetToPreCompletion();
+ return true;
+ }
+ }
+ if (keyEvent->modifiers() == Qt::ControlModifier && keyEvent->key() == \
Qt::Key_Space) { + CompletionStartParams completionStartParams = \
activateWordFromDocumentCompletion(); + \
startCompletion(completionStartParams); + return true;
+ }
+ if ((keyEvent->modifiers() == Qt::ControlModifier && keyEvent->key() == \
Qt::Key_P) || keyEvent->key() == Qt::Key_Down) { + if \
(!m_completer->popup()->isVisible()) { + const CompletionStartParams \
completionStartParams = \
m_currentMode->completionInvoked(CompletionInvocation::ExtraContext); + \
startCompletion(completionStartParams); + if (m_currentCompletionType != \
CompletionStartParams::None) { + setCompletionIndex(0);
+ }
+ } else {
+ // Descend to next row, wrapping around if necessary.
+ if (m_completer->currentRow() + 1 == m_completer->completionCount()) {
+ setCompletionIndex(0);
+ } else {
+ setCompletionIndex(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()) { + const CompletionStartParams \
completionStartParams = \
m_currentMode->completionInvoked(CompletionInvocation::NormalContext); + \
startCompletion(completionStartParams); + \
setCompletionIndex(m_completer->completionCount() - 1); + } else {
+ // Ascend to previous row, wrapping around if necessary.
+ if (m_completer->currentRow() == 0) {
+ setCompletionIndex(m_completer->completionCount() - 1);
+ } else {
+ setCompletionIndex(m_completer->currentRow() - 1);
+ }
+ }
+ return true;
+ }
+ if (keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return) {
+ if (!m_completer->popup()->isVisible() || m_currentCompletionType != \
CompletionStartParams::WordFromDocument) { + \
m_currentMode->completionChosen(); + }
+ deactivateCompletion();
+ return true;
+ }
+ return false;
+
+}
+
+void 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 != CompletionStartParams::None && \
!m_isNextTextChangeDueToCompletionChange) { + updateCompletionPrefix();
+ }
+}
+
+void Completer::setCurrentMode ( ActiveMode* currentMode )
+{
+ m_currentMode = currentMode;
+}
+
+void 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 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 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(); +}
+
+CompletionStartParams Completer::activateWordFromDocumentCompletion()
+{
+ 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.completionType = CompletionStartParams::WordFromDocument;
+ completionStartParams.completions = foundWords;
+ completionStartParams.wordStartPos = wordBeforeCursorBegin();
+ return completionStartParams;
+}
+
+QString Completer::wordBeforeCursor()
+{
+ const int wordBeforeCursorBegin = this->wordBeforeCursorBegin();
+ return m_edit->text().mid(wordBeforeCursorBegin, m_edit->cursorPosition() - \
wordBeforeCursorBegin); +}
+
+int 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 Completer::abortCompletionAndResetToPreCompletion()
+{
+ deactivateCompletion();
+ m_isNextTextChangeDueToCompletionChange = true;
+ m_edit->setText(m_textToRevertToIfCompletionAborted);
+ m_edit->setCursorPosition(m_cursorPosToRevertToIfCompletionAborted);
+ m_isNextTextChangeDueToCompletionChange = false;
+}
+
diff --git a/src/vimode/emulatedcommandbar/completer.h \
b/src/vimode/emulatedcommandbar/completer.h index aed09ea..0fb5d3d 100644
--- a/src/vimode/emulatedcommandbar/completer.h
+++ b/src/vimode/emulatedcommandbar/completer.h
@@ -3,8 +3,23 @@
#include <functional>
+#include <QStringList>
+
+namespace KTextEditor
+{
+ class ViewPrivate;
+}
+
+class QLineEdit;
+class QCompleter;
+class QStringListModel;
+class QKeyEvent;
+
namespace KateVi
{
+ class ActiveMode;
+ class EmulatedCommandBar;
+
struct CompletionStartParams
{
static CompletionStartParams createModeSpecific(const QStringList& \
completions, int wordStartPos, std::function<QString(const QString&)> \
completionTransform = std::function<QString(const QString&)>()) @@ -28,6 +43,40 @@ \
namespace KateVi QStringList completions;
std::function<QString(const QString&)> completionTransform;
};
+
+ class Completer
+ {
+ public:
+ enum class CompletionInvocation { ExtraContext, NormalContext };
+ Completer(EmulatedCommandBar* emulatedCommandBar, KTextEditor::ViewPrivate* \
view, QLineEdit* edit); + void startCompletion(const CompletionStartParams& \
completionStartParams); + void deactivateCompletion();
+ bool isCompletionActive() const;
+ bool isNextTextChangeDueToCompletionChange() const;
+ bool completerHandledKeypress(const QKeyEvent* keyEvent);
+ void editTextChanged(const QString &newText);
+ void setCurrentMode(ActiveMode* currentMode);
+ private:
+ QLineEdit *m_edit;
+ KTextEditor::ViewPrivate *m_view;
+ ActiveMode *m_currentMode = nullptr;
+
+ 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;
+ CompletionStartParams::CompletionType m_currentCompletionType = \
CompletionStartParams::None; + };
}
#endif
diff --git a/src/vimode/emulatedcommandbar/emulatedcommandbar.cpp \
b/src/vimode/emulatedcommandbar/emulatedcommandbar.cpp index 952160f..3075796 100644
--- a/src/vimode/emulatedcommandbar/emulatedcommandbar.cpp
+++ b/src/vimode/emulatedcommandbar/emulatedcommandbar.cpp
@@ -46,11 +46,8 @@
#include <QLineEdit>
#include <QVBoxLayout>
#include <QLabel>
-#include <QCompleter>
#include <QApplication>
-#include <QAbstractItemView>
#include <QWhatsThis>
-#include <QStringListModel>
#include <algorithm>
@@ -58,11 +55,6 @@ using namespace KateVi;
namespace
{
-bool caseInsensitiveLessThan(const QString &s1, const QString &s2)
-{
- return s1.toLower() < s2.toLower();
-}
-
bool isCharEscaped(const QString &string, int charPos)
{
if (charPos == 0) {
@@ -411,7 +403,7 @@ void EmulatedCommandBar::closed()
m_currentMode = nullptr;
}
-void EmulatedCommandBar::switchToMode ( EmulatedCommandBar::ActiveMode* newMode )
+void EmulatedCommandBar::switchToMode ( ActiveMode* newMode )
{
if (m_currentMode)
m_currentMode->deactivate(false);
@@ -656,42 +648,6 @@ void EmulatedCommandBar::hideAllWidgetsExcept(QWidget* \
widgetToKeepVisible)
}
-EmulatedCommandBar::ActiveMode::~ActiveMode()
-{
-
-}
-
-void EmulatedCommandBar::ActiveMode::hideAllWidgetsExcept(QWidget* \
widgetToKeepVisible)
-{
- m_emulatedCommandBar->hideAllWidgetsExcept(widgetToKeepVisible);
-}
-
-void EmulatedCommandBar::ActiveMode::moveCursorTo(const KTextEditor::Cursor \
&cursorPos)
-{
- m_emulatedCommandBar->moveCursorTo(cursorPos);
-}
-
-void EmulatedCommandBar::ActiveMode::updateMatchHighlight(const KTextEditor::Range& \
matchRange)
-{
- m_matchHighligher->updateMatchHighlight(matchRange);
-}
-
-void EmulatedCommandBar::ActiveMode::close( bool wasAborted )
-{
- m_emulatedCommandBar->m_wasAborted = wasAborted;
- m_emulatedCommandBar->hideMe();
-}
-
-void EmulatedCommandBar::ActiveMode::closeWithStatusMessage(const QString& \
exitStatusMessage)
-{
- m_emulatedCommandBar->closeWithStatusMessage(exitStatusMessage);
-}
-
-void EmulatedCommandBar::ActiveMode::startCompletion ( const CompletionStartParams& \
completionStartParams )
-{
- m_emulatedCommandBar->m_completer->startCompletion(completionStartParams);
-}
-
EmulatedCommandBar::InteractiveSedReplaceMode::InteractiveSedReplaceMode(EmulatedCommandBar* \
emulatedCommandBar, MatchHighlighter* matchHighlighter) : \
ActiveMode(emulatedCommandBar, matchHighlighter), m_isActive(false)
@@ -700,222 +656,6 @@ \
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 CompletionStartParams& \
completionStartParams )
-{
- if (completionStartParams.completionType != CompletionStartParams::None)
- {
- 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;
- m_currentCompletionType = completionStartParams.completionType;
- }
-}
-
-void EmulatedCommandBar::Completer::deactivateCompletion()
-{
- m_completer->popup()->hide();
- m_currentCompletionType = CompletionStartParams::None;
-}
-
-bool EmulatedCommandBar::Completer::isCompletionActive() const
-{
- return m_currentCompletionType != CompletionStartParams::None;
-}
-
-bool EmulatedCommandBar::Completer::isNextTextChangeDueToCompletionChange() const
-{
- return m_isNextTextChangeDueToCompletionChange;
-}
-
-bool EmulatedCommandBar::Completer::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 != CompletionStartParams::None && \
m_completer->popup()->isVisible())
- {
- abortCompletionAndResetToPreCompletion();
- return true;
- }
- }
- if (keyEvent->modifiers() == Qt::ControlModifier && keyEvent->key() == \
Qt::Key_Space) {
- CompletionStartParams completionStartParams = \
activateWordFromDocumentCompletion();
- startCompletion(completionStartParams);
- return true;
- }
- if ((keyEvent->modifiers() == Qt::ControlModifier && keyEvent->key() == \
Qt::Key_P) || keyEvent->key() == Qt::Key_Down) {
- if (!m_completer->popup()->isVisible()) {
- const CompletionStartParams completionStartParams = \
m_currentMode->completionInvoked(CompletionInvocation::ExtraContext);
- startCompletion(completionStartParams);
- if (m_currentCompletionType != CompletionStartParams::None) {
- setCompletionIndex(0);
- }
- } else {
- // Descend to next row, wrapping around if necessary.
- if (m_completer->currentRow() + 1 == m_completer->completionCount()) {
- setCompletionIndex(0);
- } else {
- setCompletionIndex(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()) {
- const CompletionStartParams completionStartParams = \
m_currentMode->completionInvoked(CompletionInvocation::NormalContext);
- startCompletion(completionStartParams);
- setCompletionIndex(m_completer->completionCount() - 1);
- } else {
- // Ascend to previous row, wrapping around if necessary.
- if (m_completer->currentRow() == 0) {
- setCompletionIndex(m_completer->completionCount() - 1);
- } else {
- setCompletionIndex(m_completer->currentRow() - 1);
- }
- }
- return true;
- }
- if (keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return) {
- if (!m_completer->popup()->isVisible() || m_currentCompletionType != \
CompletionStartParams::WordFromDocument) {
- m_currentMode->completionChosen();
- }
- deactivateCompletion();
- return true;
- }
- return false;
-
-}
-
-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 != CompletionStartParams::None && \
!m_isNextTextChangeDueToCompletionChange) {
- updateCompletionPrefix();
- }
-}
-
-void EmulatedCommandBar::Completer::setCurrentMode ( EmulatedCommandBar::ActiveMode* \
currentMode )
-{
- m_currentMode = currentMode;
-}
-
-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();
-}
-
-CompletionStartParams \
EmulatedCommandBar::Completer::activateWordFromDocumentCompletion()
-{
- 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.completionType = CompletionStartParams::WordFromDocument;
- 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"); @@ -1095,7 +835,7 @@ void \
EmulatedCommandBar::SearchMode::deactivate(bool wasAborted)
m_viInputModeManager->globalState()->searchHistory()->append(m_edit->text());
}
-CompletionStartParams EmulatedCommandBar::SearchMode::completionInvoked ( \
EmulatedCommandBar::Completer::CompletionInvocation invocationType ) \
+CompletionStartParams EmulatedCommandBar::SearchMode::completionInvoked ( \
Completer::CompletionInvocation invocationType ) {
Q_UNUSED(invocationType);
return activateSearchHistoryCompletion();
@@ -1217,10 +957,10 @@ void EmulatedCommandBar::CommandMode::deactivate ( bool \
wasAborted )
}
-CompletionStartParams \
EmulatedCommandBar::CommandMode::completionInvoked(EmulatedCommandBar::Completer::CompletionInvocation \
invocationType) +CompletionStartParams \
EmulatedCommandBar::CommandMode::completionInvoked(Completer::CompletionInvocation \
invocationType) {
CompletionStartParams completionStartParams;
- if (invocationType == \
EmulatedCommandBar::Completer::CompletionInvocation::ExtraContext) + if \
(invocationType == Completer::CompletionInvocation::ExtraContext) {
if (isCursorInFindTermOfSed()) {
completionStartParams = activateSedFindHistoryCompletion();
diff --git a/src/vimode/emulatedcommandbar/emulatedcommandbar.h \
b/src/vimode/emulatedcommandbar/emulatedcommandbar.h index 720bb9b..0e2f708 100644
--- a/src/vimode/emulatedcommandbar/emulatedcommandbar.h
+++ b/src/vimode/emulatedcommandbar/emulatedcommandbar.h
@@ -27,7 +27,7 @@
#include <ktexteditor/range.h>
#include <ktexteditor/movingrange.h>
#include "../searcher.h"
-#include "completer.h"
+#include "activemode.h"
namespace KTextEditor {
class ViewPrivate;
@@ -35,8 +35,6 @@ namespace KTextEditor {
}
class QLabel;
-class QCompleter;
-class QStringListModel;
namespace KateVi
{
@@ -86,78 +84,10 @@ private:
QScopedPointer<MatchHighlighter> m_matchHighligher;
- class ActiveMode;
- class Completer
- {
- public:
- enum class CompletionInvocation { ExtraContext, NormalContext };
- Completer(EmulatedCommandBar* emulatedCommandBar, KTextEditor::ViewPrivate* \
view, QLineEdit* edit);
- void startCompletion(const CompletionStartParams& completionStartParams);
- void deactivateCompletion();
- bool isCompletionActive() const;
- bool isNextTextChangeDueToCompletionChange() const;
- bool completerHandledKeypress(const QKeyEvent* keyEvent);
- void editTextChanged(const QString &newText);
- void setCurrentMode(ActiveMode* currentMode);
- private:
- QLineEdit *m_edit;
- KTextEditor::ViewPrivate *m_view;
- ActiveMode *m_currentMode = nullptr;
-
- 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;
- CompletionStartParams::CompletionType m_currentCompletionType = \
CompletionStartParams::None;
- };
+ friend class ActiveMode;
QScopedPointer<Completer> m_completer;
- class ActiveMode
- {
- public:
- ActiveMode(EmulatedCommandBar* emulatedCommandBar, MatchHighlighter* \
matchHighlighter)
- : m_emulatedCommandBar(emulatedCommandBar),
- m_matchHighligher(matchHighlighter)
- {
- }
- virtual ~ActiveMode() = 0;
- virtual bool handleKeyPress(const QKeyEvent *keyEvent) = 0;
- virtual void editTextChanged(const QString &newText)
- {
- Q_UNUSED(newText);
- }
- virtual CompletionStartParams \
completionInvoked(Completer::CompletionInvocation invocationType)
- {
- Q_UNUSED(invocationType);
- return CompletionStartParams();
- };
- virtual void completionChosen()
- {
- }
- virtual void deactivate(bool wasAborted) = 0;
- protected:
- // Helper methods.
- void hideAllWidgetsExcept(QWidget* widgetToKeepVisible);
- void moveCursorTo(const KTextEditor::Cursor &cursorPos);
- void updateMatchHighlight(const KTextEditor::Range &matchRange);
- void close(bool wasAborted);
- void closeWithStatusMessage(const QString& exitStatusMessage);
- void startCompletion(const CompletionStartParams& completionStartParams);
- EmulatedCommandBar *m_emulatedCommandBar;
- private:
- MatchHighlighter *m_matchHighligher;
- };
- friend ActiveMode;
+ friend class ActiveMode;
class InteractiveSedReplaceMode : public ActiveMode
{
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic