Skip to main content

open_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 open_gpui_collections::{HashSet, TypeIdHashMap};
9use smallvec::SmallVec;
10
11/// An opaque identifier of which version of the keymap is currently active.
12/// The keymap's version is changed whenever bindings are added or removed.
13#[derive(Copy, Clone, Eq, PartialEq, Default)]
14pub struct KeymapVersion(usize);
15
16/// A collection of key bindings for the user's application.
17#[derive(Default)]
18pub struct Keymap {
19    bindings: Vec<KeyBinding>,
20    binding_indices_by_action_id: TypeIdHashMap<SmallVec<[usize; 3]>>,
21    disabled_binding_indices: Vec<usize>,
22    version: KeymapVersion,
23}
24
25/// Index of a binding within a keymap.
26#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
27pub struct BindingIndex(usize);
28
29fn disabled_binding_matches_context(disabled_binding: &KeyBinding, binding: &KeyBinding) -> bool {
30    match (
31        &disabled_binding.context_predicate,
32        &binding.context_predicate,
33    ) {
34        (None, _) => true,
35        (Some(_), None) => false,
36        (Some(disabled_predicate), Some(predicate)) => disabled_predicate.is_superset(predicate),
37    }
38}
39
40fn binding_is_unbound(disabled_binding: &KeyBinding, binding: &KeyBinding) -> bool {
41    disabled_binding.keystrokes == binding.keystrokes
42        && disabled_binding
43            .action()
44            .as_any()
45            .downcast_ref::<Unbind>()
46            .is_some_and(|unbind| unbind.0.as_ref() == binding.action.name())
47}
48
49impl Keymap {
50    /// Create a new keymap with the given bindings.
51    pub fn new(bindings: Vec<KeyBinding>) -> Self {
52        let mut this = Self::default();
53        this.add_bindings(bindings);
54        this
55    }
56
57    /// Get the current version of the keymap.
58    pub fn version(&self) -> KeymapVersion {
59        self.version
60    }
61
62    /// Add more bindings to the keymap.
63    pub fn add_bindings<T: IntoIterator<Item = KeyBinding>>(&mut self, bindings: T) {
64        for binding in bindings {
65            let action_id = binding.action().as_any().type_id();
66            if is_no_action(&*binding.action) || is_unbind(&*binding.action) {
67                self.disabled_binding_indices.push(self.bindings.len());
68            } else {
69                self.binding_indices_by_action_id
70                    .entry(action_id)
71                    .or_default()
72                    .push(self.bindings.len());
73            }
74            self.bindings.push(binding);
75        }
76
77        self.version.0 += 1;
78    }
79
80    /// Reset this keymap to its initial state.
81    pub fn clear(&mut self) {
82        self.bindings.clear();
83        self.binding_indices_by_action_id.clear();
84        self.disabled_binding_indices.clear();
85        self.version.0 += 1;
86    }
87
88    /// Iterate over all bindings, in the order they were added.
89    pub fn bindings(&self) -> impl DoubleEndedIterator<Item = &KeyBinding> + ExactSizeIterator {
90        self.bindings.iter()
91    }
92
93    /// Iterate over all bindings for the given action, in the order they were added. For display,
94    /// the last binding should take precedence.
95    pub fn bindings_for_action<'a>(
96        &'a self,
97        action: &'a dyn Action,
98    ) -> impl 'a + DoubleEndedIterator<Item = &'a KeyBinding> {
99        let action_id = action.type_id();
100        let binding_indices = self
101            .binding_indices_by_action_id
102            .get(&action_id)
103            .map_or(&[] as _, SmallVec::as_slice)
104            .iter();
105
106        binding_indices.filter_map(|ix| {
107            let binding = &self.bindings[*ix];
108            if !binding.action().partial_eq(action) {
109                return None;
110            }
111
112            for disabled_ix in &self.disabled_binding_indices {
113                if disabled_ix > ix {
114                    let disabled_binding = &self.bindings[*disabled_ix];
115                    if disabled_binding.keystrokes != binding.keystrokes {
116                        continue;
117                    }
118
119                    if is_no_action(&*disabled_binding.action) {
120                        if disabled_binding_matches_context(disabled_binding, binding) {
121                            return None;
122                        }
123                    } else if is_unbind(&*disabled_binding.action)
124                        && disabled_binding_matches_context(disabled_binding, binding)
125                        && binding_is_unbound(disabled_binding, binding)
126                    {
127                        return None;
128                    }
129                }
130            }
131
132            Some(binding)
133        })
134    }
135
136    /// Returns all bindings that might match the input without checking context. The bindings
137    /// returned in precedence order (reverse of the order they were added to the keymap).
138    pub fn all_bindings_for_input(&self, input: &[Keystroke]) -> Vec<KeyBinding> {
139        self.bindings()
140            .rev()
141            .filter(|binding| {
142                binding
143                    .match_keystrokes(input)
144                    .is_some_and(|pending| !pending)
145            })
146            .cloned()
147            .collect()
148    }
149
150    /// Returns a list of bindings that match the given input, and a boolean indicating whether or
151    /// not more bindings might match if the input was longer. Bindings are returned in precedence
152    /// order (higher precedence first, reverse of the order they were added to the keymap).
153    ///
154    /// Precedence is defined by the depth in the tree (matches on the Editor take precedence over
155    /// matches on the Pane, then the Workspace, etc.). Bindings with no context are treated as the
156    /// same as the deepest context.
157    ///
158    /// In the case of multiple bindings at the same depth, the ones added to the keymap later take
159    /// precedence. User bindings are added after built-in bindings so that they take precedence.
160    ///
161    /// If a user has disabled a binding with `"x": null` it will not be returned. Disabled bindings
162    /// are evaluated with the same precedence rules so you can disable a rule in a given context
163    /// only.
164    pub fn bindings_for_input(
165        &self,
166        input: &[impl AsKeystroke],
167        context_stack: &[KeyContext],
168    ) -> (SmallVec<[KeyBinding; 1]>, bool) {
169        let mut matched_bindings = SmallVec::<[(usize, BindingIndex, &KeyBinding); 1]>::new();
170        let mut pending_bindings = SmallVec::<[(BindingIndex, &KeyBinding); 1]>::new();
171
172        for (ix, binding) in self.bindings().enumerate().rev() {
173            let Some(depth) = self.binding_enabled(binding, context_stack) else {
174                continue;
175            };
176            let Some(pending) = binding.match_keystrokes(input) else {
177                continue;
178            };
179
180            if !pending {
181                matched_bindings.push((depth, BindingIndex(ix), binding));
182            } else {
183                pending_bindings.push((BindingIndex(ix), binding));
184            }
185        }
186
187        matched_bindings.sort_by(|(depth_a, ix_a, _), (depth_b, ix_b, _)| {
188            depth_b.cmp(depth_a).then(ix_b.cmp(ix_a))
189        });
190
191        let mut bindings: SmallVec<[_; 1]> = SmallVec::new();
192        let mut first_binding_index = None;
193        let mut unbound_bindings: Vec<&KeyBinding> = Vec::new();
194
195        for (_, ix, binding) in matched_bindings {
196            if is_no_action(&*binding.action) {
197                // Only break if this is a user-defined NoAction binding
198                // This allows user keymaps to override base keymap NoAction bindings
199                if let Some(meta) = binding.meta {
200                    if meta.0 == 0 {
201                        break;
202                    }
203                } else {
204                    // If no meta is set, assume it's a user binding for safety
205                    break;
206                }
207                // For non-user NoAction bindings, continue searching for user overrides
208                continue;
209            }
210
211            if is_unbind(&*binding.action) {
212                unbound_bindings.push(binding);
213                continue;
214            }
215
216            if unbound_bindings
217                .iter()
218                .any(|disabled_binding| binding_is_unbound(disabled_binding, binding))
219            {
220                continue;
221            }
222
223            bindings.push(binding.clone());
224            first_binding_index.get_or_insert(ix);
225        }
226
227        let mut pending = HashSet::default();
228        for (ix, binding) in pending_bindings.into_iter().rev() {
229            if let Some(binding_ix) = first_binding_index
230                && binding_ix > ix
231            {
232                continue;
233            }
234            if is_no_action(&*binding.action) || is_unbind(&*binding.action) {
235                pending.remove(&&binding.keystrokes);
236                continue;
237            }
238            pending.insert(&binding.keystrokes);
239        }
240
241        (bindings, !pending.is_empty())
242    }
243    /// Check if the given binding is enabled, given a certain key context.
244    /// Returns the deepest depth at which the binding matches, or None if it doesn't match.
245    fn binding_enabled(&self, binding: &KeyBinding, contexts: &[KeyContext]) -> Option<usize> {
246        if let Some(predicate) = &binding.context_predicate {
247            predicate.depth_of(contexts)
248        } else {
249            Some(contexts.len())
250        }
251    }
252
253    /// Find the bindings that can follow the current input sequence.
254    pub fn possible_next_bindings_for_input(
255        &self,
256        input: &[Keystroke],
257        context_stack: &[KeyContext],
258    ) -> Vec<KeyBinding> {
259        let mut bindings = self
260            .bindings()
261            .enumerate()
262            .rev()
263            .filter_map(|(ix, binding)| {
264                let depth = self.binding_enabled(binding, context_stack)?;
265                let pending = binding.match_keystrokes(input);
266                match pending {
267                    None => None,
268                    Some(is_pending) => {
269                        if !is_pending
270                            || is_no_action(&*binding.action)
271                            || is_unbind(&*binding.action)
272                        {
273                            return None;
274                        }
275                        Some((depth, BindingIndex(ix), binding))
276                    }
277                }
278            })
279            .collect::<Vec<_>>();
280
281        bindings.sort_by(|(depth_a, ix_a, _), (depth_b, ix_b, _)| {
282            depth_b.cmp(depth_a).then(ix_b.cmp(ix_a))
283        });
284
285        bindings
286            .into_iter()
287            .map(|(_, _, binding)| binding.clone())
288            .collect::<Vec<_>>()
289    }
290}
291
292#[cfg(test)]
293mod tests {
294    use super::*;
295    use crate as gpui;
296    use open_gpui::{NoAction, Unbind};
297
298    actions!(
299        test_only,
300        [ActionAlpha, ActionBeta, ActionGamma, ActionDelta,]
301    );
302
303    #[test]
304    fn test_keymap() {
305        let bindings = [
306            KeyBinding::new("ctrl-a", ActionAlpha {}, None),
307            KeyBinding::new("ctrl-a", ActionBeta {}, Some("pane")),
308            KeyBinding::new("ctrl-a", ActionGamma {}, Some("editor && mode==full")),
309        ];
310
311        let mut keymap = Keymap::default();
312        keymap.add_bindings(bindings.clone());
313
314        // global bindings are enabled in all contexts
315        assert_eq!(keymap.binding_enabled(&bindings[0], &[]), Some(0));
316        assert_eq!(
317            keymap.binding_enabled(&bindings[0], &[KeyContext::parse("terminal").unwrap()]),
318            Some(1)
319        );
320
321        // contextual bindings are enabled in contexts that match their predicate
322        assert_eq!(
323            keymap.binding_enabled(&bindings[1], &[KeyContext::parse("barf x=y").unwrap()]),
324            None
325        );
326        assert_eq!(
327            keymap.binding_enabled(&bindings[1], &[KeyContext::parse("pane x=y").unwrap()]),
328            Some(1)
329        );
330
331        assert_eq!(
332            keymap.binding_enabled(&bindings[2], &[KeyContext::parse("editor").unwrap()]),
333            None
334        );
335        assert_eq!(
336            keymap.binding_enabled(
337                &bindings[2],
338                &[KeyContext::parse("editor mode=full").unwrap()]
339            ),
340            Some(1)
341        );
342    }
343
344    #[test]
345    fn test_depth_precedence() {
346        let bindings = [
347            KeyBinding::new("ctrl-a", ActionBeta {}, Some("pane")),
348            KeyBinding::new("ctrl-a", ActionGamma {}, Some("editor")),
349        ];
350
351        let mut keymap = Keymap::default();
352        keymap.add_bindings(bindings);
353
354        let (result, pending) = keymap.bindings_for_input(
355            &[Keystroke::parse("ctrl-a").unwrap()],
356            &[
357                KeyContext::parse("pane").unwrap(),
358                KeyContext::parse("editor").unwrap(),
359            ],
360        );
361
362        assert!(!pending);
363        assert_eq!(result.len(), 2);
364        assert!(result[0].action.partial_eq(&ActionGamma {}));
365        assert!(result[1].action.partial_eq(&ActionBeta {}));
366    }
367
368    #[test]
369    fn test_keymap_disabled() {
370        let bindings = [
371            KeyBinding::new("ctrl-a", ActionAlpha {}, Some("editor")),
372            KeyBinding::new("ctrl-b", ActionAlpha {}, Some("editor")),
373            KeyBinding::new("ctrl-a", NoAction {}, Some("editor && mode==full")),
374            KeyBinding::new("ctrl-b", NoAction {}, None),
375        ];
376
377        let mut keymap = Keymap::default();
378        keymap.add_bindings(bindings);
379
380        // binding is only enabled in a specific context
381        assert!(
382            keymap
383                .bindings_for_input(
384                    &[Keystroke::parse("ctrl-a").unwrap()],
385                    &[KeyContext::parse("barf").unwrap()],
386                )
387                .0
388                .is_empty()
389        );
390        assert!(
391            !keymap
392                .bindings_for_input(
393                    &[Keystroke::parse("ctrl-a").unwrap()],
394                    &[KeyContext::parse("editor").unwrap()],
395                )
396                .0
397                .is_empty()
398        );
399
400        // binding is disabled in a more specific context
401        assert!(
402            keymap
403                .bindings_for_input(
404                    &[Keystroke::parse("ctrl-a").unwrap()],
405                    &[KeyContext::parse("editor mode=full").unwrap()],
406                )
407                .0
408                .is_empty()
409        );
410
411        // binding is globally disabled
412        assert!(
413            keymap
414                .bindings_for_input(
415                    &[Keystroke::parse("ctrl-b").unwrap()],
416                    &[KeyContext::parse("barf").unwrap()],
417                )
418                .0
419                .is_empty()
420        );
421    }
422
423    #[test]
424    /// Tests for https://github.com/zed-industries/zed/issues/30259
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}