keymap.rs

  1mod binding;
  2mod context;
  3
  4pub use binding::*;
  5pub use context::*;
  6
  7use crate::{Action, AsKeystroke, Keystroke, is_no_action};
  8use collections::{HashMap, HashSet};
  9use smallvec::SmallVec;
 10use std::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    no_action_binding_indices: Vec<usize>,
 23    version: KeymapVersion,
 24}
 25
 26/// Index of a binding within a keymap.
 27#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
 28pub struct BindingIndex(usize);
 29
 30impl Keymap {
 31    /// Create a new keymap with the given bindings.
 32    pub fn new(bindings: Vec<KeyBinding>) -> Self {
 33        let mut this = Self::default();
 34        this.add_bindings(bindings);
 35        this
 36    }
 37
 38    /// Get the current version of the keymap.
 39    pub fn version(&self) -> KeymapVersion {
 40        self.version
 41    }
 42
 43    /// Add more bindings to the keymap.
 44    pub fn add_bindings<T: IntoIterator<Item = KeyBinding>>(&mut self, bindings: T) {
 45        for binding in bindings {
 46            let action_id = binding.action().as_any().type_id();
 47            if is_no_action(&*binding.action) {
 48                self.no_action_binding_indices.push(self.bindings.len());
 49            } else {
 50                self.binding_indices_by_action_id
 51                    .entry(action_id)
 52                    .or_default()
 53                    .push(self.bindings.len());
 54            }
 55            self.bindings.push(binding);
 56        }
 57
 58        self.version.0 += 1;
 59    }
 60
 61    /// Reset this keymap to its initial state.
 62    pub fn clear(&mut self) {
 63        self.bindings.clear();
 64        self.binding_indices_by_action_id.clear();
 65        self.no_action_binding_indices.clear();
 66        self.version.0 += 1;
 67    }
 68
 69    /// Iterate over all bindings, in the order they were added.
 70    pub fn bindings(&self) -> impl DoubleEndedIterator<Item = &KeyBinding> + ExactSizeIterator {
 71        self.bindings.iter()
 72    }
 73
 74    /// Iterate over all bindings for the given action, in the order they were added. For display,
 75    /// the last binding should take precedence.
 76    pub fn bindings_for_action<'a>(
 77        &'a self,
 78        action: &'a dyn Action,
 79    ) -> impl 'a + DoubleEndedIterator<Item = &'a KeyBinding> {
 80        let action_id = action.type_id();
 81        let binding_indices = self
 82            .binding_indices_by_action_id
 83            .get(&action_id)
 84            .map_or(&[] as _, SmallVec::as_slice)
 85            .iter();
 86
 87        binding_indices.filter_map(|ix| {
 88            let binding = &self.bindings[*ix];
 89            if !binding.action().partial_eq(action) {
 90                return None;
 91            }
 92
 93            for null_ix in &self.no_action_binding_indices {
 94                if null_ix > ix {
 95                    let null_binding = &self.bindings[*null_ix];
 96                    if null_binding.keystrokes == binding.keystrokes {
 97                        let null_binding_matches =
 98                            match (&null_binding.context_predicate, &binding.context_predicate) {
 99                                (None, _) => true,
100                                (Some(_), None) => false,
101                                (Some(null_predicate), Some(predicate)) => {
102                                    null_predicate.is_superset(predicate)
103                                }
104                            };
105                        if null_binding_matches {
106                            return None;
107                        }
108                    }
109                }
110            }
111
112            Some(binding)
113        })
114    }
115
116    /// Returns all bindings that might match the input without checking context. The bindings
117    /// returned in precedence order (reverse of the order they were added to the keymap).
118    pub fn all_bindings_for_input(&self, input: &[Keystroke]) -> Vec<KeyBinding> {
119        self.bindings()
120            .rev()
121            .filter(|binding| {
122                binding
123                    .match_keystrokes(input)
124                    .is_some_and(|pending| !pending)
125            })
126            .cloned()
127            .collect()
128    }
129
130    /// Returns a list of bindings that match the given input, and a boolean indicating whether or
131    /// not more bindings might match if the input was longer. Bindings are returned in precedence
132    /// order (higher precedence first, reverse of the order they were added to the keymap).
133    ///
134    /// Precedence is defined by the depth in the tree (matches on the Editor take precedence over
135    /// matches on the Pane, then the Workspace, etc.). Bindings with no context are treated as the
136    /// same as the deepest context.
137    ///
138    /// In the case of multiple bindings at the same depth, the ones added to the keymap later take
139    /// precedence. User bindings are added after built-in bindings so that they take precedence.
140    ///
141    /// If a user has disabled a binding with `"x": null` it will not be returned. Disabled bindings
142    /// are evaluated with the same precedence rules so you can disable a rule in a given context
143    /// only.
144    pub fn bindings_for_input(
145        &self,
146        input: &[impl AsKeystroke],
147        context_stack: &[KeyContext],
148    ) -> (SmallVec<[KeyBinding; 1]>, bool) {
149        let mut matched_bindings = SmallVec::<[(usize, BindingIndex, &KeyBinding); 1]>::new();
150        let mut pending_bindings = SmallVec::<[(BindingIndex, &KeyBinding); 1]>::new();
151
152        for (ix, binding) in self.bindings().enumerate().rev() {
153            let Some(depth) = self.binding_enabled(binding, context_stack) else {
154                continue;
155            };
156            let Some(pending) = binding.match_keystrokes(input) else {
157                continue;
158            };
159
160            if !pending {
161                matched_bindings.push((depth, BindingIndex(ix), binding));
162            } else {
163                pending_bindings.push((BindingIndex(ix), binding));
164            }
165        }
166
167        matched_bindings.sort_by(|(depth_a, ix_a, _), (depth_b, ix_b, _)| {
168            depth_b.cmp(depth_a).then(ix_b.cmp(ix_a))
169        });
170
171        let mut bindings: SmallVec<[_; 1]> = SmallVec::new();
172        let mut first_binding_index = None;
173
174        for (_, ix, binding) in matched_bindings {
175            if is_no_action(&*binding.action) {
176                // Only break if this is a user-defined NoAction binding
177                // This allows user keymaps to override base keymap NoAction bindings
178                if let Some(meta) = binding.meta {
179                    if meta.0 == 0 {
180                        break;
181                    }
182                } else {
183                    // If no meta is set, assume it's a user binding for safety
184                    break;
185                }
186                // For non-user NoAction bindings, continue searching for user overrides
187                continue;
188            }
189            bindings.push(binding.clone());
190            first_binding_index.get_or_insert(ix);
191        }
192
193        let mut pending = HashSet::default();
194        for (ix, binding) in pending_bindings.into_iter().rev() {
195            if let Some(binding_ix) = first_binding_index
196                && binding_ix > ix
197            {
198                continue;
199            }
200            if is_no_action(&*binding.action) {
201                pending.remove(&&binding.keystrokes);
202                continue;
203            }
204            pending.insert(&binding.keystrokes);
205        }
206
207        (bindings, !pending.is_empty())
208    }
209    /// Check if the given binding is enabled, given a certain key context.
210    /// Returns the deepest depth at which the binding matches, or None if it doesn't match.
211    fn binding_enabled(&self, binding: &KeyBinding, contexts: &[KeyContext]) -> Option<usize> {
212        if let Some(predicate) = &binding.context_predicate {
213            predicate.depth_of(contexts)
214        } else {
215            Some(contexts.len())
216        }
217    }
218
219    /// Find the bindings that can follow the current input sequence.
220    pub fn possible_next_bindings_for_input(
221        &self,
222        input: &[Keystroke],
223        context_stack: &[KeyContext],
224    ) -> Vec<KeyBinding> {
225        let mut bindings = self
226            .bindings()
227            .enumerate()
228            .rev()
229            .filter_map(|(ix, binding)| {
230                let depth = self.binding_enabled(binding, context_stack)?;
231                let pending = binding.match_keystrokes(input);
232                match pending {
233                    None => None,
234                    Some(is_pending) => {
235                        if !is_pending || is_no_action(&*binding.action) {
236                            return None;
237                        }
238                        Some((depth, BindingIndex(ix), binding))
239                    }
240                }
241            })
242            .collect::<Vec<_>>();
243
244        bindings.sort_by(|(depth_a, ix_a, _), (depth_b, ix_b, _)| {
245            depth_b.cmp(depth_a).then(ix_b.cmp(ix_a))
246        });
247
248        bindings
249            .into_iter()
250            .map(|(_, _, binding)| binding.clone())
251            .collect::<Vec<_>>()
252    }
253}
254
255#[cfg(test)]
256mod tests {
257    use super::*;
258    use crate as gpui;
259    use gpui::NoAction;
260
261    actions!(
262        test_only,
263        [ActionAlpha, ActionBeta, ActionGamma, ActionDelta,]
264    );
265
266    #[test]
267    fn test_keymap() {
268        let bindings = [
269            KeyBinding::new("ctrl-a", ActionAlpha {}, None),
270            KeyBinding::new("ctrl-a", ActionBeta {}, Some("pane")),
271            KeyBinding::new("ctrl-a", ActionGamma {}, Some("editor && mode==full")),
272        ];
273
274        let mut keymap = Keymap::default();
275        keymap.add_bindings(bindings.clone());
276
277        // global bindings are enabled in all contexts
278        assert_eq!(keymap.binding_enabled(&bindings[0], &[]), Some(0));
279        assert_eq!(
280            keymap.binding_enabled(&bindings[0], &[KeyContext::parse("terminal").unwrap()]),
281            Some(1)
282        );
283
284        // contextual bindings are enabled in contexts that match their predicate
285        assert_eq!(
286            keymap.binding_enabled(&bindings[1], &[KeyContext::parse("barf x=y").unwrap()]),
287            None
288        );
289        assert_eq!(
290            keymap.binding_enabled(&bindings[1], &[KeyContext::parse("pane x=y").unwrap()]),
291            Some(1)
292        );
293
294        assert_eq!(
295            keymap.binding_enabled(&bindings[2], &[KeyContext::parse("editor").unwrap()]),
296            None
297        );
298        assert_eq!(
299            keymap.binding_enabled(
300                &bindings[2],
301                &[KeyContext::parse("editor mode=full").unwrap()]
302            ),
303            Some(1)
304        );
305    }
306
307    #[test]
308    fn test_depth_precedence() {
309        let bindings = [
310            KeyBinding::new("ctrl-a", ActionBeta {}, Some("pane")),
311            KeyBinding::new("ctrl-a", ActionGamma {}, Some("editor")),
312        ];
313
314        let mut keymap = Keymap::default();
315        keymap.add_bindings(bindings);
316
317        let (result, pending) = keymap.bindings_for_input(
318            &[Keystroke::parse("ctrl-a").unwrap()],
319            &[
320                KeyContext::parse("pane").unwrap(),
321                KeyContext::parse("editor").unwrap(),
322            ],
323        );
324
325        assert!(!pending);
326        assert_eq!(result.len(), 2);
327        assert!(result[0].action.partial_eq(&ActionGamma {}));
328        assert!(result[1].action.partial_eq(&ActionBeta {}));
329    }
330
331    #[test]
332    fn test_keymap_disabled() {
333        let bindings = [
334            KeyBinding::new("ctrl-a", ActionAlpha {}, Some("editor")),
335            KeyBinding::new("ctrl-b", ActionAlpha {}, Some("editor")),
336            KeyBinding::new("ctrl-a", NoAction {}, Some("editor && mode==full")),
337            KeyBinding::new("ctrl-b", NoAction {}, None),
338        ];
339
340        let mut keymap = Keymap::default();
341        keymap.add_bindings(bindings);
342
343        // binding is only enabled in a specific context
344        assert!(
345            keymap
346                .bindings_for_input(
347                    &[Keystroke::parse("ctrl-a").unwrap()],
348                    &[KeyContext::parse("barf").unwrap()],
349                )
350                .0
351                .is_empty()
352        );
353        assert!(
354            !keymap
355                .bindings_for_input(
356                    &[Keystroke::parse("ctrl-a").unwrap()],
357                    &[KeyContext::parse("editor").unwrap()],
358                )
359                .0
360                .is_empty()
361        );
362
363        // binding is disabled in a more specific context
364        assert!(
365            keymap
366                .bindings_for_input(
367                    &[Keystroke::parse("ctrl-a").unwrap()],
368                    &[KeyContext::parse("editor mode=full").unwrap()],
369                )
370                .0
371                .is_empty()
372        );
373
374        // binding is globally disabled
375        assert!(
376            keymap
377                .bindings_for_input(
378                    &[Keystroke::parse("ctrl-b").unwrap()],
379                    &[KeyContext::parse("barf").unwrap()],
380                )
381                .0
382                .is_empty()
383        );
384    }
385
386    #[test]
387    /// Tests for https://github.com/zed-industries/zed/issues/30259
388    fn test_multiple_keystroke_binding_disabled() {
389        let bindings = [
390            KeyBinding::new("space w w", ActionAlpha {}, Some("workspace")),
391            KeyBinding::new("space w w", NoAction {}, Some("editor")),
392        ];
393
394        let mut keymap = Keymap::default();
395        keymap.add_bindings(bindings);
396
397        let space = || Keystroke::parse("space").unwrap();
398        let w = || Keystroke::parse("w").unwrap();
399
400        let space_w = [space(), w()];
401        let space_w_w = [space(), w(), w()];
402
403        let workspace_context = || [KeyContext::parse("workspace").unwrap()];
404
405        let editor_workspace_context = || {
406            [
407                KeyContext::parse("workspace").unwrap(),
408                KeyContext::parse("editor").unwrap(),
409            ]
410        };
411
412        // Ensure `space` results in pending input on the workspace, but not editor
413        let space_workspace = keymap.bindings_for_input(&[space()], &workspace_context());
414        assert!(space_workspace.0.is_empty());
415        assert!(space_workspace.1);
416
417        let space_editor = keymap.bindings_for_input(&[space()], &editor_workspace_context());
418        assert!(space_editor.0.is_empty());
419        assert!(!space_editor.1);
420
421        // Ensure `space w` results in pending input on the workspace, but not editor
422        let space_w_workspace = keymap.bindings_for_input(&space_w, &workspace_context());
423        assert!(space_w_workspace.0.is_empty());
424        assert!(space_w_workspace.1);
425
426        let space_w_editor = keymap.bindings_for_input(&space_w, &editor_workspace_context());
427        assert!(space_w_editor.0.is_empty());
428        assert!(!space_w_editor.1);
429
430        // Ensure `space w w` results in the binding in the workspace, but not in the editor
431        let space_w_w_workspace = keymap.bindings_for_input(&space_w_w, &workspace_context());
432        assert!(!space_w_w_workspace.0.is_empty());
433        assert!(!space_w_w_workspace.1);
434
435        let space_w_w_editor = keymap.bindings_for_input(&space_w_w, &editor_workspace_context());
436        assert!(space_w_w_editor.0.is_empty());
437        assert!(!space_w_w_editor.1);
438
439        // Now test what happens if we have another binding defined AFTER the NoAction
440        // that should result in pending
441        let bindings = [
442            KeyBinding::new("space w w", ActionAlpha {}, Some("workspace")),
443            KeyBinding::new("space w w", NoAction {}, Some("editor")),
444            KeyBinding::new("space w x", ActionAlpha {}, Some("editor")),
445        ];
446        let mut keymap = Keymap::default();
447        keymap.add_bindings(bindings);
448
449        let space_editor = keymap.bindings_for_input(&[space()], &editor_workspace_context());
450        assert!(space_editor.0.is_empty());
451        assert!(space_editor.1);
452
453        // Now test what happens if we have another binding defined BEFORE the NoAction
454        // that should result in pending
455        let bindings = [
456            KeyBinding::new("space w w", ActionAlpha {}, Some("workspace")),
457            KeyBinding::new("space w x", ActionAlpha {}, Some("editor")),
458            KeyBinding::new("space w w", NoAction {}, Some("editor")),
459        ];
460        let mut keymap = Keymap::default();
461        keymap.add_bindings(bindings);
462
463        let space_editor = keymap.bindings_for_input(&[space()], &editor_workspace_context());
464        assert!(space_editor.0.is_empty());
465        assert!(space_editor.1);
466
467        // Now test what happens if we have another binding defined at a higher context
468        // that should result in pending
469        let bindings = [
470            KeyBinding::new("space w w", ActionAlpha {}, Some("workspace")),
471            KeyBinding::new("space w x", ActionAlpha {}, Some("workspace")),
472            KeyBinding::new("space w w", NoAction {}, Some("editor")),
473        ];
474        let mut keymap = Keymap::default();
475        keymap.add_bindings(bindings);
476
477        let space_editor = keymap.bindings_for_input(&[space()], &editor_workspace_context());
478        assert!(space_editor.0.is_empty());
479        assert!(space_editor.1);
480    }
481
482    #[test]
483    fn test_override_multikey() {
484        let bindings = [
485            KeyBinding::new("ctrl-w left", ActionAlpha {}, Some("editor")),
486            KeyBinding::new("ctrl-w", NoAction {}, Some("editor")),
487        ];
488
489        let mut keymap = Keymap::default();
490        keymap.add_bindings(bindings);
491
492        // Ensure `space` results in pending input on the workspace, but not editor
493        let (result, pending) = keymap.bindings_for_input(
494            &[Keystroke::parse("ctrl-w").unwrap()],
495            &[KeyContext::parse("editor").unwrap()],
496        );
497        assert!(result.is_empty());
498        assert!(pending);
499
500        let bindings = [
501            KeyBinding::new("ctrl-w left", ActionAlpha {}, Some("editor")),
502            KeyBinding::new("ctrl-w", ActionBeta {}, Some("editor")),
503        ];
504
505        let mut keymap = Keymap::default();
506        keymap.add_bindings(bindings);
507
508        // Ensure `space` results in pending input on the workspace, but not editor
509        let (result, pending) = keymap.bindings_for_input(
510            &[Keystroke::parse("ctrl-w").unwrap()],
511            &[KeyContext::parse("editor").unwrap()],
512        );
513        assert_eq!(result.len(), 1);
514        assert!(!pending);
515    }
516
517    #[test]
518    fn test_simple_disable() {
519        let bindings = [
520            KeyBinding::new("ctrl-x", ActionAlpha {}, Some("editor")),
521            KeyBinding::new("ctrl-x", NoAction {}, Some("editor")),
522        ];
523
524        let mut keymap = Keymap::default();
525        keymap.add_bindings(bindings);
526
527        // Ensure `space` results in pending input on the workspace, but not editor
528        let (result, pending) = keymap.bindings_for_input(
529            &[Keystroke::parse("ctrl-x").unwrap()],
530            &[KeyContext::parse("editor").unwrap()],
531        );
532        assert!(result.is_empty());
533        assert!(!pending);
534    }
535
536    #[test]
537    fn test_fail_to_disable() {
538        // disabled at the wrong level
539        let bindings = [
540            KeyBinding::new("ctrl-x", ActionAlpha {}, Some("editor")),
541            KeyBinding::new("ctrl-x", NoAction {}, Some("workspace")),
542        ];
543
544        let mut keymap = Keymap::default();
545        keymap.add_bindings(bindings);
546
547        // Ensure `space` results in pending input on the workspace, but not editor
548        let (result, pending) = keymap.bindings_for_input(
549            &[Keystroke::parse("ctrl-x").unwrap()],
550            &[
551                KeyContext::parse("workspace").unwrap(),
552                KeyContext::parse("editor").unwrap(),
553            ],
554        );
555        assert_eq!(result.len(), 1);
556        assert!(!pending);
557    }
558
559    #[test]
560    fn test_disable_deeper() {
561        let bindings = [
562            KeyBinding::new("ctrl-x", ActionAlpha {}, Some("workspace")),
563            KeyBinding::new("ctrl-x", NoAction {}, Some("editor")),
564        ];
565
566        let mut keymap = Keymap::default();
567        keymap.add_bindings(bindings);
568
569        // Ensure `space` results in pending input on the workspace, but not editor
570        let (result, pending) = keymap.bindings_for_input(
571            &[Keystroke::parse("ctrl-x").unwrap()],
572            &[
573                KeyContext::parse("workspace").unwrap(),
574                KeyContext::parse("editor").unwrap(),
575            ],
576        );
577        assert_eq!(result.len(), 0);
578        assert!(!pending);
579    }
580
581    #[test]
582    fn test_pending_match_enabled() {
583        let bindings = [
584            KeyBinding::new("ctrl-x", ActionBeta, Some("vim_mode == normal")),
585            KeyBinding::new("ctrl-x 0", ActionAlpha, Some("Workspace")),
586        ];
587        let mut keymap = Keymap::default();
588        keymap.add_bindings(bindings);
589
590        let matched = keymap.bindings_for_input(
591            &[Keystroke::parse("ctrl-x")].map(Result::unwrap),
592            &[
593                KeyContext::parse("Workspace"),
594                KeyContext::parse("Pane"),
595                KeyContext::parse("Editor vim_mode=normal"),
596            ]
597            .map(Result::unwrap),
598        );
599        assert_eq!(matched.0.len(), 1);
600        assert!(matched.0[0].action.partial_eq(&ActionBeta));
601        assert!(matched.1);
602    }
603
604    #[test]
605    fn test_pending_match_enabled_extended() {
606        let bindings = [
607            KeyBinding::new("ctrl-x", ActionBeta, Some("vim_mode == normal")),
608            KeyBinding::new("ctrl-x 0", NoAction, Some("Workspace")),
609        ];
610        let mut keymap = Keymap::default();
611        keymap.add_bindings(bindings);
612
613        let matched = keymap.bindings_for_input(
614            &[Keystroke::parse("ctrl-x")].map(Result::unwrap),
615            &[
616                KeyContext::parse("Workspace"),
617                KeyContext::parse("Pane"),
618                KeyContext::parse("Editor vim_mode=normal"),
619            ]
620            .map(Result::unwrap),
621        );
622        assert_eq!(matched.0.len(), 1);
623        assert!(matched.0[0].action.partial_eq(&ActionBeta));
624        assert!(!matched.1);
625        let bindings = [
626            KeyBinding::new("ctrl-x", ActionBeta, Some("Workspace")),
627            KeyBinding::new("ctrl-x 0", NoAction, Some("vim_mode == normal")),
628        ];
629        let mut keymap = Keymap::default();
630        keymap.add_bindings(bindings);
631
632        let matched = keymap.bindings_for_input(
633            &[Keystroke::parse("ctrl-x")].map(Result::unwrap),
634            &[
635                KeyContext::parse("Workspace"),
636                KeyContext::parse("Pane"),
637                KeyContext::parse("Editor vim_mode=normal"),
638            ]
639            .map(Result::unwrap),
640        );
641        assert_eq!(matched.0.len(), 1);
642        assert!(matched.0[0].action.partial_eq(&ActionBeta));
643        assert!(!matched.1);
644    }
645
646    #[test]
647    fn test_overriding_prefix() {
648        let bindings = [
649            KeyBinding::new("ctrl-x 0", ActionAlpha, Some("Workspace")),
650            KeyBinding::new("ctrl-x", ActionBeta, Some("vim_mode == normal")),
651        ];
652        let mut keymap = Keymap::default();
653        keymap.add_bindings(bindings);
654
655        let matched = keymap.bindings_for_input(
656            &[Keystroke::parse("ctrl-x")].map(Result::unwrap),
657            &[
658                KeyContext::parse("Workspace"),
659                KeyContext::parse("Pane"),
660                KeyContext::parse("Editor vim_mode=normal"),
661            ]
662            .map(Result::unwrap),
663        );
664        assert_eq!(matched.0.len(), 1);
665        assert!(matched.0[0].action.partial_eq(&ActionBeta));
666        assert!(!matched.1);
667    }
668
669    #[test]
670    fn test_context_precedence_with_same_source() {
671        // Test case: User has both Workspace and Editor bindings for the same key
672        // Editor binding should take precedence over Workspace binding
673        let bindings = [
674            KeyBinding::new("cmd-r", ActionAlpha {}, Some("Workspace")),
675            KeyBinding::new("cmd-r", ActionBeta {}, Some("Editor")),
676        ];
677
678        let mut keymap = Keymap::default();
679        keymap.add_bindings(bindings);
680
681        // Test with context stack: [Workspace, Editor] (Editor is deeper)
682        let (result, _) = keymap.bindings_for_input(
683            &[Keystroke::parse("cmd-r").unwrap()],
684            &[
685                KeyContext::parse("Workspace").unwrap(),
686                KeyContext::parse("Editor").unwrap(),
687            ],
688        );
689
690        // Both bindings should be returned, but Editor binding should be first (highest precedence)
691        assert_eq!(result.len(), 2);
692        assert!(result[0].action.partial_eq(&ActionBeta {})); // Editor binding first
693        assert!(result[1].action.partial_eq(&ActionAlpha {})); // Workspace binding second
694    }
695
696    #[test]
697    fn test_bindings_for_action() {
698        let bindings = [
699            KeyBinding::new("ctrl-a", ActionAlpha {}, Some("pane")),
700            KeyBinding::new("ctrl-b", ActionBeta {}, Some("editor && mode == full")),
701            KeyBinding::new("ctrl-c", ActionGamma {}, Some("workspace")),
702            KeyBinding::new("ctrl-a", NoAction {}, Some("pane && active")),
703            KeyBinding::new("ctrl-b", NoAction {}, Some("editor")),
704        ];
705
706        let mut keymap = Keymap::default();
707        keymap.add_bindings(bindings);
708
709        assert_bindings(&keymap, &ActionAlpha {}, &["ctrl-a"]);
710        assert_bindings(&keymap, &ActionBeta {}, &[]);
711        assert_bindings(&keymap, &ActionGamma {}, &["ctrl-c"]);
712
713        #[track_caller]
714        fn assert_bindings(keymap: &Keymap, action: &dyn Action, expected: &[&str]) {
715            let actual = keymap
716                .bindings_for_action(action)
717                .map(|binding| binding.keystrokes[0].inner().unparse())
718                .collect::<Vec<_>>();
719            assert_eq!(actual, expected, "{:?}", action);
720        }
721    }
722
723    #[test]
724    fn test_source_precedence_sorting() {
725        // KeybindSource precedence: User (0) > Vim (1) > Base (2) > Default (3)
726        // Test that user keymaps take precedence over default keymaps at the same context depth
727        let mut keymap = Keymap::default();
728
729        // Add a default keymap binding first
730        let mut default_binding = KeyBinding::new("cmd-r", ActionAlpha {}, Some("Editor"));
731        default_binding.set_meta(KeyBindingMetaIndex(3)); // Default source
732        keymap.add_bindings([default_binding]);
733
734        // Add a user keymap binding
735        let mut user_binding = KeyBinding::new("cmd-r", ActionBeta {}, Some("Editor"));
736        user_binding.set_meta(KeyBindingMetaIndex(0)); // User source
737        keymap.add_bindings([user_binding]);
738
739        // Test with Editor context stack
740        let (result, _) = keymap.bindings_for_input(
741            &[Keystroke::parse("cmd-r").unwrap()],
742            &[KeyContext::parse("Editor").unwrap()],
743        );
744
745        // User binding should take precedence over default binding
746        assert_eq!(result.len(), 2);
747        assert!(result[0].action.partial_eq(&ActionBeta {}));
748        assert!(result[1].action.partial_eq(&ActionAlpha {}));
749    }
750}