From 327932ba3b39f832d529cd274fd81e376d7c6810 Mon Sep 17 00:00:00 2001 From: Kay Simmons Date: Fri, 10 Feb 2023 16:09:15 -0800 Subject: [PATCH] Remove catch all keymap and KeyPressed action --- crates/command_palette/src/command_palette.rs | 2 +- crates/gpui/src/keymap_matcher.rs | 19 ++-------- crates/gpui/src/keymap_matcher/binding.rs | 36 ++++++------------- crates/gpui/src/platform/mac/platform.rs | 2 +- 4 files changed, 14 insertions(+), 45 deletions(-) diff --git a/crates/command_palette/src/command_palette.rs b/crates/command_palette/src/command_palette.rs index d4625cbce0525e845ff2b85def695bcb707a029f..5b5d8f1162f352166ca2e9f733dabd2d3b129906 100644 --- a/crates/command_palette/src/command_palette.rs +++ b/crates/command_palette/src/command_palette.rs @@ -65,7 +65,7 @@ impl CommandPalette { action, keystrokes: bindings .iter() - .filter_map(|binding| binding.keystrokes()) + .map(|binding| binding.keystrokes()) .last() .map_or(Vec::new(), |keystrokes| keystrokes.to_vec()), }) diff --git a/crates/gpui/src/keymap_matcher.rs b/crates/gpui/src/keymap_matcher.rs index c7de0352328d287b1248b80699c06df7fd07ae0e..edcc458658eeff81358171ecbdf9a6cf324056e4 100644 --- a/crates/gpui/src/keymap_matcher.rs +++ b/crates/gpui/src/keymap_matcher.rs @@ -6,24 +6,15 @@ mod keystroke; use std::{any::TypeId, fmt::Debug}; use collections::HashMap; -use serde::Deserialize; use smallvec::SmallVec; -use crate::{impl_actions, Action}; +use crate::Action; pub use binding::{Binding, BindingMatchResult}; pub use keymap::Keymap; pub use keymap_context::{KeymapContext, KeymapContextPredicate}; pub use keystroke::Keystroke; -#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize)] -pub struct KeyPressed { - #[serde(default)] - pub keystroke: Keystroke, -} - -impl_actions!(gpui, [KeyPressed]); - pub struct KeymapMatcher { pub contexts: Vec, pending_views: HashMap, @@ -102,13 +93,7 @@ impl KeymapMatcher { for binding in self.keymap.bindings().iter().rev() { match binding.match_keys_and_context(&self.pending_keystrokes, &self.contexts[i..]) { - BindingMatchResult::Complete(mut action) => { - // Swap in keystroke for special KeyPressed action - if action.name() == "KeyPressed" && action.namespace() == "gpui" { - action = Box::new(KeyPressed { - keystroke: keystroke.clone(), - }); - } + BindingMatchResult::Complete(action) => { matched_bindings.push((view_id, action)) } BindingMatchResult::Partial => { diff --git a/crates/gpui/src/keymap_matcher/binding.rs b/crates/gpui/src/keymap_matcher/binding.rs index 81464378848e4ee25bdd4f3150391757035d6d98..c1cfd14e82d8d3e3235a7a2c3e7d507cd78d9648 100644 --- a/crates/gpui/src/keymap_matcher/binding.rs +++ b/crates/gpui/src/keymap_matcher/binding.rs @@ -7,7 +7,7 @@ use super::{KeymapContext, KeymapContextPredicate, Keystroke}; pub struct Binding { action: Box, - keystrokes: Option>, + keystrokes: SmallVec<[Keystroke; 2]>, context_predicate: Option, } @@ -23,16 +23,10 @@ impl Binding { None }; - let keystrokes = if keystrokes == "*" { - None // Catch all context - } else { - Some( - keystrokes - .split_whitespace() - .map(Keystroke::parse) - .collect::>()?, - ) - }; + let keystrokes = keystrokes + .split_whitespace() + .map(Keystroke::parse) + .collect::>()?; Ok(Self { keystrokes, @@ -53,20 +47,10 @@ impl Binding { pending_keystrokes: &Vec, contexts: &[KeymapContext], ) -> BindingMatchResult { - if self - .keystrokes - .as_ref() - .map(|keystrokes| keystrokes.starts_with(&pending_keystrokes)) - .unwrap_or(true) - && self.match_context(contexts) + if self.keystrokes.as_ref().starts_with(&pending_keystrokes) && self.match_context(contexts) { // If the binding is completed, push it onto the matches list - if self - .keystrokes - .as_ref() - .map(|keystrokes| keystrokes.len() == pending_keystrokes.len()) - .unwrap_or(true) - { + if self.keystrokes.as_ref().len() == pending_keystrokes.len() { BindingMatchResult::Complete(self.action.boxed_clone()) } else { BindingMatchResult::Partial @@ -82,14 +66,14 @@ impl Binding { contexts: &[KeymapContext], ) -> Option> { if self.action.eq(action) && self.match_context(contexts) { - self.keystrokes.clone() + Some(self.keystrokes.clone()) } else { None } } - pub fn keystrokes(&self) -> Option<&[Keystroke]> { - self.keystrokes.as_deref() + pub fn keystrokes(&self) -> &[Keystroke] { + self.keystrokes.as_slice() } pub fn action(&self) -> &dyn Action { diff --git a/crates/gpui/src/platform/mac/platform.rs b/crates/gpui/src/platform/mac/platform.rs index 5d132275854c8b92e317b8226cf4d869dd92f422..3d57a7fe2ae458248179d16d3ecbdddfc784fa5b 100644 --- a/crates/gpui/src/platform/mac/platform.rs +++ b/crates/gpui/src/platform/mac/platform.rs @@ -184,7 +184,7 @@ impl MacForegroundPlatform { .map(|binding| binding.keystrokes()); let item; - if let Some(keystrokes) = keystrokes.flatten() { + if let Some(keystrokes) = keystrokes { if keystrokes.len() == 1 { let keystroke = &keystrokes[0]; let mut mask = NSEventModifierFlags::empty();