mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
refactor: cd completion logic
This commit is contained in:
parent
af7377aab4
commit
db75113bb4
1 changed files with 24 additions and 29 deletions
|
|
@ -39,40 +39,35 @@ impl Completion {
|
||||||
let candidate_size = 30;
|
let candidate_size = 30;
|
||||||
|
|
||||||
// prioritize those with exact prefix
|
// prioritize those with exact prefix
|
||||||
let exact_flow = cache.iter().try_fold(Vec::with_capacity(candidate_size), |mut v, s| {
|
let candidates = cache.iter().try_fold(
|
||||||
if s.starts_with(opt.word) && s != opt.word {
|
(Vec::with_capacity(candidate_size), Vec::with_capacity(candidate_size)),
|
||||||
v.push(s.to_owned());
|
|(mut prefix_cand, mut fuzzy_cand), s| {
|
||||||
if v.len() >= candidate_size {
|
if s.starts_with(opt.word) {
|
||||||
return ControlFlow::Break(v);
|
if s != opt.word {
|
||||||
|
prefix_cand.push(s.to_owned());
|
||||||
|
if prefix_cand.len() >= candidate_size {
|
||||||
|
return ControlFlow::Break((prefix_cand, fuzzy_cand));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ControlFlow::Continue(v)
|
} else if s.contains(opt.word) && fuzzy_cand.len() < candidate_size - prefix_cand.len() {
|
||||||
});
|
// here we don't break the control flow, since we want more exact matching.
|
||||||
let mut cand = match exact_flow {
|
fuzzy_cand.push(s.to_owned())
|
||||||
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((prefix_cand, fuzzy_cand))
|
||||||
ControlFlow::Continue(v)
|
},
|
||||||
});
|
|
||||||
cand.extend(
|
|
||||||
match fuzzy_flow {
|
|
||||||
ControlFlow::Continue(v) => v,
|
|
||||||
ControlFlow::Break(v) => v,
|
|
||||||
}
|
|
||||||
.into_iter(),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
self.ticket = opt.ticket;
|
self.ticket = opt.ticket;
|
||||||
self.cands = cand;
|
self.cands = {
|
||||||
|
let (mut prefix_cand, fuzzy_cand) = match candidates {
|
||||||
|
ControlFlow::Continue(v) => v,
|
||||||
|
ControlFlow::Break(v) => v,
|
||||||
|
};
|
||||||
|
if prefix_cand.len() < candidate_size {
|
||||||
|
prefix_cand.extend(fuzzy_cand.into_iter().take(candidate_size - prefix_cand.len()))
|
||||||
|
}
|
||||||
|
prefix_cand
|
||||||
|
};
|
||||||
if self.cands.is_empty() {
|
if self.cands.is_empty() {
|
||||||
return mem::replace(&mut self.visible, false);
|
return mem::replace(&mut self.visible, false);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue