From dd2cc9de17f39e37abddba4965d476fc9cebb719 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sat, 17 Apr 2021 20:51:47 +0200 Subject: [PATCH] fix(match): fix bug that prevented the matcher from detecting word matches at start --- espanso-match/src/rolling/matcher.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/espanso-match/src/rolling/matcher.rs b/espanso-match/src/rolling/matcher.rs index c25c4ce..7576243 100644 --- a/espanso-match/src/rolling/matcher.rs +++ b/espanso-match/src/rolling/matcher.rs @@ -49,8 +49,8 @@ struct RollingMatcherStatePath<'a, Id> { } pub struct RollingMatcherOptions { - char_word_separators: Vec, - key_word_separators: Vec, + pub char_word_separators: Vec, + pub key_word_separators: Vec, } impl Default for RollingMatcherOptions { @@ -85,7 +85,7 @@ where for node_path in prev_state.paths.iter() { next_refs.extend( self - .find_refs(node_path.node, &event) + .find_refs(node_path.node, &event, true) .into_iter() .map(|node_ref| { let mut new_events = node_path.events.clone(); @@ -97,7 +97,7 @@ where } // Calculate new ones - let root_refs = self.find_refs(&self.root, &event); + let root_refs = self.find_refs(&self.root, &event, prev_state.is_some()); next_refs.extend( root_refs .into_iter() @@ -151,6 +151,7 @@ impl RollingMatcher { &'a self, node: &'a MatcherTreeNode, event: &Event, + has_previous_state: bool, ) -> Vec<&'a MatcherTreeRef> { let mut refs = Vec::new(); @@ -184,6 +185,14 @@ impl RollingMatcher { } } + // If there is no previous state, we handle it as a word separator, exploring a step forward + // in the state. + if !has_previous_state { + if let Some(MatcherTreeRef::Node(node)) = node.word_separators.as_ref() { + refs.extend(self.find_refs(&*node, event, true)); + } + } + refs } @@ -269,6 +278,11 @@ mod tests { ); assert_eq!(get_matches_after_str("hi", &matcher), vec![]); + // Word matches are also triggered when there is no left separator but it's a new state + assert_eq!( + get_matches_after_str("hi,", &matcher), + vec![match_result(1, "hi,")] + ); assert_eq!( get_matches_after_str(".hi,", &matcher), vec![match_result(1, ".hi,")]