[frameworks/ktexteditor] src/vimode/modes: vi-mode: Simplify switching to visual modes

Christoph Cullmann <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit b288396ff4bc85e917c8e2c4278a54d228357d9f by Christoph Cullmann, on behalf of Ismael Asensio.
Committed on 20/07/2026 at 21:22.
Pushed by cullmann into branch 'master'.

vi-mode: Simplify switching to visual modes

Collapse the three identical methods that switch to the different visual
modes into a single method that takes the mode as a parameter. This way
we can ensure that all cases and combinations are being considered

M  +10   -33   src/vimode/modes/modebase.cpp
M  +1    -3    src/vimode/modes/modebase.h
M  +7    -22   src/vimode/modes/normalvimode.cpp

https://invent.kde.org/frameworks/ktexteditor/-/commit/b288396ff4bc85e917c8e2c4278a54d228357d9f

diff --git a/src/vimode/modes/modebase.cpp b/src/vimode/modes/modebase.cpp
index 5dbcf0b1f..cc8fe93ee 100644
--- a/src/vimode/modes/modebase.cpp
+++ b/src/vimode/modes/modebase.cpp
@@ -1045,44 +1045,21 @@ bool ModeBase::startReplaceMode()
     return true;
 }
 
-bool ModeBase::startVisualMode()
+bool ModeBase::startVisualMode(ViMode mode)
 {
-    if (m_viInputModeManager->getCurrentViMode() == ViMode::VisualLineMode) {
-        m_viInputModeManager->getViVisualMode()->setVisualModeType(ViMode::VisualMode);
-        m_viInputModeManager->changeViMode(ViMode::VisualMode);
-    } else if (m_viInputModeManager->getCurrentViMode() == ViMode::VisualBlockMode) {
-        m_viInputModeManager->getViVisualMode()->setVisualModeType(ViMode::VisualMode);
-        m_viInputModeManager->changeViMode(ViMode::VisualMode);
-    } else {
-        m_viInputModeManager->viEnterVisualMode();
-    }
-
-    Q_EMIT m_view->viewModeChanged(m_view, m_view->viewMode());
+    Q_ASSERT(mode == ViMode::VisualMode || mode == ViMode::VisualLineMode || mode == ViMode::VisualBlockMode);
 
-    return true;
-}
-
-bool ModeBase::startVisualBlockMode()
-{
-    if (m_viInputModeManager->getCurrentViMode() == ViMode::VisualMode) {
-        m_viInputModeManager->getViVisualMode()->setVisualModeType(ViMode::VisualBlockMode);
-        m_viInputModeManager->changeViMode(ViMode::VisualBlockMode);
-    } else {
-        m_viInputModeManager->viEnterVisualMode(ViMode::VisualBlockMode);
+    if (m_viInputModeManager->getCurrentViMode() == mode) {
+        // Using a mode switch command from within that mode: reset back to normal
+        m_viInputModeManager->getViVisualMode()->reset();
+        return true;
     }
 
-    Q_EMIT m_view->viewModeChanged(m_view, m_view->viewMode());
-
-    return true;
-}
-
-bool ModeBase::startVisualLineMode()
-{
-    if (m_viInputModeManager->getCurrentViMode() == ViMode::VisualMode) {
-        m_viInputModeManager->getViVisualMode()->setVisualModeType(ViMode::VisualLineMode);
-        m_viInputModeManager->changeViMode(ViMode::VisualLineMode);
+    if (m_viInputModeManager->isAnyVisualMode()) {
+        m_viInputModeManager->changeViMode(mode);
+        m_viInputModeManager->getViVisualMode()->setVisualModeType(mode);
     } else {
-        m_viInputModeManager->viEnterVisualMode(ViMode::VisualLineMode);
+        m_viInputModeManager->viEnterVisualMode(mode);
     }
 
     Q_EMIT m_view->viewModeChanged(m_view, m_view->viewMode());
diff --git a/src/vimode/modes/modebase.h b/src/vimode/modes/modebase.h
index 839bf3322..ce51be1ab 100644
--- a/src/vimode/modes/modebase.h
+++ b/src/vimode/modes/modebase.h
@@ -115,10 +115,8 @@ protected:
 
     bool startNormalMode();
     bool startInsertMode();
-    bool startVisualMode();
-    bool startVisualLineMode();
-    bool startVisualBlockMode();
     bool startReplaceMode();
+    bool startVisualMode(ViMode mode);
 
     QChar getChosenRegister(const QChar &defaultReg) const;
     QString getRegisterContent(const QChar &reg);
diff --git a/src/vimode/modes/normalvimode.cpp b/src/vimode/modes/normalvimode.cpp
index 442caf2cf..f5abefdf6 100644
--- a/src/vimode/modes/normalvimode.cpp
+++ b/src/vimode/modes/normalvimode.cpp
@@ -575,24 +575,19 @@ bool NormalViMode::commandEnterInsertModeLast()
     return startInsertMode();
 }
 
-bool NormalViMode::commandEnterVisualLineMode()
+bool NormalViMode::commandEnterVisualMode()
 {
-    if (m_viInputModeManager->getCurrentViMode() == VisualLineMode) {
-        reset();
-        return true;
-    }
+    return startVisualMode(ViMode::VisualMode);
+}
 
-    return startVisualLineMode();
+bool NormalViMode::commandEnterVisualLineMode()
+{
+    return startVisualMode(ViMode::VisualLineMode);
 }
 
 bool NormalViMode::commandEnterVisualBlockMode()
 {
-    if (m_viInputModeManager->getCurrentViMode() == VisualBlockMode) {
-        reset();
-        return true;
-    }
-
-    return startVisualBlockMode();
+    return startVisualMode(ViMode::VisualBlockMode);
 }
 
 bool NormalViMode::commandReselectVisual()
@@ -630,16 +625,6 @@ bool NormalViMode::commandReselectVisual()
     return false;
 }
 
-bool NormalViMode::commandEnterVisualMode()
-{
-    if (m_viInputModeManager->getCurrentViMode() == ViMode::VisualMode) {
-        reset();
-        return true;
-    }
-
-    return startVisualMode();
-}
-
 bool NormalViMode::commandToOtherEnd()
 {
     if (m_viInputModeManager->isAnyVisualMode()) {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.