Skip to content

Commit

Permalink
Properly reset with Esc in the middle of command
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-elmquist committed Jul 7, 2020
1 parent aece559 commit 7421879
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/com/maddyhome/idea/vim/KeyHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,15 @@ public void handleKey(@NotNull Editor editor,
handleKeyRecursionCount++;

try {
if (isEditorReset(key, editorState)) {
handleEditorReset(editor, key, context, editorState);
}

if (!allowKeyMappings || !handleKeyMapping(editor, key, context)) {
if (isCommandCountKey(chKey, editorState)) {
commandBuilder.addCountCharacter(key);
} else if (isDeleteCommandCountKey(key, editorState)) {
commandBuilder.deleteCountCharacter();
} else if (isEditorReset(key, editorState)) {
handleEditorReset(editor, key, context, editorState);
}
// If we got this far the user is entering a command or supplying an argument to an entered command.
// First let's check to see if we are at the point of expecting a single character argument to a command.
Expand Down
50 changes: 50 additions & 0 deletions test/org/jetbrains/plugins/ideavim/action/ResetModeActionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@

package org.jetbrains.plugins.ideavim.action

import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.command.MappingMode
import com.maddyhome.idea.vim.helper.StringHelper
import com.maddyhome.idea.vim.helper.StringHelper.parseKeys
import com.maddyhome.idea.vim.key.MappingOwner
import junit.framework.TestCase
import org.jetbrains.plugins.ideavim.VimTestCase

class ResetModeActionTest : VimTestCase() {
private val owner = MappingOwner.Plugin.get("ResetModeActionTest")

fun `test reset from normal mode`() {
val keys = StringHelper.parseKeys("<C-\\><C-N>")
val before = "A Discovery"
Expand Down Expand Up @@ -63,4 +69,48 @@ class ResetModeActionTest : VimTestCase() {
doTest(keys, before, after, CommandState.Mode.COMMAND, CommandState.SubMode.NONE)
TestCase.assertFalse(myFixture.editor.selectionModel.hasSelection())
}

fun `test delete command after resetting operator-pending mode`() {
val keys = StringHelper.parseKeys("d", "<C-\\><C-N>", "dw")
val before = "A Discovery"
val after = "Discovery"
doTest(keys, before, after, CommandState.Mode.COMMAND, CommandState.SubMode.NONE)
TestCase.assertFalse(myFixture.editor.selectionModel.hasSelection())
}

fun `test delete command after resetting operator-pending mode with esc`() {
val keys = StringHelper.parseKeys("d", "<Esc>", "dw")
val before = "A Discovery"
val after = "Discovery"
doTest(keys, before, after, CommandState.Mode.COMMAND, CommandState.SubMode.NONE)
TestCase.assertFalse(myFixture.editor.selectionModel.hasSelection())
}

fun `test delete command after resetting operator-pending mode with ctrl open bracket`() {
val keys = StringHelper.parseKeys("d", "<C-[>", "dw")
val before = "A Discovery"
val after = "Discovery"
doTest(keys, before, after, CommandState.Mode.COMMAND, CommandState.SubMode.NONE)
TestCase.assertFalse(myFixture.editor.selectionModel.hasSelection())
}

fun `test delete command after resetting operator-pending mode with mapping`() {
VimPlugin.getKey()
.putKeyMapping(MappingMode.NVO, parseKeys("<C-D>"), owner, parseKeys("<Esc>"), false)

val keys = StringHelper.parseKeys("d", "<C-D>", "dw")
val before = "A Discovery"
val after = "Discovery"
doTest(keys, before, after, CommandState.Mode.COMMAND, CommandState.SubMode.NONE)
TestCase.assertFalse(myFixture.editor.selectionModel.hasSelection())
}

fun `test non-delete commands after resetting operator-pending mode`() {
val keys = StringHelper.parseKeys("c", "<C-\\><C-N>", "another")
val before = "A Discovery"
val after = "Another Discovery"
doTest(keys, before, after, CommandState.Mode.INSERT, CommandState.SubMode.NONE)
TestCase.assertFalse(myFixture.editor.selectionModel.hasSelection())
}

}

0 comments on commit 7421879

Please sign in to comment.