Skip to content

Commit

Permalink
Scroll caret into view after undo/redo
Browse files Browse the repository at this point in the history
This is to ensure that 'scrolloff' is applied. Relates to VIM-3671
  • Loading branch information
citizenmatt committed Oct 18, 2024
1 parent 755cd74 commit af67b05
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,4 +21,15 @@ class RedoCommandTest : VimTestCase() {
val command = injector.vimscriptParser.parseCommand("redo")
assertTrue(command is RedoCommand)
}
}

@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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,4 +21,15 @@ class UndoCommandTest : VimTestCase() {
val command = injector.vimscriptParser.parseCommand("undo")
assertTrue(command is UndoCommand)
}
}

@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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class RedoAction : VimActionHandler.SingleExecution() {
while ((--count > 0) && result) {
result = injector.undo.redo(editor, context)
}
injector.scroll.scrollCaretIntoView(editor)
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class UndoAction : VimActionHandler.SingleExecution() {
while ((--count > 0) && result) {
result = injector.undo.undo(editor, context)
}
injector.scroll.scrollCaretIntoView(editor)
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit af67b05

Please sign in to comment.