Browse Source

vim-mode: support canceling autoindent after o/O

Le Tan 8 years ago
parent
commit
2734b8407c
3 changed files with 26 additions and 9 deletions
  1. 15 5
      src/utils/vvim.cpp
  2. 2 2
      src/utils/vvim.h
  3. 9 2
      src/vmdeditoperations.cpp

+ 15 - 5
src/utils/vvim.cpp

@@ -340,9 +340,9 @@ static int percentageToBlockNumber(const QTextDocument *p_doc, int p_percent)
     return num >= 0 ? num : 0;
 }
 
-bool VVim::handleKeyPressEvent(QKeyEvent *p_event)
+bool VVim::handleKeyPressEvent(QKeyEvent *p_event, bool *p_autoIndented)
 {
-    bool ret = handleKeyPressEvent(p_event->key(), p_event->modifiers());
+    bool ret = handleKeyPressEvent(p_event->key(), p_event->modifiers(), p_autoIndented);
     if (ret) {
         p_event->accept();
     }
@@ -350,13 +350,17 @@ bool VVim::handleKeyPressEvent(QKeyEvent *p_event)
     return ret;
 }
 
-bool VVim::handleKeyPressEvent(int key, int modifiers)
+bool VVim::handleKeyPressEvent(int key, int modifiers, bool *p_autoIndented)
 {
     bool ret = false;
     bool resetPositionInBlock = true;
     Key keyInfo(key, modifiers);
     bool unindent = false;
 
+    if (p_autoIndented) {
+        *p_autoIndented = false;
+    }
+
     // Handle Insert mode key press.
     if (VimMode::Insert == m_mode) {
         if (key == Qt::Key_Escape
@@ -739,16 +743,22 @@ bool VVim::handleKeyPressEvent(int key, int modifiers)
                                         1);
                 }
 
+                bool textInserted = false;
                 if (vconfig.getAutoIndent()) {
-                    VEditUtils::indentBlockAsPreviousBlock(cursor);
+                    textInserted = VEditUtils::indentBlockAsPreviousBlock(cursor);
                     if (vconfig.getAutoList()) {
-                        VEditUtils::insertListMarkAsPreviousBlock(cursor);
+                        textInserted = VEditUtils::insertListMarkAsPreviousBlock(cursor)
+                                       || textInserted;
                     }
                 }
 
                 cursor.endEditBlock();
                 m_editor->setTextCursor(cursor);
 
+                if (p_autoIndented && textInserted) {
+                    *p_autoIndented = true;
+                }
+
                 setMode(VimMode::Insert);
             }
 

+ 2 - 2
src/utils/vvim.h

@@ -148,7 +148,7 @@ public:
 
     // Handle key press event.
     // Returns true if the event is consumed and need no more handling.
-    bool handleKeyPressEvent(QKeyEvent *p_event);
+    bool handleKeyPressEvent(QKeyEvent *p_event, bool *p_autoIndented = NULL);
 
     // Return current mode.
     VimMode getMode() const;
@@ -449,7 +449,7 @@ private:
     };
 
     // Returns true if the event is consumed and need no more handling.
-    bool handleKeyPressEvent(int key, int modifiers);
+    bool handleKeyPressEvent(int key, int modifiers, bool *p_autoIndented = NULL);
 
     // Reset all key state info.
     void resetState();

+ 9 - 2
src/vmdeditoperations.cpp

@@ -193,8 +193,15 @@ bool VMdEditOperations::insertImage()
 
 bool VMdEditOperations::handleKeyPressEvent(QKeyEvent *p_event)
 {
-    if (m_editConfig->m_enableVimMode && m_vim->handleKeyPressEvent(p_event)) {
-        m_autoIndentPos = -1;
+    bool autoIndentedVim = false;
+    if (m_editConfig->m_enableVimMode
+        && m_vim->handleKeyPressEvent(p_event, &autoIndentedVim)) {
+        if (autoIndentedVim) {
+            m_autoIndentPos = m_editor->textCursor().position();
+        } else {
+            m_autoIndentPos = -1;
+        }
+
         return true;
     }