-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathfuzzy.rs
More file actions
104 lines (90 loc) · 3.41 KB
/
fuzzy.rs
File metadata and controls
104 lines (90 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use nucleo_matcher::{
Matcher,
pattern::{AtomKind, CaseMatching, Normalization, Pattern},
};
/// Fuzzy-match `query` against a list of strings.
///
/// Returns original indices sorted by score descending (best match first).
/// When `query` is empty, returns all indices in their original order.
#[must_use]
pub fn fuzzy_match(query: &str, items: &[&str]) -> Vec<usize> {
if query.is_empty() {
return (0..items.len()).collect();
}
let pattern = Pattern::new(query, CaseMatching::Ignore, Normalization::Smart, AtomKind::Fuzzy);
let mut matcher = Matcher::new(nucleo_matcher::Config::DEFAULT);
let mut scored: Vec<(usize, u32)> = items
.iter()
.enumerate()
.filter_map(|(idx, item)| {
pattern
.score(nucleo_matcher::Utf32Str::Ascii(item.as_bytes()), &mut matcher)
.map(|score| (idx, score))
})
.collect();
scored.sort_by_key(|b| std::cmp::Reverse(b.1));
scored.into_iter().map(|(idx, _)| idx).collect()
}
#[cfg(test)]
mod tests {
use assert2::{assert, check};
use super::*;
const TASK_NAMES: &[&str] =
&["build", "lint", "test", "app#build", "app#lint", "app#test", "lib#build"];
#[test]
fn exact_match_scores_highest() {
let results = fuzzy_match("build", TASK_NAMES);
assert!(!results.is_empty());
// "build" should be the highest-scoring match
check!(TASK_NAMES[results[0]] == "build");
}
#[test]
fn typo_matches_similar() {
let results = fuzzy_match("buid", TASK_NAMES);
assert!(!results.is_empty());
// Should match "build" and "app#build" and "lib#build" but not "lint" or "test"
let matched_names: Vec<&str> = results.iter().map(|&i| TASK_NAMES[i]).collect();
check!(matched_names.contains(&"build"));
for name in &matched_names {
check!(!name.contains("lint"));
check!(!name.contains("test"));
}
}
#[test]
fn empty_query_returns_all() {
let results = fuzzy_match("", TASK_NAMES);
check!(results.len() == TASK_NAMES.len());
// Indices should be in original order
for (pos, &idx) in results.iter().enumerate() {
check!(idx == pos);
}
}
#[test]
fn completely_unrelated_query_returns_nothing() {
let results = fuzzy_match("zzzzz", TASK_NAMES);
check!(results.is_empty());
}
#[test]
fn package_qualified_match() {
let results = fuzzy_match("app#build", TASK_NAMES);
assert!(!results.is_empty());
check!(TASK_NAMES[results[0]] == "app#build");
}
#[test]
fn lint_matches_lint_tasks() {
let results = fuzzy_match("lint", TASK_NAMES);
assert!(!results.is_empty());
let matched_names: Vec<&str> = results.iter().map(|&i| TASK_NAMES[i]).collect();
check!(matched_names.contains(&"lint"));
check!(matched_names.contains(&"app#lint"));
}
#[test]
fn score_ordering_exact_before_fuzzy() {
let results = fuzzy_match("build", TASK_NAMES);
assert!(results.len() >= 2);
// Exact "build" should appear before "app#build" (higher score = earlier position)
let build_pos = results.iter().position(|&i| TASK_NAMES[i] == "build").unwrap();
let app_build_pos = results.iter().position(|&i| TASK_NAMES[i] == "app#build").unwrap();
check!(build_pos <= app_build_pos);
}
}