From 0cfc95993a8e79de108cdc4970a623b746e34c04 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sat, 11 Jan 2025 12:07:19 +0100 Subject: [PATCH] Swap code blocks for completing separator suffix resp. whole token Mainly to make the next commit's diff smaller. Not much functional change: since file completions never have the DONT_SORT flag set, these results will be sorted, and there are no data dependencies -- unless we're overflowing the max number of completions. But in that case the whole-token completions seem more important anyway. --- src/complete.rs | 74 +++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/src/complete.rs b/src/complete.rs index e7809df57..5639c42bc 100644 --- a/src/complete.rs +++ b/src/complete.rs @@ -1584,43 +1584,8 @@ impl<'ctx> Completer<'ctx> { }; let complete_from_start = sep_index.is_none() || !string_prefixes_string(L!("-"), s); - if let Some(sep_index) = sep_index { - let sep_string = s.slice_from(sep_index + 1); - let mut local_completions = Vec::new(); - if matches!( - expand_string( - sep_string.to_owned(), - &mut local_completions, - flags, - self.ctx, - None, - ) - .result, - ExpandResultCode::error | ExpandResultCode::overflow - ) { - FLOGF!(complete, "Error while expanding string '%ls'", sep_string); - } - - // Any COMPLETE_REPLACES_TOKEN will also stomp the separator. We need to "repair" them by - // inserting our separator and prefix. - Self::escape_opening_brackets(&mut local_completions, s); - Self::escape_separators( - &mut local_completions, - variable_override_prefix, - self.flags.autosuggestion, - true, - quoted, - ); - let prefix_with_sep = s.as_char_slice()[..sep_index + 1].into(); - for comp in &mut local_completions { - comp.prepend_token_prefix(prefix_with_sep); - } - if !self.completions.extend(local_completions) { - return; - } - } - if complete_from_start { + let mut flags = flags; // Don't do fuzzy matching for files if the string begins with a dash (issue #568). We could // consider relaxing this if there was a preceding double-dash argument. if string_prefixes_string(L!("-"), s) { @@ -1645,6 +1610,43 @@ impl<'ctx> Completer<'ctx> { quoted, ); } + + let Some(sep_index) = sep_index else { + return; + }; + let sep_string = s.slice_from(sep_index + 1); + let mut local_completions = Vec::new(); + if matches!( + expand_string( + sep_string.to_owned(), + &mut local_completions, + flags, + self.ctx, + None, + ) + .result, + ExpandResultCode::error | ExpandResultCode::overflow + ) { + FLOGF!(complete, "Error while expanding string '%ls'", sep_string); + } + + // Any COMPLETE_REPLACES_TOKEN will also stomp the separator. We need to "repair" them by + // inserting our separator and prefix. + Self::escape_opening_brackets(&mut local_completions, s); + Self::escape_separators( + &mut local_completions, + variable_override_prefix, + self.flags.autosuggestion, + true, + quoted, + ); + let prefix_with_sep = s.as_char_slice()[..sep_index + 1].into(); + for comp in &mut local_completions { + comp.prepend_token_prefix(prefix_with_sep); + } + if !self.completions.extend(local_completions) { + return; + } } fn escape_separators(