From d03398f3e822056dba4a2bbf4cea80d463138d98 Mon Sep 17 00:00:00 2001
From: Alex Plate <aleksei.plate@jetbrains.com>
Date: Tue, 18 Oct 2022 16:16:35 +0300
Subject: [PATCH] [VIM-2774] Move reset mode to another handler

---
 .../idea/vim/action/ResetModeAction.kt        | 40 +++++++++++++------
 1 file changed, 28 insertions(+), 12 deletions(-)

diff --git a/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/action/ResetModeAction.kt b/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/action/ResetModeAction.kt
index 835b7bc57..dd6a4bfae 100644
--- a/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/action/ResetModeAction.kt
+++ b/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/action/ResetModeAction.kt
@@ -19,6 +19,7 @@ package com.maddyhome.idea.vim.action
 
 import com.maddyhome.idea.vim.KeyHandler
 import com.maddyhome.idea.vim.api.ExecutionContext
+import com.maddyhome.idea.vim.api.VimCaret
 import com.maddyhome.idea.vim.api.VimEditor
 import com.maddyhome.idea.vim.api.injector
 import com.maddyhome.idea.vim.command.Command
@@ -27,8 +28,33 @@ import com.maddyhome.idea.vim.command.VimStateMachine
 import com.maddyhome.idea.vim.handler.VimActionHandler
 import com.maddyhome.idea.vim.helper.mode
 
-class ResetModeAction : VimActionHandler.SingleExecution() {
+class ResetModeAction : VimActionHandler.ConditionalMulticaret() {
+  private lateinit var modeBeforeReset: VimStateMachine.Mode
   override val type: Command.Type = Command.Type.OTHER_WRITABLE
+  override fun runAsMulticaret(
+    editor: VimEditor,
+    context: ExecutionContext,
+    cmd: Command,
+    operatorArguments: OperatorArguments,
+  ): Boolean {
+    modeBeforeReset = editor.mode
+    KeyHandler.getInstance().fullReset(editor)
+    return true
+  }
+
+  override fun execute(
+    editor: VimEditor,
+    caret: VimCaret,
+    context: ExecutionContext,
+    cmd: Command,
+    operatorArguments: OperatorArguments,
+  ): Boolean {
+    if (modeBeforeReset == VimStateMachine.Mode.INSERT) {
+      val position = injector.motion.getOffsetOfHorizontalMotion(editor, caret, -1, false)
+      caret.moveToOffset(position)
+    }
+    return true
+  }
 
   override fun execute(
     editor: VimEditor,
@@ -36,16 +62,6 @@ class ResetModeAction : VimActionHandler.SingleExecution() {
     cmd: Command,
     operatorArguments: OperatorArguments,
   ): Boolean {
-    val modeBeforeReset = editor.mode
-    KeyHandler.getInstance().fullReset(editor)
-
-    if (modeBeforeReset == VimStateMachine.Mode.INSERT) {
-      editor.forEachCaret { caret ->
-        val position = injector.motion.getOffsetOfHorizontalMotion(editor, caret, -1, false)
-        caret.moveToOffset(position)
-      }
-    }
-
-    return true
+    error("This method should not be used")
   }
 }