Skip to content

Commit 5b74652

Browse files
doypascalkuthe
authored andcommitted
allow reversing the input order
this doesn't actually reverse the stored order, it just adds additional sorting by the item index
1 parent a429295 commit 5b74652

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,12 @@ impl<T: Sync + Send + 'static> Nucleo<T> {
382382
self.worker.lock().sort_results(sort_results)
383383
}
384384

385+
// Set whether the matcher should reverse the order of the input.
386+
// Defaults to false.
387+
pub fn reverse_items(&mut self, reverse_items: bool) {
388+
self.worker.lock().reverse_items(reverse_items)
389+
}
390+
385391
/// The main way to interact with the matcher, this should be called
386392
/// regularly (for example each time a frame is rendered). To avoid
387393
/// excessive redraws this method will wait `timeout` milliseconds for the

src/worker.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub(crate) struct Worker<T: Sync + Send + 'static> {
3030
pub(crate) matches: Vec<Match>,
3131
pub(crate) pattern: MultiPattern,
3232
pub(crate) sort_results: bool,
33+
pub(crate) reverse_items: bool,
3334
pub(crate) canceled: Arc<AtomicBool>,
3435
pub(crate) should_notify: Arc<AtomicBool>,
3536
pub(crate) was_canceled: bool,
@@ -51,6 +52,9 @@ impl<T: Sync + Send + 'static> Worker<T> {
5152
pub(crate) fn sort_results(&mut self, sort_results: bool) {
5253
self.sort_results = sort_results;
5354
}
55+
pub(crate) fn reverse_items(&mut self, reverse_items: bool) {
56+
self.reverse_items = reverse_items;
57+
}
5458

5559
pub(crate) fn new(
5660
worker_threads: Option<usize>,
@@ -76,6 +80,7 @@ impl<T: Sync + Send + 'static> Worker<T> {
7680
// just a placeholder
7781
pattern: MultiPattern::new(cols as usize),
7882
sort_results: true,
83+
reverse_items: false,
7984
canceled: Arc::new(AtomicBool::new(false)),
8085
should_notify: Arc::new(AtomicBool::new(false)),
8186
was_canceled: false,
@@ -254,7 +259,11 @@ impl<T: Sync + Send + 'static> Worker<T> {
254259
.map(|haystack| haystack.len() as u32)
255260
.sum();
256261
if len1 == len2 {
257-
match1.idx < match2.idx
262+
if self.reverse_items {
263+
match2.idx < match1.idx
264+
} else {
265+
match1.idx < match2.idx
266+
}
258267
} else {
259268
len1 < len2
260269
}
@@ -271,7 +280,11 @@ impl<T: Sync + Send + 'static> Worker<T> {
271280
if match2.idx == u32::MAX {
272281
return true;
273282
}
274-
match1.idx < match2.idx
283+
if self.reverse_items {
284+
match2.idx < match1.idx
285+
} else {
286+
match1.idx < match2.idx
287+
}
275288
},
276289
&self.canceled,
277290
)

0 commit comments

Comments
 (0)