Skip to content

Commit

Permalink
Move scoring functions from Picker to FilePicker
Browse files Browse the repository at this point in the history
  • Loading branch information
sudormrfbin authored and the-mikedavis committed Jun 18, 2023
1 parent 104036b commit 7a058c7
Showing 1 changed file with 80 additions and 72 deletions.
152 changes: 80 additions & 72 deletions helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,84 @@ impl<T: Item + 'static> FilePicker<T> {
.collect();
}

pub fn score(&mut self) {
let pattern = self.prompt.line();

if pattern == &self.previous_pattern.0 {
return;
}

let (query, is_refined) = self
.previous_pattern
.1
.refine(pattern, &self.previous_pattern.0);

if pattern.is_empty() {
// Fast path for no pattern.
self.matches.clear();
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 is_refined {
// optimization: if the pattern is a more specific version of the previous one
// then we can score the filtered set.
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
pmatch.score = s;
true
}
None => false,
}
});

self.matches.sort_unstable();
} else {
self.force_score();
}

// reset cursor position
self.cursor = 0;
let pattern = self.prompt.line();
self.previous_pattern.0.clone_from(pattern);
self.previous_pattern.1 = query;
}

pub fn force_score(&mut self) {
let pattern = self.prompt.line();

let query = FuzzyQuery::new(pattern);
self.matches.clear();
self.matches.extend(
self.options
.iter()
.enumerate()
.filter_map(|(index, option)| {
let text = option.filter_text(&self.editor_data);

query
.fuzzy_match(&text, &self.matcher)
.map(|score| PickerMatch {
index,
score,
len: text.chars().count(),
})
}),
);

self.matches.sort_unstable();
}

fn current_file(&self, editor: &Editor) -> Option<FileLocation> {
self.picker
.selection()
Expand Down Expand Up @@ -569,81 +647,11 @@ impl<T: Item> Picker<T> {
}

pub fn score(&mut self) {
let pattern = self.prompt.line();

if pattern == &self.previous_pattern.0 {
return;
}

let (query, is_refined) = self
.previous_pattern
.1
.refine(pattern, &self.previous_pattern.0);

if pattern.is_empty() {
// Fast path for no pattern.
self.matches.clear();
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 is_refined {
// optimization: if the pattern is a more specific version of the previous one
// then we can score the filtered set.
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
pmatch.score = s;
true
}
None => false,
}
});

self.matches.sort_unstable();
} else {
self.force_score();
}

// reset cursor position
self.cursor = 0;
let pattern = self.prompt.line();
self.previous_pattern.0.clone_from(pattern);
self.previous_pattern.1 = query;
unimplemented!()
}

pub fn force_score(&mut self) {
let pattern = self.prompt.line();

let query = FuzzyQuery::new(pattern);
self.matches.clear();
self.matches.extend(
self.options
.iter()
.enumerate()
.filter_map(|(index, option)| {
let text = option.filter_text(&self.editor_data);

query
.fuzzy_match(&text, &self.matcher)
.map(|score| PickerMatch {
index,
score,
len: text.chars().count(),
})
}),
);

self.matches.sort_unstable();
unimplemented!()
}

/// Move the cursor by a number of lines, either down (`Forward`) or up (`Backward`)
Expand Down

0 comments on commit 7a058c7

Please sign in to comment.