From 9926c2d292712f38615cc2cb0ca75c1e8ef3bed9 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Sat, 3 Jun 2023 11:29:08 +0200 Subject: Remove Keymap(KeyTrie) and simply use KeyTrie. --- helix-term/src/config.rs | 15 +++++----- helix-term/src/keymap.rs | 60 ++++++++++------------------------------ helix-term/src/keymap/default.rs | 10 +++---- helix-term/src/keymap/macros.rs | 5 ++-- 4 files changed, 29 insertions(+), 61 deletions(-) (limited to 'helix-term') diff --git a/helix-term/src/config.rs b/helix-term/src/config.rs index 9776ef7a..f37b03ec 100644 --- a/helix-term/src/config.rs +++ b/helix-term/src/config.rs @@ -1,5 +1,5 @@ use crate::keymap; -use crate::keymap::{merge_keys, Keymap}; +use crate::keymap::{merge_keys, KeyTrie}; use helix_loader::merge_toml_values; use helix_view::document::Mode; use serde::Deserialize; @@ -12,7 +12,7 @@ use toml::de::Error as TomlError; #[derive(Debug, Clone, PartialEq)] pub struct Config { pub theme: Option, - pub keys: HashMap, + pub keys: HashMap, pub editor: helix_view::editor::Config, } @@ -20,7 +20,7 @@ pub struct Config { #[serde(deny_unknown_fields)] pub struct ConfigRaw { pub theme: Option, - pub keys: Option>, + pub keys: Option>, pub editor: Option, } @@ -138,7 +138,6 @@ mod tests { #[test] fn parsing_keymaps_config_file() { use crate::keymap; - use crate::keymap::Keymap; use helix_core::hashmap; use helix_view::document::Mode; @@ -155,13 +154,13 @@ mod tests { merge_keys( &mut keys, hashmap! { - Mode::Insert => Keymap::new(keymap!({ "Insert mode" + Mode::Insert => keymap!({ "Insert mode" "y" => move_line_down, "S-C-a" => delete_selection, - })), - Mode::Normal => Keymap::new(keymap!({ "Normal mode" + }), + Mode::Normal => keymap!({ "Normal mode" "A-F12" => move_next_word_end, - })), + }), }, ); diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs index ea2937bb..5a72a35a 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -269,41 +269,11 @@ pub enum KeymapResult { Cancelled(Vec), } -#[derive(Debug, Clone, PartialEq, Deserialize)] -#[serde(transparent)] -pub struct Keymap(KeyTrie); - /// A map of command names to keybinds that will execute the command. pub type ReverseKeymap = HashMap>>; -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 map: Box>>, + pub map: Box>>, /// Stores pending keys waiting for the next key. This is relative to a /// sticky node if one is in use. state: Vec, @@ -312,7 +282,7 @@ pub struct Keymaps { } impl Keymaps { - pub fn new(map: Box>>) -> Self { + pub fn new(map: Box>>) -> Self { Self { map, state: Vec::new(), @@ -320,7 +290,7 @@ impl Keymaps { } } - pub fn map(&self) -> DynGuard> { + pub fn map(&self) -> DynGuard> { self.map.load() } @@ -352,7 +322,7 @@ impl Keymaps { let first = self.state.get(0).unwrap_or(&key); let trie_node = match self.sticky { 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]) { @@ -395,9 +365,13 @@ impl Default for Keymaps { } /// Merge default config keys with user overwritten keys for custom user config. -pub fn merge_keys(dst: &mut HashMap, mut delta: HashMap) { +pub fn merge_keys(dst: &mut HashMap, mut delta: HashMap) { 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] fn merge_partial_keys() { let keymap = hashmap! { - Mode::Normal => Keymap::new( - keymap!({ "Normal mode" + Mode::Normal => keymap!({ "Normal mode" "i" => normal_mode, "无" => insert_mode, "z" => jump_backward, @@ -436,7 +409,6 @@ mod tests { "g" => delete_char_forward, }, }) - ) }; let mut merged_keyamp = default(); merge_keys(&mut merged_keyamp, keymap.clone()); @@ -501,8 +473,7 @@ mod tests { #[test] fn order_should_be_set() { let keymap = hashmap! { - Mode::Normal => Keymap::new( - keymap!({ "Normal mode" + Mode::Normal => keymap!({ "Normal mode" "space" => { "" "s" => { "" "v" => vsplit, @@ -510,7 +481,6 @@ mod tests { }, }, }) - ) }; let mut merged_keyamp = default(); merge_keys(&mut merged_keyamp, keymap.clone()); @@ -553,7 +523,7 @@ mod tests { }, "j" | "k" => move_line_down, }); - let keymap = Keymap::new(normal_mode); + let keymap = normal_mode; let mut reverse_map = keymap.reverse_map(); // sort keybindings in order to have consistent tests @@ -601,7 +571,7 @@ mod tests { modifiers: KeyModifiers::NONE, }; - let expectation = Keymap::new(KeyTrie::Node(KeyTrieNode::new( + let expectation = KeyTrie::Node(KeyTrieNode::new( "", hashmap! { key => KeyTrie::Sequence(vec!{ @@ -618,7 +588,7 @@ mod tests { }) }, vec![key], - ))); + )); assert_eq!(toml::from_str(keys), Ok(expectation)); } diff --git a/helix-term/src/keymap/default.rs b/helix-term/src/keymap/default.rs index d80ab1ff..c84c616c 100644 --- a/helix-term/src/keymap/default.rs +++ b/helix-term/src/keymap/default.rs @@ -1,10 +1,10 @@ use std::collections::HashMap; use super::macros::keymap; -use super::{Keymap, Mode}; +use super::{KeyTrie, Mode}; use helix_core::hashmap; -pub fn default() -> HashMap { +pub fn default() -> HashMap { let normal = keymap!({ "Normal mode" "h" | "left" => move_char_left, "j" | "down" => move_visual_line_down, @@ -380,8 +380,8 @@ pub fn default() -> HashMap { "end" => goto_line_end_newline, }); hashmap!( - Mode::Normal => Keymap::new(normal), - Mode::Select => Keymap::new(select), - Mode::Insert => Keymap::new(insert), + Mode::Normal => normal, + Mode::Select => select, + Mode::Insert => insert, ) } diff --git a/helix-term/src/keymap/macros.rs b/helix-term/src/keymap/macros.rs index b2822069..15d2aa53 100644 --- a/helix-term/src/keymap/macros.rs +++ b/helix-term/src/keymap/macros.rs @@ -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_term::keymap; -/// # use helix_term::keymap::Keymap; /// let normal_mode = keymap!({ "Normal mode" /// "i" => insert_mode, /// "g" => { "Goto" @@ -76,7 +75,7 @@ macro_rules! alt { /// }, /// "j" | "down" => move_line_down, /// }); -/// let keymap = Keymap::new(normal_mode); +/// let keymap = normal_mode; /// ``` #[macro_export] macro_rules! keymap { -- cgit v1.2.3-70-g09d2