1use nucleo_matcher::pattern::{CaseMatching, Normalization, Pattern};
8use nucleo_matcher::{Config, Matcher, Utf32Str};
9
10#[must_use]
19pub fn normalize_query(query: &str) -> String {
20 let trimmed = query.trim();
21 if trimmed.is_empty() {
22 return String::new();
23 }
24
25 let mut normalized = String::with_capacity(trimmed.len());
26 let mut last_was_space = false;
27 for ch in trimmed.chars() {
28 if ch.is_whitespace() {
29 if !last_was_space && !normalized.is_empty() {
30 normalized.push(' ');
31 }
32 last_was_space = true;
33 } else {
34 normalized.extend(ch.to_lowercase());
35 last_was_space = false;
36 }
37 }
38
39 normalized.trim_end().to_owned()
40}
41
42pub struct FuzzyQuery {
51 pattern: Pattern,
52 matcher: Matcher,
53 buffer: Vec<char>,
54 empty: bool,
55}
56
57impl FuzzyQuery {
58 #[must_use]
60 pub fn new(query: &str) -> Self {
61 Self {
62 pattern: Pattern::parse(query, CaseMatching::Ignore, Normalization::Smart),
63 matcher: Matcher::new(Config::DEFAULT),
64 buffer: Vec::new(),
65 empty: query.is_empty(),
66 }
67 }
68
69 pub fn score(&mut self, candidate: &str) -> Option<u32> {
74 if self.empty {
75 return Some(0);
76 }
77 let utf32_candidate = Utf32Str::new(candidate, &mut self.buffer);
78 self.pattern.score(utf32_candidate, &mut self.matcher)
79 }
80
81 pub fn matches(&mut self, candidate: &str) -> bool {
83 if self.empty {
84 return true;
85 }
86 self.score(candidate).is_some()
87 }
88}
89
90pub fn fuzzy_match(query: &str, candidate: &str) -> bool {
93 FuzzyQuery::new(query).matches(candidate)
94}
95
96#[deprecated(note = "use vtcode_commons::search::fuzzy_match or FuzzyQuery")]
103pub fn fuzzy_subsequence(needle: &str, haystack: &str) -> bool {
104 if needle.is_empty() {
105 return true;
106 }
107
108 let mut needle_chars = needle.chars();
109 let mut current = match needle_chars.next() {
110 Some(value) => value,
111 None => return true,
112 };
113
114 for ch in haystack.chars() {
115 if ch == current {
116 match needle_chars.next() {
117 Some(next) => current = next,
118 None => return true,
119 }
120 }
121 }
122
123 false
124}
125
126pub fn fuzzy_score(query: &str, candidate: &str) -> Option<u32> {
129 FuzzyQuery::new(query).score(candidate)
130}
131
132#[cfg(test)]
133mod tests {
134 use super::*;
135
136 #[test]
137 fn normalize_query_trims_and_lowercases() {
138 let normalized = normalize_query(" Foo Bar BAZ ");
139 assert_eq!(normalized, "foo bar baz");
140 }
141
142 #[test]
143 fn normalize_query_handles_whitespace_only() {
144 assert!(normalize_query(" ").is_empty());
145 }
146
147 #[test]
148 #[allow(
149 deprecated,
150 reason = "This test pins the behavior of the deprecated compatibility shim itself."
151 )]
152 fn fuzzy_subsequence_requires_in_order_match() {
153 assert!(fuzzy_subsequence("abc", "a_b_c"));
154 assert!(!fuzzy_subsequence("abc", "acb"));
155 }
156
157 #[test]
158 fn fuzzy_match_supports_multiple_terms() {
159 assert!(fuzzy_match("run cmd", "run command"));
160 assert!(!fuzzy_match("missing", "run command"));
161 }
162
163 #[test]
164 fn fuzzy_match_with_nucleo_basic() {
165 assert!(fuzzy_match("smr", "src/main.rs"));
166 assert!(fuzzy_match("src main", "src/main.rs"));
167 assert!(fuzzy_match("main", "src/main.rs"));
168 assert!(!fuzzy_match("xyz", "src/main.rs"));
169 }
170
171 #[test]
172 fn fuzzy_score_returns_some_for_matches() {
173 assert!(fuzzy_score("smr", "src/main.rs").is_some());
174 assert!(fuzzy_score("main", "src/main.rs").is_some());
175 }
176
177 #[test]
178 fn fuzzy_score_returns_none_for_non_matches() {
179 assert!(fuzzy_score("xyz", "src/main.rs").is_none());
180 }
181
182 #[test]
183 fn fuzzy_query_reuse_matches_one_shot() {
184 let candidates = ["src/main.rs", "src/lib.rs", "docs/readme.md", "xyz"];
185 let mut query = FuzzyQuery::new("main");
186 for candidate in candidates {
187 assert_eq!(query.score(candidate), fuzzy_score("main", candidate));
188 }
189 }
190
191 #[test]
192 fn fuzzy_query_empty_matches_everything() {
193 let mut query = FuzzyQuery::new("");
194 assert_eq!(query.score("anything"), Some(0));
195 assert!(query.matches("anything"));
196 }
197}