keymap.rs

  1use crate::Action;
  2use anyhow::anyhow;
  3use std::{
  4    any::Any,
  5    collections::{HashMap, HashSet},
  6    fmt::Debug,
  7};
  8use tree_sitter::{Language, Node, Parser};
  9
 10extern "C" {
 11    fn tree_sitter_context_predicate() -> Language;
 12}
 13
 14pub struct Matcher {
 15    pending: HashMap<usize, Pending>,
 16    keymap: Keymap,
 17}
 18
 19#[derive(Default)]
 20struct Pending {
 21    keystrokes: Vec<Keystroke>,
 22    context: Option<Context>,
 23}
 24
 25#[derive(Default)]
 26pub struct Keymap(Vec<Binding>);
 27
 28pub struct Binding {
 29    keystrokes: Vec<Keystroke>,
 30    action: Box<dyn Action>,
 31    context: Option<ContextPredicate>,
 32}
 33
 34#[derive(Clone, Debug, Eq, PartialEq)]
 35pub struct Keystroke {
 36    pub ctrl: bool,
 37    pub alt: bool,
 38    pub shift: bool,
 39    pub cmd: bool,
 40    pub key: String,
 41}
 42
 43#[derive(Clone, Debug, Default, Eq, PartialEq)]
 44pub struct Context {
 45    pub set: HashSet<String>,
 46    pub map: HashMap<String, String>,
 47}
 48
 49#[derive(Debug, Eq, PartialEq)]
 50enum ContextPredicate {
 51    Identifier(String),
 52    Equal(String, String),
 53    NotEqual(String, String),
 54    Not(Box<ContextPredicate>),
 55    And(Box<ContextPredicate>, Box<ContextPredicate>),
 56    Or(Box<ContextPredicate>, Box<ContextPredicate>),
 57}
 58
 59trait ActionArg {
 60    fn boxed_clone(&self) -> Box<dyn Any>;
 61}
 62
 63impl<T> ActionArg for T
 64where
 65    T: 'static + Any + Clone,
 66{
 67    fn boxed_clone(&self) -> Box<dyn Any> {
 68        Box::new(self.clone())
 69    }
 70}
 71
 72pub enum MatchResult {
 73    None,
 74    Pending,
 75    Action(Box<dyn Action>),
 76}
 77
 78impl Debug for MatchResult {
 79    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 80        match self {
 81            MatchResult::None => f.debug_struct("MatchResult::None").finish(),
 82            MatchResult::Pending => f.debug_struct("MatchResult::Pending").finish(),
 83            MatchResult::Action(action) => f
 84                .debug_tuple("MatchResult::Action")
 85                .field(&action.name())
 86                .finish(),
 87        }
 88    }
 89}
 90
 91impl Matcher {
 92    pub fn new(keymap: Keymap) -> Self {
 93        Self {
 94            pending: HashMap::new(),
 95            keymap,
 96        }
 97    }
 98
 99    pub fn set_keymap(&mut self, keymap: Keymap) {
100        self.pending.clear();
101        self.keymap = keymap;
102    }
103
104    pub fn add_bindings<T: IntoIterator<Item = Binding>>(&mut self, bindings: T) {
105        self.pending.clear();
106        self.keymap.add_bindings(bindings);
107    }
108
109    pub fn clear_pending(&mut self) {
110        self.pending.clear();
111    }
112
113    pub fn push_keystroke(
114        &mut self,
115        keystroke: Keystroke,
116        view_id: usize,
117        cx: &Context,
118    ) -> MatchResult {
119        let pending = self.pending.entry(view_id).or_default();
120
121        if let Some(pending_ctx) = pending.context.as_ref() {
122            if pending_ctx != cx {
123                pending.keystrokes.clear();
124            }
125        }
126
127        pending.keystrokes.push(keystroke);
128
129        let mut retain_pending = false;
130        for binding in self.keymap.0.iter().rev() {
131            if binding.keystrokes.starts_with(&pending.keystrokes)
132                && binding.context.as_ref().map(|c| c.eval(cx)).unwrap_or(true)
133            {
134                if binding.keystrokes.len() == pending.keystrokes.len() {
135                    self.pending.remove(&view_id);
136                    return MatchResult::Action(binding.action.boxed_clone());
137                } else {
138                    retain_pending = true;
139                    pending.context = Some(cx.clone());
140                }
141            }
142        }
143
144        if retain_pending {
145            MatchResult::Pending
146        } else {
147            self.pending.remove(&view_id);
148            MatchResult::None
149        }
150    }
151}
152
153impl Default for Matcher {
154    fn default() -> Self {
155        Self::new(Keymap::default())
156    }
157}
158
159impl Keymap {
160    pub fn new(bindings: Vec<Binding>) -> Self {
161        Self(bindings)
162    }
163
164    fn add_bindings<T: IntoIterator<Item = Binding>>(&mut self, bindings: T) {
165        self.0.extend(bindings.into_iter());
166    }
167}
168
169impl Binding {
170    pub fn new<A: Action>(keystrokes: &str, action: A, context: Option<&str>) -> Self {
171        let context = if let Some(context) = context {
172            Some(ContextPredicate::parse(context).unwrap())
173        } else {
174            None
175        };
176
177        Self {
178            keystrokes: keystrokes
179                .split_whitespace()
180                .map(|key| Keystroke::parse(key).unwrap())
181                .collect(),
182            action: Box::new(action),
183            context,
184        }
185    }
186}
187
188impl Keystroke {
189    pub fn parse(source: &str) -> anyhow::Result<Self> {
190        let mut ctrl = false;
191        let mut alt = false;
192        let mut shift = false;
193        let mut cmd = false;
194        let mut key = None;
195
196        let mut components = source.split("-").peekable();
197        while let Some(component) = components.next() {
198            match component {
199                "ctrl" => ctrl = true,
200                "alt" => alt = true,
201                "shift" => shift = true,
202                "cmd" => cmd = true,
203                _ => {
204                    if let Some(component) = components.peek() {
205                        if component.is_empty() && source.ends_with('-') {
206                            key = Some(String::from("-"));
207                            break;
208                        } else {
209                            return Err(anyhow!("Invalid keystroke `{}`", source));
210                        }
211                    } else {
212                        key = Some(String::from(component));
213                    }
214                }
215            }
216        }
217
218        Ok(Keystroke {
219            ctrl,
220            alt,
221            shift,
222            cmd,
223            key: key.unwrap(),
224        })
225    }
226
227    pub fn modified(&self) -> bool {
228        self.ctrl || self.alt || self.shift || self.cmd
229    }
230}
231
232impl Context {
233    pub fn extend(&mut self, other: &Context) {
234        for v in &other.set {
235            self.set.insert(v.clone());
236        }
237        for (k, v) in &other.map {
238            self.map.insert(k.clone(), v.clone());
239        }
240    }
241}
242
243impl ContextPredicate {
244    fn parse(source: &str) -> anyhow::Result<Self> {
245        let mut parser = Parser::new();
246        let language = unsafe { tree_sitter_context_predicate() };
247        parser.set_language(language).unwrap();
248        let source = source.as_bytes();
249        let tree = parser.parse(source, None).unwrap();
250        Self::from_node(tree.root_node(), source)
251    }
252
253    fn from_node(node: Node, source: &[u8]) -> anyhow::Result<Self> {
254        let parse_error = "error parsing context predicate";
255        let kind = node.kind();
256
257        match kind {
258            "source" => Self::from_node(node.child(0).ok_or(anyhow!(parse_error))?, source),
259            "identifier" => Ok(Self::Identifier(node.utf8_text(source)?.into())),
260            "not" => {
261                let child = Self::from_node(
262                    node.child_by_field_name("expression")
263                        .ok_or(anyhow!(parse_error))?,
264                    source,
265                )?;
266                Ok(Self::Not(Box::new(child)))
267            }
268            "and" | "or" => {
269                let left = Box::new(Self::from_node(
270                    node.child_by_field_name("left")
271                        .ok_or(anyhow!(parse_error))?,
272                    source,
273                )?);
274                let right = Box::new(Self::from_node(
275                    node.child_by_field_name("right")
276                        .ok_or(anyhow!(parse_error))?,
277                    source,
278                )?);
279                if kind == "and" {
280                    Ok(Self::And(left, right))
281                } else {
282                    Ok(Self::Or(left, right))
283                }
284            }
285            "equal" | "not_equal" => {
286                let left = node
287                    .child_by_field_name("left")
288                    .ok_or(anyhow!(parse_error))?
289                    .utf8_text(source)?
290                    .into();
291                let right = node
292                    .child_by_field_name("right")
293                    .ok_or(anyhow!(parse_error))?
294                    .utf8_text(source)?
295                    .into();
296                if kind == "equal" {
297                    Ok(Self::Equal(left, right))
298                } else {
299                    Ok(Self::NotEqual(left, right))
300                }
301            }
302            "parenthesized" => Self::from_node(
303                node.child_by_field_name("expression")
304                    .ok_or(anyhow!(parse_error))?,
305                source,
306            ),
307            _ => Err(anyhow!(parse_error)),
308        }
309    }
310
311    fn eval(&self, cx: &Context) -> bool {
312        match self {
313            Self::Identifier(name) => cx.set.contains(name.as_str()),
314            Self::Equal(left, right) => cx
315                .map
316                .get(left)
317                .map(|value| value == right)
318                .unwrap_or(false),
319            Self::NotEqual(left, right) => {
320                cx.map.get(left).map(|value| value != right).unwrap_or(true)
321            }
322            Self::Not(pred) => !pred.eval(cx),
323            Self::And(left, right) => left.eval(cx) && right.eval(cx),
324            Self::Or(left, right) => left.eval(cx) || right.eval(cx),
325        }
326    }
327}
328
329#[cfg(test)]
330mod tests {
331    use crate::{actions, impl_actions};
332
333    use super::*;
334
335    #[test]
336    fn test_keystroke_parsing() -> anyhow::Result<()> {
337        assert_eq!(
338            Keystroke::parse("ctrl-p")?,
339            Keystroke {
340                key: "p".into(),
341                ctrl: true,
342                alt: false,
343                shift: false,
344                cmd: false,
345            }
346        );
347
348        assert_eq!(
349            Keystroke::parse("alt-shift-down")?,
350            Keystroke {
351                key: "down".into(),
352                ctrl: false,
353                alt: true,
354                shift: true,
355                cmd: false,
356            }
357        );
358
359        assert_eq!(
360            Keystroke::parse("shift-cmd--")?,
361            Keystroke {
362                key: "-".into(),
363                ctrl: false,
364                alt: false,
365                shift: true,
366                cmd: true,
367            }
368        );
369
370        Ok(())
371    }
372
373    #[test]
374    fn test_context_predicate_parsing() -> anyhow::Result<()> {
375        use ContextPredicate::*;
376
377        assert_eq!(
378            ContextPredicate::parse("a && (b == c || d != e)")?,
379            And(
380                Box::new(Identifier("a".into())),
381                Box::new(Or(
382                    Box::new(Equal("b".into(), "c".into())),
383                    Box::new(NotEqual("d".into(), "e".into())),
384                ))
385            )
386        );
387
388        assert_eq!(
389            ContextPredicate::parse("!a")?,
390            Not(Box::new(Identifier("a".into())),)
391        );
392
393        Ok(())
394    }
395
396    #[test]
397    fn test_context_predicate_eval() -> anyhow::Result<()> {
398        let predicate = ContextPredicate::parse("a && b || c == d")?;
399
400        let mut context = Context::default();
401        context.set.insert("a".into());
402        assert!(!predicate.eval(&context));
403
404        context.set.insert("b".into());
405        assert!(predicate.eval(&context));
406
407        context.set.remove("b");
408        context.map.insert("c".into(), "x".into());
409        assert!(!predicate.eval(&context));
410
411        context.map.insert("c".into(), "d".into());
412        assert!(predicate.eval(&context));
413
414        let predicate = ContextPredicate::parse("!a")?;
415        assert!(predicate.eval(&Context::default()));
416
417        Ok(())
418    }
419
420    #[test]
421    fn test_matcher() -> anyhow::Result<()> {
422        #[derive(Clone)]
423        pub struct A(pub &'static str);
424        impl_actions!(test, [A]);
425        actions!(test, [B, Ab]);
426
427        impl PartialEq for A {
428            fn eq(&self, other: &Self) -> bool {
429                self.0 == other.0
430            }
431        }
432        impl Eq for A {}
433        impl Debug for A {
434            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
435                write!(f, "A({:?})", &self.0)
436            }
437        }
438
439        #[derive(Clone, Debug, Eq, PartialEq)]
440        struct ActionArg {
441            a: &'static str,
442        }
443
444        let keymap = Keymap(vec![
445            Binding::new("a", A("x"), Some("a")),
446            Binding::new("b", B, Some("a")),
447            Binding::new("a b", Ab, Some("a || b")),
448        ]);
449
450        let mut ctx_a = Context::default();
451        ctx_a.set.insert("a".into());
452
453        let mut ctx_b = Context::default();
454        ctx_b.set.insert("b".into());
455
456        let mut matcher = Matcher::new(keymap);
457
458        // Basic match
459        assert_eq!(matcher.test_keystroke("a", 1, &ctx_a), Some(A("x")));
460
461        // Multi-keystroke match
462        assert_eq!(matcher.test_keystroke::<A>("a", 1, &ctx_b), None);
463        assert_eq!(matcher.test_keystroke("b", 1, &ctx_b), Some(Ab));
464
465        // Failed matches don't interfere with matching subsequent keys
466        assert_eq!(matcher.test_keystroke::<A>("x", 1, &ctx_a), None);
467        assert_eq!(matcher.test_keystroke("a", 1, &ctx_a), Some(A("x")));
468
469        // Pending keystrokes are cleared when the context changes
470        assert_eq!(matcher.test_keystroke::<A>("a", 1, &ctx_b), None);
471        assert_eq!(matcher.test_keystroke("b", 1, &ctx_a), Some(B));
472
473        let mut ctx_c = Context::default();
474        ctx_c.set.insert("c".into());
475
476        // Pending keystrokes are maintained per-view
477        assert_eq!(matcher.test_keystroke::<A>("a", 1, &ctx_b), None);
478        assert_eq!(matcher.test_keystroke::<A>("a", 2, &ctx_c), None);
479        assert_eq!(matcher.test_keystroke("b", 1, &ctx_b), Some(Ab));
480
481        Ok(())
482    }
483
484    impl Matcher {
485        fn test_keystroke<A>(&mut self, keystroke: &str, view_id: usize, cx: &Context) -> Option<A>
486        where
487            A: Action + Debug + Eq,
488        {
489            if let MatchResult::Action(action) =
490                self.push_keystroke(Keystroke::parse(keystroke).unwrap(), view_id, cx)
491            {
492                Some(*action.boxed_clone_as_any().downcast().unwrap())
493            } else {
494                None
495            }
496        }
497    }
498}