Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fixes for incsearch highlighting and caret/scroll position for undo #1010

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ private fun updateSearchHighlights(
// Update highlights in all visible editors. We update non-visible editors when they get focus.
// Note that this now includes all editors - main, diff windows, even toolwindows like the Commit editor and consoles
val editors = injector.editorGroup.getEditors().filter {
injector.application.isUnitTest() || it.ij.component.isShowing
(injector.application.isUnitTest() || it.ij.component.isShowing)
&& (currentEditor == null || it.projectId == currentEditor.projectId)
}

editors.forEach {
Expand Down
23 changes: 17 additions & 6 deletions src/main/java/com/maddyhome/idea/vim/helper/UndoRedoHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import com.intellij.openapi.command.CommandProcessor
import com.intellij.openapi.command.undo.UndoManager
import com.intellij.openapi.components.Service
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.fileEditor.TextEditor
import com.intellij.openapi.fileEditor.TextEditorWithPreview
import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.registry.Registry
Expand Down Expand Up @@ -42,13 +44,13 @@ internal class UndoRedoHelper : UndoRedoBase() {
override fun undo(editor: VimEditor, context: ExecutionContext): Boolean {
val ijContext = context.context as DataContext
val project = PlatformDataKeys.PROJECT.getData(ijContext) ?: return false
val fileEditor = TextEditorProvider.getInstance().getTextEditor(editor.ij)
val textEditor = getTextEditor(editor.ij)
val undoManager = UndoManager.getInstance(project)
if (undoManager.isUndoAvailable(fileEditor)) {
if (undoManager.isUndoAvailable(textEditor)) {
val scrollingModel = editor.getScrollingModel()
scrollingModel.accumulateViewportChanges()

performUndo(editor, undoManager, fileEditor)
performUndo(editor, undoManager, textEditor)

scrollingModel.flushViewportChanges()

Expand All @@ -57,6 +59,15 @@ internal class UndoRedoHelper : UndoRedoBase() {
return false
}

private fun getTextEditor(editor: Editor): TextEditor {
// If the Editor is hosted in a TextEditor with a preview, then TextEditorProvider will return a TextEditor for the
// hosted instance, not for the main editor that also contains the preview. If we pass the inner TextEditor to the
// UndoManager, it doesn't correctly restore state. Specifically, the change is undone/redone, but the caret is not
// moved. See VIM-3671.
val currentTextEditor = TextEditorProvider.getInstance().getTextEditor(editor)
return TextEditorWithPreview.getParentSplitEditor(currentTextEditor) as? TextEditor ?: currentTextEditor
}

private fun performUndo(
editor: VimEditor,
undoManager: UndoManager,
Expand Down Expand Up @@ -112,10 +123,10 @@ internal class UndoRedoHelper : UndoRedoBase() {
override fun redo(editor: VimEditor, context: ExecutionContext): Boolean {
val ijContext = context.context as DataContext
val project = PlatformDataKeys.PROJECT.getData(ijContext) ?: return false
val fileEditor = TextEditorProvider.getInstance().getTextEditor(editor.ij)
val textEditor = getTextEditor(editor.ij)
val undoManager = UndoManager.getInstance(project)
if (undoManager.isRedoAvailable(fileEditor)) {
performRedo(undoManager, fileEditor, editor)
if (undoManager.isRedoAvailable(textEditor)) {
performRedo(undoManager, textEditor, editor)

return true
}
Expand Down
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
}
}
Loading