Skip to content

Rework how completer works - #18761

Merged
fdncred merged 53 commits into
nushell:mainfrom
philocalyst:cacher
Aug 2, 2026
Merged

fdncred merged 53 commits into
nushell:mainfrom
philocalyst:cacher

Conversation

@philocalyst

@philocalyst philocalyst commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

Requires nushell/reedline#1136
Related PR (Builds ontop of): #18660

Description

This is a follow-up to #18334 (non-blocking completions, merged) and #18671 (test-infra sync, merged). Those landed the async worker model (CompletionResult Fresh/Stale/Pending) but left... several issues unresolved. Sorry to all the folks who put up with that on the nightlies :(

Overarching changes:

  • Across prompts we're using a LRU cache for storing and managing the caches. This is configurable, and default bounded to 100. For many completions nothing is loaded onto the LRU cache, only the intensive operations (git checkout, carapace stuff, etc.). Open to dramatically reducing this number, but it sure feels nice when it works.
  • Cache work is a lot more robust, we do narrowing and carry always an env for the completor environment, so we have a rough idea of when to complete for the sources that are mostly pure.
  • Added completionengine for the death of hardcoded dispatch with a routing enum. This allowed me to rewrite the completor trait around the context and fetched types, wiring a lot more logic directly through it.
  • Added completionsite and sitekind models for a catch-all way for resolving the facts of what is under the cursor, which kills maybe fifty sites of span math (what caused the super annoying completion duplication)
  • *commandline complete --type` now type-checks its argument (previously accepted any string and could silently no-op) and clamps the cursor to a valid boundary instead of indexing out of range.
  • complete paths is shared between file and directory completions, which eliminates a panic risk and more manual span constructions.
  • Flag/span plumbing. FlagRef and Flag::long_name are adopted across nu-parser/nu-engine/nu-cmd-lang so flag identity survives through parsing and into completion and help-text rendering, which eliminated a lot of re-derive logic.

User-facing changes (Release notes)

Improved completion caching, dispatch, and reliability

  • Added $env.config.completions.cache_size (default: 100) to control that cap.
  • Completion results now persist across prompts instead of being discarded on every new prompt,.
  • Fixed a potential panic when narrowing file/directory completions on a path containing multi-byte (non-ASCII) characters.
  • commandline complete --type now validates its --type argument and no longer panics on an out-of-range cursor.
  • use, overlay use, export use, source-env, hide-env, attr complete, and which now go through the same completion dispatch as other builtins, fixing inconsistent/missing completions in a few of them

Additional Notes

  • Can't promise that this eliminates all bugs, just most of them. The bigger thing it gives us is a more robust framework to fix bugs, going forward. Will be responsive to anything related to completions, and am okay with a longer review period before merge. The big things are covered now, however, with the added tests in this PR.

fixes #11211

@github-actions

Copy link
Copy Markdown

Hey, just a bot checking in! You edited files related to the configuration.
If you changed any of the default values or added a new config option, don't forget to update the doc_config.nu which documents the options for our users including the defaults provided by the Rust implementation.
If you didn't make a change here, you can just ignore me.

@github-actions github-actions Bot added A:parser Issues related to parsing A:LSP Language Server Protocol (nu-lsp) labels Jul 29, 2026
@fdncred
fdncred marked this pull request as ready for review July 29, 2026 11:26
@fdncred
fdncred requested a review from Copilot July 29, 2026 11:27

Copilot AI 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.

Pull request overview

This PR is a large refactor and hardening of Nushell’s completion pipeline: it introduces a more structured completion “engine” with explicit completion-site modeling, adds a cross-prompt bounded LRU narrowing cache (configurable via $env.config.completions.cache_size), and pushes more completion dispatch into typed signature metadata (e.g. built-in argument completions) to reduce brittle name-based routing.

Changes:

  • Introduces CompletionEngine + structured completion-site resolution (SiteKind/CompletionSite) and updates completion sources to a unified Context -> Fetched interface.
  • Adds cross-prompt LRU caching of narrowing results and new config/docs for completions.cache_size.
  • Extends signature/AST metadata (BuiltinCompletion, FlagRef, Flag::long_name) and updates multiple consumers (engine, parser, help/docs, LSP) to use the new flag/completion plumbing.

Reviewed changes

Copilot reviewed 48 out of 49 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/repl/test_commandline.rs Updates expected error text for commandline complete --type invalid input.
crates/nu-protocol/src/span.rs Adds Span::point helper and uses it in span construction paths.
crates/nu-protocol/src/signature.rs Adds Flag::long_name; introduces BuiltinCompletion and Completion::Builtin.
crates/nu-protocol/src/config/completions.rs Adds completions.cache_size config setting with default.
crates/nu-protocol/src/ast/call.rs Introduces FlagRef to preserve long/short flag identity.
crates/nu-parser/src/parser.rs Re-exports parse_shorter_head_reading.
crates/nu-parser/src/parse_expressions.rs Preserves assignment-expression AST shape for incomplete assignments.
crates/nu-parser/src/parse_calls.rs Reduces allocations, preserves flag identity on incomplete flags, adds parse_shorter_head_reading.
crates/nu-parser/src/lib.rs Re-exports parse_shorter_head_reading.
crates/nu-parser/src/known_external.rs Adjusts named-flag forwarding logic/comments for extern calls.
crates/nu-lsp/src/signature.rs Switches to Flag::long_name for display formatting.
crates/nu-lsp/src/completion.rs Migrates from NuCompleter to CompletionEngine.
crates/nu-engine/src/eval_ir.rs Switches named flag lookup to Flag::long_name logic.
crates/nu-engine/src/documentation.rs Switches help rendering to Flag::long_name.
crates/nu-engine/src/compile/call.rs Uses FlagRef to compile named args without empty-long-name heuristics.
crates/nu-engine/src/command_prelude.rs Re-exports new protocol types used by commands (BuiltinCompletion, Parameter, etc.).
crates/nu-config/default_files/doc_config.nu Documents and sets default completions.cache_size.
crates/nu-command/src/system/which_.rs Adds typed builtin completion for which rest args.
crates/nu-command/src/system/run_external.rs Minor splice range simplification.
crates/nu-command/src/help/help_commands.rs Uses Flag::long_name to format flags consistently.
crates/nu-command/src/env/source_env.rs Adds typed builtin .nu file completion for source-env.
crates/nu-cmd-lang/src/core_commands/use_.rs Adds typed builtin module path + export-member completions.
crates/nu-cmd-lang/src/core_commands/overlay/use_.rs Adds typed builtin module path completion for overlay use.
crates/nu-cmd-lang/src/core_commands/hide_env.rs Adds typed builtin env-var-name completion for hide-env.
crates/nu-cmd-lang/src/core_commands/export_use.rs Adds typed builtin module path + export-member completions.
crates/nu-cmd-lang/src/core_commands/attr/complete.rs Adds typed builtin internal-command completion for attr complete.
crates/nu-cli/tests/completions/mod.rs Adds regression test preventing stale file completions from answering flag tokens.
crates/nu-cli/src/repl.rs Persists narrowing cache across prompts while rebuilding completer per prompt.
crates/nu-cli/src/lib.rs Re-exports CompletionEngine alongside existing completion types.
crates/nu-cli/src/completions/variable_completions.rs Converts variable completion to Context -> Fetched API.
crates/nu-cli/src/completions/static_completions.rs Converts static completion to Context -> Fetched API.
crates/nu-cli/src/completions/operator_completions.rs Converts operator completion to Context -> Fetched API and improves mutability detection.
crates/nu-cli/src/completions/mod.rs Re-exports new completion engine types and helpers.
crates/nu-cli/src/completions/flag_completions.rs Converts flag completion to Context -> Fetched and uses Flag::long_name.
crates/nu-cli/src/completions/file_completions.rs Routes file completion through shared complete_paths.
crates/nu-cli/src/completions/exportable_completions.rs Converts module-export completion to Context -> Fetched API.
crates/nu-cli/src/completions/env_var_completions.rs Converts env-var completion to Context -> Fetched API.
crates/nu-cli/src/completions/dotnu_completions.rs Converts .nu completion to Context -> Fetched and marks cacheability.
crates/nu-cli/src/completions/directory_completions.rs Routes directory completion through shared complete_paths.
crates/nu-cli/src/completions/custom_completions.rs Refactors custom/command-wide completion to Fetched + reduces per-keystroke engine cloning.
crates/nu-cli/src/completions/completion_common.rs Adds shared complete_paths, improves matching/perf, fixes multi-byte path slicing risk.
crates/nu-cli/src/completions/completer.rs Major completion engine refactor: sites, dispatch, LRU narrowing cache, background worker changes, partial insertion logic.
crates/nu-cli/src/completions/command_completions.rs Adds CommandScope, refactors internal/external command completion and cacheability.
crates/nu-cli/src/completions/cell_path_completions.rs Converts cell-path completion to Context -> Fetched API and fixes cursor/span handling.
crates/nu-cli/src/completions/base.rs Introduces Fetched result type and to_reedline_span helper; updates Completer trait.
crates/nu-cli/src/completions/attribute_completions.rs Converts attribute completion to Context -> Fetched API.
crates/nu-cli/src/completions/arg_value_completion.rs Refactors arg-value completion: dynamic completion safety, module export completion extraction, fallback logic via Fetched.
crates/nu-cli/src/commands/commandline/complete.rs Refactors commandline complete, clamps cursor boundary, adds typed --type parsing via FromValue.
crates/nu-cli/Cargo.toml Adds lru workspace dependency for the narrowing cache.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/nu-parser/src/known_external.rs Resolved
@fdncred fdncred added A:completions Issues related to tab completion notes:ready Indicates Ready for Release notes notes:additions Noted in "Additions" section labels Jul 29, 2026
@fdncred
fdncred marked this pull request as draft July 29, 2026 13:37
@philocalyst

Copy link
Copy Markdown
Contributor Author

This should be in a great place now!

@fdncred
fdncred marked this pull request as ready for review July 31, 2026 23:48
@fdncred

fdncred commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

@philocalyst I fixed the trivial CI problems but there are test failures that need to be addressed that may take changes. Can you fix those please? I'd like to get the CI green ASAP.

added a small test to ensure this doesn't occur again
@philocalyst

Copy link
Copy Markdown
Contributor Author

@fdncred This should round it out :)

@fdncred
fdncred merged commit 1385580 into nushell:main Aug 2, 2026
16 checks passed
@fdncred

fdncred commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Let's go!

@github-actions github-actions Bot added this to the v0.115.0 milestone Aug 2, 2026
@fdncred fdncred changed the title HUGE COMPLETER WORK (WIP) Rework how completer works Aug 5, 2026
@philocalyst philocalyst mentioned this pull request Aug 5, 2026
fdncred pushed a commit that referenced this pull request Aug 13, 2026
## Description

#18761 swapped the wholesale `self.keybindings = keybindings` assignment
for a loop that merges each incoming binding into the existing list,
matching on `name`.
`name` is `Option<Value>` on `ParsedKeybinding`, so an unnamed binding
matches nothing and falls through to the `push`, every time.
Re-sourcing a config therefore grows the list by one entry per unnamed
binding on each pass.

The fix matches on the identity reedline already keys a binding by:
named bindings compare on the expanded `name`, unnamed ones on
`modifier`, `keycode` and `mode`, and a named/unnamed pair never
matches.

Also fixes `doc_config.nu`, which still claimed a full list replacement
(`=`) clears the defaults for `keybindings` and `menus`.
That was true when #18747 wrote it, but #18761 made both assignments
no-ops without updating the text, so I dropped the two commented
examples.

Worth landing before 0.115, since neither PR has shipped yet (last tag
is 0.114.1).

## User-facing changes (Release notes)

Assigning `$env.config.keybindings` or `$env.config.menus` now merges
into the defaults rather than replacing them, so
`$env.config.keybindings = []` and `$env.config.menus = []` no longer
clear anything.
Set `event: null` on a matching binding to unbind a key.

## Additional notes

The merge change rode in on #18761 alongside a large amount of unrelated
completer work, which is probably why the doc drift went unnoticed.

Out of scope: the merge still has no way to drop a binding outright.
fdncred pushed a commit that referenced this pull request Oct 1, 2026
…19130)

## Description

In a `def --wrapped` command, `what --<Tab>` never called the completer
on `...args` (#19097).
Both `trailing_slot` and `resolve_argument_site` classify any token
starting with `-` as `SiteKind::FlagName`, which dispatches to flag-name
completion only, even though a wrapped command binds an unknown `--x` to
its rest parameter.
This is a regression from #18761: before it, only a bare `-` routed to
flag completion, so `--x` on a wrapped command reached the rest
completer, which took precedence over `@complete` as it does again now.

`SiteKind::FlagName` now carries the positional the token would bind to.
On a wrapped command whose positional declares a completer, the flag
names are merged with that positional's completion, through the same
`complete_argument_value` chain a plain positional takes.
`site_completer` resolves the same completer, so an `@interactive` one
still runs inline.

An undeclared rest parameter keeps flag-name completion only, since its
shape fallback would list files for every `-` typed.
`place.kind` stays `flag-name`.

## User-facing changes (Release notes)

Fixed a regression since 0.115 where custom completers on a `def
--wrapped` rest parameter were not called for arguments starting with
`-`, so `what --<Tab>` only offered declared flags.

## Additional notes

fixes #19097
kronberger-droid added a commit to kronberger-droid/nushell that referenced this pull request Nov 3, 2026
Since nushell#18761 assigning `$env.config.keybindings` merged into the
current list, thus no assignment could drop a single binding, and a
binding with a bad keycode errored on every prompt until the whole
list was cleared.

The list is now stored as written, like any other list: `=` replaces
it, `++=` appends and a filtered list drops what it filtered out.
Reedline applies the entries in order on top of its base maps, so the
last entry on a key wins, in each mode it names. A name is only a
label.

`= [mine]` now drops the default menu bindings as well, since nushell#18747
moved them into the list. Use `++=` to keep them.

`SharedKeybindingName` goes: one name on several keys (atuin) is no
longer ambiguous once nothing merges by name.
kronberger-droid added a commit to kronberger-droid/nushell that referenced this pull request Nov 3, 2026
Since nushell#18761 assigning `$env.config.keybindings` merged into the
current list, thus no assignment could drop a single binding, and a
binding with a bad keycode errored on every prompt until the whole
list was cleared.

The list is now stored as written, like any other list: `=` replaces
it, `++=` appends and a filtered list drops what it filtered out.
Reedline applies the entries in order on top of its base maps, so the
last entry on a key wins, in each mode it names. A name is only a
label.

`= [mine]` now drops the default menu bindings as well, since nushell#18747
moved them into the list. Use `++=` to keep them.

`SharedKeybindingName` goes: one name on several keys (atuin) is no
longer ambiguous once nothing merges by name.
kronberger-droid added a commit to kronberger-droid/nushell that referenced this pull request Nov 3, 2026
Since nushell#18761 assigning `$env.config.keybindings` merged into the
current list, thus no assignment could drop a single binding, and a
binding with a bad keycode errored on every prompt until the whole
list was cleared.

The list is now stored as written, like any other list: `=` replaces
it, `++=` appends and a filtered list drops what it filtered out.
Reedline applies the entries in order on top of its base maps, so the
last entry on a key wins, in each mode it names. A name is only a
label.

`= [mine]` now drops the default menu bindings as well, since nushell#18747
moved them into the list. Use `++=` to keep them.

`SharedKeybindingName` goes: one name on several keys (atuin) is no
longer ambiguous once nothing merges by name.
fdncred pushed a commit that referenced this pull request Nov 4, 2026
…19418)

## Description

Since #18761 assigning `$env.config.keybindings` or `$env.config.menus`
merges into the current list in the config layer.
That made the two lists behave unlike every other list in nushell: `=`
could not drop a single binding, `menus = []` cleared nothing, and a
binding with a bad keycode errored on every prompt until the whole list
was cleared.
#18828 and #18870 patched the merge (unnamed bindings, then name plus
key, with a warning for atuin's shared names in #18848), but each patch
added a rule on top of the merge.

This PR takes the merge out.
Both lists are stored as written, like any other list:

```nu
$env.config.keybindings = [...]                                              # replaces the list
$env.config.keybindings ++= [...]                                            # appends to it
$env.config.keybindings | where name != x                                    # filters it
$env.config.keybindings | reverse | uniq-by modifier keycode mode | reverse  # drops exact repeats
```

Overlaps are resolved where the list is used, by reedline, with one rule
for both lists: entries are applied in order and the last one wins.

| | spot | free spot | taken spot |
|---|---|---|---|
| keybinding | mode, modifier, keycode | filled | replaced |
| menu | name | appended | replaced in place |

Keys already worked like this in reedline, since `add_binding` is a
`HashMap::insert`.
Menus did not, since the lookup by name takes the first match, so
nushell/reedline#1294 made a later menu replace one of the same name.
A keybinding's `name` is only a label and plays no part in what a key
does.

`SharedKeybindingName` goes, since one name on several keys (atuin) is
no longer ambiguous once nothing merges by name.
The `KeyIdentity` machinery that mirrored reedline's key parsing goes
with it.

For keybindings there is a test where nushell hands the list over:
`create_keybindings` is checked for last-wins, for `event: null`
removing a reedline base binding, and for a later entry covering fewer
modes.
Menus rely on the reedline test for the same rule, since nushell only
passes the list on.

## User-facing changes (Release notes)

### Assigning `$env.config.keybindings` and `$env.config.menus` replaces
the list

Breaking: `$env.config.keybindings = [...]` and `$env.config.menus =
[...]` now replace the whole list, including nushell's default menus and
the keys that open them (Tab and Shift+Tab completion, Ctrl+Space, F1
help, Ctrl+X and Ctrl+Z paging, the Ctrl+R history menu).
If your config assigns either list, or sets `keybindings` or `menus`
inside a `$env.config = {...}` record, switch to `++=` or Tab stops
completing.
Reedline's own bindings (movement, editing) are not part of the list and
stay.

```nu
$env.config.keybindings ++= [{ name: my_binding, modifier: control, keycode: char_t, mode: emacs, event: { send: clearscreen } }]
```

To get the defaults back in a running shell, assign them again from a
clean `nu -n`:

```nu
$env.config.keybindings = (^$nu.current-exe -n -c '$env.config.keybindings | to nuon' | from nuon)
$env.config.menus = (^$nu.current-exe -n -c '$env.config.menus | to nuon' | from nuon)
```

Filtering a list now removes what it drops, and `= []` clears either
list.
When several keybindings bind the same key in the same mode, or several
menus share a name, the last one wins.
A keybinding's `name` is only a label, so sharing one between keys no
longer warns.
To unbind a key that reedline itself binds, add an entry for it with
`event: null`.

## Additional notes

Menus need nushell/reedline#1294, which is in the reedline bump commit
before the menus commit.

The book still describes the merge in `line_editor.md` (the keybindings
section and the menus note), so that needs a follow-up PR to
nushell.github.io.

Related: #18761, #18828, #18870, #18848.
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:LSP Language Server Protocol (nu-lsp) 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.

Completion algorithm: try prefix, fallback to fuzzy if, and only if, prefix gives no results

4 participants