adjusting the best_match function and satisfying the linter

pull/11618/head
Alice C. Munduruca 3 months ago
parent c9bca100bd
commit e914584478

@ -425,44 +425,34 @@ pub mod completers {
fn best_match(opt1: &str, opt2: &str, input: &str) -> std::cmp::Ordering { fn best_match(opt1: &str, opt2: &str, input: &str) -> std::cmp::Ordering {
let min_len = std::cmp::min(std::cmp::min(opt1.len(), opt2.len()), input.len()); let min_len = std::cmp::min(std::cmp::min(opt1.len(), opt2.len()), input.len());
let mut opt1vals = (0, false); let (opt1match, opt2match) = (0..min_len)
let mut opt2vals = (0, false); .fold(((false, false), (0, 0)), |acc, i| match acc {
((false, b), (n, m)) => {
for i in 0..min_len { if opt1[..=i] == input[..=i] {
if opt1vals.1 && opt2vals.1 { ((false, b), (n + 1, m))
break;
}
if !opt1vals.1 {
if opt1[i..=i] == input[i..=i] {
opt1vals.0 += 1;
} else { } else {
opt1vals.1 = true; ((true, b), (n, m))
} }
} }
if !opt2vals.1 { ((b, false), (n, m)) => {
if opt2[i..=i] == input[i..=i] { if opt2[..=i] == input[..=i] {
opt2vals.0 += 1; ((b, false), (n, m + 1))
} else { } else {
opt2vals.1 = true; ((b, true), (n, m))
}
} }
} }
let ord = opt2vals.0.cmp(&opt1vals.0); v => v,
})
.1;
if ord == std::cmp::Ordering::Equal { match opt2match.cmp(&opt1match) {
if opt1.len() < opt2.len() { std::cmp::Ordering::Equal => opt1.len().cmp(&opt2.len()),
return std::cmp::Ordering::Less; ord => ord,
} else if opt1.len() > opt2.len() {
return std::cmp::Ordering::Greater;
} }
} }
ord
}
// TODO: we could return an iter/lazy thing so it can fetch as many as it needs. // TODO: we could return an iter/lazy thing so it can fetch as many as it needs.
fn filename_impl<F>( fn filename_impl<F>(
_editor: &Editor, _editor: &Editor,

Loading…
Cancel
Save