on master help sign in
Move scoring functions from Picker to FilePicker
commit7a058c73617bb827a57dd635bba798a972809503 authorGokul Soumya <gokulps15@gmail.com> dateSun 18 Jun 2023, 12:05 (3 years ago) parent104036b
1 file changed+80−72
helix-term/src/ui/picker.rs+80−72
helix-term/src/ui/picker.rs+80−72
@@ -234,6 +234,84 @@ impl<T: Item + 'static> FilePicker<T> {
234234 .collect();
235235 }
236236
237+ pub fn score(&mut self) {
238+ let pattern = self.prompt.line();
239+
240+ if pattern == &self.previous_pattern.0 {
241+ return;
242+ }
243+
244+ let (query, is_refined) = self
245+ .previous_pattern
246+ .1
247+ .refine(pattern, &self.previous_pattern.0);
248+
249+ if pattern.is_empty() {
250+ // Fast path for no pattern.
251+ self.matches.clear();
252+ self.matches
253+ .extend(self.options.iter().enumerate().map(|(index, option)| {
254+ let text = option.filter_text(&self.editor_data);
255+ PickerMatch {
256+ index,
257+ score: 0,
258+ len: text.chars().count(),
259+ }
260+ }));
261+ } else if is_refined {
262+ // optimization: if the pattern is a more specific version of the previous one
263+ // then we can score the filtered set.
264+ self.matches.retain_mut(|pmatch| {
265+ let option = &self.options[pmatch.index];
266+ let text = option.sort_text(&self.editor_data);
267+
268+ match query.fuzzy_match(&text, &self.matcher) {
269+ Some(s) => {
270+ // Update the score
271+ pmatch.score = s;
272+ true
273+ }
274+ None => false,
275+ }
276+ });
277+
278+ self.matches.sort_unstable();
279+ } else {
280+ self.force_score();
281+ }
282+
283+ // reset cursor position
284+ self.cursor = 0;
285+ let pattern = self.prompt.line();
286+ self.previous_pattern.0.clone_from(pattern);
287+ self.previous_pattern.1 = query;
288+ }
289+
290+ pub fn force_score(&mut self) {
291+ let pattern = self.prompt.line();
292+
293+ let query = FuzzyQuery::new(pattern);
294+ self.matches.clear();
295+ self.matches.extend(
296+ self.options
297+ .iter()
298+ .enumerate()
299+ .filter_map(|(index, option)| {
300+ let text = option.filter_text(&self.editor_data);
301+
302+ query
303+ .fuzzy_match(&text, &self.matcher)
304+ .map(|score| PickerMatch {
305+ index,
306+ score,
307+ len: text.chars().count(),
308+ })
309+ }),
310+ );
311+
312+ self.matches.sort_unstable();
313+ }
314+
237315 fn current_file(&self, editor: &Editor) -> Option<FileLocation> {
238316 self.picker
239317 .selection()
@@ -569,81 +647,11 @@ impl<T: Item> Picker<T> {
569647 }
570648
571649 pub fn score(&mut self) {
572− let pattern = self.prompt.line();
573−
574− if pattern == &self.previous_pattern.0 {
575− return;
576− }
577−
578− let (query, is_refined) = self
579− .previous_pattern
580− .1
581− .refine(pattern, &self.previous_pattern.0);
582−
583− if pattern.is_empty() {
584− // Fast path for no pattern.
585− self.matches.clear();
586− self.matches
587− .extend(self.options.iter().enumerate().map(|(index, option)| {
588− let text = option.filter_text(&self.editor_data);
589− PickerMatch {
590− index,
591− score: 0,
592− len: text.chars().count(),
593− }
594− }));
595− } else if is_refined {
596− // optimization: if the pattern is a more specific version of the previous one
597− // then we can score the filtered set.
598− self.matches.retain_mut(|pmatch| {
599− let option = &self.options[pmatch.index];
600− let text = option.sort_text(&self.editor_data);
601−
602− match query.fuzzy_match(&text, &self.matcher) {
603− Some(s) => {
604− // Update the score
605− pmatch.score = s;
606− true
607− }
608− None => false,
609− }
610− });
611−
612− self.matches.sort_unstable();
613− } else {
614− self.force_score();
615− }
616−
617− // reset cursor position
618− self.cursor = 0;
619− let pattern = self.prompt.line();
620− self.previous_pattern.0.clone_from(pattern);
621− self.previous_pattern.1 = query;
650+ unimplemented!()
622651 }
623652
624653 pub fn force_score(&mut self) {
625− let pattern = self.prompt.line();
626−
627− let query = FuzzyQuery::new(pattern);
628− self.matches.clear();
629− self.matches.extend(
630− self.options
631− .iter()
632− .enumerate()
633− .filter_map(|(index, option)| {
634− let text = option.filter_text(&self.editor_data);
635−
636− query
637− .fuzzy_match(&text, &self.matcher)
638− .map(|score| PickerMatch {
639− index,
640− score,
641− len: text.chars().count(),
642− })
643− }),
644− );
645−
646− self.matches.sort_unstable();
654+ unimplemented!()
647655 }
648656
649657 /// Move the cursor by a number of lines, either down (`Forward`) or up (`Backward`)