mirror of
https://github.com/helix-editor/helix.git
synced 2024-11-22 09:26:19 +04:00
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.
This commit is contained in:
parent
eda4c79f2f
commit
b8563685ec
@ -195,6 +195,32 @@ fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
|
||||
}
|
||||
|
||||
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user