Skip to content

Contextful completions #5035

Closed
Closed

Description

@zetanumbers

Related problem

Currently completions are not aware of already entered parameter values. For example cargo-run completion could only complete --bin or --example parameters without knowing what --package is used, so the current implementation in https://github.com/nushell/nu_scripts only completes these fields for the current directory package.

Describe the solution you'd like

There could be a record of all currently entered arguments.

Describe alternatives you've considered

We could use a custom prompt, or peek inside of some internal states of nushell for that, but that would be only a hack.

Additional context and details

No response

Activity

  1. mxfp0 commented on Apr 7, 2022

    @mxfp0
    Contributor

    @zetanumbers the current implementation sends to the custom completion two args [current_line: string, pos: int]. Based on that you could parse the current line and be aware of that was already specified previously. It's not the best way to do but at least some starting point.

    Here's one example:

    def "extract-flag" [line: string] {
        line | parse -r "(?P<n>-n [a-zA-Z-0-9]+)"
    }
    
    def "nu-complete mycmd test" [line: string, pos: int] {
        let my_flag = extract-flag $line
    
        // flag can be ""
        // or "-n Something123"
    
        if ($my_flag | str length) == 0 {
            // no flag specified, do something
           [ no flags input ]
        } else {
           // flag specified, do anything with it $my_flag
          [ the flag is $my_flag ]
       }
    }
    
    export extern "mycmd test" [
        target?: string@"nu-complete mycmd test"
    ]
    

    This came with some refactor in the completions part (#5102)

  2. zetanumbers commented on Apr 7, 2022

    @zetanumbers
    ContributorAuthor

    Handling current_line is probably not a good idea because you have to account for quotes, spaces, etc. Perhaps there could be some sort of parse_args function? Or maybe we could just pass string array and not worry, tho it would be opinionated to handle args in some way or another? I think there will be downside of copying the logic in completions, until we pass fully preprocessed args.

  3. added
    category:enhancement
    A:completions
    on Apr 16, 2022
  4. added a commit that references this issue on Oct 6, 2025
  5. kronberger-droid commented on Nov 13, 2026

    @kronberger-droid
    Contributor

    Completers now get the already parsed arguments of the command.
    Declare the completer with a place: record parameter and $place.command holds them (#16765, #18791).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:completionscategory:enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions