Skip to content

Commit

Permalink
Fix incorrect double registration when opening editors
Browse files Browse the repository at this point in the history
Ensures that the editor listeners are only registered once when the editor is created. IdeaVim uses two different events to track editor creation, to try to understand how the editor is being created (new window, split, preview, etc.) and this can lead to calling the `EditorListeners.add` method twice. This would create a second `Disposable` and cause handlers to leak. This is most visible when creating the first window and then disabling IdeaVim. This window would still handle drag events like IdeaVim, even changing the caret back to block after the drag finishes.
  • Loading branch information
citizenmatt authored and AlexPl292 committed Jun 21, 2024
1 parent 1452c11 commit a7814e6
Showing 1 changed file with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import com.maddyhome.idea.vim.helper.moveToInlayAwareOffset
import com.maddyhome.idea.vim.helper.resetVimLastColumn
import com.maddyhome.idea.vim.helper.updateCaretsVisualAttributes
import com.maddyhome.idea.vim.helper.vimDisabled
import com.maddyhome.idea.vim.helper.vimInitialised
import com.maddyhome.idea.vim.newapi.IjVimEditor
import com.maddyhome.idea.vim.newapi.InsertTimeRecorder
import com.maddyhome.idea.vim.newapi.ij
Expand Down Expand Up @@ -276,6 +277,11 @@ internal object VimListenerManager {
// editor's disposable individually.
val disposable = editor.project?.vimDisposable ?: return

// Protect against double initialisation
if (editor.getUserData(editorListenersDisposableKey) != null) {
return
}

val listenersDisposable = Disposer.newDisposable(disposable)
editor.putUserData(editorListenersDisposableKey, listenersDisposable)

Expand Down Expand Up @@ -468,6 +474,9 @@ internal object VimListenerManager {
(it.fileEditor as? TextEditor)?.editor?.let { editor ->
if (vimDisabled(editor)) return@let

// Protect against double initialisation, in case the editor was already initialised in editorCreated
if (editor.vimInitialised) return@let

val openingEditor = editor.removeUserData(openingEditorKey)
val owningEditorWindow = getOwningEditorWindow(editor)
val isInSameSplit = owningEditorWindow == openingEditor?.owningEditorWindow
Expand Down

0 comments on commit a7814e6

Please sign in to comment.