Create "substring" match type

# Description
Similar behavior to zsh's history-substring-search method,
this matching algorithm will check if a given string is contained
within the other, regardless of position.
This commit is contained in:
MilesCranmer 2024-04-22 11:58:50 +01:00
parent bae6d694ca
commit b79d6c3164
No known key found for this signature in database
3 changed files with 14 additions and 1 deletions

View File

@ -24,6 +24,12 @@ pub enum MatchAlgorithm {
/// Example: /// Example:
/// "git checkout" is matched by "gco" /// "git checkout" is matched by "gco"
Fuzzy, Fuzzy,
/// Only show suggestions which contain the substring starting at any place
///
/// Example:
/// "git checkout" is matched by "check"
Substring
} }
impl MatchAlgorithm { impl MatchAlgorithm {
@ -37,6 +43,7 @@ impl MatchAlgorithm {
let matcher = SkimMatcherV2::default(); let matcher = SkimMatcherV2::default();
matcher.fuzzy_match(haystack, needle).is_some() matcher.fuzzy_match(haystack, needle).is_some()
} }
MatchAlgorithm::Substring => haystack.contains(needle),
} }
} }
@ -51,6 +58,7 @@ impl MatchAlgorithm {
let matcher = SkimMatcherV2::default(); let matcher = SkimMatcherV2::default();
matcher.fuzzy_match(&haystack_str, &needle_str).is_some() matcher.fuzzy_match(&haystack_str, &needle_str).is_some()
} }
MatchAlgorithm::Substring => haystack.windows(needle.len()).any(|window| window == needle),
} }
} }
} }
@ -60,6 +68,7 @@ impl From<CompletionAlgorithm> for MatchAlgorithm {
match value { match value {
CompletionAlgorithm::Prefix => MatchAlgorithm::Prefix, CompletionAlgorithm::Prefix => MatchAlgorithm::Prefix,
CompletionAlgorithm::Fuzzy => MatchAlgorithm::Fuzzy, CompletionAlgorithm::Fuzzy => MatchAlgorithm::Fuzzy,
CompletionAlgorithm::Substring => MatchAlgorithm::Substring,
} }
} }
} }
@ -71,6 +80,7 @@ impl TryFrom<String> for MatchAlgorithm {
match value.as_str() { match value.as_str() {
"prefix" => Ok(Self::Prefix), "prefix" => Ok(Self::Prefix),
"fuzzy" => Ok(Self::Fuzzy), "fuzzy" => Ok(Self::Fuzzy),
"substring" => Ok(Self::Substring),
_ => Err(InvalidMatchAlgorithm::Unknown), _ => Err(InvalidMatchAlgorithm::Unknown),
} }
} }

View File

@ -163,7 +163,7 @@ fn filter(
} }
} }
}, },
MatchAlgorithm::Fuzzy => options MatchAlgorithm::Fuzzy | MatchAlgorithm::Substring => options
.match_algorithm .match_algorithm
.matches_u8(it.suggestion.value.as_bytes(), prefix), .matches_u8(it.suggestion.value.as_bytes(), prefix),
}) })

View File

@ -11,6 +11,7 @@ pub enum CompletionAlgorithm {
#[default] #[default]
Prefix, Prefix,
Fuzzy, Fuzzy,
Substring,
} }
impl FromStr for CompletionAlgorithm { impl FromStr for CompletionAlgorithm {
@ -20,6 +21,7 @@ impl FromStr for CompletionAlgorithm {
match s.to_ascii_lowercase().as_str() { match s.to_ascii_lowercase().as_str() {
"prefix" => Ok(Self::Prefix), "prefix" => Ok(Self::Prefix),
"fuzzy" => Ok(Self::Fuzzy), "fuzzy" => Ok(Self::Fuzzy),
"substring" => Ok(Self::Substring),
_ => Err("expected either 'prefix' or 'fuzzy'"), _ => Err("expected either 'prefix' or 'fuzzy'"),
} }
} }
@ -30,6 +32,7 @@ impl ReconstructVal for CompletionAlgorithm {
let str = match self { let str = match self {
CompletionAlgorithm::Prefix => "prefix", CompletionAlgorithm::Prefix => "prefix",
CompletionAlgorithm::Fuzzy => "fuzzy", CompletionAlgorithm::Fuzzy => "fuzzy",
CompletionAlgorithm::Substring => "substring",
}; };
Value::string(str, span) Value::string(str, span)
} }