fix: custom completion for flags only if we are on the flag value

This commit is contained in:
blindfs 2025-04-22 10:57:36 +08:00
parent 8688cdee11
commit 50cb7623ac

View File

@ -352,27 +352,38 @@ impl NuCompleter {
let custom_completion_decl_id = { let custom_completion_decl_id = {
// Check PositionalArg or Flag from Signature // Check PositionalArg or Flag from Signature
let signature = working_set.get_decl(call.decl_id).signature(); let signature = working_set.get_decl(call.decl_id).signature();
match arg { match arg {
// For named arguments, check Flag // For named arguments, check Flag
Argument::Named((name, short, _)) => { Argument::Named((name, short, value)) => {
// Try to find matching flag (long or short) if value.as_ref().is_none_or(|e| !e.span.contains(pos)) {
let flag = signature.get_long_flag(&name.item) None
.or_else(|| short.as_ref().and_then(|s| } else {
signature.get_short_flag(s.item.chars().next().unwrap_or('_')))); // If we're completing the value of the flag,
flag.and_then(|f| f.custom_completion) // 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 // For positional arguments, check PositionalArg
Argument::Positional(_) => { Argument::Positional(_) => {
// Find the right positional argument by index // Find the right positional argument by index
let arg_pos = positional_arg_indices.len(); 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) .and_then(|pos_arg| pos_arg.custom_completion)
} }
_ => None _ => None,
} }
}; };
if let Some(decl_id) = custom_completion_decl_id { if let Some(decl_id) = custom_completion_decl_id {
// for `--foo <tab>` and `--foo=<tab>`, the arg span should be trimmed // for `--foo <tab>` and `--foo=<tab>`, the arg span should be trimmed
let (new_span, prefix) = if matches!(arg, Argument::Named(_)) { let (new_span, prefix) = if matches!(arg, Argument::Named(_)) {