on master help sign in
Better sorting in picker in case of ties (#5169)
commite6a2df8c798537a7dc5aff264eeccc773525aa6c authorAlex Kladov <aleksey.kladov@gmail.com> dateSat 17 Dec 2022, 19:30 (4 years ago) parentb12c656
2 files changed+15−7
helix-term/src/ui/mod.rs+2−1 helix-term/src/ui/picker.rs+13−6
helix-term/src/ui/mod.rs+2−1
@@ -207,13 +207,14 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
207207
208208 // Cap the number of files if we aren't in a git project, preventing
209209 // hangs when using the picker in your home directory
210− let files: Vec<_> = if root.join(".git").exists() {
210+ let mut files: Vec<PathBuf> = if root.join(".git").exists() {
211211 files.collect()
212212 } else {
213213 // const MAX: usize = 8192;
214214 const MAX: usize = 100_000;
215215 files.take(MAX).collect()
216216 };
217+ files.sort();
217218
218219 log::debug!("file_picker init {:?}", Instant::now().duration_since(now));
219220
helix-term/src/ui/picker.rs+13−6
@@ -12,7 +12,10 @@ use tui::{
1212use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
1313use tui::widgets::Widget;
1414
15−use std::{cmp::Ordering, time::Instant};
15+use std::{
16+ cmp::{self, Ordering},
17+ time::Instant,
18+};
1619use std::{collections::HashMap, io::Read, path::PathBuf};
1720
1821use crate::ui::{Prompt, PromptEvent};
@@ -344,11 +347,17 @@ impl<T: Item + 'static> Component for FilePicker<T> {
344347
345348#[derive(PartialEq, Eq, Debug)]
346349struct PickerMatch {
347− index: usize,
348350 score: i64,
351+ index: usize,
349352 len: usize,
350353}
351354
355+impl PickerMatch {
356+ fn key(&self) -> impl Ord {
357+ (cmp::Reverse(self.score), self.len, self.index)
358+ }
359+}
360+
352361impl PartialOrd for PickerMatch {
353362 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
354363 Some(self.cmp(other))
@@ -357,10 +366,7 @@ impl PartialOrd for PickerMatch {
357366
358367impl Ord for PickerMatch {
359368 fn cmp(&self, other: &Self) -> Ordering {
360− self.score
361− .cmp(&other.score)
362− .reverse()
363− .then_with(|| self.len.cmp(&other.len))
369+ self.key().cmp(&other.key())
364370 }
365371}
366372
@@ -502,6 +508,7 @@ impl<T: Item> Picker<T> {
502508 })
503509 }),
504510 );
511+
505512 self.matches.sort_unstable();
506513 }
507514