Browse Source

bug-fix: do not cancel auto-indentation if cursor is not at block end

Le Tan 8 years ago
parent
commit
04fa3654a4
4 changed files with 26 additions and 27 deletions
  1. 19 0
      src/utils/veditutils.cpp
  2. 4 0
      src/utils/veditutils.h
  3. 2 14
      src/utils/vvim.cpp
  4. 1 13
      src/vmdeditoperations.cpp

+ 19 - 0
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;
+}

+ 4 - 0
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() {}
 };

+ 2 - 14
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;

+ 1 - 13
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);