From 6314f0da4721cafd35734e7f3ee5fe3c295b41e2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 15 Dec 2022 14:21:53 +0000 Subject: [PATCH] Better sorting in picker in case of ties --- helix-term/src/ui/mod.rs | 3 ++- helix-term/src/ui/picker.rs | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 5b5924bff0e8..ade1d8cf4c7f 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -207,13 +207,14 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi // Cap the number of files if we aren't in a git project, preventing // hangs when using the picker in your home directory - let files: Vec<_> = if root.join(".git").exists() { + let mut files: Vec = if root.join(".git").exists() { files.collect() } else { // const MAX: usize = 8192; const MAX: usize = 100_000; files.take(MAX).collect() }; + files.sort(); log::debug!("file_picker init {:?}", Instant::now().duration_since(now)); diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 2d471aae6e8e..aad3f81ceac3 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -12,7 +12,10 @@ use tui::{ use fuzzy_matcher::skim::SkimMatcherV2 as Matcher; use tui::widgets::Widget; -use std::{cmp::Ordering, time::Instant}; +use std::{ + cmp::{self, Ordering}, + time::Instant, +}; use std::{collections::HashMap, io::Read, path::PathBuf}; use crate::ui::{Prompt, PromptEvent}; @@ -344,11 +347,17 @@ impl Component for FilePicker { #[derive(PartialEq, Eq, Debug)] struct PickerMatch { - index: usize, score: i64, + index: usize, len: usize, } +impl PickerMatch { + fn key(&self) -> impl Ord { + (cmp::Reverse(self.score), self.len, self.index) + } +} + impl PartialOrd for PickerMatch { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) @@ -357,10 +366,7 @@ impl PartialOrd for PickerMatch { impl Ord for PickerMatch { fn cmp(&self, other: &Self) -> Ordering { - self.score - .cmp(&other.score) - .reverse() - .then_with(|| self.len.cmp(&other.len)) + self.key().cmp(&other.key()) } } @@ -502,6 +508,7 @@ impl Picker { }) }), ); + self.matches.sort_unstable(); }