diff --git a/src/main/java/com/maddyhome/idea/vim/newapi/IjVimCaret.kt b/src/main/java/com/maddyhome/idea/vim/newapi/IjVimCaret.kt index 24453d81a..bbcc13c20 100644 --- a/src/main/java/com/maddyhome/idea/vim/newapi/IjVimCaret.kt +++ b/src/main/java/com/maddyhome/idea/vim/newapi/IjVimCaret.kt @@ -22,6 +22,7 @@ import com.maddyhome.idea.vim.api.VimCaret import com.maddyhome.idea.vim.api.VimCaretBase import com.maddyhome.idea.vim.api.VimEditor import com.maddyhome.idea.vim.api.VimVisualPosition +import com.maddyhome.idea.vim.api.injector import com.maddyhome.idea.vim.common.EditorLine import com.maddyhome.idea.vim.common.LiveRange import com.maddyhome.idea.vim.common.Offset @@ -88,7 +89,7 @@ internal class IjVimCaret(val caret: Caret) : VimCaretBase() { override val editor: VimEditor get() = IjVimEditor(caret.editor) override val offset: Offset - get() = caret.offset.offset + get() = injector.application.runReadAction { caret.offset.offset } override var vimLastColumn: Int get() = caret.vimLastColumn set(value) { diff --git a/src/main/java/com/maddyhome/idea/vim/newapi/IjVimEditor.kt b/src/main/java/com/maddyhome/idea/vim/newapi/IjVimEditor.kt index e3a9ee2d7..1711dca80 100644 --- a/src/main/java/com/maddyhome/idea/vim/newapi/IjVimEditor.kt +++ b/src/main/java/com/maddyhome/idea/vim/newapi/IjVimEditor.kt @@ -35,6 +35,7 @@ import com.maddyhome.idea.vim.api.VimScrollingModel import com.maddyhome.idea.vim.api.VimSelectionModel import com.maddyhome.idea.vim.api.VimVisualPosition import com.maddyhome.idea.vim.api.VirtualFile +import com.maddyhome.idea.vim.api.injector import com.maddyhome.idea.vim.command.OperatorArguments import com.maddyhome.idea.vim.common.EditorLine import com.maddyhome.idea.vim.common.IndentConfig @@ -428,7 +429,9 @@ internal class IjVimEditor(editor: Editor) : MutableLinearEditor() { } override fun createLiveMarker(start: Offset, end: Offset): LiveRange { - return editor.document.createRangeMarker(start.point, end.point).vim + return injector.application.runReadAction { + editor.document.createRangeMarker(start.point, end.point).vim + } } /** diff --git a/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/api/VimChangeGroupBase.kt b/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/api/VimChangeGroupBase.kt index 73e43fe5c..47682f12a 100644 --- a/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/api/VimChangeGroupBase.kt +++ b/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/api/VimChangeGroupBase.kt @@ -432,57 +432,59 @@ public abstract class VimChangeGroupBase : VimChangeGroup { * @param mode The mode - indicate insert or replace */ override fun initInsert(editor: VimEditor, context: ExecutionContext, mode: Mode) { - val state = getInstance(editor) - for (caret in editor.nativeCarets()) { - caret.vimInsertStart = editor.createLiveMarker(caret.offset, caret.offset) - injector.markService.setMark(caret, MARK_CHANGE_START, caret.offset.point) - } - val cmd = state.executingCommand - if (cmd != null && state.isDotRepeatInProgress) { - state.mode = mode - if (mode == Mode.REPLACE) { - editor.insertMode = false + injector.application.invokeAndWait { + val state = getInstance(editor) + for (caret in editor.nativeCarets()) { + caret.vimInsertStart = editor.createLiveMarker(caret.offset, caret.offset) + injector.markService.setMark(caret, MARK_CHANGE_START, caret.offset.point) } - if (cmd.flags.contains(CommandFlags.FLAG_NO_REPEAT_INSERT)) { - val commandState = getInstance(editor) - repeatInsert( - editor, - context, - 1, - false, - OperatorArguments(false, 1, commandState.mode), - ) + val cmd = state.executingCommand + if (cmd != null && state.isDotRepeatInProgress) { + state.mode = mode + if (mode == Mode.REPLACE) { + editor.insertMode = false + } + if (cmd.flags.contains(CommandFlags.FLAG_NO_REPEAT_INSERT)) { + val commandState = getInstance(editor) + repeatInsert( + editor, + context, + 1, + false, + OperatorArguments(false, 1, commandState.mode), + ) + } else { + val commandState = getInstance(editor) + repeatInsert( + editor, + context, + cmd.count, + false, + OperatorArguments(false, cmd.count, commandState.mode), + ) + } + if (mode == Mode.REPLACE) { + editor.insertMode = true + } + state.mode = Mode.NORMAL() } else { - val commandState = getInstance(editor) - repeatInsert( - editor, - context, - cmd.count, - false, - OperatorArguments(false, cmd.count, commandState.mode), - ) + lastInsert = cmd + strokes.clear() + repeatCharsCount = 0 + val myVimDocument = vimDocument + if (myVimDocument != null && vimDocumentListener != null) { + myVimDocument.removeChangeListener(vimDocumentListener!!) + } + vimDocument = editor.document + val myChangeListener = VimChangesListener() + vimDocumentListener = myChangeListener + vimDocument!!.addChangeListener(myChangeListener) + oldOffset = editor.currentCaret().offset.point + editor.insertMode = mode == Mode.INSERT + state.mode = mode } - if (mode == Mode.REPLACE) { - editor.insertMode = true - } - state.mode = Mode.NORMAL() - } else { - lastInsert = cmd - strokes.clear() - repeatCharsCount = 0 - val myVimDocument = vimDocument - if (myVimDocument != null && vimDocumentListener != null) { - myVimDocument.removeChangeListener(vimDocumentListener!!) - } - vimDocument = editor.document - val myChangeListener = VimChangesListener() - vimDocumentListener = myChangeListener - vimDocument!!.addChangeListener(myChangeListener) - oldOffset = editor.currentCaret().offset.point - editor.insertMode = mode == Mode.INSERT - state.mode = mode + notifyListeners(editor) } - notifyListeners(editor) } override fun runEnterAction(editor: VimEditor, context: ExecutionContext) {