make merge_keys consume both arguments for more convenient usage

pull/9545/head
Dipsy 10 months ago
parent bca836c82a
commit 1b5605abd5

@ -365,14 +365,17 @@ impl Default for Keymaps {
} }
/// Merge default config keys with user overwritten keys for custom user config. /// Merge default config keys with user overwritten keys for custom user config.
pub fn merge_keys(dst: &mut HashMap<Mode, KeyTrie>, mut delta: HashMap<Mode, KeyTrie>) { pub fn merge_keys(
for (mode, keys) in dst { mut left: HashMap<Mode, KeyTrie>, mut right: HashMap<Mode, KeyTrie>,
) -> HashMap<Mode, KeyTrie> {
for (mode, keys) in &mut left {
keys.merge_nodes( keys.merge_nodes(
delta right
.remove(mode) .remove(mode)
.unwrap_or_else(|| KeyTrie::Node(KeyTrieNode::default())), .unwrap_or_else(|| KeyTrie::Node(KeyTrieNode::default())),
) )
} }
left
} }
#[cfg(test)] #[cfg(test)]
@ -410,11 +413,10 @@ mod tests {
}, },
}) })
}; };
let mut merged_keyamp = default(); let mut merged_keymap = merge_keys(default(), keymap.clone());
merge_keys(&mut merged_keyamp, keymap.clone()); assert_ne!(keymap, merged_keymap);
assert_ne!(keymap, merged_keyamp);
let mut keymap = Keymaps::new(Box::new(Constant(merged_keyamp.clone()))); let mut keymap = Keymaps::new(Box::new(Constant(merged_keymap.clone())));
assert_eq!( assert_eq!(
keymap.get(Mode::Normal, key!('i')), keymap.get(Mode::Normal, key!('i')),
KeymapResult::Matched(MappableCommand::normal_mode), KeymapResult::Matched(MappableCommand::normal_mode),
@ -432,7 +434,7 @@ mod tests {
"Leaf should replace node" "Leaf should replace node"
); );
let keymap = merged_keyamp.get_mut(&Mode::Normal).unwrap(); let keymap = merged_keymap.get_mut(&Mode::Normal).unwrap();
// Assumes that `g` is a node in default keymap // Assumes that `g` is a node in default keymap
assert_eq!( assert_eq!(
keymap.search(&[key!('g'), key!('$')]).unwrap(), keymap.search(&[key!('g'), key!('$')]).unwrap(),
@ -453,7 +455,7 @@ mod tests {
); );
assert!( assert!(
merged_keyamp merged_keymap
.get(&Mode::Normal) .get(&Mode::Normal)
.and_then(|key_trie| key_trie.node()) .and_then(|key_trie| key_trie.node())
.unwrap() .unwrap()
@ -461,7 +463,7 @@ mod tests {
> 1 > 1
); );
assert!( assert!(
merged_keyamp merged_keymap
.get(&Mode::Insert) .get(&Mode::Insert)
.and_then(|key_trie| key_trie.node()) .and_then(|key_trie| key_trie.node())
.unwrap() .unwrap()
@ -482,10 +484,9 @@ mod tests {
}, },
}) })
}; };
let mut merged_keyamp = default(); let mut merged_keymap = merge_keys(default(), keymap.clone());
merge_keys(&mut merged_keyamp, keymap.clone()); assert_ne!(keymap, merged_keymap);
assert_ne!(keymap, merged_keyamp); let keymap = merged_keymap.get_mut(&Mode::Normal).unwrap();
let keymap = merged_keyamp.get_mut(&Mode::Normal).unwrap();
// Make sure mapping works // Make sure mapping works
assert_eq!( assert_eq!(
keymap.search(&[key!(' '), key!('s'), key!('v')]).unwrap(), keymap.search(&[key!(' '), key!('s'), key!('v')]).unwrap(),

Loading…
Cancel
Save