Skip to content

Commit

Permalink
Fix bug removing map that is also a prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
citizenmatt committed Nov 7, 2024
1 parent c496d77 commit b228219
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

package org.jetbrains.plugins.ideavim.ex.implementation.commands

import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.command.MappingMode
import org.jetbrains.plugins.ideavim.VimTestCase
import org.junit.jupiter.api.BeforeEach
Expand All @@ -35,7 +34,7 @@ class UnMapCommandTest : VimTestCase() {
fun testMapKtoJ() {
putMapping(MappingMode.N, "k", "j", false)

typeText(commandToKeys("unmap k"))
enterCommand("unmap k")

assertNoMapping("k")
}
Expand All @@ -44,7 +43,7 @@ class UnMapCommandTest : VimTestCase() {
fun `test mappings in insert mode`() {
putMapping(MappingMode.I, "jk", "<Esc>", false)

typeText(commandToKeys("iunmap jk"))
enterCommand("iunmap jk")

assertNoMapping("jk")
}
Expand All @@ -53,17 +52,28 @@ class UnMapCommandTest : VimTestCase() {
fun `test removing only part of keys`() {
putMapping(MappingMode.I, "jk", "<Esc>", false)

typeText(commandToKeys("unmap j"))
enterCommand("iunmap j")

assertNoMapping("j")
assertMappingExists("jk", "<Esc>", MappingMode.I)
}

@Test
fun `test removing mapping that is also a prefix`() {
putMapping(MappingMode.I, "jk", "<Esc>", false)
putMapping(MappingMode.I, "jkl", "<Esc>", false)

enterCommand("iunmap jk")

assertNoMapping("jk")
assertMappingExists("jkl", "<Esc>", MappingMode.I)
}

@Test
fun `test removing keys from a different mode`() {
putMapping(MappingMode.I, "jk", "<Esc>", false)

typeText(commandToKeys("unmap jk"))
enterCommand("unmap jk")

assertMappingExists("jk", "<Esc>", MappingMode.I)
}
Expand All @@ -76,7 +86,7 @@ class UnMapCommandTest : VimTestCase() {

// We've just mapped "foo" to "bar" in Command-line mode. We can't type it directly!
// And enterCommand doesn't parse special keys!
typeText(injector.parser.parseKeys(":unmap! fox<BS>o<CR>"))
typeText(":unmap! fox<BS>o<CR>")

assertNoMapping("foo", MappingMode.IC)
assertMappingExists("quux", "baz", MappingMode.IC)
Expand All @@ -91,7 +101,7 @@ class UnMapCommandTest : VimTestCase() {

// We've just mapped "foo" to "bar" in Command-line mode. We can't type it directly!
// And enterCommand doesn't parse special keys!
typeText(injector.parser.parseKeys(":unmap! fox<BS>o<CR>"))
typeText(":unmap! fox<BS>o<CR>")

assertNoMapping("foo", MappingMode.IC)
assertMappingExists("quux", "baz", MappingMode.IC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ class KeyStrokeTrie<T>(private val name: String) {
}
}

// TODO: This is wrong. If the sequence exists but is also a prefix, it should just null out the data
path.asReversed().forEach { (parent, key) ->
val child = parent.children.value[key] ?: return
child.data = null
if (child.children.isInitialized() && child.children.value.isNotEmpty()) return
parent.children.value.remove(key)
if (parent.children.value.isNotEmpty() || parent.data != null) return
Expand Down

0 comments on commit b228219

Please sign in to comment.