Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Improved command completion when using '--'. #104

Merged
merged 4 commits into from
Aug 9, 2021

Conversation

toothbrush
Copy link
Member

I have found that yak's completion breaks down in the following situation:

$ yak env -- [TAB]

I expect it to expand possible executables, but instead, it completes filenames (which is just a Zsh fallback shining though, i believe). A side-effect is that completions for commands i rely on also break:

$ yak env -- aws [TAB] # Completes external commands, instead of aws-subcommands
$ yak env -- make [TAB] # Completes external commands, instead of make targets

This patch to the Zsh completion file does two main things:

  • Enable the -S flag to _arguments as documented here: https://zsh.sourceforge.io/Doc/Release/Completion-System.html#Completion-Functions. Specifically, "-S — Do not complete options after a ‘--’ appearing on the line, and ignore the ‘--’."

  • Don't modify the words array ourselves; just call _normal completion once we've got an environment specified in positional argument 1. Also, use 3 instead of 2 colons for '*:::command..., which according to the docs means: With three colons before the message, the words special array and the CURRENT special parameter are modified to refer only to the normal arguments covered by this description.

  • Finally, i've removed -- as an explicit option, because -S takes care of this in a nice built-in way.

Completions now work as i expect them to, with or without the -- between yak and the command i'm keen to run.

I have found that yak's completion breaks down in the following situation:

```ShellSession
$ yak env -- <TAB>
```

I expect it to expand possible executables, but instead, it completes filenames (which is just a Zsh
fallback shining though, i believe).  A side-effect is that completions for commands i rely on also
break:

```ShellSession
$ yak env -- aws <TAB> # Completes external commands, instead of aws-subcommands
```

This improvement does two things:

* Enable the `-S` flag to `_arguments` as documented here:
  https://zsh.sourceforge.io/Doc/Release/Completion-System.html#Completion-Functions.  Specifically,
  "-S -> Do not complete options after a ‘--’ appearing on the line, and ignore the ‘--’."

* Don't modify the `words` array ourselves; just call `_normal` completion once we've got an
  environment specified in positional argument 1.  Also, use 3 instead of 2 colons for
  `'*:::command...`, which according to the docs means: With three colons before the message, the
  words special array and the CURRENT special parameter are modified to refer only to the normal
  arguments covered by this description.

* Finally, i've removed `--` as an explicit option, because `-S` takes care of this in a nice
  built-in way.

Completions now work as i expect them to, with or without the `--` between yak and the command i'm
keen to run.
…ubcommand".

To me, "subcommand" conjures something like the "s3" in "aws s3", that is, a subcommand of the tool
itself.  Whereas in fact, we mean to refer to external commands that yak will execute on our
behalf.  I think just calling it `<command>` in the docs and help text is clearer.
Technically, the '--' can be either before or after yak's role argument, in fact i'm in the habit of
running commands like `yak prod -- dothefoo` (which is a bit more GNU-opt-y i believe), whereas the
documentation implies this isn't right.  I've updated the copy to be a bit more general in that
regard.
Copy link
Contributor

@rbvigilante rbvigilante left a comment

Choose a reason for hiding this comment

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

I'm fine with this so long as it works

@toothbrush
Copy link
Member Author

I'm fine with this so long as it works

Cool. I feel good about it but 19:30 isn't the time to be shipping code. Chat tomorrow! 👋

@toothbrush toothbrush merged commit 2696abf into master Aug 9, 2021
@toothbrush toothbrush deleted the paul/better-command-completions branch August 9, 2021 23:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants