Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show pending keys in status line #515

Merged
merged 2 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions helix-term/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ pub struct Keymap {
/// Always a Node
#[serde(flatten)]
root: KeyTrie,
/// Stores pending keys waiting for the next key
#[serde(skip)]
state: Vec<KeyEvent>,
}
Expand All @@ -250,6 +251,14 @@ impl Keymap {
&self.root
}

/// Returns list of keys waiting to be disambiguated.
pub fn pending(&self) -> Option<&Vec<KeyEvent>> {
sudormrfbin marked this conversation as resolved.
Show resolved Hide resolved
match self.state.is_empty() {
true => None,
false => Some(&self.state),
}
}

/// Lookup `key` in the keymap to try and find a command to execute
pub fn get(&mut self, key: KeyEvent) -> KeymapResult {
let &first = self.state.get(0).unwrap_or(&key);
Expand Down Expand Up @@ -292,6 +301,13 @@ impl Default for Keymap {
#[serde(transparent)]
pub struct Keymaps(pub HashMap<Mode, Keymap>);

impl Keymaps {
/// Returns list of keys waiting to be disambiguated in current mode.
pub fn pending(&self) -> Option<&Vec<KeyEvent>> {
self.0.values().find_map(|keymap| keymap.pending())
}
}

impl Deref for Keymaps {
type Target = HashMap<Mode, Keymap>;

Expand Down
29 changes: 28 additions & 1 deletion helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use helix_core::{
coords_at_pos,
graphemes::{ensure_grapheme_boundary, next_grapheme_boundary},
syntax::{self, HighlightEvent},
unicode::width::UnicodeWidthStr,
LineEnding, Position, Range,
};
use helix_view::{
Expand Down Expand Up @@ -627,7 +628,7 @@ impl EditorView {
}
_ => {
// set the count
cxt.count = cxt.editor.count.take();
cxt.count = cxt.editor.count;
// TODO: edge case: 0j -> reset to 1
// if this fails, count was Some(0)
// debug_assert!(cxt.count != 0);
Expand All @@ -636,6 +637,9 @@ impl EditorView {
cxt.selected_register = cxt.editor.selected_register.take();

self.handle_keymap_event(mode, cxt, event);
if self.keymaps.pending().is_none() {
cxt.editor.count = None
}
}
}
}
Expand Down Expand Up @@ -810,6 +814,29 @@ impl Component for EditorView {
status_msg,
style,
);
} else {
pickfire marked this conversation as resolved.
Show resolved Hide resolved
let mut disp = String::new();
if let Some(count) = cx.editor.count {
disp.push_str(&count.get().to_string())
}
if let Some(keys) = self.keymaps.pending() {
for key in keys {
let s = key.to_string();
if s.width() > 1 {
pickfire marked this conversation as resolved.
Show resolved Hide resolved
disp.push_str(&format!("<{}>", s));
} else {
disp.push_str(&s);
}
}
}
let width = 10usize;
sudormrfbin marked this conversation as resolved.
Show resolved Hide resolved
surface.set_string(
area.x + area.width.saturating_sub(width as u16),
area.y + area.height.saturating_sub(1),
disp.get(disp.len().saturating_sub(width)..)
.unwrap_or(&disp),
pickfire marked this conversation as resolved.
Show resolved Hide resolved
cx.editor.theme.get("ui.text"),
);
}

if let Some(completion) = &self.completion {
Expand Down