Skip to content

Commit

Permalink
Fix(VIM-3190): Do not use octopus handler if the enter key is used wi…
Browse files Browse the repository at this point in the history
…th modifiers like shift or control
  • Loading branch information
AlexPl292 committed Nov 22, 2023
1 parent 515f613 commit 22062f0
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/maddyhome/idea/vim/group/KeyGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ public void registerCommandAction(@NotNull VimActionsInitiator actionHolder) {
private void registerRequiredShortcut(@NotNull List<KeyStroke> keys, MappingOwner owner) {
for (KeyStroke key : keys) {
if (key.getKeyChar() == KeyEvent.CHAR_UNDEFINED &&
key.getKeyCode() != KeyEvent.VK_ESCAPE &&
key.getKeyCode() != KeyEvent.VK_ENTER) {
!(key.getKeyCode() == KeyEvent.VK_ESCAPE && key.getModifiers() == 0) &&
!(key.getKeyCode() == KeyEvent.VK_ENTER && key.getModifiers() == 0)) {
getRequiredShortcutKeys().add(new RequiredShortcut(key, owner));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,9 @@ internal fun isOctopusEnabled(s: KeyStroke, editor: Editor): Boolean {
// CMD line has a different processing mechanizm: the processing actions are registered
// for the input field component. These keys are not dispatched via the octopus handler.
if (editor.vim.mode is Mode.CMD_LINE) return false
when (s.keyCode) {
KeyEvent.VK_ENTER -> return true
KeyEvent.VK_ESCAPE -> return true
when {
s.keyCode == KeyEvent.VK_ENTER && s.modifiers == 0 -> return true
s.keyCode == KeyEvent.VK_ESCAPE && s.modifiers == 0 -> return true
}
return false
}
6 changes: 5 additions & 1 deletion src/test/java/org/jetbrains/plugins/ideavim/VimTestCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,11 @@ abstract class VimTestCase {
private fun KeyStroke.getChar(editor: Editor): CharType {
if (keyChar != KeyEvent.CHAR_UNDEFINED) return CharType.CharDetected(keyChar)
if (isOctopusEnabled(this, editor)) {
if (keyCode in setOf(KeyEvent.VK_ENTER)) return CharType.CharDetected(keyCode.toChar())
if (keyCode in setOf(KeyEvent.VK_ENTER)) {
if (modifiers == 0) {
return CharType.CharDetected(keyCode.toChar())
}
}
if (keyCode == KeyEvent.VK_ESCAPE) return CharType.EditorAction("EditorEscape")
}
return CharType.UNDEFINED
Expand Down
96 changes: 96 additions & 0 deletions src/test/java/org/jetbrains/plugins/ideavim/action/EscapeTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2003-2023 The IdeaVim authors
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE.txt file or at
* https://opensource.org/licenses/MIT.
*/

package org.jetbrains.plugins.ideavim.action

import com.intellij.idea.TestFor
import org.jetbrains.plugins.ideavim.VimTestCase
import org.junit.jupiter.api.Test

class EscapeTest : VimTestCase() {
@Test
@TestFor(issues = ["VIM-3190"])
fun `mapping to control esc`() {
configureByText(
"""
Lorem Ipsum
Lorem ipsum dolor sit amet,$c consectetur adipiscing elit
Sed in orci mauris.
Cras id tellus in ex imperdiet egestas.
""".trimIndent()
)

typeText(commandToKeys("nmap <C-Esc> k"))
typeText("<C-Esc>")

assertState(
"""
Lorem Ipsum
$c
Lorem ipsum dolor sit amet, consectetur adipiscing elit
Sed in orci mauris.
Cras id tellus in ex imperdiet egestas.
""".trimIndent()
)
}

@Test
@TestFor(issues = ["VIM-3190"])
fun `mapping to alt esc`() {
configureByText(
"""
Lorem Ipsum
Lorem ipsum dolor sit amet,$c consectetur adipiscing elit
Sed in orci mauris.
Cras id tellus in ex imperdiet egestas.
""".trimIndent()
)

typeText(commandToKeys("nmap <A-Esc> k"))
typeText("<A-Esc>")

assertState(
"""
Lorem Ipsum
$c
Lorem ipsum dolor sit amet, consectetur adipiscing elit
Sed in orci mauris.
Cras id tellus in ex imperdiet egestas.
""".trimIndent()
)
}

@Test
@TestFor(issues = ["VIM-3190"])
fun `mapping to shift esc`() {
configureByText(
"""
Lorem Ipsum
Lorem ipsum dolor sit amet,$c consectetur adipiscing elit
Sed in orci mauris.
Cras id tellus in ex imperdiet egestas.
""".trimIndent()
)

typeText(commandToKeys("nmap <S-Esc> k"))
typeText("<S-Esc>")

assertState(
"""
Lorem Ipsum
$c
Lorem ipsum dolor sit amet, consectetur adipiscing elit
Sed in orci mauris.
Cras id tellus in ex imperdiet egestas.
""".trimIndent()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2003-2023 The IdeaVim authors
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE.txt file or at
* https://opensource.org/licenses/MIT.
*/

package org.jetbrains.plugins.ideavim.action.motion.updown

import com.intellij.idea.TestFor
import org.jetbrains.plugins.ideavim.VimTestCase
import org.junit.jupiter.api.Test

class EnterNormalActionTest : VimTestCase() {
@Test
@TestFor(issues = ["VIM-3190"])
fun `mapping to control enter`() {
configureByText(
"""
Lorem Ipsum
Lorem ipsum dolor sit amet,$c consectetur adipiscing elit
Sed in orci mauris.
Cras id tellus in ex imperdiet egestas.
""".trimIndent()
)

typeText(commandToKeys("nmap <C-Enter> k"))
typeText("<C-Enter>")

assertState(
"""
Lorem Ipsum
$c
Lorem ipsum dolor sit amet, consectetur adipiscing elit
Sed in orci mauris.
Cras id tellus in ex imperdiet egestas.
""".trimIndent()
)
}

@Test
@TestFor(issues = ["VIM-3190"])
fun `mapping to alt enter`() {
configureByText(
"""
Lorem Ipsum
Lorem ipsum dolor sit amet,$c consectetur adipiscing elit
Sed in orci mauris.
Cras id tellus in ex imperdiet egestas.
""".trimIndent()
)

typeText(commandToKeys("nmap <A-Enter> k"))
typeText("<A-Enter>")

assertState(
"""
Lorem Ipsum
$c
Lorem ipsum dolor sit amet, consectetur adipiscing elit
Sed in orci mauris.
Cras id tellus in ex imperdiet egestas.
""".trimIndent()
)
}

@Test
@TestFor(issues = ["VIM-3190"])
fun `mapping to shift enter`() {
configureByText(
"""
Lorem Ipsum
Lorem ipsum dolor sit amet,$c consectetur adipiscing elit
Sed in orci mauris.
Cras id tellus in ex imperdiet egestas.
""".trimIndent()
)

typeText(commandToKeys("nmap <S-Enter> k"))
typeText("<S-Enter>")

assertState(
"""
Lorem Ipsum
$c
Lorem ipsum dolor sit amet, consectetur adipiscing elit
Sed in orci mauris.
Cras id tellus in ex imperdiet egestas.
""".trimIndent()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ public abstract class VimKeyGroupBase : VimKeyGroup {
private fun registerKeyMapping(fromKeys: List<KeyStroke>, owner: MappingOwner) {
val oldSize = requiredShortcutKeys.size
for (key in fromKeys) {
if (key.keyChar == KeyEvent.CHAR_UNDEFINED && key.keyCode != KeyEvent.VK_ESCAPE && key.keyCode != KeyEvent.VK_ENTER) {
if (key.keyChar == KeyEvent.CHAR_UNDEFINED &&
!(key.keyCode == KeyEvent.VK_ESCAPE && key.modifiers == 0) &&
!(key.keyCode == KeyEvent.VK_ENTER && key.modifiers == 0)
) {
requiredShortcutKeys.add(RequiredShortcut(key, owner))
}
}
Expand Down

0 comments on commit 22062f0

Please sign in to comment.