diff options
author | Gokul Soumya | 2023-06-18 17:05:17 +0000 |
---|---|---|
committer | Michael Davis | 2023-06-18 17:05:17 +0000 |
commit | 7a058c73617bb827a57dd635bba798a972809503 (patch) | |
tree | 7250459ca55509a9c60eb869090fe9bca0aa9175 | |
parent | 104036bd7feed66b6f5d2467d5dbf1fbaa5ff9c3 (diff) |
Move scoring functions from Picker to FilePicker
-rw-r--r-- | helix-term/src/ui/picker.rs | 152 |
1 files changed, 80 insertions, 72 deletions
diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index d1f6d45e..b7f593a8 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -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() @@ -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`) |