Skip to main content

boltz_gpui/
keymap.rs

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