diff --git a/src/main/java/com/maddyhome/idea/vim/listener/IdeaSpecifics.kt b/src/main/java/com/maddyhome/idea/vim/listener/IdeaSpecifics.kt index e06f6cc18..fad528540 100644 --- a/src/main/java/com/maddyhome/idea/vim/listener/IdeaSpecifics.kt +++ b/src/main/java/com/maddyhome/idea/vim/listener/IdeaSpecifics.kt @@ -29,6 +29,7 @@ import com.intellij.openapi.actionSystem.ex.AnActionListener import com.intellij.openapi.actionSystem.impl.ProxyShortcutSet import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.actions.EnterAction +import com.intellij.openapi.editor.impl.ScrollingModelImpl import com.intellij.openapi.keymap.KeymapManager import com.intellij.openapi.project.DumbAwareToggleAction import com.intellij.openapi.util.TextRange @@ -60,6 +61,7 @@ internal object IdeaSpecifics { private val surrounderAction = "com.intellij.codeInsight.generation.surroundWith.SurroundWithHandler\$InvokeSurrounderAction" private var editor: Editor? = null + private var caretOffset = -1 private var completionPrevDocumentLength: Int? = null private var completionPrevDocumentOffset: Int? = null @@ -69,6 +71,7 @@ internal object IdeaSpecifics { val hostEditor = event.dataContext.getData(CommonDataKeys.HOST_EDITOR) if (hostEditor != null) { editor = hostEditor + caretOffset = hostEditor.caretModel.offset } val isVimAction = (action as? AnActionWrapper)?.delegate is VimShortcutKeyAction @@ -122,17 +125,18 @@ internal object IdeaSpecifics { if (VimPlugin.isNotEnabled()) return val editor = editor - if (editor != null && action is ChooseItemAction && injector.registerGroup.isRecording) { - val prevDocumentLength = completionPrevDocumentLength - val prevDocumentOffset = completionPrevDocumentOffset + if (editor != null) { + if (action is ChooseItemAction && injector.registerGroup.isRecording) { + val prevDocumentLength = completionPrevDocumentLength + val prevDocumentOffset = completionPrevDocumentOffset - if (prevDocumentLength != null && prevDocumentOffset != null) { - val register = VimPlugin.getRegister() - val addedTextLength = editor.document.textLength - prevDocumentLength - val caretShift = addedTextLength - (editor.caretModel.primaryCaret.offset - prevDocumentOffset) - val leftArrow = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0) + if (prevDocumentLength != null && prevDocumentOffset != null) { + val register = VimPlugin.getRegister() + val addedTextLength = editor.document.textLength - prevDocumentLength + val caretShift = addedTextLength - (editor.caretModel.primaryCaret.offset - prevDocumentOffset) + val leftArrow = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0) - register.recordText( + register.recordText( editor.document.getText( TextRange( prevDocumentOffset, @@ -140,31 +144,45 @@ internal object IdeaSpecifics { ) ) ) - repeat(caretShift.coerceAtLeast(0)) { - register.recordKeyStroke(leftArrow) + repeat(caretShift.coerceAtLeast(0)) { + register.recordKeyStroke(leftArrow) + } + } + + this.completionPrevDocumentLength = null + this.completionPrevDocumentOffset = null + } + + //region Enter insert mode after surround with if + if (surrounderAction == action.javaClass.name && surrounderItems.any { + action.templatePresentation.text.endsWith( + it, + ) + } + ) { + editor?.let { + it.vim.mode = Mode.NORMAL() + VimPlugin.getChange().insertBeforeCursor(it.vim, event.dataContext.vim) + KeyHandler.getInstance().reset(it.vim) } } + //endregion - this.completionPrevDocumentLength = null - this.completionPrevDocumentOffset = null - } - - //region Enter insert mode after surround with if - if (surrounderAction == action.javaClass.name && surrounderItems.any { - action.templatePresentation.text.endsWith( - it, - ) - } - ) { - editor?.let { - it.vim.mode = Mode.NORMAL() - VimPlugin.getChange().insertBeforeCursor(it.vim, event.dataContext.vim) - KeyHandler.getInstance().reset(it.vim) + if (caretOffset != -1 && caretOffset != editor.caretModel.offset) { + val scrollModel = editor.scrollingModel as ScrollingModelImpl + if (scrollModel.isScrollingNow) { + val v = scrollModel.verticalScrollOffset + val h = scrollModel.horizontalScrollOffset + scrollModel.finishAnimation() + scrollModel.scroll(h, v) + scrollModel.finishAnimation() + } + injector.scroll.scrollCaretIntoView(editor.vim) } } - //endregion this.editor = null + this.caretOffset = -1 } } diff --git a/src/main/java/com/maddyhome/idea/vim/listener/VimListenerManager.kt b/src/main/java/com/maddyhome/idea/vim/listener/VimListenerManager.kt index 40feee040..e7e8a6410 100644 --- a/src/main/java/com/maddyhome/idea/vim/listener/VimListenerManager.kt +++ b/src/main/java/com/maddyhome/idea/vim/listener/VimListenerManager.kt @@ -419,6 +419,8 @@ internal object VimListenerManager { editor.vim.mode = Mode.NORMAL() KeyHandler.getInstance().reset(editor.vim) } + // Breaks relativenumber for some reason +// injector.scroll.scrollCaretIntoView(editor.vim) } MotionGroup.fileEditorManagerSelectionChangedCallback(event)