on master help sign in
keymap: Store pending/sticky on the root level
7909d6fBlaž Hrastnik
helix-term/src/commands.rs+1−1
@@ -2105,7 +2105,7 @@ pub fn command_palette(cx: &mut Context) {
2105 move |compositor: &mut Compositor, cx: &mut compositor::Context| {
2105 move |compositor: &mut Compositor, cx: &mut compositor::Context| {
2106 let doc = doc_mut!(cx.editor);
2106 let doc = doc_mut!(cx.editor);
2107 let keymap =
2107 let keymap =
2108− compositor.find::<ui::EditorView>().unwrap().keymaps[&doc.mode].reverse_map();
2108+ compositor.find::<ui::EditorView>().unwrap().keymaps.map[&doc.mode].reverse_map();
2109
2109
2110 let mut commands: Vec<MappableCommand> = MappableCommand::STATIC_COMMAND_LIST.into();
2110 let mut commands: Vec<MappableCommand> = MappableCommand::STATIC_COMMAND_LIST.into();
2111 commands.extend(typed::TYPABLE_COMMAND_LIST.iter().map(|cmd| {
2111 commands.extend(typed::TYPABLE_COMMAND_LIST.iter().map(|cmd| {
helix-term/src/config.rs+1−1
@@ -43,7 +43,7 @@ mod tests {
43 assert_eq!(
43 assert_eq!(
44 toml::from_str::<Config>(sample_keymaps).unwrap(),
44 toml::from_str::<Config>(sample_keymaps).unwrap(),
45 Config {
45 Config {
46− keys: Keymaps(hashmap! {
46+ keys: Keymaps::new(hashmap! {
47 Mode::Insert => Keymap::new(keymap!({ "Insert mode"
47 Mode::Insert => Keymap::new(keymap!({ "Insert mode"
48 "y" => move_line_down,
48 "y" => move_line_down,
49 "S-C-a" => delete_selection,
49 "S-C-a" => delete_selection,
helix-term/src/keymap.rs+68−80
@@ -328,26 +328,15 @@ impl<'a> KeymapResult<'a> {
328}
328}
329
329
330#[derive(Debug, Clone, PartialEq, Deserialize)]
330#[derive(Debug, Clone, PartialEq, Deserialize)]
331+#[serde(transparent)]
331pub struct Keymap {
332pub struct Keymap {
332 /// Always a Node
333 /// Always a Node
333− #[serde(flatten)]
334 root: KeyTrie,
334 root: KeyTrie,
335− /// Stores pending keys waiting for the next key. This is relative to a
336− /// sticky node if one is in use.
337− #[serde(skip)]
338− state: Vec<KeyEvent>,
339− /// Stores the sticky node if one is activated.
340− #[serde(skip)]
341− sticky: Option<KeyTrieNode>,
342}
335}
343
336
344impl Keymap {
337impl Keymap {
345 pub fn new(root: KeyTrie) -> Self {
338 pub fn new(root: KeyTrie) -> Self {
346− Keymap {
339+ Keymap { root }
347− root,
348− state: Vec::new(),
349− sticky: None,
350− }
351 }
340 }
352
341
353 pub fn reverse_map(&self) -> HashMap<String, Vec<Vec<KeyEvent>>> {
342 pub fn reverse_map(&self) -> HashMap<String, Vec<Vec<KeyEvent>>> {
@@ -387,19 +376,65 @@ impl Keymap {
387 &self.root
376 &self.root
388 }
377 }
389
378
379+ pub fn merge(&mut self, other: Self) {
380+ self.root.merge_nodes(other.root);
381+ }
382+}
383+
384+impl Deref for Keymap {
390− pub fn sticky(&self) -> Option<&KeyTrieNode> {
385+ type Target = KeyTrieNode;
391− self.sticky.as_ref()
386+
387+ fn deref(&self) -> &Self::Target {
388+ self.root.node().unwrap()
392 }
389 }
390+}
393
391
392+impl Default for Keymap {
393+ fn default() -> Self {
394+ Self::new(KeyTrie::Node(KeyTrieNode::default()))
395+ }
396+}
397+
398+#[derive(Debug, Clone, PartialEq, Deserialize)]
399+pub struct Keymaps {
400+ #[serde(flatten)]
401+ pub map: HashMap<Mode, Keymap>,
402+
403+ /// Stores pending keys waiting for the next key. This is relative to a
404+ /// sticky node if one is in use.
405+ #[serde(skip)]
406+ state: Vec<KeyEvent>,
407+
408+ /// Stores the sticky node if one is activated.
409+ #[serde(skip)]
410+ pub sticky: Option<KeyTrieNode>,
411+}
412+
413+impl Keymaps {
414+ pub fn new(map: HashMap<Mode, Keymap>) -> Self {
415+ Self {
416+ map,
417+ state: Vec::new(),
418+ sticky: None,
419+ }
420+ }
421+
394− /// Returns list of keys waiting to be disambiguated.
422+ /// Returns list of keys waiting to be disambiguated in current mode.
395 pub fn pending(&self) -> &[KeyEvent] {
423 pub fn pending(&self) -> &[KeyEvent] {
396 &self.state
424 &self.state
397 }
425 }
398
426
427+ pub fn sticky(&self) -> Option<&KeyTrieNode> {
428+ self.sticky.as_ref()
429+ }
430+
399 /// Lookup `key` in the keymap to try and find a command to execute. Escape
431 /// Lookup `key` in the keymap to try and find a command to execute. Escape
400 /// key cancels pending keystrokes. If there are no pending keystrokes but a
432 /// key cancels pending keystrokes. If there are no pending keystrokes but a
401 /// sticky node is in use, it will be cleared.
433 /// sticky node is in use, it will be cleared.
402− pub fn get(&mut self, key: KeyEvent) -> KeymapResult {
434+ pub fn get(&mut self, mode: Mode, key: KeyEvent) -> KeymapResult {
435+ // TODO: remove the sticky part and look up manually
436+ let keymap = &self.map[&mode];
437+
403 if key!(Esc) == key {
438 if key!(Esc) == key {
404 if !self.state.is_empty() {
439 if !self.state.is_empty() {
405 return KeymapResult::new(
440 return KeymapResult::new(
@@ -414,7 +449,7 @@ impl Keymap {
414 let first = self.state.get(0).unwrap_or(&key);
449 let first = self.state.get(0).unwrap_or(&key);
415 let trie_node = match self.sticky {
450 let trie_node = match self.sticky {
416 Some(ref trie) => Cow::Owned(KeyTrie::Node(trie.clone())),
451 Some(ref trie) => Cow::Owned(KeyTrie::Node(trie.clone())),
417− None => Cow::Borrowed(&self.root),
452+ None => Cow::Borrowed(&keymap.root),
418 };
453 };
419
454
420 let trie = match trie_node.search(&[*first]) {
455 let trie = match trie_node.search(&[*first]) {
@@ -457,55 +492,6 @@ impl Keymap {
457 ),
492 ),
458 }
493 }
459 }
494 }
460−
461− pub fn merge(&mut self, other: Self) {
462− self.root.merge_nodes(other.root);
463− }
464−}
465−
466−impl Deref for Keymap {
467− type Target = KeyTrieNode;
468−
469− fn deref(&self) -> &Self::Target {
470− self.root.node().unwrap()
471− }
472−}
473−
474−impl Default for Keymap {
475− fn default() -> Self {
476− Self::new(KeyTrie::Node(KeyTrieNode::default()))
477− }
478−}
479−
480−#[derive(Debug, Clone, PartialEq, Deserialize)]
481−#[serde(transparent)]
482−pub struct Keymaps(pub HashMap<Mode, Keymap>);
483−
484−impl Keymaps {
485− /// Returns list of keys waiting to be disambiguated in current mode.
486− pub fn pending(&self) -> &[KeyEvent] {
487− self.0
488− .values()
489− .find_map(|keymap| match keymap.pending().is_empty() {
490− true => None,
491− false => Some(keymap.pending()),
492− })
493− .unwrap_or_default()
494− }
495−}
496−
497−impl Deref for Keymaps {
498− type Target = HashMap<Mode, Keymap>;
499−
500− fn deref(&self) -> &Self::Target {
501− &self.0
502− }
503−}
504−
505−impl DerefMut for Keymaps {
506− fn deref_mut(&mut self) -> &mut Self::Target {
507− &mut self.0
508− }
509}
495}
510
496
511impl Default for Keymaps {
497impl Default for Keymaps {
@@ -856,7 +842,7 @@ impl Default for Keymaps {
856 "C-x" => completion,
842 "C-x" => completion,
857 "C-r" => insert_register,
843 "C-r" => insert_register,
858 });
844 });
859− Self(hashmap!(
845+ Self::new(hashmap!(
860 Mode::Normal => Keymap::new(normal),
846 Mode::Normal => Keymap::new(normal),
861 Mode::Select => Keymap::new(select),
847 Mode::Select => Keymap::new(select),
862 Mode::Insert => Keymap::new(insert),
848 Mode::Insert => Keymap::new(insert),
@@ -867,8 +853,8 @@ impl Default for Keymaps {
867/// Merge default config keys with user overwritten keys for custom user config.
853/// Merge default config keys with user overwritten keys for custom user config.
868pub fn merge_keys(mut config: Config) -> Config {
854pub fn merge_keys(mut config: Config) -> Config {
869 let mut delta = std::mem::take(&mut config.keys);
855 let mut delta = std::mem::take(&mut config.keys);
870− for (mode, keys) in &mut *config.keys {
856+ for (mode, keys) in &mut config.keys.map {
871− keys.merge(delta.remove(mode).unwrap_or_default())
857+ keys.merge(delta.map.remove(mode).unwrap_or_default())
872 }
858 }
873 config
859 config
874}
860}
@@ -895,7 +881,7 @@ mod tests {
895 #[test]
881 #[test]
896 fn merge_partial_keys() {
882 fn merge_partial_keys() {
897 let config = Config {
883 let config = Config {
898− keys: Keymaps(hashmap! {
884+ keys: Keymaps::new(hashmap! {
899 Mode::Normal => Keymap::new(
885 Mode::Normal => Keymap::new(
900 keymap!({ "Normal mode"
886 keymap!({ "Normal mode"
901 "i" => normal_mode,
887 "i" => normal_mode,
@@ -913,23 +899,25 @@ mod tests {
913 let mut merged_config = merge_keys(config.clone());
899 let mut merged_config = merge_keys(config.clone());
914 assert_ne!(config, merged_config);
900 assert_ne!(config, merged_config);
915
901
916− let keymap = merged_config.keys.0.get_mut(&Mode::Normal).unwrap();
902+ let keymap = &mut merged_config.keys;
917 assert_eq!(
903 assert_eq!(
918− keymap.get(key!('i')).kind,
904+ keymap.get(Mode::Normal, key!('i')).kind,
919 KeymapResultKind::Matched(MappableCommand::normal_mode),
905 KeymapResultKind::Matched(MappableCommand::normal_mode),
920 "Leaf should replace leaf"
906 "Leaf should replace leaf"
921 );
907 );
922 assert_eq!(
908 assert_eq!(
923− keymap.get(key!('无')).kind,
909+ keymap.get(Mode::Normal, key!('无')).kind,
924 KeymapResultKind::Matched(MappableCommand::insert_mode),
910 KeymapResultKind::Matched(MappableCommand::insert_mode),
925 "New leaf should be present in merged keymap"
911 "New leaf should be present in merged keymap"
926 );
912 );
927 // Assumes that z is a node in the default keymap
913 // Assumes that z is a node in the default keymap
928 assert_eq!(
914 assert_eq!(
929− keymap.get(key!('z')).kind,
915+ keymap.get(Mode::Normal, key!('z')).kind,
930 KeymapResultKind::Matched(MappableCommand::jump_backward),
916 KeymapResultKind::Matched(MappableCommand::jump_backward),
931 "Leaf should replace node"
917 "Leaf should replace node"
932 );
918 );
919+
920+ let keymap = merged_config.keys.map.get_mut(&Mode::Normal).unwrap();
933 // Assumes that `g` is a node in default keymap
921 // Assumes that `g` is a node in default keymap
934 assert_eq!(
922 assert_eq!(
935 keymap.root().search(&[key!('g'), key!('$')]).unwrap(),
923 keymap.root().search(&[key!('g'), key!('$')]).unwrap(),
@@ -949,14 +937,14 @@ mod tests {
949 "Old leaves in subnode should be present in merged node"
937 "Old leaves in subnode should be present in merged node"
950 );
938 );
951
939
952− assert!(merged_config.keys.0.get(&Mode::Normal).unwrap().len() > 1);
940+ assert!(merged_config.keys.map.get(&Mode::Normal).unwrap().len() > 1);
953− assert!(merged_config.keys.0.get(&Mode::Insert).unwrap().len() > 0);
941+ assert!(merged_config.keys.map.get(&Mode::Insert).unwrap().len() > 0);
954 }
942 }
955
943
956 #[test]
944 #[test]
957 fn order_should_be_set() {
945 fn order_should_be_set() {
958 let config = Config {
946 let config = Config {
959− keys: Keymaps(hashmap! {
947+ keys: Keymaps::new(hashmap! {
960 Mode::Normal => Keymap::new(
948 Mode::Normal => Keymap::new(
961 keymap!({ "Normal mode"
949 keymap!({ "Normal mode"
962 "space" => { ""
950 "space" => { ""
@@ -972,7 +960,7 @@ mod tests {
972 };
960 };
973 let mut merged_config = merge_keys(config.clone());
961 let mut merged_config = merge_keys(config.clone());
974 assert_ne!(config, merged_config);
962 assert_ne!(config, merged_config);
975− let keymap = merged_config.keys.0.get_mut(&Mode::Normal).unwrap();
963+ let keymap = merged_config.keys.map.get_mut(&Mode::Normal).unwrap();
976 // Make sure mapping works
964 // Make sure mapping works
977 assert_eq!(
965 assert_eq!(
978 keymap
966 keymap
@@ -990,7 +978,7 @@ mod tests {
990 #[test]
978 #[test]
991 fn aliased_modes_are_same_in_default_keymap() {
979 fn aliased_modes_are_same_in_default_keymap() {
992 let keymaps = Keymaps::default();
980 let keymaps = Keymaps::default();
993− let root = keymaps.get(&Mode::Normal).unwrap().root();
981+ let root = keymaps.map.get(&Mode::Normal).unwrap().root();
994 assert_eq!(
982 assert_eq!(
995 root.search(&[key!(' '), key!('w')]).unwrap(),
983 root.search(&[key!(' '), key!('w')]).unwrap(),
996 root.search(&["C-w".parse::<KeyEvent>().unwrap()]).unwrap(),
984 root.search(&["C-w".parse::<KeyEvent>().unwrap()]).unwrap(),
helix-term/src/ui/editor.rs+7−8
@@ -703,7 +703,7 @@ impl EditorView {
703 event: KeyEvent,
703 event: KeyEvent,
704 ) -> Option<KeymapResult> {
704 ) -> Option<KeymapResult> {
705 cxt.editor.autoinfo = None;
705 cxt.editor.autoinfo = None;
706− let key_result = self.keymaps.get_mut(&mode).unwrap().get(event);
706+ let key_result = self.keymaps.get(mode, event);
707 cxt.editor.autoinfo = key_result.sticky.map(|node| node.infobox());
707 cxt.editor.autoinfo = key_result.sticky.map(|node| node.infobox());
708
708
709 match &key_result.kind {
709 match &key_result.kind {
@@ -733,7 +733,7 @@ impl EditorView {
733 Some(ch) => commands::insert::insert_char(cx, ch),
733 Some(ch) => commands::insert::insert_char(cx, ch),
734 None => {
734 None => {
735 if let KeymapResultKind::Matched(command) =
735 if let KeymapResultKind::Matched(command) =
736− self.keymaps.get_mut(&Mode::Insert).unwrap().get(ev).kind
736+ self.keymaps.get(Mode::Insert, ev).kind
737 {
737 {
738 command.execute(cx);
738 command.execute(cx);
739 }
739 }
@@ -1182,12 +1182,11 @@ impl Component for EditorView {
1182 // how we entered insert mode is important, and we should track that so
1182 // how we entered insert mode is important, and we should track that so
1183 // we can repeat the side effect.
1183 // we can repeat the side effect.
1184
1184
1185− self.last_insert.0 =
1186− match self.keymaps.get_mut(&mode).unwrap().get(key).kind {
1185+ self.last_insert.0 = match self.keymaps.get(mode, key).kind {
1187− KeymapResultKind::Matched(command) => command,
1186+ KeymapResultKind::Matched(command) => command,
1188− // FIXME: insert mode can only be entered through single KeyCodes
1187+ // FIXME: insert mode can only be entered through single KeyCodes
1189− _ => unimplemented!(),
1188+ _ => unimplemented!(),
1190− };
1189+ };
1191 self.last_insert.1.clear();
1190 self.last_insert.1.clear();
1192 }
1191 }
1193 (Mode::Insert, Mode::Normal) => {
1192 (Mode::Insert, Mode::Normal) => {