From f24cc6a8fc08e143cdcfe61b8bd93fa854300283 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Fri, 2 Jan 2026 07:46:45 +0100 Subject: [PATCH] reader handle_completions(): remove code clone While at it, 1. add assertions to tighten some screws 2. migrate to closed form / inline computation. --- src/reader/reader.rs | 29 +++++++---------------------- src/wcstringutil.rs | 2 +- src/wildcard.rs | 1 - 3 files changed, 8 insertions(+), 24 deletions(-) diff --git a/src/reader/reader.rs b/src/reader/reader.rs index a9a9c37ec..c3c9a4de3 100644 --- a/src/reader/reader.rs +++ b/src/reader/reader.rs @@ -6520,15 +6520,6 @@ fn reader_can_replace(s: &wstr, flags: CompleteFlags) -> bool { .any(|c| matches!(c, '$' | '*' | '?' | '(' | '{' | '}' | ')')) } -/// Determine the best (lowest) match rank for a set of completions. -fn get_best_rank(comp: &[Completion]) -> u32 { - let mut best_rank = u32::MAX; - for c in comp { - best_rank = best_rank.min(c.rank()); - } - best_rank -} - impl<'a> Reader<'a> { /// Compute completions and update the pager and/or commandline as needed. fn compute_and_apply_completions(&mut self, c: ReadlineCmd) { @@ -6687,28 +6678,22 @@ fn handle_completions(&mut self, token_range: Range, comp: Vec= best_rank); + c.rank() == best_rank + }); // Determine whether we are going to replace the token or not. If any commands of the best // rank do not require replacement, then ignore all those that want to use replacement. - let mut will_replace_token = true; - for c in comp { - if c.rank() <= best_rank && !c.replaces_token() { - will_replace_token = false; - break; - } - } + let will_replace_token = comp.clone().all(|c| c.replaces_token()); // Decide which completions survived. There may be a lot of them; it would be nice if we could // figure out how to avoid copying them here. let mut surviving_completions = vec![]; let mut all_matches_exact_or_prefix = true; for c in comp { - // Ignore completions with a less suitable match rank than the best. - if c.rank() > best_rank { - continue; - } - // Only use completions that match replace_token. let completion_replaces_token = c.replaces_token(); let replaces_only_due_to_case_mismatch = { diff --git a/src/wcstringutil.rs b/src/wcstringutil.rs index 3b005dfb4..31ca4f343 100644 --- a/src/wcstringutil.rs +++ b/src/wcstringutil.rs @@ -138,7 +138,7 @@ fn fuzzy_canonicalize(c: char) -> char { // Note that the order of entries below affects the sort order of completions. #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum ContainType { - /// Exact match: `foobar` matches `foo` + /// Exact match Exact, /// Prefix match: `foo` matches `foobar` Prefix, diff --git a/src/wildcard.rs b/src/wildcard.rs index 63547ba5b..580cf67c5 100644 --- a/src/wildcard.rs +++ b/src/wildcard.rs @@ -809,7 +809,6 @@ fn expand_literal_intermediate_segment_with_fuzz( let Some(m) = string_fuzzy_match_string(wc_segment, &entry.name, false) else { continue; }; - // The first port had !n.is_samecase_exact if m.is_samecase_exact() { continue; }