From 04fa3654a418e09972dd879b1abca407d4d9e2fc Mon Sep 17 00:00:00 2001 From: Le Tan Date: Sun, 24 Sep 2017 12:04:35 +0800 Subject: [PATCH] bug-fix: do not cancel auto-indentation if cursor is not at block end --- src/utils/veditutils.cpp | 19 +++++++++++++++++++ src/utils/veditutils.h | 4 ++++ src/utils/vvim.cpp | 16 ++-------------- src/vmdeditoperations.cpp | 14 +------------- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/utils/veditutils.cpp b/src/utils/veditutils.cpp index 325b23f1..5152e855 100644 --- a/src/utils/veditutils.cpp +++ b/src/utils/veditutils.cpp @@ -670,3 +670,22 @@ int VEditUtils::findNextEmptyBlock(const QTextCursor &p_cursor, return p_repeat > 0 ? -1 : res; } + +bool VEditUtils::needToCancelAutoIndent(int p_autoIndentPos, const QTextCursor &p_cursor) +{ + // Cancel the auto indent/list if the pos is the same and cursor is at + // the end of a block. + QTextBlock block = p_cursor.block(); + if (p_cursor.position() == p_autoIndentPos + && !p_cursor.hasSelection() + && p_cursor.atBlockEnd()) { + if (isListBlock(block)) { + return true; + } else if (isSpaceToBlockStart(block, + p_cursor.positionInBlock())) { + return true; + } + } + + return false; +} diff --git a/src/utils/veditutils.h b/src/utils/veditutils.h index eaf13a1b..bae56cca 100644 --- a/src/utils/veditutils.h +++ b/src/utils/veditutils.h @@ -130,6 +130,10 @@ public: bool p_forward, int p_repeat); + // Check if we need to cancel auto indent. + // @p_autoIndentPos: the position of the cursor after auto indent. + static bool needToCancelAutoIndent(int p_autoIndentPos, const QTextCursor &p_cursor); + private: VEditUtils() {} }; diff --git a/src/utils/vvim.cpp b/src/utils/vvim.cpp index f64099eb..74bb2c1c 100644 --- a/src/utils/vvim.cpp +++ b/src/utils/vvim.cpp @@ -518,7 +518,7 @@ bool VVim::handleKeyPressEvent(int key, int modifiers, int *p_autoIndentPos) bool resetPositionInBlock = true; Key keyInfo(key, modifiers); bool unindent = false; - int autoIndentPos = -1; + int autoIndentPos = p_autoIndentPos ? *p_autoIndentPos : -1; // Handle Insert mode key press. if (VimMode::Insert == m_mode) { @@ -527,20 +527,8 @@ bool VVim::handleKeyPressEvent(int key, int modifiers, int *p_autoIndentPos) // See if we need to cancel auto indent. bool cancelAutoIndent = false; if (p_autoIndentPos && *p_autoIndentPos > -1) { - // Cancel the auto indent/list if the pos is the same and cursor is at - // the end of a block. QTextCursor cursor = m_editor->textCursor(); - QTextBlock block = cursor.block(); - if (cursor.position() == *p_autoIndentPos && !cursor.hasSelection()) { - if (VEditUtils::isListBlock(block)) { - if (cursor.atBlockEnd()) { - cancelAutoIndent = true; - } - } else if (VEditUtils::isSpaceToBlockStart(block, - cursor.positionInBlock())) { - cancelAutoIndent = true; - } - } + cancelAutoIndent = VEditUtils::needToCancelAutoIndent(*p_autoIndentPos, cursor); if (cancelAutoIndent) { autoIndentPos = -1; diff --git a/src/vmdeditoperations.cpp b/src/vmdeditoperations.cpp index f03dfa59..bb80e883 100644 --- a/src/vmdeditoperations.cpp +++ b/src/vmdeditoperations.cpp @@ -553,20 +553,8 @@ bool VMdEditOperations::handleKeyReturn(QKeyEvent *p_event) if (m_autoIndentPos > -1) { // Cancel the auto indent/list if the pos is the same and cursor is at // the end of a block. - bool cancelAutoIndent = false; QTextCursor cursor = m_editor->textCursor(); - QTextBlock block = cursor.block(); - if (cursor.position() == m_autoIndentPos && !cursor.hasSelection()) { - if (VEditUtils::isListBlock(block)) { - if (cursor.atBlockEnd()) { - cancelAutoIndent = true; - } - } else if (VEditUtils::isSpaceToBlockStart(block, - cursor.positionInBlock())) { - cancelAutoIndent = true; - } - } - if (cancelAutoIndent) { + if (VEditUtils::needToCancelAutoIndent(m_autoIndentPos, cursor)) { m_autoIndentPos = -1; VEditUtils::deleteIndentAndListMark(cursor); m_editor->setTextCursor(cursor);