Skip to content

Commit

Permalink
Remove EDT and write action from the VimTestCase
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPl292 committed Feb 21, 2025
1 parent 24c0d31 commit 564ab7d
Show file tree
Hide file tree
Showing 56 changed files with 1,718 additions and 1,162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.intellij.openapi.editor.CaretVisualAttributes
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.ex.EditorEx
import com.intellij.openapi.editor.ex.EditorSettingsExternalizable
import com.intellij.util.concurrency.annotations.RequiresEdt
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.globalOptions
Expand Down Expand Up @@ -88,7 +89,9 @@ private fun isBlockCursorOverride() = EditorSettingsExternalizable.getInstance()
private fun Editor.updatePrimaryCaretVisualAttributes() {
if (VimPlugin.isNotEnabled()) thisLogger().error("The caret attributes should not be updated if the IdeaVim is disabled")
if (isIdeaVimDisabledHere) return
caretModel.primaryCaret.visualAttributes = AttributesCache.getCaretVisualAttributes(this)
ApplicationManager.getApplication().invokeAndWait {
caretModel.primaryCaret.visualAttributes = AttributesCache.getCaretVisualAttributes(this)
}

// Make sure the caret is visible as soon as it's set. It might be invisible while blinking
// NOTE: At the moment, this causes project leak in tests
Expand Down Expand Up @@ -163,12 +166,14 @@ class CaretVisualAttributesListener : IsReplaceCharListener, ModeChangeListener,
updateCaretsVisual(editor)
}

@RequiresEdt
private fun updateCaretsVisual(editor: VimEditor) {
val ijEditor = (editor as IjVimEditor).editor
ijEditor.updateCaretsVisualAttributes()
ijEditor.updateCaretsVisualPosition()
}

@RequiresEdt
fun updateAllEditorsCaretsVisual() {
injector.editorGroup.getEditors().forEach { editor ->
updateCaretsVisual(editor)
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/com/maddyhome/idea/vim/helper/InlayHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import com.intellij.openapi.editor.Caret
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.LogicalPosition
import com.intellij.openapi.editor.VisualPosition
import com.intellij.util.concurrency.annotations.RequiresEdt
import com.maddyhome.idea.vim.api.injector

/**
* Move the caret to the given offset, handling inline inlays
Expand All @@ -35,6 +37,7 @@ import com.intellij.openapi.editor.VisualPosition
* It is recommended to call this method even if the caret hasn't been moved. It will handle the situation where the
* document has been changed to add an inlay at the caret position, and will move the caret appropriately.
*/
@RequiresEdt
internal fun Caret.moveToInlayAwareOffset(offset: Int) {
// If the target is inside a fold, call the standard moveToOffset to expand and move
if (editor.foldingModel.isOffsetCollapsed(offset) || !editor.hasBlockOrUnderscoreCaret()) {
Expand All @@ -51,6 +54,7 @@ internal fun Caret.moveToInlayAwareLogicalPosition(pos: LogicalPosition) {
moveToInlayAwareOffset(editor.logicalPositionToOffset(pos))
}

@RequiresEdt
private fun getVisualPositionForTextAtOffset(editor: Editor, offset: Int): VisualPosition {
var logicalPosition = editor.offsetToLogicalPosition(offset)
val e = if (editor is EditorWindow) {
Expand Down Expand Up @@ -81,15 +85,18 @@ internal fun Editor.amountOfInlaysBeforeVisualPosition(pos: VisualPosition): Int
return this.inlayModel.getInlineElementsInRange(lineStartOffset, offset).size
}

@RequiresEdt
internal fun Editor.updateCaretsVisualPosition() {
// Caret visual position depends on the current mode, especially with respect to inlays. E.g. if an inlay is
// related to preceding text, the caret is placed between inlay and preceding text in insert mode (usually bar
// caret) but after the inlay in normal mode (block caret).
// By repositioning to the same offset, we will recalculate the expected visual position and put the caret in the
// right location. Don't open a fold if the caret is inside
this.vimForEachCaret {
if (!this.foldingModel.isOffsetCollapsed(it.offset)) {
it.moveToInlayAwareOffset(it.offset)
injector.application.runReadAction {
this.vimForEachCaret {
if (!this.foldingModel.isOffsetCollapsed(it.offset)) {
it.moveToInlayAwareOffset(it.offset)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ internal object VimListenerManager {
injector.listenersNotifier.myEditorListeners.add(caretVisualAttributesListener)
injector.listenersNotifier.modeChangeListeners.add(caretVisualAttributesListener)
injector.listenersNotifier.isReplaceCharListeners.add(caretVisualAttributesListener)
caretVisualAttributesListener.updateAllEditorsCaretsVisual()
ApplicationManager.getApplication().invokeAndWait {
caretVisualAttributesListener.updateAllEditorsCaretsVisual()
}

val insertTimeRecorder = InsertTimeRecorder()
injector.listenersNotifier.modeChangeListeners.add(insertTimeRecorder)
Expand Down Expand Up @@ -328,7 +330,9 @@ internal object VimListenerManager {
injector.listenersNotifier.notifyEditorCreated(vimEditor)

Disposer.register(perEditorDisposable) {
VimPlugin.getEditor().editorDeinit(editor)
ApplicationManager.getApplication().invokeLater {
VimPlugin.getEditor().editorDeinit(editor)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.jetbrains.plugins.ideavim.action

import com.intellij.idea.TestFor
import com.intellij.openapi.application.ApplicationManager
import com.maddyhome.idea.vim.KeyHandler
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.api.injector
Expand Down Expand Up @@ -185,7 +186,9 @@ class CopyActionTest : VimTestCase() {
""".trimIndent(),
)
kotlin.test.assertEquals(0, editor.caretModel.offset)
ApplicationManager.getApplication().runReadAction {
kotlin.test.assertEquals(0, editor.caretModel.offset)
}
}

// VIM-632 |CTRL-V| |v_y| |p|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class MacroActionTest : VimTestCase() {
typeText("@q")
}
}
assertEquals(ExceptionHandler.exceptionMessage, exception.cause!!.cause!!.message)
assertEquals(ExceptionHandler.exceptionMessage, exception.cause!!.cause!!.cause!!.message)

assertTrue(KeyHandler.getInstance().keyStack.isEmpty())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.jetbrains.plugins.ideavim.action

import com.intellij.idea.TestFor
import com.intellij.openapi.application.ApplicationManager
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.action.motion.search.SearchWholeWordForwardAction
import com.maddyhome.idea.vim.api.injector
Expand Down Expand Up @@ -2184,15 +2185,17 @@ rtyfg${c}hzxc"""
val vimEditor = fixture.editor.vim
val context = injector.executionContextManager.getEditorExecutionContext(vimEditor)
injector.registerGroup.storeText(vimEditor, context, '*', "fgh")
VimPlugin.getRegister()
.storeText(
IjVimEditor(editor),
context,
editor.vim.primaryCaret(),
TextRange(16, 19),
SelectionType.CHARACTER_WISE,
false
)
ApplicationManager.getApplication().runWriteAction {
VimPlugin.getRegister()
.storeText(
IjVimEditor(editor),
context,
editor.vim.primaryCaret(),
TextRange(16, 19),
SelectionType.CHARACTER_WISE,
false
)
}
typeText(injector.parser.parseKeys("\"*P"))
val after = "fg${c}hqfg${c}hwe asd zxc rty fg${c}hfgh vbn"
assertState(after)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package org.jetbrains.plugins.ideavim.action

import com.intellij.openapi.application.ApplicationManager
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.command.MappingMode
Expand All @@ -27,7 +28,9 @@ class ResetModeActionTest : VimTestCase() {
val before = "Lorem Ipsum"
val after = "Lorem Ipsum"
doTest(keys, before, after, Mode.NORMAL())
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
ApplicationManager.getApplication().runReadAction {
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
}
}

@Test
Expand All @@ -36,7 +39,9 @@ class ResetModeActionTest : VimTestCase() {
val before = "Lorem Ipsum"
val after = "Lorem Ipsum"
doTest(keys, before, after, Mode.NORMAL())
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
ApplicationManager.getApplication().runReadAction {
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
}
}

@Test
Expand All @@ -45,7 +50,9 @@ class ResetModeActionTest : VimTestCase() {
val before = "A Disc${c}overy"
val after = "A Dis${c}covery"
doTest(keys, before, after, Mode.NORMAL())
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
ApplicationManager.getApplication().runReadAction {
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
}
}

@Test
Expand All @@ -54,7 +61,9 @@ class ResetModeActionTest : VimTestCase() {
val before = "${c}Lorem Ipsum"
val after = "Lorem Ipsum"
doTest(keys, before, after, Mode.NORMAL())
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
ApplicationManager.getApplication().runReadAction {
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
}
}

@Test
Expand All @@ -63,7 +72,9 @@ class ResetModeActionTest : VimTestCase() {
val before = "Lorem Ipsum"
val after = "Lorem Ipsum"
doTest(keys, before, after, Mode.NORMAL())
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
ApplicationManager.getApplication().runReadAction {
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
}
}

@Test
Expand All @@ -72,7 +83,9 @@ class ResetModeActionTest : VimTestCase() {
val before = "Lorem Ipsum"
val after = "Lorem Ipsum"
doTest(keys, before, after, Mode.NORMAL())
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
ApplicationManager.getApplication().runReadAction {
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
}
}

@Test
Expand All @@ -81,7 +94,9 @@ class ResetModeActionTest : VimTestCase() {
val before = "Lorem Ipsum"
val after = "Lorem Ipsum"
doTest(keys, before, after, Mode.NORMAL())
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
ApplicationManager.getApplication().runReadAction {
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
}
}

@Test
Expand All @@ -90,7 +105,9 @@ class ResetModeActionTest : VimTestCase() {
val before = "Lorem Ipsum"
val after = "Ipsum"
doTest(keys, before, after, Mode.NORMAL())
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
ApplicationManager.getApplication().runReadAction {
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
}
}

@Test
Expand All @@ -99,7 +116,9 @@ class ResetModeActionTest : VimTestCase() {
val before = "Lorem Ipsum"
val after = "Ipsum"
doTest(keys, before, after, Mode.NORMAL())
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
ApplicationManager.getApplication().runReadAction {
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
}
}

@Test
Expand All @@ -108,7 +127,9 @@ class ResetModeActionTest : VimTestCase() {
val before = "Lorem Ipsum"
val after = "Ipsum"
doTest(keys, before, after, Mode.NORMAL())
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
ApplicationManager.getApplication().runReadAction {
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
}
}

@TestWithoutNeovim(SkipNeovimReason.CTRL_CODES)
Expand All @@ -118,7 +139,9 @@ class ResetModeActionTest : VimTestCase() {
val before = "Lorem Ipsum"
val after = "Ipsum"
doTest(keys, before, after, Mode.NORMAL())
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
ApplicationManager.getApplication().runReadAction {
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
}
}

@TestWithoutNeovim(SkipNeovimReason.MAPPING)
Expand All @@ -137,7 +160,9 @@ class ResetModeActionTest : VimTestCase() {
val before = "Lorem Ipsum"
val after = "Ipsum"
doTest(keys, before, after, Mode.NORMAL())
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
ApplicationManager.getApplication().runReadAction {
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
}
}

@Test
Expand All @@ -146,7 +171,9 @@ class ResetModeActionTest : VimTestCase() {
val before = "Lorem Ipsum"
val after = "Lnotherorem Ipsum"
doTest(keys, before, after, Mode.INSERT)
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
ApplicationManager.getApplication().runReadAction {
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
}
}

@Test
Expand All @@ -155,6 +182,8 @@ class ResetModeActionTest : VimTestCase() {
val before = "A ${c}Discovery"
val after = "A "
doTest(keys, before, after, Mode.NORMAL())
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
ApplicationManager.getApplication().runReadAction {
kotlin.test.assertFalse(fixture.editor.selectionModel.hasSelection())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.jetbrains.plugins.ideavim.action.change

import com.intellij.idea.TestFor
import com.intellij.openapi.application.ApplicationManager
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.newapi.globalIjOptions
import com.maddyhome.idea.vim.state.mode.Mode
Expand All @@ -31,7 +32,9 @@ class UndoActionTest : VimTestCase() {
val after = before
doTest(keys, before, after, Mode.NORMAL())
val editor = fixture.editor
kotlin.test.assertFalse(editor.caretModel.primaryCaret.hasSelection())
ApplicationManager.getApplication().runReadAction {
kotlin.test.assertFalse(editor.caretModel.primaryCaret.hasSelection())
}
}

@Test
Expand All @@ -56,7 +59,9 @@ class UndoActionTest : VimTestCase() {
Cras id tellus in ex imperdiet egestas.
""".trimIndent()
doTest(keys, before, after, Mode.NORMAL())
kotlin.test.assertFalse(hasSelection())
ApplicationManager.getApplication().runReadAction {
kotlin.test.assertFalse(hasSelection())
}
}
}

Expand All @@ -80,7 +85,9 @@ class UndoActionTest : VimTestCase() {
Cras id tellus in ex imperdiet egestas.
""".trimIndent()
doTest(keys, before, after, Mode.NORMAL())
kotlin.test.assertFalse(hasSelection())
ApplicationManager.getApplication().runReadAction {
kotlin.test.assertFalse(hasSelection())
}
}

@Test
Expand All @@ -105,7 +112,9 @@ class UndoActionTest : VimTestCase() {
Cras id tellus in ex imperdiet egestas.
""".trimIndent()
doTest(keys, before, after, Mode.NORMAL())
kotlin.test.assertFalse(hasSelection())
ApplicationManager.getApplication().runReadAction {
kotlin.test.assertFalse(hasSelection())
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package org.jetbrains.plugins.ideavim.action.change.delete

import com.intellij.openapi.application.ApplicationManager
import org.jetbrains.plugins.ideavim.VimTestCase
import org.junit.jupiter.api.Test

Expand Down Expand Up @@ -118,7 +119,9 @@ class DeleteCharacterLeftActionTest : VimTestCase() {
// Scroll 70 characters to the left. First character on line should now be 71. sidescrolloff puts us at 76
typeText("70zl")
assertVisualPosition(0, 75)
assertVisibleLineBounds(0, 70, 149)
ApplicationManager.getApplication().invokeAndWait {
assertVisibleLineBounds(0, 70, 149)
}

typeText("20X")

Expand Down
Loading

0 comments on commit 564ab7d

Please sign in to comment.