From dd408d10e6826fc7c2a939309b8bbb935314dd2d Mon Sep 17 00:00:00 2001
From: Kornelia Poenitz <kpie@hrz.tu-chemnitz.de>
Date: Mon, 27 Jul 2009 11:26:35 +0200
Subject: [PATCH] FakeVim: Correct the selection in VisualBlockMode

The selection was not visible when moving cursor up.

Reviewed-by: hjk

Merge-request: 1016
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@nokia.com>
---
 src/plugins/fakevim/fakevimhandler.cpp | 27 +++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/src/plugins/fakevim/fakevimhandler.cpp b/src/plugins/fakevim/fakevimhandler.cpp
index ade45054a5a..10f1195c4a7 100644
--- a/src/plugins/fakevim/fakevimhandler.cpp
+++ b/src/plugins/fakevim/fakevimhandler.cpp
@@ -536,7 +536,8 @@ EventResult FakeVimHandler::Private::handleEvent(QKeyEvent *ev)
     //endEditBlock();
 
     // We fake vi-style end-of-line behaviour
-    m_fakeEnd = (atEndOfLine() && m_mode == CommandMode);
+    m_fakeEnd = (atEndOfLine() && m_mode == CommandMode
+                 && m_visualMode != VisualBlockMode);
 
     if (m_fakeEnd)
         moveLeft();
@@ -774,18 +775,22 @@ void FakeVimHandler::Private::updateSelection()
         } else if (m_visualMode == VisualBlockMode) {
             QTextCursor tc = m_tc;
             tc.setPosition(anchorPos);
+            int anchorColumn = tc.columnNumber();
+            int cursorColumn = m_tc.columnNumber();
+            int anchorRow    = tc.blockNumber();
+            int cursorRow    = m_tc.blockNumber();
+            int startColumn  = qMin(anchorColumn, cursorColumn);
+            int endColumn    = qMax(anchorColumn, cursorColumn);
+            int diffRow      = cursorRow - anchorRow;
+            if (anchorRow > cursorRow) {
+                tc.setPosition(cursorPos);
+                diffRow = -diffRow;
+            }
             tc.movePosition(StartOfLine, MoveAnchor);
-            QTextBlock anchorBlock = tc.block();
-            QTextBlock cursorBlock = m_tc.block();
-            int anchorColumn = anchorPos - anchorBlock.position();
-            int cursorColumn = cursorPos - cursorBlock.position();
-            int startColumn = qMin(anchorColumn, cursorColumn);
-            int endColumn = qMax(anchorColumn, cursorColumn);
-            int endPos = cursorBlock.position();
-            while (tc.position() <= endPos) {
+            for (int i = 0; i <= diffRow; ++i) {
                 if (startColumn < tc.block().length() - 1) {
-                    int last = qMin(tc.block().length() - 1, endColumn);
-                    int len = last - startColumn + 1;
+                    int last = qMin(tc.block().length(), endColumn + 1);
+                    int len = last - startColumn;
                     sel.cursor = tc;
                     sel.cursor.movePosition(Right, MoveAnchor, startColumn);
                     sel.cursor.movePosition(Right, KeepAnchor, len);
-- 
GitLab