fix(match): fix bug that prevented the matcher from detecting word matches at start

This commit is contained in:
Federico Terzi 2021-04-17 20:51:47 +02:00
parent 5c8333e307
commit dd2cc9de17

View File

@ -49,8 +49,8 @@ struct RollingMatcherStatePath<'a, Id> {
} }
pub struct RollingMatcherOptions { pub struct RollingMatcherOptions {
char_word_separators: Vec<String>, pub char_word_separators: Vec<String>,
key_word_separators: Vec<Key>, pub key_word_separators: Vec<Key>,
} }
impl Default for RollingMatcherOptions { impl Default for RollingMatcherOptions {
@ -85,7 +85,7 @@ where
for node_path in prev_state.paths.iter() { for node_path in prev_state.paths.iter() {
next_refs.extend( next_refs.extend(
self self
.find_refs(node_path.node, &event) .find_refs(node_path.node, &event, true)
.into_iter() .into_iter()
.map(|node_ref| { .map(|node_ref| {
let mut new_events = node_path.events.clone(); let mut new_events = node_path.events.clone();
@ -97,7 +97,7 @@ where
} }
// Calculate new ones // 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( next_refs.extend(
root_refs root_refs
.into_iter() .into_iter()
@ -151,6 +151,7 @@ impl<Id: Clone> RollingMatcher<Id> {
&'a self, &'a self,
node: &'a MatcherTreeNode<Id>, node: &'a MatcherTreeNode<Id>,
event: &Event, event: &Event,
has_previous_state: bool,
) -> Vec<&'a MatcherTreeRef<Id>> { ) -> Vec<&'a MatcherTreeRef<Id>> {
let mut refs = Vec::new(); let mut refs = Vec::new();
@ -184,6 +185,14 @@ impl<Id: Clone> RollingMatcher<Id> {
} }
} }
// 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 refs
} }
@ -269,6 +278,11 @@ mod tests {
); );
assert_eq!(get_matches_after_str("hi", &matcher), vec![]); 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!( assert_eq!(
get_matches_after_str(".hi,", &matcher), get_matches_after_str(".hi,", &matcher),
vec![match_result(1, ".hi,")] vec![match_result(1, ".hi,")]