diff options
author | gibbz00 | 2023-06-03 08:59:54 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2023-06-07 01:11:13 +0000 |
commit | b8563685ec44fd6958514f478d879ba785e916ad (patch) | |
tree | ed629b016987efcb5263637ef2e93dc90883a733 | |
parent | eda4c79f2f9e8383bda1ccc995c02223904f8be1 (diff) |
Move `keymap.reverse_keymap()` to `Keytrie`:
The plan is let `Keymaps` simply store `KeyTrie`s, as the `Keymap(Keytrie)` wrapping serves little to no purpose.
-rw-r--r-- | helix-term/src/keymap.rs | 52 |
1 files changed, 26 insertions, 26 deletions
diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs index e0934f76..ea2937bb 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -195,6 +195,32 @@ impl<'de> serde::de::Visitor<'de> for KeyTrieVisitor { } impl KeyTrie { + pub fn reverse_map(&self) -> ReverseKeymap { + // recursively visit all nodes in keymap + fn map_node(cmd_map: &mut ReverseKeymap, node: &KeyTrie, keys: &mut Vec<KeyEvent>) { + match node { + KeyTrie::MappableCommand(cmd) => { + let name = cmd.name(); + if name != "no_op" { + cmd_map.entry(name.into()).or_default().push(keys.clone()) + } + } + KeyTrie::Node(next) => { + for (key, trie) in &next.map { + keys.push(*key); + map_node(cmd_map, trie, keys); + keys.pop(); + } + } + KeyTrie::Sequence(_) => {} + }; + } + + let mut res = HashMap::new(); + map_node(&mut res, self, &mut Vec::new()); + res + } + pub fn node(&self) -> Option<&KeyTrieNode> { match *self { KeyTrie::Node(ref node) => Some(node), @@ -254,32 +280,6 @@ impl Keymap { pub fn new(root: KeyTrie) -> Self { Keymap(root) } - - pub fn reverse_map(&self) -> ReverseKeymap { - // recursively visit all nodes in keymap - fn map_node(cmd_map: &mut ReverseKeymap, node: &KeyTrie, keys: &mut Vec<KeyEvent>) { - match node { - KeyTrie::MappableCommand(cmd) => { - let name = cmd.name(); - if name != "no_op" { - cmd_map.entry(name.into()).or_default().push(keys.clone()) - } - } - KeyTrie::Node(next) => { - for (key, trie) in &next.map { - keys.push(*key); - map_node(cmd_map, trie, keys); - keys.pop(); - } - } - KeyTrie::Sequence(_) => {} - }; - } - - let mut res = HashMap::new(); - map_node(&mut res, &self.0, &mut Vec::new()); - res - } } impl Deref for Keymap { |