Repository navigation
Rework how completer works - #18761
Conversation
|
Hey, just a bot checking in! You edited files related to the configuration. |
There was a problem hiding this comment.
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 unifiedContext -> Fetchedinterface. - 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.
|
This should be in a great place now! |
|
@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. |
# Conflicts: # crates/nu-parser/src/lib.rs
added a small test to ensure this doesn't occur again
|
@fdncred This should round it out :) |
|
Let's go! |
## 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.
…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
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.
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.
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.
…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.
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 (
CompletionResultFresh/Stale/Pending) but left... several issues unresolved. Sorry to all the folks who put up with that on the nightlies :(Overarching changes:
FlagRefandFlag::long_nameare adopted acrossnu-parser/nu-engine/nu-cmd-langso 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
$env.config.completions.cache_size(default:100) to control that cap.commandline complete --typenow validates its--typeargument and no longer panics on an out-of-range cursor.use,overlay use,export use,source-env,hide-env,attr complete, andwhichnow go through the same completion dispatch as other builtins, fixing inconsistent/missing completions in a few of themAdditional Notes
fixes #11211