1mod binding;
2mod context;
3
4pub use binding::*;
5pub use context::*;
6
7use crate::{Action, Keystroke, NoAction};
8use collections::HashMap;
9use smallvec::SmallVec;
10use std::any::{Any, TypeId};
11
12/// An opaque identifier of which version of the keymap is currently active.
13/// The keymap's version is changed whenever bindings are added or removed.
14#[derive(Copy, Clone, Eq, PartialEq, Default)]
15pub struct KeymapVersion(usize);
16
17/// A collection of key bindings for the user's application.
18#[derive(Default)]
19pub struct Keymap {
20 bindings: Vec<KeyBinding>,
21 binding_indices_by_action_id: HashMap<TypeId, SmallVec<[usize; 3]>>,
22 version: KeymapVersion,
23}
24
25impl Keymap {
26 /// Create a new keymap with the given bindings.
27 pub fn new(bindings: Vec<KeyBinding>) -> Self {
28 let mut this = Self::default();
29 this.add_bindings(bindings);
30 this
31 }
32
33 /// Get the current version of the keymap.
34 pub fn version(&self) -> KeymapVersion {
35 self.version
36 }
37
38 /// Add more bindings to the keymap.
39 pub fn add_bindings<T: IntoIterator<Item = KeyBinding>>(&mut self, bindings: T) {
40 for binding in bindings {
41 let action_id = binding.action().as_any().type_id();
42 self.binding_indices_by_action_id
43 .entry(action_id)
44 .or_default()
45 .push(self.bindings.len());
46 self.bindings.push(binding);
47 }
48
49 self.version.0 += 1;
50 }
51
52 /// Reset this keymap to its initial state.
53 pub fn clear(&mut self) {
54 self.bindings.clear();
55 self.binding_indices_by_action_id.clear();
56 self.version.0 += 1;
57 }
58
59 /// Iterate over all bindings, in the order they were added.
60 pub fn bindings(&self) -> impl DoubleEndedIterator<Item = &KeyBinding> {
61 self.bindings.iter()
62 }
63
64 /// Iterate over all bindings for the given action, in the order they were added.
65 pub fn bindings_for_action<'a>(
66 &'a self,
67 action: &'a dyn Action,
68 ) -> impl 'a + DoubleEndedIterator<Item = &'a KeyBinding> {
69 let action_id = action.type_id();
70 self.binding_indices_by_action_id
71 .get(&action_id)
72 .map_or(&[] as _, SmallVec::as_slice)
73 .iter()
74 .map(|ix| &self.bindings[*ix])
75 .filter(move |binding| binding.action().partial_eq(action))
76 }
77
78 /// all bindings for input returns all bindings that might match the input
79 /// (without checking context)
80 pub fn all_bindings_for_input(&self, input: &[Keystroke]) -> Vec<KeyBinding> {
81 self.bindings()
82 .rev()
83 .filter_map(|binding| {
84 binding.match_keystrokes(input).filter(|pending| !pending)?;
85 Some(binding.clone())
86 })
87 .collect()
88 }
89
90 /// bindings_for_input returns a list of bindings that match the given input,
91 /// and a boolean indicating whether or not more bindings might match if
92 /// the input was longer.
93 ///
94 /// Precedence is defined by the depth in the tree (matches on the Editor take
95 /// precedence over matches on the Pane, then the Workspace, etc.). Bindings with
96 /// no context are treated as the same as the deepest context.
97 ///
98 /// In the case of multiple bindings at the same depth, the ones defined later in the
99 /// keymap take precedence (so user bindings take precedence over built-in bindings).
100 ///
101 /// If a user has disabled a binding with `"x": null` it will not be returned. Disabled
102 /// bindings are evaluated with the same precedence rules so you can disable a rule in
103 /// a given context only.
104 ///
105 /// In the case of multi-key bindings, the
106 pub fn bindings_for_input(
107 &self,
108 input: &[Keystroke],
109 context_stack: &[KeyContext],
110 ) -> (SmallVec<[KeyBinding; 1]>, bool) {
111 let possibilities = self.bindings().rev().filter_map(|binding| {
112 binding
113 .match_keystrokes(input)
114 .map(|pending| (binding, pending))
115 });
116
117 let mut bindings: SmallVec<[(KeyBinding, usize); 1]> = SmallVec::new();
118 let mut is_pending = None;
119
120 'outer: for (binding, pending) in possibilities {
121 for depth in (0..=context_stack.len()).rev() {
122 if self.binding_enabled(binding, &context_stack[0..depth]) {
123 if is_pending.is_none() {
124 is_pending = Some(pending);
125 }
126 if !pending {
127 bindings.push((binding.clone(), depth));
128 continue 'outer;
129 }
130 }
131 }
132 }
133 bindings.sort_by(|a, b| a.1.cmp(&b.1).reverse());
134 let bindings = bindings
135 .into_iter()
136 .map_while(|(binding, _)| {
137 if binding.action.as_any().type_id() == (NoAction {}).type_id() {
138 None
139 } else {
140 Some(binding)
141 }
142 })
143 .collect();
144
145 (bindings, is_pending.unwrap_or_default())
146 }
147
148 /// Check if the given binding is enabled, given a certain key context.
149 fn binding_enabled(&self, binding: &KeyBinding, context: &[KeyContext]) -> bool {
150 // If binding has a context predicate, it must match the current context,
151 if let Some(predicate) = &binding.context_predicate {
152 if !predicate.eval(context) {
153 return false;
154 }
155 }
156
157 true
158 }
159}
160
161#[cfg(test)]
162mod tests {
163 use super::*;
164 use crate as gpui;
165 use gpui::actions;
166
167 actions!(
168 keymap_test,
169 [ActionAlpha, ActionBeta, ActionGamma, ActionDelta,]
170 );
171
172 #[test]
173 fn test_keymap() {
174 let bindings = [
175 KeyBinding::new("ctrl-a", ActionAlpha {}, None),
176 KeyBinding::new("ctrl-a", ActionBeta {}, Some("pane")),
177 KeyBinding::new("ctrl-a", ActionGamma {}, Some("editor && mode==full")),
178 ];
179
180 let mut keymap = Keymap::default();
181 keymap.add_bindings(bindings.clone());
182
183 // global bindings are enabled in all contexts
184 assert!(keymap.binding_enabled(&bindings[0], &[]));
185 assert!(keymap.binding_enabled(&bindings[0], &[KeyContext::parse("terminal").unwrap()]));
186
187 // contextual bindings are enabled in contexts that match their predicate
188 assert!(!keymap.binding_enabled(&bindings[1], &[KeyContext::parse("barf x=y").unwrap()]));
189 assert!(keymap.binding_enabled(&bindings[1], &[KeyContext::parse("pane x=y").unwrap()]));
190
191 assert!(!keymap.binding_enabled(&bindings[2], &[KeyContext::parse("editor").unwrap()]));
192 assert!(keymap.binding_enabled(
193 &bindings[2],
194 &[KeyContext::parse("editor mode=full").unwrap()]
195 ));
196 }
197
198 #[test]
199 fn test_keymap_disabled() {
200 let bindings = [
201 KeyBinding::new("ctrl-a", ActionAlpha {}, Some("editor")),
202 KeyBinding::new("ctrl-b", ActionAlpha {}, Some("editor")),
203 KeyBinding::new("ctrl-a", NoAction {}, Some("editor && mode==full")),
204 KeyBinding::new("ctrl-b", NoAction {}, None),
205 ];
206
207 let mut keymap = Keymap::default();
208 keymap.add_bindings(bindings.clone());
209
210 // binding is only enabled in a specific context
211 assert!(keymap
212 .bindings_for_input(
213 &[Keystroke::parse("ctrl-a").unwrap()],
214 &[KeyContext::parse("barf").unwrap()],
215 )
216 .0
217 .is_empty());
218 assert!(!keymap
219 .bindings_for_input(
220 &[Keystroke::parse("ctrl-a").unwrap()],
221 &[KeyContext::parse("editor").unwrap()],
222 )
223 .0
224 .is_empty());
225
226 // binding is disabled in a more specific context
227 assert!(keymap
228 .bindings_for_input(
229 &[Keystroke::parse("ctrl-a").unwrap()],
230 &[KeyContext::parse("editor mode=full").unwrap()],
231 )
232 .0
233 .is_empty());
234
235 // binding is globally disabled
236 assert!(keymap
237 .bindings_for_input(
238 &[Keystroke::parse("ctrl-b").unwrap()],
239 &[KeyContext::parse("barf").unwrap()],
240 )
241 .0
242 .is_empty());
243 }
244}