From 638d7c26486d6c88261cabc86d694619444cceba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?= Date: Sun, 24 Apr 2022 20:11:26 +0200 Subject: [PATCH] Sort fuzzy_matcher results based on score (#1183) Co-authored-by: Stephan D <776816+extrawurst@users.noreply.github.com> --- CHANGELOG.md | 1 + src/components/file_find_popup.rs | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 020202a5..86fe866f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * tabs indentation in blame [[@fersilva16](https://github.com/fersilva16)] ([#1111](https://github.com/extrawurst/gitui/issues/1117)) * switch focus to index after staging last file ([#1169](https://github.com/extrawurst/gitui/pull/1169)) * fix stashlist multi marking not updated after dropping ([#1207](https://github.com/extrawurst/gitui/pull/1207)) +* exact matches have a higher priority and are placed to the top of the list when fuzzily finding files ([#1183](https://github.com/extrawurst/gitui/pull/1183)) ## [0.20.1] - 2021-01-26 diff --git a/src/components/file_find_popup.rs b/src/components/file_find_popup.rs index 530aaae9..b35c089b 100644 --- a/src/components/file_find_popup.rs +++ b/src/components/file_find_popup.rs @@ -88,15 +88,18 @@ impl FileFindPopup { let matcher = fuzzy_matcher::skim::SkimMatcherV2::default(); - self.files_filtered.extend( + let mut files = self.files.iter().enumerate().filter_map(|a| { a.1.path.to_str().and_then(|path| { matcher .fuzzy_indices(path, q) - .map(|(_, indicies)| (a.0, indicies)) + .map(|(score, indices)| (score, a.0, indices)) }) - }), - ); + }).collect::>(); + + files.sort_by(|(score1, _, _), (score2, _, _)| score2.cmp(score1)); + + self.files_filtered.extend(files.into_iter().map(|entry| (entry.1, entry.2))); } self.selection = 0;