From 7f46721cafc9b0a52b82dd508adbbce306901b3d Mon Sep 17 00:00:00 2001 From: mars Date: Fri, 14 Apr 2023 14:51:46 -0400 Subject: [PATCH] Better hardcoded keybind semantics --- src/keybinds.rs | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/src/keybinds.rs b/src/keybinds.rs index 5b7958d..fd2c4b2 100644 --- a/src/keybinds.rs +++ b/src/keybinds.rs @@ -50,7 +50,7 @@ impl Default for Keybinds { (End, goto_line_end_newline), ]; - let normalish_keys = &[ + let normalish_keys = [ (Char(':'), command_mode as Action), (Char('h'), move_char_left), (Char('j'), move_line_down), @@ -68,7 +68,7 @@ impl Default for Keybinds { .iter() .chain(basic_nav); - let insert_keys = &[ + let insert_keys = [ (Backspace, delete_char_backward as Action), (Delete, delete_char_forward), (Enter, insert_newline), @@ -77,9 +77,18 @@ impl Default for Keybinds { .chain(basic_nav); Self { - normal: normalish_keys.clone().into(), - insert: insert_keys.clone().into(), - visual: normalish_keys.clone().into(), + normal: ModeKeys { + submodes: [].into(), + map: key_map_from_iter(normalish_keys.clone()), + }, + insert: ModeKeys { + submodes: [].into(), + map: key_map_from_iter(insert_keys), + }, + visual: ModeKeys { + submodes: [].into(), + map: key_map_from_iter(normalish_keys.clone()), + }, } } } @@ -90,21 +99,6 @@ pub struct ModeKeys { pub map: KeyMap, } -impl<'a, T> From for ModeKeys -where - T: IntoIterator, -{ - fn from(iter: T) -> Self { - let submodes = HashMap::new(); - let mut map = KeyMap::new(); - for (key, action) in iter { - map.insert(*key, Keybind::Action(*action)); - } - - Self { submodes, map } - } -} - impl ModeKeys { pub fn apply_table(values: Table) -> Result { let mut keys = Self::default(); @@ -162,6 +156,18 @@ impl TryFrom<&str> for Keybind { } } +pub fn key_map_from_iter<'a, T>(iter: T) -> KeyMap +where + T: IntoIterator, +{ + let mut map = KeyMap::new(); + for (key, action) in iter { + map.insert(*key, Keybind::Action(*action)); + } + + map +} + pub fn parse_key_map(values: Table) -> Result { let mut map = KeyMap::with_capacity(values.len());