Skip to content

Commit

Permalink
Allow numbers as second input event (helix-editor#8471)
Browse files Browse the repository at this point in the history
* Make sure pending key list is empty when count handling

This will allow using numbers as second key event.

* count handling; add an exception for 'g'

* Lookup the key event before considering a number as count

* Avoid the allocation of another vec for the pending keys

---------

Co-authored-by: x <x@torrent>
  • Loading branch information
2 people authored and Schuyler Mortimer committed Jul 10, 2024
1 parent a8641b8 commit 5a7e514
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 9 additions & 0 deletions helix-term/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,15 @@ impl Keymaps {
self.sticky.as_ref()
}

pub fn contains_key(&self, mode: Mode, key: KeyEvent) -> bool {
let keymaps = &*self.map();
let keymap = &keymaps[&mode];
keymap
.search(self.pending())
.and_then(KeyTrie::node)
.is_some_and(|node| node.contains_key(&key))
}

/// Lookup `key` in the keymap to try and find a command to execute. Escape
/// key cancels pending keystrokes. If there are no pending keystrokes but a
/// sticky node is in use, it will be cleared.
Expand Down
4 changes: 3 additions & 1 deletion helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,9 @@ impl EditorView {
fn command_mode(&mut self, mode: Mode, cxt: &mut commands::Context, event: KeyEvent) {
match (event, cxt.editor.count) {
// count handling
(key!(i @ '0'), Some(_)) | (key!(i @ '1'..='9'), _) => {
(key!(i @ '0'), Some(_)) | (key!(i @ '1'..='9'), _)
if !self.keymaps.contains_key(mode, event) =>
{
let i = i.to_digit(10).unwrap() as usize;
cxt.editor.count =
std::num::NonZeroUsize::new(cxt.editor.count.map_or(i, |c| c.get() * 10 + i));
Expand Down

0 comments on commit 5a7e514

Please sign in to comment.