Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure completion script uses valid function name #2388

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

davidmurdoch
Copy link

Fixes #2387

test/completion.cjs Outdated Show resolved Hide resolved
@davidmurdoch
Copy link
Author

Anything else you need for this?

lib/completion.ts Outdated Show resolved Hide resolved
lib/completion.ts Outdated Show resolved Hide resolved
test/completion.cjs Outdated Show resolved Hide resolved
@davidmurdoch davidmurdoch marked this pull request as ready for review February 25, 2024 20:10
@davidmurdoch
Copy link
Author

Is there anything I can do to push this over the line?

@shadowspawn
Copy link
Member

Ok, my main problem is I am dubious this is useful in practice. Working with command names with spaces in them is pretty clunky and I have never seen it being used. I need convincing that users will benefit from this before I give it a positive review. (I don't think completion will work through a yarn run script, but haven't tried that scenario mentioned in the issue.)

My secondary concern is whether it even works. I had another look. The current PR completion does not seem to be functional for a script name with a space as the compdef line at the end is adding completion for foo_bar.

% 'foo bar' completion        
#compdef foo_bar
###-begin-foo_bar-completions-###
#
# yargs command completion script
#
# Installation: foo bar completion >> ~/.zshrc
#    or foo bar completion >> ~/.zprofile on OSX.
#
_foo_bar_yargs_completions()
{
  local reply
  local si=$IFS
  IFS=$'
' reply=($(COMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" foo bar --get-yargs-completions "${words[@]}"))
  IFS=$si
  _describe 'values' reply
}
compdef _foo_bar_yargs_completions foo_bar
###-end-foo_bar-completions-###

I wrote out the completion and edited it to have:

compdef _foo_bar_yargs_completions 'foo bar'

And could then get completion for

% 'foo bar' --<tab>
--boolean  --string  --                                                                                                                                              
--help               -- Show help                                                                                                                                    
--version            -- Show version number                                                                                                                          

@davidmurdoch
Copy link
Author

davidmurdoch commented Mar 19, 2024

I have never seen it being used.

Its in every npm script that ends up calling out out to yargs.

And I'm using it in the MetaMask extension, er, I want to use it in MetaMask but can't without quite a bit of customization because the generated completion script isn't valid.

I've been editing the minified code, so sorry my PR doesn't even work 😢

Looks like zhs just needs single quotes around commands with spaces. But bash needs a bit more work, as it doesn't work even when quoted. The bash version would be something like:

_yarn_webpack_yargs_completions()
{
    local cur prev command_line

    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    command_line="${COMP_WORDS[*]}"

    # Check if the command line specifically starts with 'yarn webpack'
    if [[ ${command_line} == yarn\ webpack* ]]; then
        # Ensure we are adjusting the argument list appropriately
        local adjusted_args=("${COMP_WORDS[@]:2}") # Skip 'yarn' and 'webpack'
        local type_list

        # Simulate the command as if it started with 'webpack' for yargs
        type_list=$(yarn webpack --get-yargs-completions "${adjusted_args[@]}")
        COMPREPLY=( $(compgen -W "${type_list}" -- ${cur}) )
        return 0
    fi
}

complete -F _yarn_webpack_yargs_completions yarn

It sounds like even if I got it working you wouldn't want this, correct?

@shadowspawn
Copy link
Member

shadowspawn commented Mar 19, 2024

It's in every npm script that ends up calling out out to yargs.

This might be where I am missing something. How does this involve a command name with a space in it? I'll explain what I am thinking.

So in my head, I have an npm run-script called foo. The run-script uses (say) a command named bar. The user types npm run foo -- --compress and it invokes bar --compress. You suggested the author might specify .scriptName('npm run foo') to make the help reflect the main way it is called. I can see that.

Where does completion come into it? I don't think the user will be able to get completion on npm run foo -- --c<tab>. What is the command that the user types that gets completion?

(One step simpler with yarn if can omit the run, but I think otherwise similar.)

@shadowspawn
Copy link
Member

shadowspawn commented Mar 19, 2024

And I'm using it in the MetaMask extension...

This sounds like you do have a concrete plan! What does the help show, and what is user typing on command-line when completion works? Or is the completion used in a non-interactive way by some higher level tooling?

@shadowspawn
Copy link
Member

(I am not expert in completion handlers, and my mental model of one handler per command might be preventing me seeing the goal.)

@shadowspawn
Copy link
Member

(Oh, multi-word completion handlers, that rings a vague bell...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

scriptName breaks completion script if it contains whitespace
2 participants