context.rs

  1use crate::SharedString;
  2use anyhow::{Context as _, Result};
  3use std::fmt;
  4
  5/// A datastructure for resolving whether an action should be dispatched
  6/// at this point in the element tree. Contains a set of identifiers
  7/// and/or key value pairs representing the current context for the
  8/// keymap.
  9#[derive(Clone, Default, Eq, PartialEq, Hash)]
 10pub struct KeyContext(Vec<ContextEntry>);
 11
 12#[derive(Clone, Debug, Eq, PartialEq, Hash)]
 13/// An entry in a KeyContext
 14pub struct ContextEntry {
 15    /// The key (or name if no value)
 16    pub key: SharedString,
 17    /// The value
 18    pub value: Option<SharedString>,
 19}
 20
 21impl<'a> TryFrom<&'a str> for KeyContext {
 22    type Error = anyhow::Error;
 23
 24    fn try_from(value: &'a str) -> Result<Self> {
 25        Self::parse(value)
 26    }
 27}
 28
 29impl KeyContext {
 30    /// Initialize a new [`KeyContext`] that contains an `os` key set to either `macos`, `linux`, `windows` or `unknown`.
 31    pub fn new_with_defaults() -> Self {
 32        let mut context = Self::default();
 33        #[cfg(target_os = "macos")]
 34        context.set("os", "macos");
 35        #[cfg(any(target_os = "linux", target_os = "freebsd"))]
 36        context.set("os", "linux");
 37        #[cfg(target_os = "windows")]
 38        context.set("os", "windows");
 39        #[cfg(not(any(
 40            target_os = "macos",
 41            target_os = "linux",
 42            target_os = "freebsd",
 43            target_os = "windows"
 44        )))]
 45        context.set("os", "unknown");
 46        context
 47    }
 48
 49    /// Returns the primary context entry (usually the name of the component)
 50    pub fn primary(&self) -> Option<&ContextEntry> {
 51        self.0.iter().find(|p| p.value.is_none())
 52    }
 53
 54    /// Returns everything except the primary context entry.
 55    pub fn secondary(&self) -> impl Iterator<Item = &ContextEntry> {
 56        let primary = self.primary();
 57        self.0.iter().filter(move |&p| Some(p) != primary)
 58    }
 59
 60    /// Parse a key context from a string.
 61    /// The key context format is very simple:
 62    /// - either a single identifier, such as `StatusBar`
 63    /// - or a key value pair, such as `mode = visible`
 64    /// - separated by whitespace, such as `StatusBar mode = visible`
 65    pub fn parse(source: &str) -> Result<Self> {
 66        let mut context = Self::default();
 67        let source = skip_whitespace(source);
 68        Self::parse_expr(source, &mut context)?;
 69        Ok(context)
 70    }
 71
 72    fn parse_expr(mut source: &str, context: &mut Self) -> Result<()> {
 73        if source.is_empty() {
 74            return Ok(());
 75        }
 76
 77        let key = source
 78            .chars()
 79            .take_while(|c| is_identifier_char(*c))
 80            .collect::<String>();
 81        source = skip_whitespace(&source[key.len()..]);
 82        if let Some(suffix) = source.strip_prefix('=') {
 83            source = skip_whitespace(suffix);
 84            let value = source
 85                .chars()
 86                .take_while(|c| is_identifier_char(*c))
 87                .collect::<String>();
 88            source = skip_whitespace(&source[value.len()..]);
 89            context.set(key, value);
 90        } else {
 91            context.add(key);
 92        }
 93
 94        Self::parse_expr(source, context)
 95    }
 96
 97    /// Check if this context is empty.
 98    pub fn is_empty(&self) -> bool {
 99        self.0.is_empty()
100    }
101
102    /// Clear this context.
103    pub fn clear(&mut self) {
104        self.0.clear();
105    }
106
107    /// Extend this context with another context.
108    pub fn extend(&mut self, other: &Self) {
109        for entry in &other.0 {
110            if !self.contains(&entry.key) {
111                self.0.push(entry.clone());
112            }
113        }
114    }
115
116    /// Add an identifier to this context, if it's not already in this context.
117    pub fn add<I: Into<SharedString>>(&mut self, identifier: I) {
118        let key = identifier.into();
119
120        if !self.contains(&key) {
121            self.0.push(ContextEntry { key, value: None })
122        }
123    }
124
125    /// Set a key value pair in this context, if it's not already set.
126    pub fn set<S1: Into<SharedString>, S2: Into<SharedString>>(&mut self, key: S1, value: S2) {
127        let key = key.into();
128        if !self.contains(&key) {
129            self.0.push(ContextEntry {
130                key,
131                value: Some(value.into()),
132            })
133        }
134    }
135
136    /// Check if this context contains a given identifier or key.
137    pub fn contains(&self, key: &str) -> bool {
138        self.0.iter().any(|entry| entry.key.as_ref() == key)
139    }
140
141    /// Get the associated value for a given identifier or key.
142    pub fn get(&self, key: &str) -> Option<&SharedString> {
143        self.0
144            .iter()
145            .find(|entry| entry.key.as_ref() == key)?
146            .value
147            .as_ref()
148    }
149}
150
151impl fmt::Debug for KeyContext {
152    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
153        let mut entries = self.0.iter().peekable();
154        while let Some(entry) = entries.next() {
155            if let Some(ref value) = entry.value {
156                write!(f, "{}={}", entry.key, value)?;
157            } else {
158                write!(f, "{}", entry.key)?;
159            }
160            if entries.peek().is_some() {
161                write!(f, " ")?;
162            }
163        }
164        Ok(())
165    }
166}
167
168/// A datastructure for resolving whether an action should be dispatched
169/// Representing a small language for describing which contexts correspond
170/// to which actions.
171#[derive(Clone, Debug, Eq, PartialEq, Hash)]
172pub enum KeyBindingContextPredicate {
173    /// A predicate that will match a given identifier.
174    Identifier(SharedString),
175    /// A predicate that will match a given key-value pair.
176    Equal(SharedString, SharedString),
177    /// A predicate that will match a given key-value pair not being present.
178    NotEqual(SharedString, SharedString),
179    /// A predicate that will match a given predicate appearing below another predicate.
180    /// in the element tree
181    Descendant(
182        Box<KeyBindingContextPredicate>,
183        Box<KeyBindingContextPredicate>,
184    ),
185    /// Predicate that will invert another predicate.
186    Not(Box<KeyBindingContextPredicate>),
187    /// A predicate that will match if both of its children match.
188    And(
189        Box<KeyBindingContextPredicate>,
190        Box<KeyBindingContextPredicate>,
191    ),
192    /// A predicate that will match if either of its children match.
193    Or(
194        Box<KeyBindingContextPredicate>,
195        Box<KeyBindingContextPredicate>,
196    ),
197}
198
199impl fmt::Display for KeyBindingContextPredicate {
200    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
201        match self {
202            Self::Identifier(name) => write!(f, "{}", name),
203            Self::Equal(left, right) => write!(f, "{} == {}", left, right),
204            Self::NotEqual(left, right) => write!(f, "{} != {}", left, right),
205            Self::Not(pred) => write!(f, "!{}", pred),
206            Self::Descendant(parent, child) => write!(f, "{} > {}", parent, child),
207            Self::And(left, right) => write!(f, "({} && {})", left, right),
208            Self::Or(left, right) => write!(f, "({} || {})", left, right),
209        }
210    }
211}
212
213impl KeyBindingContextPredicate {
214    /// Parse a string in the same format as the keymap's context field.
215    ///
216    /// A basic equivalence check against a set of identifiers can performed by
217    /// simply writing a string:
218    ///
219    /// `StatusBar` -> A predicate that will match a context with the identifier `StatusBar`
220    ///
221    /// You can also specify a key-value pair:
222    ///
223    /// `mode == visible` -> A predicate that will match a context with the key `mode`
224    ///                      with the value `visible`
225    ///
226    /// And a logical operations combining these two checks:
227    ///
228    /// `StatusBar && mode == visible` -> A predicate that will match a context with the
229    ///                                   identifier `StatusBar` and the key `mode`
230    ///                                   with the value `visible`
231    ///
232    ///
233    /// There is also a special child `>` operator that will match a predicate that is
234    /// below another predicate:
235    ///
236    /// `StatusBar > mode == visible` -> A predicate that will match a context identifier `StatusBar`
237    ///                                  and a child context that has the key `mode` with the
238    ///                                  value `visible`
239    ///
240    /// This syntax supports `!=`, `||` and `&&` as logical operators.
241    /// You can also preface an operation or check with a `!` to negate it.
242    pub fn parse(source: &str) -> Result<Self> {
243        let source = skip_whitespace(source);
244        let (predicate, rest) = Self::parse_expr(source, 0)?;
245        if let Some(next) = rest.chars().next() {
246            anyhow::bail!("unexpected character '{next:?}'");
247        } else {
248            Ok(predicate)
249        }
250    }
251
252    /// Find the deepest depth at which the predicate matches.
253    pub fn depth_of(&self, contexts: &[KeyContext]) -> Option<usize> {
254        for depth in (0..=contexts.len()).rev() {
255            let context_slice = &contexts[0..depth];
256            if self.eval_inner(context_slice, contexts) {
257                return Some(depth);
258            }
259        }
260        None
261    }
262
263    /// Eval a predicate against a set of contexts, arranged from lowest to highest.
264    #[allow(unused)]
265    pub(crate) fn eval(&self, contexts: &[KeyContext]) -> bool {
266        self.eval_inner(contexts, contexts)
267    }
268
269    /// Eval a predicate against a set of contexts, arranged from lowest to highest.
270    pub fn eval_inner(&self, contexts: &[KeyContext], all_contexts: &[KeyContext]) -> bool {
271        let Some(context) = contexts.last() else {
272            return false;
273        };
274        match self {
275            Self::Identifier(name) => context.contains(name),
276            Self::Equal(left, right) => context
277                .get(left)
278                .map(|value| value == right)
279                .unwrap_or(false),
280            Self::NotEqual(left, right) => context
281                .get(left)
282                .map(|value| value != right)
283                .unwrap_or(true),
284            Self::Not(pred) => {
285                for i in 0..all_contexts.len() {
286                    if pred.eval_inner(&all_contexts[..=i], all_contexts) {
287                        return false;
288                    }
289                }
290                return true;
291            }
292            // Workspace > Pane > Editor
293            //
294            // Pane > (Pane > Editor) // should match?
295            // (Pane > Pane) > Editor // should not match?
296            // Pane > !Workspace <-- should match?
297            // !Workspace        <-- shouldn't match?
298            Self::Descendant(parent, child) => {
299                for i in 0..contexts.len() - 1 {
300                    // [Workspace >  Pane], [Editor]
301                    if parent.eval_inner(&contexts[..=i], all_contexts) {
302                        if !child.eval_inner(&contexts[i + 1..], &contexts[i + 1..]) {
303                            return false;
304                        }
305                        return true;
306                    }
307                }
308                return false;
309            }
310            Self::And(left, right) => {
311                left.eval_inner(contexts, all_contexts) && right.eval_inner(contexts, all_contexts)
312            }
313            Self::Or(left, right) => {
314                left.eval_inner(contexts, all_contexts) || right.eval_inner(contexts, all_contexts)
315            }
316        }
317    }
318
319    /// Returns whether or not this predicate matches all possible contexts matched by
320    /// the other predicate.
321    pub fn is_superset(&self, other: &Self) -> bool {
322        if self == other {
323            return true;
324        }
325
326        if let KeyBindingContextPredicate::Or(left, right) = self {
327            return left.is_superset(other) || right.is_superset(other);
328        }
329
330        match other {
331            KeyBindingContextPredicate::Descendant(_, child) => self.is_superset(child),
332            KeyBindingContextPredicate::And(left, right) => {
333                self.is_superset(left) || self.is_superset(right)
334            }
335            KeyBindingContextPredicate::Identifier(_) => false,
336            KeyBindingContextPredicate::Equal(_, _) => false,
337            KeyBindingContextPredicate::NotEqual(_, _) => false,
338            KeyBindingContextPredicate::Not(_) => false,
339            KeyBindingContextPredicate::Or(_, _) => false,
340        }
341    }
342
343    fn parse_expr(mut source: &str, min_precedence: u32) -> anyhow::Result<(Self, &str)> {
344        type Op = fn(
345            KeyBindingContextPredicate,
346            KeyBindingContextPredicate,
347        ) -> Result<KeyBindingContextPredicate>;
348
349        let (mut predicate, rest) = Self::parse_primary(source)?;
350        source = rest;
351
352        'parse: loop {
353            for (operator, precedence, constructor) in [
354                (">", PRECEDENCE_CHILD, Self::new_child as Op),
355                ("&&", PRECEDENCE_AND, Self::new_and as Op),
356                ("||", PRECEDENCE_OR, Self::new_or as Op),
357                ("==", PRECEDENCE_EQ, Self::new_eq as Op),
358                ("!=", PRECEDENCE_EQ, Self::new_neq as Op),
359            ] {
360                if source.starts_with(operator) && precedence >= min_precedence {
361                    source = skip_whitespace(&source[operator.len()..]);
362                    let (right, rest) = Self::parse_expr(source, precedence + 1)?;
363                    predicate = constructor(predicate, right)?;
364                    source = rest;
365                    continue 'parse;
366                }
367            }
368            break;
369        }
370
371        Ok((predicate, source))
372    }
373
374    fn parse_primary(mut source: &str) -> anyhow::Result<(Self, &str)> {
375        let next = source.chars().next().context("unexpected end")?;
376        match next {
377            '(' => {
378                source = skip_whitespace(&source[1..]);
379                let (predicate, rest) = Self::parse_expr(source, 0)?;
380                let stripped = rest.strip_prefix(')').context("expected a ')'")?;
381                source = skip_whitespace(stripped);
382                Ok((predicate, source))
383            }
384            '!' => {
385                let source = skip_whitespace(&source[1..]);
386                let (predicate, source) = Self::parse_expr(source, PRECEDENCE_NOT)?;
387                Ok((KeyBindingContextPredicate::Not(Box::new(predicate)), source))
388            }
389            _ if is_identifier_char(next) => {
390                let len = source
391                    .find(|c: char| !is_identifier_char(c) && !is_vim_operator_char(c))
392                    .unwrap_or(source.len());
393                let (identifier, rest) = source.split_at(len);
394                source = skip_whitespace(rest);
395                Ok((
396                    KeyBindingContextPredicate::Identifier(identifier.to_string().into()),
397                    source,
398                ))
399            }
400            _ if is_vim_operator_char(next) => {
401                let (operator, rest) = source.split_at(1);
402                source = skip_whitespace(rest);
403                Ok((
404                    KeyBindingContextPredicate::Identifier(operator.to_string().into()),
405                    source,
406                ))
407            }
408            _ => anyhow::bail!("unexpected character '{next:?}'"),
409        }
410    }
411
412    fn new_or(self, other: Self) -> Result<Self> {
413        Ok(Self::Or(Box::new(self), Box::new(other)))
414    }
415
416    fn new_and(self, other: Self) -> Result<Self> {
417        Ok(Self::And(Box::new(self), Box::new(other)))
418    }
419
420    fn new_child(self, other: Self) -> Result<Self> {
421        Ok(Self::Descendant(Box::new(self), Box::new(other)))
422    }
423
424    fn new_eq(self, other: Self) -> Result<Self> {
425        if let (Self::Identifier(left), Self::Identifier(right)) = (self, other) {
426            Ok(Self::Equal(left, right))
427        } else {
428            anyhow::bail!("operands of == must be identifiers");
429        }
430    }
431
432    fn new_neq(self, other: Self) -> Result<Self> {
433        if let (Self::Identifier(left), Self::Identifier(right)) = (self, other) {
434            Ok(Self::NotEqual(left, right))
435        } else {
436            anyhow::bail!("operands of != must be identifiers");
437        }
438    }
439}
440
441const PRECEDENCE_CHILD: u32 = 1;
442const PRECEDENCE_OR: u32 = 2;
443const PRECEDENCE_AND: u32 = 3;
444const PRECEDENCE_EQ: u32 = 4;
445const PRECEDENCE_NOT: u32 = 5;
446
447fn is_identifier_char(c: char) -> bool {
448    c.is_alphanumeric() || c == '_' || c == '-'
449}
450
451fn is_vim_operator_char(c: char) -> bool {
452    c == '>' || c == '<' || c == '~' || c == '"' || c == '?'
453}
454
455fn skip_whitespace(source: &str) -> &str {
456    let len = source
457        .find(|c: char| !c.is_whitespace())
458        .unwrap_or(source.len());
459    &source[len..]
460}
461
462#[cfg(test)]
463mod tests {
464    use super::*;
465    use crate as gpui;
466    use KeyBindingContextPredicate::*;
467
468    #[test]
469    fn test_actions_definition() {
470        {
471            actions!(test_only, [A, B, C, D, E, F, G]);
472        }
473
474        {
475            actions!(
476                test_only,
477                [
478                    H, I, J, K, L, M, N, // Don't wrap, test the trailing comma
479                ]
480            );
481        }
482    }
483
484    #[test]
485    fn test_parse_context() {
486        let mut expected = KeyContext::default();
487        expected.add("baz");
488        expected.set("foo", "bar");
489        assert_eq!(KeyContext::parse("baz foo=bar").unwrap(), expected);
490        assert_eq!(KeyContext::parse("baz foo = bar").unwrap(), expected);
491        assert_eq!(
492            KeyContext::parse("  baz foo   =   bar baz").unwrap(),
493            expected
494        );
495        assert_eq!(KeyContext::parse(" baz foo = bar").unwrap(), expected);
496    }
497
498    #[test]
499    fn test_parse_identifiers() {
500        // Identifiers
501        assert_eq!(
502            KeyBindingContextPredicate::parse("abc12").unwrap(),
503            Identifier("abc12".into())
504        );
505        assert_eq!(
506            KeyBindingContextPredicate::parse("_1a").unwrap(),
507            Identifier("_1a".into())
508        );
509    }
510
511    #[test]
512    fn test_parse_negations() {
513        assert_eq!(
514            KeyBindingContextPredicate::parse("!abc").unwrap(),
515            Not(Box::new(Identifier("abc".into())))
516        );
517        assert_eq!(
518            KeyBindingContextPredicate::parse(" ! ! abc").unwrap(),
519            Not(Box::new(Not(Box::new(Identifier("abc".into())))))
520        );
521    }
522
523    #[test]
524    fn test_parse_equality_operators() {
525        assert_eq!(
526            KeyBindingContextPredicate::parse("a == b").unwrap(),
527            Equal("a".into(), "b".into())
528        );
529        assert_eq!(
530            KeyBindingContextPredicate::parse("c!=d").unwrap(),
531            NotEqual("c".into(), "d".into())
532        );
533        assert_eq!(
534            KeyBindingContextPredicate::parse("c == !d")
535                .unwrap_err()
536                .to_string(),
537            "operands of == must be identifiers"
538        );
539    }
540
541    #[test]
542    fn test_parse_boolean_operators() {
543        assert_eq!(
544            KeyBindingContextPredicate::parse("a || b").unwrap(),
545            Or(
546                Box::new(Identifier("a".into())),
547                Box::new(Identifier("b".into()))
548            )
549        );
550        assert_eq!(
551            KeyBindingContextPredicate::parse("a || !b && c").unwrap(),
552            Or(
553                Box::new(Identifier("a".into())),
554                Box::new(And(
555                    Box::new(Not(Box::new(Identifier("b".into())))),
556                    Box::new(Identifier("c".into()))
557                ))
558            )
559        );
560        assert_eq!(
561            KeyBindingContextPredicate::parse("a && b || c&&d").unwrap(),
562            Or(
563                Box::new(And(
564                    Box::new(Identifier("a".into())),
565                    Box::new(Identifier("b".into()))
566                )),
567                Box::new(And(
568                    Box::new(Identifier("c".into())),
569                    Box::new(Identifier("d".into()))
570                ))
571            )
572        );
573        assert_eq!(
574            KeyBindingContextPredicate::parse("a == b && c || d == e && f").unwrap(),
575            Or(
576                Box::new(And(
577                    Box::new(Equal("a".into(), "b".into())),
578                    Box::new(Identifier("c".into()))
579                )),
580                Box::new(And(
581                    Box::new(Equal("d".into(), "e".into())),
582                    Box::new(Identifier("f".into()))
583                ))
584            )
585        );
586        assert_eq!(
587            KeyBindingContextPredicate::parse("a && b && c && d").unwrap(),
588            And(
589                Box::new(And(
590                    Box::new(And(
591                        Box::new(Identifier("a".into())),
592                        Box::new(Identifier("b".into()))
593                    )),
594                    Box::new(Identifier("c".into())),
595                )),
596                Box::new(Identifier("d".into()))
597            ),
598        );
599    }
600
601    #[test]
602    fn test_parse_parenthesized_expressions() {
603        assert_eq!(
604            KeyBindingContextPredicate::parse("a && (b == c || d != e)").unwrap(),
605            And(
606                Box::new(Identifier("a".into())),
607                Box::new(Or(
608                    Box::new(Equal("b".into(), "c".into())),
609                    Box::new(NotEqual("d".into(), "e".into())),
610                )),
611            ),
612        );
613        assert_eq!(
614            KeyBindingContextPredicate::parse(" ( a || b ) ").unwrap(),
615            Or(
616                Box::new(Identifier("a".into())),
617                Box::new(Identifier("b".into())),
618            )
619        );
620    }
621
622    #[test]
623    fn test_is_superset() {
624        assert_is_superset("editor", "editor", true);
625        assert_is_superset("editor", "workspace", false);
626
627        assert_is_superset("editor", "editor && vim_mode", true);
628        assert_is_superset("editor", "mode == full && editor", true);
629        assert_is_superset("editor && mode == full", "editor", false);
630
631        assert_is_superset("editor", "something > editor", true);
632        assert_is_superset("editor", "editor > menu", false);
633
634        assert_is_superset("foo || bar || baz", "bar", true);
635        assert_is_superset("foo || bar || baz", "quux", false);
636
637        #[track_caller]
638        fn assert_is_superset(a: &str, b: &str, result: bool) {
639            let a = KeyBindingContextPredicate::parse(a).unwrap();
640            let b = KeyBindingContextPredicate::parse(b).unwrap();
641            assert_eq!(a.is_superset(&b), result, "({a:?}).is_superset({b:?})");
642        }
643    }
644
645    #[test]
646    fn test_child_operator() {
647        let predicate = KeyBindingContextPredicate::parse("parent > child").unwrap();
648
649        let parent_context = KeyContext::try_from("parent").unwrap();
650        let child_context = KeyContext::try_from("child").unwrap();
651
652        let contexts = vec![parent_context.clone(), child_context.clone()];
653        assert!(predicate.eval(&contexts));
654
655        let grandparent_context = KeyContext::try_from("grandparent").unwrap();
656
657        let contexts = vec![
658            grandparent_context,
659            parent_context.clone(),
660            child_context.clone(),
661        ];
662        assert!(predicate.eval(&contexts));
663
664        let other_context = KeyContext::try_from("other").unwrap();
665
666        let contexts = vec![other_context.clone(), child_context.clone()];
667        assert!(!predicate.eval(&contexts));
668
669        let contexts = vec![
670            parent_context.clone(),
671            other_context.clone(),
672            child_context.clone(),
673        ];
674        assert!(predicate.eval(&contexts));
675
676        assert!(!predicate.eval(&[]));
677        assert!(!predicate.eval(&[child_context.clone()]));
678        assert!(!predicate.eval(&[parent_context]));
679
680        let zany_predicate = KeyBindingContextPredicate::parse("child > child").unwrap();
681        assert!(!zany_predicate.eval(&[child_context.clone()]));
682        assert!(zany_predicate.eval(&[child_context.clone(), child_context.clone()]));
683    }
684
685    #[test]
686    fn test_not_operator() {
687        let not_predicate = KeyBindingContextPredicate::parse("!editor").unwrap();
688        let editor_context = KeyContext::try_from("editor").unwrap();
689        let workspace_context = KeyContext::try_from("workspace").unwrap();
690        let parent_context = KeyContext::try_from("parent").unwrap();
691        let child_context = KeyContext::try_from("child").unwrap();
692
693        assert!(not_predicate.eval(&[workspace_context.clone()]));
694        assert!(!not_predicate.eval(&[editor_context.clone()]));
695        assert!(!not_predicate.eval(&[editor_context.clone(), workspace_context.clone()]));
696        assert!(!not_predicate.eval(&[workspace_context.clone(), editor_context.clone()]));
697
698        let complex_not = KeyBindingContextPredicate::parse("!editor && workspace").unwrap();
699        assert!(complex_not.eval(&[workspace_context.clone()]));
700        assert!(!complex_not.eval(&[editor_context.clone(), workspace_context.clone()]));
701
702        let not_mode_predicate = KeyBindingContextPredicate::parse("!(mode == full)").unwrap();
703        let mut mode_context = KeyContext::default();
704        mode_context.set("mode", "full");
705        assert!(!not_mode_predicate.eval(&[mode_context.clone()]));
706
707        let mut other_mode_context = KeyContext::default();
708        other_mode_context.set("mode", "partial");
709        assert!(not_mode_predicate.eval(&[other_mode_context]));
710
711        let not_descendant = KeyBindingContextPredicate::parse("!(parent > child)").unwrap();
712        assert!(not_descendant.eval(&[parent_context.clone()]));
713        assert!(not_descendant.eval(&[child_context.clone()]));
714        assert!(!not_descendant.eval(&[parent_context.clone(), child_context.clone()]));
715
716        let not_descendant = KeyBindingContextPredicate::parse("parent > !child").unwrap();
717        assert!(!not_descendant.eval(&[parent_context.clone()]));
718        assert!(!not_descendant.eval(&[child_context.clone()]));
719        assert!(!not_descendant.eval(&[parent_context.clone(), child_context.clone()]));
720
721        let double_not = KeyBindingContextPredicate::parse("!!editor").unwrap();
722        assert!(double_not.eval(&[editor_context.clone()]));
723        assert!(!double_not.eval(&[workspace_context.clone()]));
724
725        // Test complex descendant cases
726        let workspace_context = KeyContext::try_from("Workspace").unwrap();
727        let pane_context = KeyContext::try_from("Pane").unwrap();
728        let editor_context = KeyContext::try_from("Editor").unwrap();
729
730        // Workspace > Pane > Editor
731        let workspace_pane_editor = vec![
732            workspace_context.clone(),
733            pane_context.clone(),
734            editor_context.clone(),
735        ];
736
737        // Pane > (Pane > Editor) - should not match
738        let pane_pane_editor = KeyBindingContextPredicate::parse("Pane > (Pane > Editor)").unwrap();
739        assert!(!pane_pane_editor.eval(&workspace_pane_editor));
740
741        let workspace_pane_editor_predicate =
742            KeyBindingContextPredicate::parse("Workspace > Pane > Editor").unwrap();
743        assert!(workspace_pane_editor_predicate.eval(&workspace_pane_editor));
744
745        // (Pane > Pane) > Editor - should not match
746        let pane_pane_then_editor =
747            KeyBindingContextPredicate::parse("(Pane > Pane) > Editor").unwrap();
748        assert!(!pane_pane_then_editor.eval(&workspace_pane_editor));
749
750        // Pane > !Workspace - should match
751        let pane_not_workspace = KeyBindingContextPredicate::parse("Pane > !Workspace").unwrap();
752        assert!(pane_not_workspace.eval(&[pane_context.clone(), editor_context.clone()]));
753        assert!(!pane_not_workspace.eval(&[pane_context.clone(), workspace_context.clone()]));
754
755        // !Workspace - shouldn't match when Workspace is in the context
756        let not_workspace = KeyBindingContextPredicate::parse("!Workspace").unwrap();
757        assert!(!not_workspace.eval(&[workspace_context.clone()]));
758        assert!(not_workspace.eval(&[pane_context.clone()]));
759        assert!(not_workspace.eval(&[editor_context.clone()]));
760        assert!(!not_workspace.eval(&workspace_pane_editor));
761    }
762}