Skip to content

Commit

Permalink
feat(term): support go to first/end keymap
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed Mar 21, 2024
1 parent a9712d5 commit 874cfbe
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions crates/synd_term/src/keymap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct Keymap {
id: KeymapId,
enable: bool,
trie: KeyTrie,
pending_keys: Vec<KeyEvent>,
}

impl Keymap {
Expand All @@ -32,13 +33,29 @@ impl Keymap {
id,
enable: false,
trie,
pending_keys: Vec::with_capacity(2),
}
}

fn search(&mut self, event: &KeyEvent) -> Option<Command> {
match self.trie.search(&[event]) {
Some(KeyTrie::Command(cmd)) => Some(cmd),
Some(KeyTrie::Node(_)) | None => None,
let first = self.pending_keys.first().unwrap_or(event);
let trie = match self.trie.search(&[*first]) {
Some(KeyTrie::Command(cmd)) => return Some(cmd),
Some(KeyTrie::Node(node)) => KeyTrie::Node(node),
None => return None,
};

self.pending_keys.push(*event);
match trie.search(&self.pending_keys[1..]) {
Some(KeyTrie::Command(cmd)) => {
self.pending_keys.drain(..);
Some(cmd)
}
Some(KeyTrie::Node(_)) => None,
_ => {
self.pending_keys.drain(..);
None
}
}
}
}
Expand Down Expand Up @@ -114,7 +131,7 @@ pub enum KeyTrie {
}

impl KeyTrie {
pub fn search(&self, keys: &[&KeyEvent]) -> Option<KeyTrie> {
pub fn search(&self, keys: &[KeyEvent]) -> Option<KeyTrie> {
let mut trie = self;
for key in keys {
trie = match trie {
Expand Down

0 comments on commit 874cfbe

Please sign in to comment.