Skip to content

Commit

Permalink
Fix(VIM-3165): Do not process enter key as IdeaVim shortcut if it's n…
Browse files Browse the repository at this point in the history
…ot an actual keypress
  • Loading branch information
AlexPl292 committed Nov 13, 2023
1 parent b3ad222 commit 41025d7
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/main/java/com/maddyhome/idea/vim/handler/VimEnterHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

package com.maddyhome.idea.vim.handler

import com.intellij.codeInsight.editorActions.AutoHardWrapHandler
import com.intellij.codeInsight.lookup.LookupManager
import com.intellij.ide.DataManager
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.invokeLater
Expand Down Expand Up @@ -99,14 +101,34 @@ internal abstract class OctopusHandler(private val nextHandler: EditorActionHand
}
}

@Suppress("RedundantIf")
private fun isThisHandlerEnabled(editor: Editor, caret: Caret?, dataContext: DataContext?): Boolean {
if (!VimPlugin.isEnabled()) return false
if (!isHandlerEnabled(editor, dataContext)) return false
if (dataContext?.actionStartedFromVim == true) return false
if (isNotActualKeyPress(dataContext)) return false
return true
}

/**
* In some cases IJ runs handlers to imitate "enter" or other key. In such cases we should not process it on the
* IdeaVim side because the user may have mappings on enter the we'll get an unexpected behaviour.
* This method should return true if we detect that this handler is called in such case and this is not an
* actual keypress from the user.
*/
private fun isNotActualKeyPress(dataContext: DataContext?): Boolean {
if (dataContext != null) {
// This flag is set when the enter handlers are executed as a part of moving the comment on the new line
if (DataManager.getInstance()
.loadFromDataContext(dataContext, AutoHardWrapHandler.AUTO_WRAP_LINE_IN_PROGRESS_KEY) == true
) {
return true
}
}

if (dataContext?.actionStartedFromVim == true) return true

return false
}

final override fun isEnabledForCaret(editor: Editor, caret: Caret, dataContext: DataContext?): Boolean {
return isThisHandlerEnabled(editor, caret, dataContext)
|| nextHandler?.isEnabled(editor, caret, dataContext) == true
Expand Down

0 comments on commit 41025d7

Please sign in to comment.