on master help sign in
No need for KeymapResult anymore since we can query .sticky()
commita7ee9f74f771a486f99dca9e7a6084f74f19eb46 authorBlaž Hrastnik <blaz@mxxn.io> dateSun 20 Mar 2022, 15:55 (5 years ago) parent7909d6f
2 files changed+31−57
helix-term/src/keymap.rs+16−42 helix-term/src/ui/editor.rs+15−15
helix-term/src/keymap.rs+16−42
@@ -300,7 +300,7 @@ impl KeyTrie {
300300}
301301
302302#[derive(Debug, Clone, PartialEq)]
303−pub enum KeymapResultKind {
303+pub enum KeymapResult {
304304 /// Needs more keys to execute a command. Contains valid keys for next keystroke.
305305 Pending(KeyTrieNode),
306306 Matched(MappableCommand),
@@ -313,20 +313,6 @@ pub enum KeymapResultKind {
313313 Cancelled(Vec<KeyEvent>),
314314}
315315
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−
330316#[derive(Debug, Clone, PartialEq, Deserialize)]
331317#[serde(transparent)]
332318pub struct Keymap {
@@ -437,11 +423,8 @@ impl Keymaps {
437423
438424 if key!(Esc) == key {
439425 if !self.state.is_empty() {
440− return KeymapResult::new(
441− // Note that Esc is not included here
442− KeymapResultKind::Cancelled(self.state.drain(..).collect()),
443− self.sticky(),
444− );
426+ // Note that Esc is not included here
427+ return KeymapResult::Cancelled(self.state.drain(..).collect());
445428 }
446429 self.sticky = None;
447430 }
@@ -454,15 +437,12 @@ impl Keymaps {
454437
455438 let trie = match trie_node.search(&[*first]) {
456439 Some(KeyTrie::Leaf(ref cmd)) => {
457− return KeymapResult::new(KeymapResultKind::Matched(cmd.clone()), self.sticky())
440+ return KeymapResult::Matched(cmd.clone());
458441 }
459442 Some(KeyTrie::Sequence(ref cmds)) => {
460− return KeymapResult::new(
461− KeymapResultKind::MatchedSequence(cmds.clone()),
462− self.sticky(),
463− )
443+ return KeymapResult::MatchedSequence(cmds.clone());
464444 }
465− None => return KeymapResult::new(KeymapResultKind::NotFound, self.sticky()),
445+ None => return KeymapResult::NotFound,
466446 Some(t) => t,
467447 };
468448
@@ -473,23 +453,17 @@ impl Keymaps {
473453 self.state.clear();
474454 self.sticky = Some(map.clone());
475455 }
476− KeymapResult::new(KeymapResultKind::Pending(map.clone()), self.sticky())
456+ KeymapResult::Pending(map.clone())
477457 }
478458 Some(&KeyTrie::Leaf(ref cmd)) => {
479459 self.state.clear();
480− return KeymapResult::new(KeymapResultKind::Matched(cmd.clone()), self.sticky());
460+ KeymapResult::Matched(cmd.clone())
481461 }
482462 Some(&KeyTrie::Sequence(ref cmds)) => {
483463 self.state.clear();
484− KeymapResult::new(
485− KeymapResultKind::MatchedSequence(cmds.clone()),
486− self.sticky(),
487− )
464+ KeymapResult::MatchedSequence(cmds.clone())
488465 }
489− None => KeymapResult::new(
490− KeymapResultKind::Cancelled(self.state.drain(..).collect()),
491− self.sticky(),
492− ),
466+ None => KeymapResult::Cancelled(self.state.drain(..).collect()),
493467 }
494468 }
495469}
@@ -901,19 +875,19 @@ mod tests {
901875
902876 let keymap = &mut merged_config.keys;
903877 assert_eq!(
904− keymap.get(Mode::Normal, key!('i')).kind,
905− KeymapResultKind::Matched(MappableCommand::normal_mode),
878+ keymap.get(Mode::Normal, key!('i')),
879+ KeymapResult::Matched(MappableCommand::normal_mode),
906880 "Leaf should replace leaf"
907881 );
908882 assert_eq!(
909− keymap.get(Mode::Normal, key!('无')).kind,
910− KeymapResultKind::Matched(MappableCommand::insert_mode),
883+ keymap.get(Mode::Normal, key!('无')),
884+ KeymapResult::Matched(MappableCommand::insert_mode),
911885 "New leaf should be present in merged keymap"
912886 );
913887 // Assumes that z is a node in the default keymap
914888 assert_eq!(
915− keymap.get(Mode::Normal, key!('z')).kind,
916− KeymapResultKind::Matched(MappableCommand::jump_backward),
889+ keymap.get(Mode::Normal, key!('z')),
890+ KeymapResult::Matched(MappableCommand::jump_backward),
917891 "Leaf should replace node"
918892 );
919893
helix-term/src/ui/editor.rs+15−15
@@ -2,7 +2,7 @@ use crate::{
22 commands,
33 compositor::{Component, Context, EventResult},
44 key,
5− keymap::{KeymapResult, KeymapResultKind, Keymaps},
5+ keymap::{KeymapResult, Keymaps},
66 ui::{Completion, ProgressSpinners},
77};
88
@@ -694,7 +694,7 @@ impl EditorView {
694694
695695 /// Handle events by looking them up in `self.keymaps`. Returns None
696696 /// 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
698698 /// otherwise.
699699 fn handle_keymap_event(
700700 &mut self,
@@ -704,36 +704,36 @@ impl EditorView {
704704 ) -> Option<KeymapResult> {
705705 cxt.editor.autoinfo = None;
706706 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());
708708
709− match &key_result.kind {
710− KeymapResultKind::Matched(command) => command.execute(cxt),
711− KeymapResultKind::Pending(node) => cxt.editor.autoinfo = Some(node.infobox()),
712− KeymapResultKind::MatchedSequence(commands) => {
709+ match &key_result {
710+ KeymapResult::Matched(command) => command.execute(cxt),
711+ KeymapResult::Pending(node) => cxt.editor.autoinfo = Some(node.infobox()),
712+ KeymapResult::MatchedSequence(commands) => {
713713 for command in commands {
714714 command.execute(cxt);
715715 }
716716 }
717− KeymapResultKind::NotFound | KeymapResultKind::Cancelled(_) => return Some(key_result),
717+ KeymapResult::NotFound | KeymapResult::Cancelled(_) => return Some(key_result),
718718 }
719719 None
720720 }
721721
722722 fn insert_mode(&mut self, cx: &mut commands::Context, event: KeyEvent) {
723723 if let Some(keyresult) = self.handle_keymap_event(Mode::Insert, cx, event) {
724− match keyresult.kind {
725− KeymapResultKind::NotFound => {
724+ match keyresult {
725+ KeymapResult::NotFound => {
726726 if let Some(ch) = event.char() {
727727 commands::insert::insert_char(cx, ch)
728728 }
729729 }
730− KeymapResultKind::Cancelled(pending) => {
730+ KeymapResult::Cancelled(pending) => {
731731 for ev in pending {
732732 match ev.char() {
733733 Some(ch) => commands::insert::insert_char(cx, ch),
734734 None => {
735− if let KeymapResultKind::Matched(command) =
736− self.keymaps.get(Mode::Insert, ev).kind
735+ if let KeymapResult::Matched(command) =
736+ self.keymaps.get(Mode::Insert, ev)
737737 {
738738 command.execute(cx);
739739 }
@@ -1182,8 +1182,8 @@ impl Component for EditorView {
11821182 // how we entered insert mode is important, and we should track that so
11831183 // we can repeat the side effect.
11841184
1185− self.last_insert.0 = match self.keymaps.get(mode, key).kind {
1186− KeymapResultKind::Matched(command) => command,
1185+ self.last_insert.0 = match self.keymaps.get(mode, key) {
1186+ KeymapResult::Matched(command) => command,
11871187 // FIXME: insert mode can only be entered through single KeyCodes
11881188 _ => unimplemented!(),
11891189 };