fix(switcher): rank initials matches like cw first
Word-start hits no longer pay a distance penalty, a second pass prefers word starts over the first occurrence, and ties go to the shorter title. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -8,23 +8,38 @@ pub fn score(query: &str, candidate: &str) -> Option<i64> {
8
8
return Some(0);
9
9
}
10
10
let lowered: Vec<char> = candidate.to_lowercase().chars().collect();
11
score_plain(&query, &lowered)
11
let plain = score_plain(&query, &lowered, false);
12
let word_starts = score_plain(&query, &lowered, true);
13
plain.max(word_starts)
12
14
}
13
15
14
fn score_plain(query: &[char], candidate: &[char]) -> Option<i64> {
16
fn is_word_start(candidate: &[char], index: usize) -> bool {
17
index == 0 || !candidate[index - 1].is_alphanumeric()
18
}
19
20
fn score_plain(query: &[char], candidate: &[char], prefer_word_starts: bool) -> Option<i64> {
15
21
let mut score = 0i64;
16
22
let mut position = 0usize;
17
23
let mut previous_match: Option<usize> = None;
18
24
for &wanted in query {
19
let found = candidate[position..].iter().position(|&c| c == wanted)? + position;
25
let mut occurrences =
26
(position..candidate.len()).filter(|&index| candidate[index] == wanted);
27
let found = if prefer_word_starts {
28
(position..candidate.len())
29
.find(|&index| candidate[index] == wanted && is_word_start(candidate, index))
30
.or_else(|| occurrences.next())?
31
} else {
32
occurrences.next()?
33
};
20
34
score += 10;
21
35
if previous_match.is_some_and(|previous| previous + 1 == found) {
22
36
score += 15;
23
37
}
24
if found == 0 || !candidate[found - 1].is_alphanumeric() {
38
if is_word_start(candidate, found) {
25
39
score += 20;
40
} else {
41
score -= (found - previous_match.map_or(0, |previous| previous + 1)) as i64;
26
42
}
27
score -= (found - previous_match.map_or(0, |previous| previous + 1)) as i64;
28
43
previous_match = Some(found);
29
44
position = found + 1;
30
45
}
@@ -32,16 +47,24 @@ fn score_plain(query: &[char], candidate: &[char]) -> Option<i64> {
32
47
}
33
48
34
49
pub fn rank<T: Clone>(query: &str, items: &[(String, T)], limit: usize) -> Vec<T> {
35
let mut scored: Vec<(i64, usize)> = items
50
let mut scored: Vec<(i64, usize, usize)> = items
36
51
.iter()
37
52
.enumerate()
38
.filter_map(|(index, (label, _))| score(query, label).map(|score| (score, index)))
53
.filter_map(|(index, (label, _))| {
54
score(query, label).map(|score| (score, label.chars().count(), index))
55
})
39
56
.collect();
40
scored.sort_by(|left, right| right.0.cmp(&left.0).then(left.1.cmp(&right.1)));
57
scored.sort_by(|left, right| {
58
right
59
.0
60
.cmp(&left.0)
61
.then(left.1.cmp(&right.1))
62
.then(left.2.cmp(&right.2))
63
});
41
64
scored
42
65
.into_iter()
43
66
.take(limit)
44
.map(|(_, index)| items[index].1.clone())
67
.map(|(_, _, index)| items[index].1.clone())
45
68
.collect()
46
69
}
47
70
@@ -75,6 +98,16 @@ mod tests {
75
98
assert_eq!(rank("a", &items, 1).len(), 1);
76
99
}
77
100
101
#[test]
102
fn initials_beat_a_contiguous_match_inside_a_word() {
103
let items = vec![
104
("QA cw review".to_owned(), "meeting"),
105
("Ada Lovelace, Christoph Wiechert".to_owned(), "group"),
106
("Christoph Wiechert".to_owned(), "person"),
107
];
108
assert_eq!(rank("cw", &items, 3), ["person", "group", "meeting"]);
109
}
110
78
111
#[test]
79
112
fn case_is_ignored() {
80
113
assert!(score("ALPHA", "alpha").is_some());
@@ -8,23 +8,38 @@ pub fn score(query: &str, candidate: &str) -> Option<i64> {8
return Some(0);8
return Some(0);9
}9
}10
let lowered: Vec<char> = candidate.to_lowercase().chars().collect();10
let lowered: Vec<char> = candidate.to_lowercase().chars().collect();11
score_plain(&query, &lowered)11
let plain = score_plain(&query, &lowered, false);12
let word_starts = score_plain(&query, &lowered, true);13
plain.max(word_starts)12
}14
}13
15
14
fn score_plain(query: &[char], candidate: &[char]) -> Option<i64> {16
fn is_word_start(candidate: &[char], index: usize) -> bool {17
index == 0 || !candidate[index - 1].is_alphanumeric()18
}19
20
fn score_plain(query: &[char], candidate: &[char], prefer_word_starts: bool) -> Option<i64> {15
let mut score = 0i64;21
let mut score = 0i64;16
let mut position = 0usize;22
let mut position = 0usize;17
let mut previous_match: Option<usize> = None;23
let mut previous_match: Option<usize> = None;18
for &wanted in query {24
for &wanted in query {19
let found = candidate[position..].iter().position(|&c| c == wanted)? + position;25
let mut occurrences =26
(position..candidate.len()).filter(|&index| candidate[index] == wanted);27
let found = if prefer_word_starts {28
(position..candidate.len())29
.find(|&index| candidate[index] == wanted && is_word_start(candidate, index))30
.or_else(|| occurrences.next())?31
} else {32
occurrences.next()?33
};20
score += 10;34
score += 10;21
if previous_match.is_some_and(|previous| previous + 1 == found) {35
if previous_match.is_some_and(|previous| previous + 1 == found) {22
score += 15;36
score += 15;23
}37
}24
if found == 0 || !candidate[found - 1].is_alphanumeric() {38
if is_word_start(candidate, found) {25
score += 20;39
score += 20;40
} else {41
score -= (found - previous_match.map_or(0, |previous| previous + 1)) as i64;26
}42
}27
score -= (found - previous_match.map_or(0, |previous| previous + 1)) as i64;28
previous_match = Some(found);43
previous_match = Some(found);29
position = found + 1;44
position = found + 1;30
}45
}@@ -32,16 +47,24 @@ fn score_plain(query: &[char], candidate: &[char]) -> Option<i64> {32
}47
}33
48
34
pub fn rank<T: Clone>(query: &str, items: &[(String, T)], limit: usize) -> Vec<T> {49
pub fn rank<T: Clone>(query: &str, items: &[(String, T)], limit: usize) -> Vec<T> {35
let mut scored: Vec<(i64, usize)> = items50
let mut scored: Vec<(i64, usize, usize)> = items36
.iter()51
.iter()37
.enumerate()52
.enumerate()38
.filter_map(|(index, (label, _))| score(query, label).map(|score| (score, index)))53
.filter_map(|(index, (label, _))| {54
score(query, label).map(|score| (score, label.chars().count(), index))55
})39
.collect();56
.collect();40
scored.sort_by(|left, right| right.0.cmp(&left.0).then(left.1.cmp(&right.1)));57
scored.sort_by(|left, right| {58
right59
.060
.cmp(&left.0)61
.then(left.1.cmp(&right.1))62
.then(left.2.cmp(&right.2))63
});41
scored64
scored42
.into_iter()65
.into_iter()43
.take(limit)66
.take(limit)44
.map(|(_, index)| items[index].1.clone())67
.map(|(_, _, index)| items[index].1.clone())45
.collect()68
.collect()46
}69
}47
70
@@ -75,6 +98,16 @@ mod tests {75
assert_eq!(rank("a", &items, 1).len(), 1);98
assert_eq!(rank("a", &items, 1).len(), 1);76
}99
}77
100
101
#[test]102
fn initials_beat_a_contiguous_match_inside_a_word() {103
let items = vec![104
("QA cw review".to_owned(), "meeting"),105
("Ada Lovelace, Christoph Wiechert".to_owned(), "group"),106
("Christoph Wiechert".to_owned(), "person"),107
];108
assert_eq!(rank("cw", &items, 3), ["person", "group", "meeting"]);109
}110
78
#[test]111
#[test]79
fn case_is_ignored() {112
fn case_is_ignored() {80
assert!(score("ALPHA", "alpha").is_some());113
assert!(score("ALPHA", "alpha").is_some());