diff --git a/espanso-match/src/event.rs b/espanso-match/src/event.rs index 85aa4af..10a3cbb 100644 --- a/espanso-match/src/event.rs +++ b/espanso-match/src/event.rs @@ -19,10 +19,7 @@ #[derive(Debug, Clone, PartialEq)] pub enum Event { - Key { - key: Key, - chars: Option - }, + Key { key: Key, chars: Option }, VirtualSeparator, } @@ -81,4 +78,4 @@ pub enum Key { // Others Other, -} \ No newline at end of file +} diff --git a/espanso-match/src/regex/mod.rs b/espanso-match/src/regex/mod.rs index 1ee223f..e23846a 100644 --- a/espanso-match/src/regex/mod.rs +++ b/espanso-match/src/regex/mod.rs @@ -22,8 +22,8 @@ use std::collections::HashMap; use log::error; use regex::{Regex, RegexSet}; -use crate::{MatchResult, event::Event}; use crate::Matcher; +use crate::{event::Event, MatchResult}; #[derive(Debug)] pub struct RegexMatch { @@ -92,7 +92,6 @@ where if let Some(captures) = regex.captures(&buffer) { let full_match = captures.get(0).map_or("", |m| m.as_str()); if !full_match.is_empty() { - // Now extract the captured names as variables let variables: HashMap = regex .capture_names() @@ -110,12 +109,15 @@ where } } } else { - error!("received inconsistent index from regex set with index: {}", index); + error!( + "received inconsistent index from regex set with index: {}", + index + ); } } if !matches.is_empty() { - return (RegexMatcherState::default(), matches) + return (RegexMatcherState::default(), matches); } } @@ -144,8 +146,12 @@ impl RegexMatcher { } let regex_set = RegexSet::new(&good_regexes).expect("unable to build regex set"); - - Self { ids, regex_set, regexes } + + Self { + ids, + regex_set, + regexes, + } } } @@ -155,9 +161,10 @@ mod tests { use crate::util::tests::get_matches_after_str; fn match_result(id: Id, trigger: &str, vars: &[(&str, &str)]) -> MatchResult { - let vars: HashMap = vars.iter().map(|(key, value)| { - (key.to_string(), value.to_string()) - }).collect(); + let vars: HashMap = vars + .iter() + .map(|(key, value)| (key.to_string(), value.to_string())) + .collect(); MatchResult { id, @@ -173,10 +180,22 @@ mod tests { RegexMatch::new(2, "num\\d{1,3}s"), ]); assert_eq!(get_matches_after_str("hi", &matcher), vec![]); - assert_eq!(get_matches_after_str("hello", &matcher), vec![match_result(1, "hello", &[])]); - assert_eq!(get_matches_after_str("say hello", &matcher), vec![match_result(1, "hello", &[])]); - assert_eq!(get_matches_after_str("num1s", &matcher), vec![match_result(2, "num1s", &[])]); - assert_eq!(get_matches_after_str("num134s", &matcher), vec![match_result(2, "num134s", &[])]); + assert_eq!( + get_matches_after_str("hello", &matcher), + vec![match_result(1, "hello", &[])] + ); + assert_eq!( + get_matches_after_str("say hello", &matcher), + vec![match_result(1, "hello", &[])] + ); + assert_eq!( + get_matches_after_str("num1s", &matcher), + vec![match_result(2, "num1s", &[])] + ); + assert_eq!( + get_matches_after_str("num134s", &matcher), + vec![match_result(2, "num134s", &[])] + ); assert_eq!(get_matches_after_str("nums", &matcher), vec![]); } @@ -187,8 +206,18 @@ mod tests { RegexMatch::new(2, "multi\\((?P.*?),(?P.*?)\\)"), ]); assert_eq!(get_matches_after_str("hi", &matcher), vec![]); - assert_eq!(get_matches_after_str("say hello(mary)", &matcher), vec![match_result(1, "hello(mary)", &[("name", "mary")])]); + assert_eq!( + get_matches_after_str("say hello(mary)", &matcher), + vec![match_result(1, "hello(mary)", &[("name", "mary")])] + ); assert_eq!(get_matches_after_str("hello(mary", &matcher), vec![]); - assert_eq!(get_matches_after_str("multi(mary,jane)", &matcher), vec![match_result(2, "multi(mary,jane)", &[("name1", "mary"), ("name2", "jane")])]); + assert_eq!( + get_matches_after_str("multi(mary,jane)", &matcher), + vec![match_result( + 2, + "multi(mary,jane)", + &[("name1", "mary"), ("name2", "jane")] + )] + ); } -} \ No newline at end of file +} diff --git a/espanso-match/src/rolling/matcher.rs b/espanso-match/src/rolling/matcher.rs index 1c45a90..c25c4ce 100644 --- a/espanso-match/src/rolling/matcher.rs +++ b/espanso-match/src/rolling/matcher.rs @@ -19,9 +19,16 @@ use std::collections::HashMap; -use super::{RollingMatch, tree::{MatcherTreeNode, MatcherTreeRef}, util::extract_string_from_events}; -use crate::{MatchResult, event::{Event, Key}}; +use super::{ + tree::{MatcherTreeNode, MatcherTreeRef}, + util::extract_string_from_events, + RollingMatch, +}; use crate::Matcher; +use crate::{ + event::{Event, Key}, + MatchResult, +}; use unicase::UniCase; #[derive(Clone)] @@ -76,19 +83,26 @@ where // First compute the old refs if let Some(prev_state) = prev_state { for node_path in prev_state.paths.iter() { - next_refs.extend(self.find_refs(node_path.node, &event).into_iter().map(|node_ref| { - let mut new_events = node_path.events.clone(); - new_events.push(event.clone()); - (node_ref, new_events) - })); + next_refs.extend( + self + .find_refs(node_path.node, &event) + .into_iter() + .map(|node_ref| { + let mut new_events = node_path.events.clone(); + new_events.push(event.clone()); + (node_ref, new_events) + }), + ); } } // Calculate new ones let root_refs = self.find_refs(&self.root, &event); - next_refs.extend(root_refs.into_iter().map(|node_ref| { - (node_ref, vec![event.clone()]) - })); + next_refs.extend( + root_refs + .into_iter() + .map(|node_ref| (node_ref, vec![event.clone()])), + ); let mut next_paths = Vec::new(); @@ -96,13 +110,14 @@ where match node_ref { MatcherTreeRef::Matches(matches) => { let trigger = extract_string_from_events(&events); - let results = matches.iter().map(|id| { - MatchResult { - id: id.clone(), - trigger: trigger.clone(), + let results = matches + .iter() + .map(|id| MatchResult { + id: id.clone(), + trigger: trigger.clone(), vars: HashMap::new(), - } - }).collect(); + }) + .collect(); // Reset the state and return the matches return (RollingMatcherState::default(), results); @@ -191,7 +206,7 @@ impl RollingMatcher { #[cfg(test)] mod tests { use super::*; - use crate::rolling::{StringMatchOptions}; + use crate::rolling::StringMatchOptions; use crate::util::tests::get_matches_after_str; fn match_result(id: Id, trigger: &str) -> MatchResult { @@ -217,9 +232,18 @@ mod tests { }, ); - assert_eq!(get_matches_after_str("hi", &matcher), vec![match_result(1, "hi"), match_result(5, "hi")]); - assert_eq!(get_matches_after_str("my", &matcher), vec![match_result(3, "my")]); - assert_eq!(get_matches_after_str("mmy", &matcher), vec![match_result(3, "my")]); + assert_eq!( + get_matches_after_str("hi", &matcher), + vec![match_result(1, "hi"), match_result(5, "hi")] + ); + assert_eq!( + get_matches_after_str("my", &matcher), + vec![match_result(3, "my")] + ); + assert_eq!( + get_matches_after_str("mmy", &matcher), + vec![match_result(3, "my")] + ); assert_eq!(get_matches_after_str("invalid", &matcher), vec![]); } @@ -245,7 +269,10 @@ mod tests { ); assert_eq!(get_matches_after_str("hi", &matcher), vec![]); - 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,")] + ); } #[test] @@ -277,11 +304,26 @@ mod tests { }, ); - 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")]); - assert_eq!(get_matches_after_str("HI", &matcher), vec![match_result(1, "HI")]); - assert_eq!(get_matches_after_str("arty", &matcher), vec![match_result(3, "arty")]); - assert_eq!(get_matches_after_str("arTY", &matcher), vec![match_result(3, "arTY")]); + 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")] + ); + assert_eq!( + get_matches_after_str("HI", &matcher), + vec![match_result(1, "HI")] + ); + assert_eq!( + get_matches_after_str("arty", &matcher), + vec![match_result(3, "arty")] + ); + assert_eq!( + get_matches_after_str("arTY", &matcher), + vec![match_result(3, "arTY")] + ); assert_eq!(get_matches_after_str("ARTY", &matcher), vec![]); } } diff --git a/espanso-match/src/rolling/mod.rs b/espanso-match/src/rolling/mod.rs index 71abf33..3936f61 100644 --- a/espanso-match/src/rolling/mod.rs +++ b/espanso-match/src/rolling/mod.rs @@ -119,10 +119,14 @@ mod tests { #[test] fn test_match_from_string_left_word() { assert_eq!( - RollingMatch::from_string(1, "test", &StringMatchOptions { - left_word: true, - ..Default::default() - }), + RollingMatch::from_string( + 1, + "test", + &StringMatchOptions { + left_word: true, + ..Default::default() + } + ), RollingMatch { id: 1, items: vec![ @@ -139,10 +143,14 @@ mod tests { #[test] fn test_match_from_string_right_word() { assert_eq!( - RollingMatch::from_string(1, "test", &StringMatchOptions { - right_word: true, - ..Default::default() - }), + RollingMatch::from_string( + 1, + "test", + &StringMatchOptions { + right_word: true, + ..Default::default() + } + ), RollingMatch { id: 1, items: vec![ @@ -159,10 +167,14 @@ mod tests { #[test] fn test_match_from_string_case_insensitive() { assert_eq!( - RollingMatch::from_string(1, "test", &StringMatchOptions { - case_insensitive: true, - ..Default::default() - }), + RollingMatch::from_string( + 1, + "test", + &StringMatchOptions { + case_insensitive: true, + ..Default::default() + } + ), RollingMatch { id: 1, items: vec![ @@ -178,11 +190,15 @@ mod tests { #[test] fn test_match_from_string_preserve_case_markers() { assert_eq!( - RollingMatch::from_string(1, "test", &StringMatchOptions { - case_insensitive: true, - preserve_case_markers: true, - ..Default::default() - }), + RollingMatch::from_string( + 1, + "test", + &StringMatchOptions { + case_insensitive: true, + preserve_case_markers: true, + ..Default::default() + } + ), RollingMatch { id: 1, items: vec![ diff --git a/espanso-match/src/rolling/util.rs b/espanso-match/src/rolling/util.rs index f8c86c6..a5a84b7 100644 --- a/espanso-match/src/rolling/util.rs +++ b/espanso-match/src/rolling/util.rs @@ -41,33 +41,84 @@ mod tests { #[test] fn extract_string_from_events_all_chars() { - assert_eq!(extract_string_from_events(&[ - Event::Key { key: Key::Other, chars: Some("h".to_string()) }, - Event::Key { key: Key::Other, chars: Some("e".to_string()) }, - Event::Key { key: Key::Other, chars: Some("l".to_string()) }, - Event::Key { key: Key::Other, chars: Some("l".to_string()) }, - Event::Key { key: Key::Other, chars: Some("o".to_string()) }, - ]), "hello"); + assert_eq!( + extract_string_from_events(&[ + Event::Key { + key: Key::Other, + chars: Some("h".to_string()) + }, + Event::Key { + key: Key::Other, + chars: Some("e".to_string()) + }, + Event::Key { + key: Key::Other, + chars: Some("l".to_string()) + }, + Event::Key { + key: Key::Other, + chars: Some("l".to_string()) + }, + Event::Key { + key: Key::Other, + chars: Some("o".to_string()) + }, + ]), + "hello" + ); } #[test] fn extract_string_from_events_no_chars() { - assert_eq!(extract_string_from_events(&[ - Event::Key { key: Key::ArrowUp, chars: None }, - Event::Key { key: Key::ArrowUp, chars: None }, - Event::Key { key: Key::ArrowUp, chars: None }, - ]), ""); + assert_eq!( + extract_string_from_events(&[ + Event::Key { + key: Key::ArrowUp, + chars: None + }, + Event::Key { + key: Key::ArrowUp, + chars: None + }, + Event::Key { + key: Key::ArrowUp, + chars: None + }, + ]), + "" + ); } #[test] fn extract_string_from_events_mixed() { - assert_eq!(extract_string_from_events(&[ - Event::Key { key: Key::Other, chars: Some("h".to_string()) }, - Event::Key { key: Key::Other, chars: Some("e".to_string()) }, - Event::Key { key: Key::Other, chars: Some("l".to_string()) }, - Event::Key { key: Key::Other, chars: Some("l".to_string()) }, - Event::Key { key: Key::Other, chars: Some("o".to_string()) }, - Event::Key { key: Key::ArrowUp, chars: None }, - ]), "hello"); + assert_eq!( + extract_string_from_events(&[ + Event::Key { + key: Key::Other, + chars: Some("h".to_string()) + }, + Event::Key { + key: Key::Other, + chars: Some("e".to_string()) + }, + Event::Key { + key: Key::Other, + chars: Some("l".to_string()) + }, + Event::Key { + key: Key::Other, + chars: Some("l".to_string()) + }, + Event::Key { + key: Key::Other, + chars: Some("o".to_string()) + }, + Event::Key { + key: Key::ArrowUp, + chars: None + }, + ]), + "hello" + ); } -} \ No newline at end of file +} diff --git a/espanso-match/src/util.rs b/espanso-match/src/util.rs index 08ee612..be9e62d 100644 --- a/espanso-match/src/util.rs +++ b/espanso-match/src/util.rs @@ -19,9 +19,15 @@ #[cfg(test)] pub(crate) mod tests { - use crate::{MatchResult, Matcher, event::{Event, Key}}; + use crate::{ + event::{Event, Key}, + MatchResult, Matcher, + }; - pub(crate) fn get_matches_after_str<'a, Id: Clone, S, M: Matcher<'a, S, Id>>(string: &str, matcher: &'a M) -> Vec> { + pub(crate) fn get_matches_after_str<'a, Id: Clone, S, M: Matcher<'a, S, Id>>( + string: &str, + matcher: &'a M, + ) -> Vec> { let mut prev_state = None; let mut matches = Vec::new(); @@ -40,4 +46,4 @@ pub(crate) mod tests { matches } -} \ No newline at end of file +}