From 50cb7623ac005e8111e871aadef960a429fe49a6 Mon Sep 17 00:00:00 2001 From: blindfs Date: Tue, 22 Apr 2025 10:57:36 +0800 Subject: [PATCH] fix: custom completion for flags only if we are on the flag value --- crates/nu-cli/src/completions/completer.rs | 31 +++++++++++++++------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/crates/nu-cli/src/completions/completer.rs b/crates/nu-cli/src/completions/completer.rs index 00324eb74e..dc3919e220 100644 --- a/crates/nu-cli/src/completions/completer.rs +++ b/crates/nu-cli/src/completions/completer.rs @@ -352,27 +352,38 @@ impl NuCompleter { let custom_completion_decl_id = { // Check PositionalArg or Flag from Signature let signature = working_set.get_decl(call.decl_id).signature(); - + match arg { // For named arguments, check Flag - Argument::Named((name, short, _)) => { - // Try to find matching flag (long or short) - let flag = signature.get_long_flag(&name.item) - .or_else(|| short.as_ref().and_then(|s| - signature.get_short_flag(s.item.chars().next().unwrap_or('_')))); - flag.and_then(|f| f.custom_completion) + Argument::Named((name, short, value)) => { + if value.as_ref().is_none_or(|e| !e.span.contains(pos)) { + None + } else { + // If we're completing the value of the flag, + // search for the matching custom completion decl_id (long or short) + let flag = + signature.get_long_flag(&name.item).or_else(|| { + short.as_ref().and_then(|s| { + signature.get_short_flag( + s.item.chars().next().unwrap_or('_'), + ) + }) + }); + flag.and_then(|f| f.custom_completion) + } } // For positional arguments, check PositionalArg Argument::Positional(_) => { // Find the right positional argument by index let arg_pos = positional_arg_indices.len(); - signature.get_positional(arg_pos) + signature + .get_positional(arg_pos) .and_then(|pos_arg| pos_arg.custom_completion) } - _ => None + _ => None, } }; - + if let Some(decl_id) = custom_completion_decl_id { // for `--foo ` and `--foo=`, the arg span should be trimmed let (new_span, prefix) = if matches!(arg, Argument::Named(_)) {