Repository: nushell/nushell
File: crates/nu-cli/src/completions/completer.rs (Rust)

Code before:
                            suggestions.extend(self.process_completion(&mut completer, &ctx));
                            break;
                        }

                        // normal arguments completion
                        let (new_span, prefix) =
                            strip_placeholder_if_any(working_set, &span, strip);
                        let ctx = Context::new(working_set, new_span, prefix, offset);
                        let flag_completion_helper = || {
                            let mut flag_completions = FlagCompletion {
                                decl_id: call.decl_id,
                            };
                            self.process_completion(&mut flag_completions, &ctx)
                        };
                        suggestions.extend(match arg {
                            // flags
                            Argument::Named(_) | Argument::Unknown(_)
                                if prefix.starts_with(b"-") =>
                            {
                                flag_completion_helper()
                            }
                            // only when `strip` == false
                            Argument::Positional(_) if prefix == b"-" => flag_completion_helper(),
                            // complete according to expression type and command head
                            Argument::Positional(expr) => {
                                let command_head = working_set.get_span_contents(call.head);
                                positional_arg_indices.push(arg_idx);
                                self.argument_completion_helper(
                                    PositionalArguments {
                                        command_head,
                                        positional_arg_indices,
                                        arguments: &call.arguments,
                                        expr,
                                    },
                                    pos,
                                    &ctx,
                                    suggestions.is_empty(),
                                )
                            }
                            _ => vec![],
                        });
                        break;
                    } else if !matches!(arg, Argument::Named(_)) {
                        positional_arg_indices.push(arg_idx);
                    }
                }
            }
            Expr::ExternalCall(head, arguments) => {
                for (i, arg) in arguments.iter().enumerate() {
                    let span = arg.expr().span;
                    if span.contains(pos) {

Review comment (suggestion):
Perf wise this shouldn't be a hit `get_decl` is doing direct lookup compared to the loop of `get_span_contents`, the indirections of hitting `Command::name` through the vtable should be less impactful.

Code after:
                            suggestions.extend(self.process_completion(&mut completer, &ctx));
                            break;
                        }

                        // normal arguments completion
                        let (new_span, prefix) =
                            strip_placeholder_if_any(working_set, &span, strip);
                        let ctx = Context::new(working_set, new_span, prefix, offset);
                        let flag_completion_helper = || {
                            let mut flag_completions = FlagCompletion {
                                decl_id: call.decl_id,
                            };
                            self.process_completion(&mut flag_completions, &ctx)
                        };
                        suggestions.extend(match arg {
                            // flags
                            Argument::Named(_) | Argument::Unknown(_)
                                if prefix.starts_with(b"-") =>
                            {
                                flag_completion_helper()
                            }
                            // only when `strip` == false
                            Argument::Positional(_) if prefix == b"-" => flag_completion_helper(),
                            // complete according to expression type and command head
                            Argument::Positional(expr) => {
                                let command_head = working_set.get_decl(call.decl_id).name();
                                positional_arg_indices.push(arg_idx);
                                self.argument_completion_helper(
                                    PositionalArguments {
                                        command_head,
                                        positional_arg_indices,
                                        arguments: &call.arguments,
                                        expr,
                                    },
                                    pos,
                                    &ctx,
                                    suggestions.is_empty(),
                                )
                            }
                            _ => vec![],
                        });
                        break;
                    } else if !matches!(arg, Argument::Named(_)) {
                        positional_arg_indices.push(arg_idx);
                    }
                }
            }
            Expr::ExternalCall(head, arguments) => {
                for (i, arg) in arguments.iter().enumerate() {
                    let span = arg.expr().span;
                    if span.contains(pos) {