Remove Keymap(KeyTrie) and simply use KeyTrie.

pull/7085/merge
gibbz00 1 year ago committed by Blaž Hrastnik
parent b8563685ec
commit 9926c2d292

@ -1,5 +1,5 @@
use crate::keymap; use crate::keymap;
use crate::keymap::{merge_keys, Keymap}; use crate::keymap::{merge_keys, KeyTrie};
use helix_loader::merge_toml_values; use helix_loader::merge_toml_values;
use helix_view::document::Mode; use helix_view::document::Mode;
use serde::Deserialize; use serde::Deserialize;
@ -12,7 +12,7 @@ use toml::de::Error as TomlError;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct Config { pub struct Config {
pub theme: Option<String>, pub theme: Option<String>,
pub keys: HashMap<Mode, Keymap>, pub keys: HashMap<Mode, KeyTrie>,
pub editor: helix_view::editor::Config, pub editor: helix_view::editor::Config,
} }
@ -20,7 +20,7 @@ pub struct Config {
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
pub struct ConfigRaw { pub struct ConfigRaw {
pub theme: Option<String>, pub theme: Option<String>,
pub keys: Option<HashMap<Mode, Keymap>>, pub keys: Option<HashMap<Mode, KeyTrie>>,
pub editor: Option<toml::Value>, pub editor: Option<toml::Value>,
} }
@ -138,7 +138,6 @@ mod tests {
#[test] #[test]
fn parsing_keymaps_config_file() { fn parsing_keymaps_config_file() {
use crate::keymap; use crate::keymap;
use crate::keymap::Keymap;
use helix_core::hashmap; use helix_core::hashmap;
use helix_view::document::Mode; use helix_view::document::Mode;
@ -155,13 +154,13 @@ mod tests {
merge_keys( merge_keys(
&mut keys, &mut keys,
hashmap! { hashmap! {
Mode::Insert => Keymap::new(keymap!({ "Insert mode" Mode::Insert => keymap!({ "Insert mode"
"y" => move_line_down, "y" => move_line_down,
"S-C-a" => delete_selection, "S-C-a" => delete_selection,
})), }),
Mode::Normal => Keymap::new(keymap!({ "Normal mode" Mode::Normal => keymap!({ "Normal mode"
"A-F12" => move_next_word_end, "A-F12" => move_next_word_end,
})), }),
}, },
); );

@ -269,41 +269,11 @@ pub enum KeymapResult {
Cancelled(Vec<KeyEvent>), Cancelled(Vec<KeyEvent>),
} }
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(transparent)]
pub struct Keymap(KeyTrie);
/// A map of command names to keybinds that will execute the command. /// A map of command names to keybinds that will execute the command.
pub type ReverseKeymap = HashMap<String, Vec<Vec<KeyEvent>>>; pub type ReverseKeymap = HashMap<String, Vec<Vec<KeyEvent>>>;
impl Keymap {
pub fn new(root: KeyTrie) -> Self {
Keymap(root)
}
}
impl Deref for Keymap {
type Target = KeyTrie;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Keymap {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Default for Keymap {
fn default() -> Self {
Self(KeyTrie::Node(KeyTrieNode::default()))
}
}
pub struct Keymaps { pub struct Keymaps {
pub map: Box<dyn DynAccess<HashMap<Mode, Keymap>>>, pub map: Box<dyn DynAccess<HashMap<Mode, KeyTrie>>>,
/// Stores pending keys waiting for the next key. This is relative to a /// Stores pending keys waiting for the next key. This is relative to a
/// sticky node if one is in use. /// sticky node if one is in use.
state: Vec<KeyEvent>, state: Vec<KeyEvent>,
@ -312,7 +282,7 @@ pub struct Keymaps {
} }
impl Keymaps { impl Keymaps {
pub fn new(map: Box<dyn DynAccess<HashMap<Mode, Keymap>>>) -> Self { pub fn new(map: Box<dyn DynAccess<HashMap<Mode, KeyTrie>>>) -> Self {
Self { Self {
map, map,
state: Vec::new(), state: Vec::new(),
@ -320,7 +290,7 @@ impl Keymaps {
} }
} }
pub fn map(&self) -> DynGuard<HashMap<Mode, Keymap>> { pub fn map(&self) -> DynGuard<HashMap<Mode, KeyTrie>> {
self.map.load() self.map.load()
} }
@ -352,7 +322,7 @@ impl Keymaps {
let first = self.state.get(0).unwrap_or(&key); let first = self.state.get(0).unwrap_or(&key);
let trie_node = match self.sticky { let trie_node = match self.sticky {
Some(ref trie) => Cow::Owned(KeyTrie::Node(trie.clone())), Some(ref trie) => Cow::Owned(KeyTrie::Node(trie.clone())),
None => Cow::Borrowed(&keymap.0), None => Cow::Borrowed(keymap),
}; };
let trie = match trie_node.search(&[*first]) { let trie = match trie_node.search(&[*first]) {
@ -395,9 +365,13 @@ 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, Keymap>, mut delta: HashMap<Mode, Keymap>) { pub fn merge_keys(dst: &mut HashMap<Mode, KeyTrie>, mut delta: HashMap<Mode, KeyTrie>) {
for (mode, keys) in dst { for (mode, keys) in dst {
keys.merge_nodes(delta.remove(mode).unwrap_or_default().0) keys.merge_nodes(
delta
.remove(mode)
.unwrap_or_else(|| KeyTrie::Node(KeyTrieNode::default())),
)
} }
} }
@ -426,8 +400,7 @@ mod tests {
#[test] #[test]
fn merge_partial_keys() { fn merge_partial_keys() {
let keymap = hashmap! { let keymap = hashmap! {
Mode::Normal => Keymap::new( Mode::Normal => keymap!({ "Normal mode"
keymap!({ "Normal mode"
"i" => normal_mode, "i" => normal_mode,
"无" => insert_mode, "无" => insert_mode,
"z" => jump_backward, "z" => jump_backward,
@ -436,7 +409,6 @@ mod tests {
"g" => delete_char_forward, "g" => delete_char_forward,
}, },
}) })
)
}; };
let mut merged_keyamp = default(); let mut merged_keyamp = default();
merge_keys(&mut merged_keyamp, keymap.clone()); merge_keys(&mut merged_keyamp, keymap.clone());
@ -501,8 +473,7 @@ mod tests {
#[test] #[test]
fn order_should_be_set() { fn order_should_be_set() {
let keymap = hashmap! { let keymap = hashmap! {
Mode::Normal => Keymap::new( Mode::Normal => keymap!({ "Normal mode"
keymap!({ "Normal mode"
"space" => { "" "space" => { ""
"s" => { "" "s" => { ""
"v" => vsplit, "v" => vsplit,
@ -510,7 +481,6 @@ mod tests {
}, },
}, },
}) })
)
}; };
let mut merged_keyamp = default(); let mut merged_keyamp = default();
merge_keys(&mut merged_keyamp, keymap.clone()); merge_keys(&mut merged_keyamp, keymap.clone());
@ -553,7 +523,7 @@ mod tests {
}, },
"j" | "k" => move_line_down, "j" | "k" => move_line_down,
}); });
let keymap = Keymap::new(normal_mode); let keymap = normal_mode;
let mut reverse_map = keymap.reverse_map(); let mut reverse_map = keymap.reverse_map();
// sort keybindings in order to have consistent tests // sort keybindings in order to have consistent tests
@ -601,7 +571,7 @@ mod tests {
modifiers: KeyModifiers::NONE, modifiers: KeyModifiers::NONE,
}; };
let expectation = Keymap::new(KeyTrie::Node(KeyTrieNode::new( let expectation = KeyTrie::Node(KeyTrieNode::new(
"", "",
hashmap! { hashmap! {
key => KeyTrie::Sequence(vec!{ key => KeyTrie::Sequence(vec!{
@ -618,7 +588,7 @@ mod tests {
}) })
}, },
vec![key], vec![key],
))); ));
assert_eq!(toml::from_str(keys), Ok(expectation)); assert_eq!(toml::from_str(keys), Ok(expectation));
} }

@ -1,10 +1,10 @@
use std::collections::HashMap; use std::collections::HashMap;
use super::macros::keymap; use super::macros::keymap;
use super::{Keymap, Mode}; use super::{KeyTrie, Mode};
use helix_core::hashmap; use helix_core::hashmap;
pub fn default() -> HashMap<Mode, Keymap> { pub fn default() -> HashMap<Mode, KeyTrie> {
let normal = keymap!({ "Normal mode" let normal = keymap!({ "Normal mode"
"h" | "left" => move_char_left, "h" | "left" => move_char_left,
"j" | "down" => move_visual_line_down, "j" | "down" => move_visual_line_down,
@ -380,8 +380,8 @@ pub fn default() -> HashMap<Mode, Keymap> {
"end" => goto_line_end_newline, "end" => goto_line_end_newline,
}); });
hashmap!( hashmap!(
Mode::Normal => Keymap::new(normal), Mode::Normal => normal,
Mode::Select => Keymap::new(select), Mode::Select => select,
Mode::Insert => Keymap::new(insert), Mode::Insert => insert,
) )
} }

@ -62,12 +62,11 @@ macro_rules! alt {
}; };
} }
/// Macro for defining the root of a `Keymap` object. Example: /// Macro for defining a `KeyTrie`. Example:
/// ///
/// ``` /// ```
/// # use helix_core::hashmap; /// # use helix_core::hashmap;
/// # use helix_term::keymap; /// # use helix_term::keymap;
/// # use helix_term::keymap::Keymap;
/// let normal_mode = keymap!({ "Normal mode" /// let normal_mode = keymap!({ "Normal mode"
/// "i" => insert_mode, /// "i" => insert_mode,
/// "g" => { "Goto" /// "g" => { "Goto"
@ -76,7 +75,7 @@ macro_rules! alt {
/// }, /// },
/// "j" | "down" => move_line_down, /// "j" | "down" => move_line_down,
/// }); /// });
/// let keymap = Keymap::new(normal_mode); /// let keymap = normal_mode;
/// ``` /// ```
#[macro_export] #[macro_export]
macro_rules! keymap { macro_rules! keymap {

Loading…
Cancel
Save