Skip to content

Commit

Permalink
sort fuzzy matches with equal score by length in picker
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalkuthe committed Nov 13, 2022
1 parent 758bace commit 0209c0d
Showing 1 changed file with 55 additions and 27 deletions.
82 changes: 55 additions & 27 deletions helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ use tui::{
use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
use tui::widgets::Widget;

use std::time::Instant;
use std::{cmp::Ordering, time::Instant};
use std::{
cmp::Reverse,
collections::HashMap,
io::Read,
path::{Path, PathBuf},
Expand Down Expand Up @@ -309,13 +308,34 @@ impl<T: Item + 'static> Component for FilePicker<T> {
}
}

#[derive(PartialEq, Eq, Debug)]
struct PickerMatch {
index: usize,
score: i64,
len: usize,
}

impl PartialOrd for PickerMatch {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for PickerMatch {
fn cmp(&self, other: &Self) -> Ordering {
self.score
.cmp(&other.score)
.reverse()
.then_with(|| self.len.cmp(&other.len))
}
}

pub struct Picker<T: Item> {
options: Vec<T>,
editor_data: T::Data,
// filter: String,
matcher: Box<Matcher>,
/// (index, score)
matches: Vec<(usize, i64)>,
matches: Vec<PickerMatch>,

/// Current height of the completions box
completion_height: u16,
Expand Down Expand Up @@ -361,13 +381,16 @@ impl<T: Item> Picker<T> {

// scoring on empty input:
// TODO: just reuse score()
picker.matches.extend(
picker
.options
.iter()
.enumerate()
.map(|(index, _option)| (index, 0)),
);
picker
.matches
.extend(picker.options.iter().enumerate().map(|(index, option)| {
let text = option.filter_text(&picker.editor_data);
PickerMatch {
index,
score: 0,
len: text.chars().count(),
}
}));

picker
}
Expand All @@ -384,32 +407,34 @@ impl<T: Item> Picker<T> {
if pattern.is_empty() {
// Fast path for no pattern.
self.matches.clear();
self.matches.extend(
self.options
.iter()
.enumerate()
.map(|(index, _option)| (index, 0)),
);
self.matches
.extend(self.options.iter().enumerate().map(|(index, option)| {
let text = option.filter_text(&self.editor_data);
PickerMatch {
index,
score: 0,
len: text.chars().count(),
}
}));
} else if pattern.starts_with(&self.previous_pattern) {
let query = FuzzyQuery::new(pattern);
// optimization: if the pattern is a more specific version of the previous one
// then we can score the filtered set.
self.matches.retain_mut(|(index, score)| {
let option = &self.options[*index];
self.matches.retain_mut(|pmatch| {
let option = &self.options[pmatch.index];
let text = option.sort_text(&self.editor_data);

match query.fuzzy_match(&text, &self.matcher) {
Some(s) => {
// Update the score
*score = s;
pmatch.score = s;
true
}
None => false,
}
});

self.matches
.sort_unstable_by_key(|(_, score)| Reverse(*score));
self.matches.sort_unstable();
} else {
let query = FuzzyQuery::new(pattern);
self.matches.clear();
Expand All @@ -422,11 +447,14 @@ impl<T: Item> Picker<T> {

query
.fuzzy_match(&text, &self.matcher)
.map(|score| (index, score))
.map(|score| PickerMatch {
index,
score,
len: text.chars().count(),
})
}),
);
self.matches
.sort_unstable_by_key(|(_, score)| Reverse(*score));
self.matches.sort_unstable();
}

log::debug!("picker score {:?}", Instant::now().duration_since(now));
Expand Down Expand Up @@ -478,7 +506,7 @@ impl<T: Item> Picker<T> {
pub fn selection(&self) -> Option<&T> {
self.matches
.get(self.cursor)
.map(|(index, _score)| &self.options[*index])
.map(|pmatch| &self.options[pmatch.index])
}

pub fn toggle_preview(&mut self) {
Expand Down Expand Up @@ -625,7 +653,7 @@ impl<T: Item + 'static> Component for Picker<T> {
.matches
.iter()
.skip(offset)
.map(|(index, _score)| (*index, self.options.get(*index).unwrap()));
.map(|pmatch| (pmatch.index, self.options.get(pmatch.index).unwrap()));

for (i, (_index, option)) in files.take(rows as usize).enumerate() {
let is_active = i == (self.cursor - offset);
Expand Down

0 comments on commit 0209c0d

Please sign in to comment.