From af7377aab46ef9f5a18f9da2aba774ffa0722d86 Mon Sep 17 00:00:00 2001 From: XOR-op <17672363+XOR-op@users.noreply.github.com> Date: Thu, 9 Nov 2023 12:03:26 -0500 Subject: [PATCH] fix: prefix matching should have high priority in completion --- yazi-core/src/completion/commands/show.rs | 34 +++++++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/yazi-core/src/completion/commands/show.rs b/yazi-core/src/completion/commands/show.rs index 204e5ab9..b6ccd4b9 100644 --- a/yazi-core/src/completion/commands/show.rs +++ b/yazi-core/src/completion/commands/show.rs @@ -36,21 +36,43 @@ impl Completion { return false; }; - let flow = cache.iter().try_fold(Vec::with_capacity(30), |mut v, s| { - if s.contains(opt.word) && s != opt.word { + let candidate_size = 30; + + // prioritize those with exact prefix + let exact_flow = cache.iter().try_fold(Vec::with_capacity(candidate_size), |mut v, s| { + if s.starts_with(opt.word) && s != opt.word { v.push(s.to_owned()); - if v.len() >= 30 { + if v.len() >= candidate_size { return ControlFlow::Break(v); } } ControlFlow::Continue(v) }); - - self.ticket = opt.ticket; - self.cands = match flow { + let mut cand = match exact_flow { ControlFlow::Continue(v) => v, ControlFlow::Break(v) => v, }; + + let fuzzy_flow = + cache.iter().try_fold(Vec::with_capacity(candidate_size - cand.len()), |mut v, s| { + if s.contains(opt.word) && !s.starts_with(opt.word) && s != opt.word { + v.push(s.to_owned()); + if v.len() >= candidate_size - cand.len() { + return ControlFlow::Break(v); + } + } + ControlFlow::Continue(v) + }); + cand.extend( + match fuzzy_flow { + ControlFlow::Continue(v) => v, + ControlFlow::Break(v) => v, + } + .into_iter(), + ); + + self.ticket = opt.ticket; + self.cands = cand; if self.cands.is_empty() { return mem::replace(&mut self.visible, false); }