Skip to content

Command-wide completions - #16765

Merged
Bahex merged 16 commits into
nushell:mainfrom
Bahex:CommandWideCompletion
Oct 6, 2025
Merged

Bahex merged 16 commits into
nushell:mainfrom
Bahex:CommandWideCompletion

Conversation

@Bahex

@Bahex Bahex commented Sep 27, 2025 •

Copy link
Copy Markdown
Member

Also includes the following bugfix:

  • spans: list<string> argument provided to the external completer (and completer commands used with attr complete with this addition) previously had invalid spans (from Span::unknown).
    These values now have correct spans pointing to their location in the source code.
This change was moved out to another PR

Using a nonexistent command name as a parameter completer now throws a parse error:

Error: nu::parser::unknown_command

  × Unknown command.
   ╭─[source:1:17]
 1 │ def foo [p: any@bar] { }
   ·                 ─┬─
   ·                  ╰── unknown command
   ╰────

Release notes summary - What our users need to know

Command-wide completion handler

There is big difference between custom completions that can be specified in a command's signature, and the "global" external completer ($env.config.completions.external.completer).

The former is added to individual parameters, while the latter provides completions for all arguments of (external) commands.

This makes using custom commands that wrap external commands awkward. The global external completer won't provide completions for the wrapper command and adding completions to that command uses a completely different API. It would be nice to be able to use the external completer's API for custom completions, or use the external completer for wrapper commands...

@complete external

Now it's possible to explicitly enable the global external completer for extern and custom command definitions:

@complete external
def --wrapped jc [...args] {
    ^jc ...$args | from json
}

@complete <completer>

Instead of using the external completer, command-wide custom completers can be used for specific commands:

def carapace-completer [spans: list<string>] {
    ^carapace ..
}

@complete carapace-completer
def --env get-env [name] { $env | get $name }

@complete carapace-completer
def --env set-env [name, value] { load-env { $name: $value } }

@complete carapace-completer
def --env unset-env [name] { hide-env $name }

Combined with extern definitions, this makes it trivial to use specific completion sources for specific commands:

def fish-completer [spans: list<string>] {
	^fish ..
}

@complete fish-completer
extern git []

@complete fish-completer
extern asdf []

Yes, you can remove the match expression from your external completer!

{|spans: list<string>|

    # ...

    # no need for this anymore! 
    match $spans.0 {
        # fish completes commits and branch names in a nicer way
        git => $fish_completer
        # carapace doesn't have completions for asdf
        asdf => $fish_completer
        _ => $carapace_completer
    } | do $in $spans
Full implementation of fish-completer from the cookbook.
def fish-completer [spans: list<string>] {
	^fish --command $"complete '--do-complete=($spans | str replace --all "'" "\\'" | str join ' ')'"
	| from tsv --flexible --noheaders --no-infer
	| rename value description
	| update value {|row|
		let value = $row.value
		let need_quote = ['\' ',' '[' ']' '(' ')' ' ' '\t' "'" '"' "`"] | any { $in in $value }
		if ($need_quote and ($value | path exists)) {
			let expanded_path = if ($value starts-with ~) { $value | path expand --no-symlink } else { $value }
			$'"($expanded_path | str replace --all "\"" "\\\"")"'
		} else { $value }
	}
}

Tasks after submitting

@Bahex Bahex added A:external-commands Issues related to external commands A:completions Issues related to tab completion notes:additions Noted in "Additions" section labels Sep 27, 2025
@github-actions github-actions Bot added the A:parser Issues related to parsing label Sep 27, 2025
@ysthakur
ysthakur self-requested a review September 27, 2025 21:29
Comment thread crates/nu-protocol/src/signature.rs Resolved
}
}

pub fn get_command_arguments(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make it pub(super)? Or make it more general and move it somewhere in flatten.rs?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this might come in handy somewhere else. Not a blocker for this PR though

@blindFS blindFS left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't fully tested it yet, but I agree with the overall design.
Probably need careful documentation on this topic, because the custom/external completer stuff is already somewhat complicated to grasp.

@Bahex
Bahex force-pushed the CommandWideCompletion branch from 27bd0cc to 151990e Compare October 1, 2025 18:06
@Bahex
Bahex force-pushed the CommandWideCompletion branch from 151990e to 945068d Compare October 1, 2025 18:59
@Bahex
Bahex marked this pull request as ready for review October 1, 2025 19:30
Comment thread crates/nu-cli/tests/completions/mod.rs Resolved
Comment thread crates/nu-cli/tests/completions/mod.rs Resolved

@ysthakur ysthakur left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall, this is going to be really great. I do agree with blindFS that it needs to be documented very well, since our completion code is not easy to grasp.

@Bahex
Bahex merged commit d96dbbd into nushell:main Oct 6, 2025
@github-actions github-actions Bot added this to the v0.108.0 milestone Oct 6, 2025
@blindFS

blindFS commented Oct 9, 2025 •

Copy link
Copy Markdown
Contributor

There seems to be a really weird regression:

#16075 (comment)

$env.config.completions.algorithm = "fuzzy"
def l [arg: string@[a b c]] {  }

l <tab> now automatically appends an a

image

The interesting part:

def l [arg: string@[b c]] {  }

def ll [arg: string@[a b c]] {  }

both have no regression issue.

And I've checked the returning values of complete_by_expression. They are exactly the same before or after this PR.

Plus, lsp completion is not affected, which means it certainly has something to do with the placeholder character:

image

@blindFS

blindFS commented Oct 9, 2025 •

Copy link
Copy Markdown
Contributor

@ysthakur @Bahex @b0o Can you guys reproduce it?

Most of other single character named commands don't share this problem, only l/L (also rl and its case variants) according to my test.

Could be a reedline issue happens to be triggered by those l fuzzy matched internal commands.

But I don't get what it has to do with this PR.

@Bahex

Bahex commented Oct 9, 2025

Copy link
Copy Markdown
Member Author

@blindFS yeah I can reproduce it. I'm also guessing it has something to do with the placeholder character.

But I don't get what it has to do with this PR.

Maybe fallback order/behavior got modified?

@blindFS

blindFS commented Oct 9, 2025

Copy link
Copy Markdown
Contributor

Maybe fallback order/behavior got modified?

Not likely, but I have absolutely no clue 🤔.

I feel it may reveal something very funny underneath. What's so special about l and rl after all 🥲.

@b0o

b0o commented Oct 9, 2025

Copy link
Copy Markdown

Yes, I can also reproduce it. Strange.

@ysthakur

Copy link
Copy Markdown
Member

Since it's specifically [a b c] that this happens with, and we use a as our placeholder, could it be that the placeholder isn't getting stripped correctly? Should be easy to confirm but I can't compile/run stuff right now

@blindFS

blindFS commented Oct 10, 2025

Copy link
Copy Markdown
Contributor

Since it's specifically [a b c] that this happens with, and we use a as our placeholder, could it be that the placeholder isn't getting stripped correctly? Should be easy to confirm but I can't compile/run stuff right now

Actually it's not the case. As I stated in a previous comment, the vector of suggestions sent to reedline remains unchanged. That's why I said it might be a hidden reedline issue.

@blindFS

blindFS commented Oct 10, 2025

Copy link
Copy Markdown
Contributor

Well, I think I've found the connection:

It turns out that

the vector of suggestions sent to reedline remains unchanged

is not correct:

image

The diff is this attr complete external new command.

@ofek

ofek commented Feb 16, 2026

Copy link
Copy Markdown
Contributor

Heads-up that the TODO item in the PR description hasn't been done so this functionality is not mentioned in the docs nor cookbook, only in the release note. I was looking into implementing completion for a tool and didn't even come across this by way of the release note but rather through Clap's ongoing discussion of their implementation.

image

@ysthakur

Copy link
Copy Markdown
Member

@Bahex do you have time to document this? No worries if not, there are other completion-related doc updates that I was planning to do anyway

@ofek

ofek commented Mar 2, 2026

Copy link
Copy Markdown
Contributor

@ysthakur Hello! Did you happen to document this in one of your updates?

@ysthakur

ysthakur commented Mar 2, 2026

Copy link
Copy Markdown
Member

@ofek Sorry, not yet. I'll try to get to it this week

@ofek

ofek commented Mar 7, 2026

Copy link
Copy Markdown
Contributor

No worries, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:completions Issues related to tab completion A:external-commands Issues related to external commands A:parser Issues related to parsing notes:additions Noted in "Additions" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enable dynamic completion by providing a list of strings during completion def --wrapped does not invoke shell completions for positional arguments

5 participants