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::{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<String>,
pub keys: HashMap<Mode, Keymap>,
pub keys: HashMap<Mode, KeyTrie>,
pub editor: helix_view::editor::Config,
}
@ -20,7 +20,7 @@ pub struct Config {
#[serde(deny_unknown_fields)]
pub struct ConfigRaw {
pub theme: Option<String>,
pub keys: Option<HashMap<Mode, Keymap>>,
pub keys: Option<HashMap<Mode, KeyTrie>>,
pub editor: Option<toml::Value>,
}
@ -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,
})),
}),
},
);

@ -269,41 +269,11 @@ pub enum KeymapResult {
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.
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 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
/// sticky node if one is in use.
state: Vec<KeyEvent>,
@ -312,7 +282,7 @@ pub struct 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 {
map,
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()
}
@ -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<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 {
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));
}

@ -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<Mode, Keymap> {
pub fn default() -> HashMap<Mode, KeyTrie> {
let normal = keymap!({ "Normal mode"
"h" | "left" => move_char_left,
"j" | "down" => move_visual_line_down,
@ -380,8 +380,8 @@ pub fn default() -> HashMap<Mode, Keymap> {
"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,
)
}

@ -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 {

Loading…
Cancel
Save