summaryrefslogtreecommitdiff
path: root/src/plugins/fakevim
diff options
context:
space:
mode:
authorLukas Holecek <hluk@email.cz>2016-05-25 19:33:37 +0200
committerhjk <hjk@theqtcompany.com>2016-05-31 10:47:24 +0000
commita8a1b3b57305f5c986077b24cfdc05a933a80ed5 (patch)
treedd6f23261086f2fd709b588e13f6ec06622c5022 /src/plugins/fakevim
parent2de7f3e7231e895d6ac2414113524d2fb30844ec (diff)
downloadqt-creator-a8a1b3b57305f5c986077b24cfdc05a933a80ed5.tar.gz
FakeVim: Ignore external changes in insert mode
Task-number: QTCREATORBUG-16208 Change-Id: Ic357b855f740e6c86900c13f616cd58a27175413 Reviewed-by: hjk <hjk@theqtcompany.com>
Diffstat (limited to 'src/plugins/fakevim')
-rw-r--r--src/plugins/fakevim/fakevimhandler.cpp42
1 files changed, 31 insertions, 11 deletions
diff --git a/src/plugins/fakevim/fakevimhandler.cpp b/src/plugins/fakevim/fakevimhandler.cpp
index d0248934b1..c0657ee9ea 100644
--- a/src/plugins/fakevim/fakevimhandler.cpp
+++ b/src/plugins/fakevim/fakevimhandler.cpp
@@ -7606,20 +7606,40 @@ void FakeVimHandler::Private::onContentsChanged(int position, int charsRemoved,
// Ignore changes outside inserted text (e.g. renaming other occurrences of a variable).
if (position + charsRemoved >= insertState.pos1 && position <= insertState.pos2) {
if (charsRemoved > 0) {
+ // Assume that in a manual edit operation a text can be removed only
+ // in front of cursor (<DELETE>) or behind it (<BACKSPACE>).
+
+ // If the recorded amount of backspace/delete keys doesn't correspond with
+ // number of removed characters, assume that the document has been changed
+ // externally and invalidate current insert state.
+
+ const bool wholeDocumentChanged =
+ charsRemoved > 1
+ && charsAdded > 0
+ && charsAdded + 1 == document()->characterCount();
+
if (position < insertState.pos1) {
- // backspaces
- const int bs = insertState.pos1 - position;
- const QString inserted = textAt(position, oldPosition);
- const QString removed = insertState.textBeforeCursor.right(bs);
- // Ignore backspaces if same text was just inserted.
- if ( !inserted.endsWith(removed) ) {
- insertState.backspaces += bs;
- insertState.pos1 = position;
- insertState.pos2 = qMax(position, insertState.pos2 - bs);
+ // <BACKSPACE>
+ const int backspaceCount = insertState.pos1 - position;
+ if (backspaceCount != charsRemoved || (oldPosition == charsRemoved && wholeDocumentChanged)) {
+ invalidateInsertState();
+ } else {
+ const QString inserted = textAt(position, oldPosition);
+ const QString removed = insertState.textBeforeCursor.right(backspaceCount);
+ // Ignore backspaces if same text was just inserted.
+ if ( !inserted.endsWith(removed) ) {
+ insertState.backspaces += backspaceCount;
+ insertState.pos1 = position;
+ insertState.pos2 = qMax(position, insertState.pos2 - backspaceCount);
+ }
}
} else if (position + charsRemoved > insertState.pos2) {
- // deletes
- insertState.deletes += position + charsRemoved - insertState.pos2;
+ // <DELETE>
+ const int deleteCount = position + charsRemoved - insertState.pos2;
+ if (deleteCount != charsRemoved || (oldPosition == 0 && wholeDocumentChanged))
+ invalidateInsertState();
+ else
+ insertState.deletes += deleteCount;
}
} else if (charsAdded > 0 && insertState.insertingSpaces) {
for (int i = position; i < position + charsAdded; ++i) {