-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Zsh completion V2 #1070
Zsh completion V2 #1070
Conversation
Is this going to be merged? I'm very interested in being able to apply the changes from PR 1035 for zsh completion. |
@marckhouzam I tried your last commit rebased onto master, with one modification to allow it to build (53b3354). The custom completions work great, but all other completions are broken - not even completing the commands themselves. Is that expected or a bug? Let me know if I can contribute in any way - I'm new to Go but could take a stab at it if you can point me in the right direction. |
Thanks @mike-stewart for trying it out. I'm about to post an updated version that will work. |
b6e476d
to
1f61673
Compare
Ok, so this new version should work.
|
@marckhouzam This works great on my (fairly simple) app w/ custom completions.
What is the correct way to turn off this file completion? |
I'll direct you to the documentation so you can let us know if it's understandable or not. Look for the description for |
@marckhouzam Yes, that works. I had implemented that already for some commands, but thought your comment suggested a way to disable file completion for commands where custom completion is not being used. I worked around that by adding this to commands that I don't want any completion on:
Thanks again for getting this branch working. |
That's exactly how you should do it. Should we add an example in the docs for this case, which I expect will be common or will users figure it out easily enough? As the first user, what do you think? |
@marckhouzam The workaround was obvious upon reading the code and documentation, but it might not have been immediately apparent to a casual reader unless they specifically set out to disable file completion. Adding it to the documentation would make it clearer that it's a "feature" that is available to users. |
I will rebase this later today to adapt to the |
@mike-stewart would you like to make a small PR to add this explanation to the documentation, as it applies to all shells? |
1f61673
to
657d1c4
Compare
I rebased this PR after the |
@marckhouzam There's definitely interest in this change! 😅 I've been following your other PRs and was glad when they got merged and v1.0.0 released. The only thing missing is this PR. For context I've used custom Go completions to implement completion of flag values using |
I will soon get back to this PR. But first I want migrate the Helm project to using Cobra 1.0.0 🎉 |
I looked a bit into the existing zsh completion support to see if we really need a V2 version or if we could completely fulfill the current zsh completion API with this new solution. IIUC the current zsh solution has some APIs of its own that are not relevant to bash completion. This seems confirmed by these comments from the original author of the current zsh completion solution: As that comment mentions, there really should not be different APIs for different shells but we should strive for a single API working for all shells. I think we can get very close to that goal with the current PR. But that confirms we do need to introduce a V2 version for zsh completion as this PR should not support the zsh-only API of the other zsh solution. Now that I'm more comfortable about having to introduce a V2, I'll be able to get a polished version ready soon. |
93e8ceb
to
37bd6e8
Compare
I've rebased on master and updated the documentation. To my knowledge, all types of completions are supported except the ones enabled by I'm not removing the WIP label because I'm still thinking about how to avoid having a V2 version. The only two APIs that are zsh-specific are I'll keep thinking about it. |
zsh_completions_v2.go
Outdated
fi | ||
} | ||
|
||
compdef _%[1]s %[1]s |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line allows to source the zsh completion script as is currently done for helm
and kubectl
. I like the idea of providing that option.
However, I need to point out that @jharshman disagreed with this in #887 (comment), so he may prefer to have this removed, once we get to review time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@marckhouzam yeah I still maintain my previous opinion about this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jharshman is right. I found this article to be a good explanation of why sourcing a completion script should not be recommended: https://medium.com/@jzelinskie/please-dont-ship-binaries-with-shell-completion-as-commands-a8b1bcb8a0d0
For projects that need to keep backwards-compatibility, they can insert that one extra line themselves. I will need to do that for Helm.
I will remove this line in my next update.
I've continued investigating the existing zsh-completion support. There are multiple differences but one major difference is that the current zsh completion does not do file completion by default while bash does (see #886). Say I have a program To get file completion (for the first argument only) for zsh, the program developer would need to add the line
On the other hand, to turn off file completion for bash, the developer would need to use the new This means that a developer that wants to support both bash and zsh must write code to handle zsh completion (current version) and different code for the bash completion. I think we should strive to align completion behavior across all shells or else it becomes a nightmare for the program developers. This leads me to backwards-compatibility. I strongly feel that the new zsh completion of this PR should provide file completion by default like bash, as justified above. This would technically be a breaking-change compared to the current zsh completion script. So I think we are at the point of making a decision: do we do a V2 version for zsh like this PR is doing right now, or do we modify the current behavior of zsh completion to align with bash? @jharshman I'm looking for guidance from the maintainers, as this PR is close to finished and mostly need to know which direction to take with respect to V2 or not. Thanks in advance. |
@marckhouzam Having all the shell completion work the same is definitely appealing. |
I agree. Otherwise, I think we end up with "drift" between the different shells which creates pain for the cobra application developers. They end up having to maintain any number of different completions that all have different APIs for similar behavior. I'm in favor of introducing breaking changes for zsh completions that strives for closer parity among the various shell completions. And maybe we begin to deprecate the old version or announce that this is the new way to go about completions. But I'm not sure how this project in the past has approached deprecating features so I'll leave that up to @jharshman |
Thank you @jpmcb and @jharshman for your help on getting this done! I will rebase #1122 now. |
@marckhouzam I am having trouble getting custom dynamic flag completions for zsh working for my project. The custom flag completions work in bash, but when I hit tab tab after typing in the flag, nothing happens. I went through the code in custom_completions.go, bash_completions.go, and zsh_completions.go, and noticed that the var flagCompletionFunctions(which stores a map of the custom flag completion functions) is used in bash_completions.go, but not zsh_completions.go. Is that why I can't get custom flag completions for zsh working, or is something else the problem? |
@jakrosh could you give an example to reproduce the problem? Also, how did you setup the zsh completion script in your environment? |
@jakrosh actually, the first step to debug your custom completions is to call the
Then you should run
to see if you get the proper completions. |
This is because the function you registered with What exactly is the problem you are seeing with zsh compared to bash?
That should be ok. |
It may be your zsh that is not configured to list all the possible completions where there are more than one. |
Then you can try to see what the zsh completion script generated by Cobra does. For example:
You'll see a bunch of traces following what the script does. |
Thank you for all that hard work @marckhouzam I'm really excited for this feature, @jharshman how far out are we from the next release? |
+1 for @austintraver's question, when will this be available in a release? |
1.1.0 has just been released! For people that will want to start using zsh completion from Cobra be aware that custom completions will only work for zsh if they are implemented in Go (using |
This is a new implementation of Zsh completion but based on the Custom Go Completions of PRs #1035 and #1048.
The current Zsh completion has a major limitation in the fact that it does not support custom completions. Furthermore, if it ever did, it would require the program using Cobra to implement a second, zsh-flavoured version of its bash completion code.
This v2 version allows to automatically re-use custom completions once they have been migrated to the Custom Go Completion solution of PR #1035.
Advantages:
=
for flags