From af67b059e69941ab018b17751e5d6f538490e0b3 Mon Sep 17 00:00:00 2001 From: Matt Ellis Date: Fri, 18 Oct 2024 16:19:24 +0300 Subject: [PATCH] Scroll caret into view after undo/redo This is to ensure that 'scrolloff' is applied. Relates to VIM-3671 --- .../ideavim/action/change/UndoActionTest.kt | 10 ++++++++++ .../ex/implementation/commands/RedoCommandTest.kt | 14 +++++++++++++- .../ex/implementation/commands/UndoCommandTest.kt | 14 +++++++++++++- .../maddyhome/idea/vim/action/change/RedoAction.kt | 1 + .../maddyhome/idea/vim/action/change/UndoAction.kt | 1 + .../vim/vimscript/model/commands/RedoCommand.kt | 5 ++++- .../vim/vimscript/model/commands/UndoCommand.kt | 5 ++++- 7 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/jetbrains/plugins/ideavim/action/change/UndoActionTest.kt b/src/test/java/org/jetbrains/plugins/ideavim/action/change/UndoActionTest.kt index 28daf1b9a6..8e92d64c90 100644 --- a/src/test/java/org/jetbrains/plugins/ideavim/action/change/UndoActionTest.kt +++ b/src/test/java/org/jetbrains/plugins/ideavim/action/change/UndoActionTest.kt @@ -162,6 +162,16 @@ class UndoActionTest : VimTestCase() { configureByText("Lorem ${c}ipsum dolor sit amet") } + @Test + @TestFor(issues = ["VIM-3671"]) + fun `test undo scrolls caret to reset scrolloff`() { + configureByLines(200, "lorem ipsum dolor sit amet") + enterCommand("set scrolloff=10") + typeText("50G", "dd", "G", "u") + assertPosition(49, 0) + assertVisibleArea(39, 73) + } + private fun hasSelection(): Boolean { val editor = fixture.editor return editor.caretModel.primaryCaret.hasSelection() diff --git a/src/test/java/org/jetbrains/plugins/ideavim/ex/implementation/commands/RedoCommandTest.kt b/src/test/java/org/jetbrains/plugins/ideavim/ex/implementation/commands/RedoCommandTest.kt index ea440c84fd..51a2baef26 100644 --- a/src/test/java/org/jetbrains/plugins/ideavim/ex/implementation/commands/RedoCommandTest.kt +++ b/src/test/java/org/jetbrains/plugins/ideavim/ex/implementation/commands/RedoCommandTest.kt @@ -8,6 +8,7 @@ package org.jetbrains.plugins.ideavim.ex.implementation.commands +import com.intellij.idea.TestFor import com.maddyhome.idea.vim.api.injector import com.maddyhome.idea.vim.vimscript.model.commands.RedoCommand import org.jetbrains.plugins.ideavim.VimTestCase @@ -20,4 +21,15 @@ class RedoCommandTest : VimTestCase() { val command = injector.vimscriptParser.parseCommand("redo") assertTrue(command is RedoCommand) } -} \ No newline at end of file + + @Test + @TestFor(issues = ["VIM-3671"]) + fun `test redo scrolls caret to reset scrolloff`() { + configureByLines(200, "lorem ipsum dolor sit amet") + enterCommand("set scrolloff=10") + typeText("50G", "dd", "u", "G") + enterCommand("redo") + assertPosition(49, 0) + assertVisibleArea(39, 73) + } +} diff --git a/src/test/java/org/jetbrains/plugins/ideavim/ex/implementation/commands/UndoCommandTest.kt b/src/test/java/org/jetbrains/plugins/ideavim/ex/implementation/commands/UndoCommandTest.kt index 5e80d9a4cb..b8939ec65c 100644 --- a/src/test/java/org/jetbrains/plugins/ideavim/ex/implementation/commands/UndoCommandTest.kt +++ b/src/test/java/org/jetbrains/plugins/ideavim/ex/implementation/commands/UndoCommandTest.kt @@ -8,6 +8,7 @@ package org.jetbrains.plugins.ideavim.ex.implementation.commands +import com.intellij.idea.TestFor import com.maddyhome.idea.vim.api.injector import com.maddyhome.idea.vim.vimscript.model.commands.UndoCommand import org.jetbrains.plugins.ideavim.VimTestCase @@ -20,4 +21,15 @@ class UndoCommandTest : VimTestCase() { val command = injector.vimscriptParser.parseCommand("undo") assertTrue(command is UndoCommand) } -} \ No newline at end of file + + @Test + @TestFor(issues = ["VIM-3671"]) + fun `test undo scrolls caret to reset scrolloff`() { + configureByLines(200, "lorem ipsum dolor sit amet") + enterCommand("set scrolloff=10") + typeText("50G", "dd", "G") + enterCommand("undo") + assertPosition(49, 0) + assertVisibleArea(39, 73) + } +} diff --git a/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/action/change/RedoAction.kt b/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/action/change/RedoAction.kt index a0e606b4c4..ffa99d9ffe 100644 --- a/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/action/change/RedoAction.kt +++ b/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/action/change/RedoAction.kt @@ -31,6 +31,7 @@ class RedoAction : VimActionHandler.SingleExecution() { while ((--count > 0) && result) { result = injector.undo.redo(editor, context) } + injector.scroll.scrollCaretIntoView(editor) return result } } diff --git a/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/action/change/UndoAction.kt b/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/action/change/UndoAction.kt index 1b1d7f7280..d74266c985 100644 --- a/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/action/change/UndoAction.kt +++ b/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/action/change/UndoAction.kt @@ -31,6 +31,7 @@ class UndoAction : VimActionHandler.SingleExecution() { while ((--count > 0) && result) { result = injector.undo.undo(editor, context) } + injector.scroll.scrollCaretIntoView(editor) return result } } diff --git a/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/vimscript/model/commands/RedoCommand.kt b/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/vimscript/model/commands/RedoCommand.kt index f22e39ac73..99b742f9c7 100644 --- a/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/vimscript/model/commands/RedoCommand.kt +++ b/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/vimscript/model/commands/RedoCommand.kt @@ -23,6 +23,9 @@ import com.maddyhome.idea.vim.vimscript.model.ExecutionResult data class RedoCommand(val range: Range, val argument: String) : Command.SingleExecution(range, argument) { override val argFlags: CommandHandlerFlags = flags(RangeFlag.RANGE_FORBIDDEN, ArgumentFlag.ARGUMENT_FORBIDDEN, Access.WRITABLE) override fun processCommand(editor: VimEditor, context: ExecutionContext, operatorArguments: OperatorArguments): ExecutionResult { - return if (injector.undo.redo(editor, context)) ExecutionResult.Success else ExecutionResult.Error + return if (injector.undo.redo(editor, context)) { + injector.scroll.scrollCaretIntoView(editor) + ExecutionResult.Success + } else ExecutionResult.Error } } diff --git a/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/vimscript/model/commands/UndoCommand.kt b/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/vimscript/model/commands/UndoCommand.kt index 4709e9222b..72f9955bc8 100644 --- a/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/vimscript/model/commands/UndoCommand.kt +++ b/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/vimscript/model/commands/UndoCommand.kt @@ -24,6 +24,9 @@ data class UndoCommand(val range: Range, val argument: String) : Command.SingleE override val argFlags: CommandHandlerFlags = flags(RangeFlag.RANGE_FORBIDDEN, ArgumentFlag.ARGUMENT_FORBIDDEN, Access.WRITABLE) override fun processCommand(editor: VimEditor, context: ExecutionContext, operatorArguments: OperatorArguments): ExecutionResult { - return if (injector.undo.undo(editor, context)) ExecutionResult.Success else ExecutionResult.Error + return if (injector.undo.undo(editor, context)) { + injector.scroll.scrollCaretIntoView(editor) + ExecutionResult.Success + } else ExecutionResult.Error } }