瀏覽代碼

vim-mode: scroll to center after Ctrl+O/I

Le Tan 8 年之前
父節點
當前提交
7c2f1a8927
共有 3 個文件被更改,包括 33 次插入0 次删除
  1. 5 0
      src/utils/vvim.cpp
  2. 26 0
      src/vedit.cpp
  3. 2 0
      src/vedit.h

+ 5 - 0
src/utils/vvim.cpp

@@ -4410,6 +4410,11 @@ void VVim::processJumpLocationAction(QList<Token> &p_tokens, bool p_next)
             pib = block.length() - 1;
         }
 
+        if (!m_editor->isBlockVisible(block)) {
+            // Scroll the block to the center of screen.
+            VEditUtils::scrollBlockInPage(m_editor, block.blockNumber(), 1);
+        }
+
         cursor.setPosition(block.position() + pib);
         m_editor->setTextCursor(cursor);
     }

+ 26 - 0
src/vedit.cpp

@@ -1230,3 +1230,29 @@ void VEdit::makeBlockVisible(const QTextBlock &p_block)
         qDebug() << "scroll page up to make block visible";
     }
 }
+
+bool VEdit::isBlockVisible(const QTextBlock &p_block)
+{
+    if (!p_block.isValid() || !p_block.isVisible()) {
+        return false;
+    }
+
+    QScrollBar *vbar = verticalScrollBar();
+    if (!vbar || !vbar->isVisible()) {
+        // No vertical scrollbar.
+        return true;
+    }
+
+    QAbstractTextDocumentLayout *layout = document()->documentLayout();
+    int height = rect().height();
+    QScrollBar *hbar = horizontalScrollBar();
+    if (hbar && hbar->isVisible()) {
+        height -= hbar->height();
+    }
+
+    QRectF rect = layout->blockBoundingRect(p_block);
+    int y = contentOffsetY() + (int)rect.y();
+    int rectHeight = (int)rect.height();
+
+    return (y >= 0 && y < height) || (y < 0 && y + rectHeight > 0);
+}

+ 2 - 0
src/vedit.h

@@ -129,6 +129,8 @@ public:
     // Will not change current cursor.
     void makeBlockVisible(const QTextBlock &p_block);
 
+    bool isBlockVisible(const QTextBlock &p_block);
+
 signals:
     // Request VEditTab to save and exit edit mode.
     void saveAndRead();