Skip to content

Commit 59f8621

Browse files
committed
Use struct for fuzzy matches instead of 4-tuple
1 parent 0c77f8d commit 59f8621

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

crates/nu-cli/src/completions/completion_options.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,17 @@ enum State<T> {
4848
Fuzzy {
4949
matcher: Matcher,
5050
atom: Atom,
51-
/// Holds (haystack, item, score, match_indices)
52-
items: Vec<(String, T, u16, Vec<usize>)>,
51+
matches: Vec<FuzzyMatch<T>>,
5352
},
5453
}
5554

55+
struct FuzzyMatch<T> {
56+
item: T,
57+
haystack: String,
58+
score: u16,
59+
match_indices: Vec<usize>,
60+
}
61+
5662
const QUOTES: [char; 3] = ['"', '\'', '`'];
5763

5864
/// Filters and sorts suggestions
@@ -111,7 +117,7 @@ impl<T> NuMatcher<'_, T> {
111117
cfg
112118
}),
113119
atom,
114-
items: Vec::new(),
120+
matches: Vec::new(),
115121
},
116122
}
117123
}
@@ -154,7 +160,7 @@ impl<T> NuMatcher<'_, T> {
154160
State::Fuzzy {
155161
matcher,
156162
atom,
157-
items,
163+
matches: items,
158164
} => {
159165
let mut haystack_buf = Vec::new();
160166
let haystack_utf32 = Utf32Str::new(haystack, &mut haystack_buf);
@@ -171,7 +177,12 @@ impl<T> NuMatcher<'_, T> {
171177
.expect("should be on at least a 32-bit system")
172178
})
173179
.collect();
174-
items.push((haystack.to_string(), item, score, indices));
180+
items.push(FuzzyMatch {
181+
item,
182+
haystack: haystack.to_string(),
183+
score,
184+
match_indices: indices,
185+
});
175186
}
176187
true
177188
}
@@ -205,16 +216,12 @@ impl<T> NuMatcher<'_, T> {
205216
}
206217
});
207218
}
208-
State::Fuzzy { items, .. } => match self.options.sort {
219+
State::Fuzzy { matches: items, .. } => match self.options.sort {
209220
CompletionSort::Alphabetical => {
210-
items.sort_by(|(haystack1, _, _, _), (haystack2, _, _, _)| {
211-
haystack1.cmp(haystack2)
212-
});
221+
items.sort_by(|a, b| a.haystack.cmp(&b.haystack));
213222
}
214223
CompletionSort::Smart => {
215-
items.sort_by(|(haystack1, _, score1, _), (haystack2, _, score2, _)| {
216-
score2.cmp(score1).then(haystack1.cmp(haystack2))
217-
});
224+
items.sort_by(|a, b| b.score.cmp(&a.score).then(a.haystack.cmp(&b.haystack)));
218225
}
219226
},
220227
}
@@ -227,9 +234,9 @@ impl<T> NuMatcher<'_, T> {
227234
.into_iter()
228235
.map(|(_, item)| (item, None))
229236
.collect::<Vec<_>>(),
230-
State::Fuzzy { items, .. } => items
237+
State::Fuzzy { matches: items, .. } => items
231238
.into_iter()
232-
.map(|(_, item, _, indices)| (item, Some(indices)))
239+
.map(|mat| (mat.item, Some(mat.match_indices)))
233240
.collect::<Vec<_>>(),
234241
}
235242
}

0 commit comments

Comments
 (0)