feat(modulo): implement additional search items. #789

This commit is contained in:
Federico Terzi 2022-01-04 21:17:09 +01:00
parent 35ed59bd23
commit 90be91d1e9
4 changed files with 14 additions and 1 deletions

View File

@ -44,7 +44,9 @@ fn exact_match(query: &str, items: &[SearchItem]) -> Vec<usize> {
.iter() .iter()
.enumerate() .enumerate()
.filter(|(_, item)| { .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) .map(|(i, _)| i)
.collect() .collect()
@ -61,6 +63,10 @@ fn case_insensitive_exact_match(query: &str, items: &[SearchItem]) -> Vec<usize>
.trigger .trigger
.as_deref() .as_deref()
.map_or(false, |t| t.to_lowercase().contains(query)) .map_or(false, |t| t.to_lowercase().contains(query))
|| item
.search_terms
.iter()
.any(|term| term.to_lowercase().contains(&lowercase_query))
}) })
.map(|(i, _)| i) .map(|(i, _)| i)
.collect() .collect()
@ -79,6 +85,10 @@ fn case_insensitive_keyword(query: &str, items: &[SearchItem]) -> Vec<usize> {
.trigger .trigger
.as_deref() .as_deref()
.map_or(false, |t| t.to_lowercase().contains(keyword)) .map_or(false, |t| t.to_lowercase().contains(keyword))
&& !item
.search_terms
.iter()
.any(|term| term.to_lowercase().contains(keyword))
{ {
return false; return false;
} }

View File

@ -58,6 +58,7 @@ pub struct SearchItem {
pub id: String, pub id: String,
pub label: String, pub label: String,
pub trigger: Option<String>, pub trigger: Option<String>,
pub search_terms: Vec<String>,
#[serde(default)] #[serde(default)]
pub is_builtin: bool, pub is_builtin: bool,

View File

@ -28,6 +28,7 @@ pub fn generate(config: SearchConfig) -> types::Search {
id: item.id, id: item.id,
label: item.label, label: item.label,
trigger: item.trigger, trigger: item.trigger,
search_terms: item.search_terms,
is_builtin: item.is_builtin, is_builtin: item.is_builtin,
}) })
.collect(); .collect();

View File

@ -26,6 +26,7 @@ pub mod types {
pub id: String, pub id: String,
pub label: String, pub label: String,
pub trigger: Option<String>, pub trigger: Option<String>,
pub search_terms: Vec<String>,
pub is_builtin: bool, pub is_builtin: bool,
} }