on master help sign in
No need for KeymapResult anymore since we can query .sticky()
a7ee9f7Blaž Hrastnik
helix-term/src/keymap.rs+16−42
@@ -300,7 +300,7 @@ impl KeyTrie {
300}
300}
301
301
302#[derive(Debug, Clone, PartialEq)]
302#[derive(Debug, Clone, PartialEq)]
303−pub enum KeymapResultKind {
303+pub enum KeymapResult {
304 /// Needs more keys to execute a command. Contains valid keys for next keystroke.
304 /// Needs more keys to execute a command. Contains valid keys for next keystroke.
305 Pending(KeyTrieNode),
305 Pending(KeyTrieNode),
306 Matched(MappableCommand),
306 Matched(MappableCommand),
@@ -313,20 +313,6 @@ pub enum KeymapResultKind {
313 Cancelled(Vec<KeyEvent>),
313 Cancelled(Vec<KeyEvent>),
314}
314}
315
315
316−/// Returned after looking up a key in [`Keymap`]. The `sticky` field has a
317−/// reference to the sticky node if one is currently active.
318−#[derive(Debug)]
319−pub struct KeymapResult<'a> {
320− pub kind: KeymapResultKind,
321− pub sticky: Option<&'a KeyTrieNode>,
322−}
323−
324−impl<'a> KeymapResult<'a> {
325− pub fn new(kind: KeymapResultKind, sticky: Option<&'a KeyTrieNode>) -> Self {
326− Self { kind, sticky }
327− }
328−}
329−
330#[derive(Debug, Clone, PartialEq, Deserialize)]
316#[derive(Debug, Clone, PartialEq, Deserialize)]
331#[serde(transparent)]
317#[serde(transparent)]
332pub struct Keymap {
318pub struct Keymap {
@@ -437,11 +423,8 @@ impl Keymaps {
437
423
438 if key!(Esc) == key {
424 if key!(Esc) == key {
439 if !self.state.is_empty() {
425 if !self.state.is_empty() {
440− return KeymapResult::new(
441− // Note that Esc is not included here
426+ // Note that Esc is not included here
442− KeymapResultKind::Cancelled(self.state.drain(..).collect()),
427+ return KeymapResult::Cancelled(self.state.drain(..).collect());
443− self.sticky(),
444− );
445 }
428 }
446 self.sticky = None;
429 self.sticky = None;
447 }
430 }
@@ -454,15 +437,12 @@ impl Keymaps {
454
437
455 let trie = match trie_node.search(&[*first]) {
438 let trie = match trie_node.search(&[*first]) {
456 Some(KeyTrie::Leaf(ref cmd)) => {
439 Some(KeyTrie::Leaf(ref cmd)) => {
457− return KeymapResult::new(KeymapResultKind::Matched(cmd.clone()), self.sticky())
440+ return KeymapResult::Matched(cmd.clone());
458 }
441 }
459 Some(KeyTrie::Sequence(ref cmds)) => {
442 Some(KeyTrie::Sequence(ref cmds)) => {
460− return KeymapResult::new(
461− KeymapResultKind::MatchedSequence(cmds.clone()),
443+ return KeymapResult::MatchedSequence(cmds.clone());
462− self.sticky(),
463− )
464 }
444 }
465− None => return KeymapResult::new(KeymapResultKind::NotFound, self.sticky()),
445+ None => return KeymapResult::NotFound,
466 Some(t) => t,
446 Some(t) => t,
467 };
447 };
468
448
@@ -473,23 +453,17 @@ impl Keymaps {
473 self.state.clear();
453 self.state.clear();
474 self.sticky = Some(map.clone());
454 self.sticky = Some(map.clone());
475 }
455 }
476− KeymapResult::new(KeymapResultKind::Pending(map.clone()), self.sticky())
456+ KeymapResult::Pending(map.clone())
477 }
457 }
478 Some(&KeyTrie::Leaf(ref cmd)) => {
458 Some(&KeyTrie::Leaf(ref cmd)) => {
479 self.state.clear();
459 self.state.clear();
480− return KeymapResult::new(KeymapResultKind::Matched(cmd.clone()), self.sticky());
460+ KeymapResult::Matched(cmd.clone())
481 }
461 }
482 Some(&KeyTrie::Sequence(ref cmds)) => {
462 Some(&KeyTrie::Sequence(ref cmds)) => {
483 self.state.clear();
463 self.state.clear();
484− KeymapResult::new(
485− KeymapResultKind::MatchedSequence(cmds.clone()),
464+ KeymapResult::MatchedSequence(cmds.clone())
486− self.sticky(),
487− )
488 }
465 }
489− None => KeymapResult::new(
490− KeymapResultKind::Cancelled(self.state.drain(..).collect()),
466+ None => KeymapResult::Cancelled(self.state.drain(..).collect()),
491− self.sticky(),
492− ),
493 }
467 }
494 }
468 }
495}
469}
@@ -901,19 +875,19 @@ mod tests {
901
875
902 let keymap = &mut merged_config.keys;
876 let keymap = &mut merged_config.keys;
903 assert_eq!(
877 assert_eq!(
904− keymap.get(Mode::Normal, key!('i')).kind,
878+ keymap.get(Mode::Normal, key!('i')),
905− KeymapResultKind::Matched(MappableCommand::normal_mode),
879+ KeymapResult::Matched(MappableCommand::normal_mode),
906 "Leaf should replace leaf"
880 "Leaf should replace leaf"
907 );
881 );
908 assert_eq!(
882 assert_eq!(
909− keymap.get(Mode::Normal, key!('无')).kind,
883+ keymap.get(Mode::Normal, key!('无')),
910− KeymapResultKind::Matched(MappableCommand::insert_mode),
884+ KeymapResult::Matched(MappableCommand::insert_mode),
911 "New leaf should be present in merged keymap"
885 "New leaf should be present in merged keymap"
912 );
886 );
913 // Assumes that z is a node in the default keymap
887 // Assumes that z is a node in the default keymap
914 assert_eq!(
888 assert_eq!(
915− keymap.get(Mode::Normal, key!('z')).kind,
889+ keymap.get(Mode::Normal, key!('z')),
916− KeymapResultKind::Matched(MappableCommand::jump_backward),
890+ KeymapResult::Matched(MappableCommand::jump_backward),
917 "Leaf should replace node"
891 "Leaf should replace node"
918 );
892 );
919
893
helix-term/src/ui/editor.rs+15−15
@@ -2,7 +2,7 @@ use crate::{
2 commands,
2 commands,
3 compositor::{Component, Context, EventResult},
3 compositor::{Component, Context, EventResult},
4 key,
4 key,
5− keymap::{KeymapResult, KeymapResultKind, Keymaps},
5+ keymap::{KeymapResult, Keymaps},
6 ui::{Completion, ProgressSpinners},
6 ui::{Completion, ProgressSpinners},
7};
7};
8
8
@@ -694,7 +694,7 @@ impl EditorView {
694
694
695 /// Handle events by looking them up in `self.keymaps`. Returns None
695 /// Handle events by looking them up in `self.keymaps`. Returns None
696 /// if event was handled (a command was executed or a subkeymap was
696 /// if event was handled (a command was executed or a subkeymap was
697− /// activated). Only KeymapResultKind::{NotFound, Cancelled} is returned
697+ /// activated). Only KeymapResult::{NotFound, Cancelled} is returned
698 /// otherwise.
698 /// otherwise.
699 fn handle_keymap_event(
699 fn handle_keymap_event(
700 &mut self,
700 &mut self,
@@ -704,36 +704,36 @@ impl EditorView {
704 ) -> Option<KeymapResult> {
704 ) -> Option<KeymapResult> {
705 cxt.editor.autoinfo = None;
705 cxt.editor.autoinfo = None;
706 let key_result = self.keymaps.get(mode, 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 = self.keymaps.sticky().map(|node| node.infobox());
708
708
709− match &key_result.kind {
709+ match &key_result {
710− KeymapResultKind::Matched(command) => command.execute(cxt),
710+ KeymapResult::Matched(command) => command.execute(cxt),
711− KeymapResultKind::Pending(node) => cxt.editor.autoinfo = Some(node.infobox()),
711+ KeymapResult::Pending(node) => cxt.editor.autoinfo = Some(node.infobox()),
712− KeymapResultKind::MatchedSequence(commands) => {
712+ KeymapResult::MatchedSequence(commands) => {
713 for command in commands {
713 for command in commands {
714 command.execute(cxt);
714 command.execute(cxt);
715 }
715 }
716 }
716 }
717− KeymapResultKind::NotFound | KeymapResultKind::Cancelled(_) => return Some(key_result),
717+ KeymapResult::NotFound | KeymapResult::Cancelled(_) => return Some(key_result),
718 }
718 }
719 None
719 None
720 }
720 }
721
721
722 fn insert_mode(&mut self, cx: &mut commands::Context, event: KeyEvent) {
722 fn insert_mode(&mut self, cx: &mut commands::Context, event: KeyEvent) {
723 if let Some(keyresult) = self.handle_keymap_event(Mode::Insert, cx, event) {
723 if let Some(keyresult) = self.handle_keymap_event(Mode::Insert, cx, event) {
724− match keyresult.kind {
724+ match keyresult {
725− KeymapResultKind::NotFound => {
725+ KeymapResult::NotFound => {
726 if let Some(ch) = event.char() {
726 if let Some(ch) = event.char() {
727 commands::insert::insert_char(cx, ch)
727 commands::insert::insert_char(cx, ch)
728 }
728 }
729 }
729 }
730− KeymapResultKind::Cancelled(pending) => {
730+ KeymapResult::Cancelled(pending) => {
731 for ev in pending {
731 for ev in pending {
732 match ev.char() {
732 match ev.char() {
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 KeymapResult::Matched(command) =
736− self.keymaps.get(Mode::Insert, ev).kind
736+ self.keymaps.get(Mode::Insert, ev)
737 {
737 {
738 command.execute(cx);
738 command.execute(cx);
739 }
739 }
@@ -1182,8 +1182,8 @@ 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 = match self.keymaps.get(mode, key).kind {
1185+ self.last_insert.0 = match self.keymaps.get(mode, key) {
1186− KeymapResultKind::Matched(command) => command,
1186+ KeymapResult::Matched(command) => command,
1187 // FIXME: insert mode can only be entered through single KeyCodes
1187 // FIXME: insert mode can only be entered through single KeyCodes
1188 _ => unimplemented!(),
1188 _ => unimplemented!(),
1189 };
1189 };