From 90be91d1e99aeeae5daceee1622e4024934147d6 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Tue, 4 Jan 2022 21:17:09 +0100 Subject: [PATCH] feat(modulo): implement additional search items. #789 --- espanso-modulo/src/search/algorithm.rs | 12 +++++++++++- espanso-modulo/src/search/config.rs | 1 + espanso-modulo/src/search/generator.rs | 1 + espanso-modulo/src/sys/search/mod.rs | 1 + 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/espanso-modulo/src/search/algorithm.rs b/espanso-modulo/src/search/algorithm.rs index 80c829a..9faf0be 100644 --- a/espanso-modulo/src/search/algorithm.rs +++ b/espanso-modulo/src/search/algorithm.rs @@ -44,7 +44,9 @@ fn exact_match(query: &str, items: &[SearchItem]) -> Vec { .iter() .enumerate() .filter(|(_, item)| { - item.label.contains(query) || item.trigger.as_deref().map_or(false, |t| t.contains(query)) + item.label.contains(query) + || item.trigger.as_deref().map_or(false, |t| t.contains(query)) + || item.search_terms.iter().any(|term| term.contains(query)) }) .map(|(i, _)| i) .collect() @@ -61,6 +63,10 @@ fn case_insensitive_exact_match(query: &str, items: &[SearchItem]) -> Vec .trigger .as_deref() .map_or(false, |t| t.to_lowercase().contains(query)) + || item + .search_terms + .iter() + .any(|term| term.to_lowercase().contains(&lowercase_query)) }) .map(|(i, _)| i) .collect() @@ -79,6 +85,10 @@ fn case_insensitive_keyword(query: &str, items: &[SearchItem]) -> Vec { .trigger .as_deref() .map_or(false, |t| t.to_lowercase().contains(keyword)) + && !item + .search_terms + .iter() + .any(|term| term.to_lowercase().contains(keyword)) { return false; } diff --git a/espanso-modulo/src/search/config.rs b/espanso-modulo/src/search/config.rs index 69fc542..38635fc 100644 --- a/espanso-modulo/src/search/config.rs +++ b/espanso-modulo/src/search/config.rs @@ -58,6 +58,7 @@ pub struct SearchItem { pub id: String, pub label: String, pub trigger: Option, + pub search_terms: Vec, #[serde(default)] pub is_builtin: bool, diff --git a/espanso-modulo/src/search/generator.rs b/espanso-modulo/src/search/generator.rs index 98a9c88..497d69c 100644 --- a/espanso-modulo/src/search/generator.rs +++ b/espanso-modulo/src/search/generator.rs @@ -28,6 +28,7 @@ pub fn generate(config: SearchConfig) -> types::Search { id: item.id, label: item.label, trigger: item.trigger, + search_terms: item.search_terms, is_builtin: item.is_builtin, }) .collect(); diff --git a/espanso-modulo/src/sys/search/mod.rs b/espanso-modulo/src/sys/search/mod.rs index de44836..6ed1efe 100644 --- a/espanso-modulo/src/sys/search/mod.rs +++ b/espanso-modulo/src/sys/search/mod.rs @@ -26,6 +26,7 @@ pub mod types { pub id: String, pub label: String, pub trigger: Option, + pub search_terms: Vec, pub is_builtin: bool, }