Simplify key dispatch code. Previously we would maintain a cache of key matchers for each context that would store the pending input. For the last while we've also stored the typed prefix on the window. This is redundant, we only need one copy, so now it's just stored on the window, which lets us avoid the boilerplate of keeping all the matchers in sync. This stops us from losing multikey bindings when the context on a node changes (#11009) (though we still interrupt multikey bindings if the focus changes). While in the code, I fixed up a few other things with multi-key bindings that were causing problems: Previously we assumed that all multi-key bindings took precedence over any single-key binding, now this is done such that if a user binds a single-key binding, it will take precedence over all system-defined multi-key bindings (irrespective of the depth in the context tree). This was a common cause of confusion for new users trying to bind to `cmd-k` or `ctrl-w` in vim mode (#13543). Previously after a pending multi-key keystroke failed to match, we would drop the prefix if it was an input event. Now we correctly replay it (#14725). Release Notes: - Fixed multi-key shortcuts not working across completion menu changes ([#11009](https://github.com/zed-industries/zed/issues/11009)) - Fixed multi-key shortcuts discarding earlier input ([#14445](https://github.com/zed-industries/zed/pull/14445)) - vim: Fixed `jk` binding preventing you from repeating `j` ([#14725](https://github.com/zed-industries/zed/issues/14725)) - vim: Fixed `escape` in normal mode to also clear the selected register. - Fixed key maps so user-defined mappings take precedence over builtin multi-key mappings ([#13543](https://github.com/zed-industries/zed/issues/13543)) - Fixed a bug where overridden shortcuts would still show in the Command Palette
83 lines
2.5 KiB
Rust
83 lines
2.5 KiB
Rust
use crate::{Action, KeyBindingContextPredicate, Keystroke};
|
|
use anyhow::Result;
|
|
use smallvec::SmallVec;
|
|
|
|
/// A keybinding and its associated metadata, from the keymap.
|
|
pub struct KeyBinding {
|
|
pub(crate) action: Box<dyn Action>,
|
|
pub(crate) keystrokes: SmallVec<[Keystroke; 2]>,
|
|
pub(crate) context_predicate: Option<KeyBindingContextPredicate>,
|
|
}
|
|
|
|
impl Clone for KeyBinding {
|
|
fn clone(&self) -> Self {
|
|
KeyBinding {
|
|
action: self.action.boxed_clone(),
|
|
keystrokes: self.keystrokes.clone(),
|
|
context_predicate: self.context_predicate.clone(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl KeyBinding {
|
|
/// Construct a new keybinding from the given data.
|
|
pub fn new<A: Action>(keystrokes: &str, action: A, context_predicate: Option<&str>) -> Self {
|
|
Self::load(keystrokes, Box::new(action), context_predicate).unwrap()
|
|
}
|
|
|
|
/// Load a keybinding from the given raw data.
|
|
pub fn load(keystrokes: &str, action: Box<dyn Action>, context: Option<&str>) -> Result<Self> {
|
|
let context = if let Some(context) = context {
|
|
Some(KeyBindingContextPredicate::parse(context)?)
|
|
} else {
|
|
None
|
|
};
|
|
|
|
let keystrokes = keystrokes
|
|
.split_whitespace()
|
|
.map(Keystroke::parse)
|
|
.collect::<Result<_>>()?;
|
|
|
|
Ok(Self {
|
|
keystrokes,
|
|
action,
|
|
context_predicate: context,
|
|
})
|
|
}
|
|
|
|
/// Check if the given keystrokes match this binding.
|
|
pub fn match_keystrokes(&self, typed: &[Keystroke]) -> Option<bool> {
|
|
if self.keystrokes.len() < typed.len() {
|
|
return None;
|
|
}
|
|
|
|
for (target, typed) in self.keystrokes.iter().zip(typed.iter()) {
|
|
if !typed.should_match(target) {
|
|
return None;
|
|
}
|
|
}
|
|
|
|
return Some(self.keystrokes.len() > typed.len());
|
|
}
|
|
|
|
/// Get the keystrokes associated with this binding
|
|
pub fn keystrokes(&self) -> &[Keystroke] {
|
|
self.keystrokes.as_slice()
|
|
}
|
|
|
|
/// Get the action associated with this binding
|
|
pub fn action(&self) -> &dyn Action {
|
|
self.action.as_ref()
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Debug for KeyBinding {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
f.debug_struct("KeyBinding")
|
|
.field("keystrokes", &self.keystrokes)
|
|
.field("context_predicate", &self.context_predicate)
|
|
.field("action", &self.action.name())
|
|
.finish()
|
|
}
|
|
}
|