Better hardcoded keybind semantics

This commit is contained in:
mars 2023-04-14 14:51:46 -04:00
parent c03991f110
commit 7f46721caf
1 changed files with 26 additions and 20 deletions

View File

@ -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<T> for ModeKeys
where
T: IntoIterator<Item = &'a (Key, Action)>,
{
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<Self, String> {
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<Item = &'a (Key, Action)>,
{
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<KeyMap, String> {
let mut map = KeyMap::with_capacity(values.len());