Without this, slightly older versions of zsh fail to correctly parse the output of the __complete
command. Tested that with zsh 5.0.2 and zsh 5.8. Since this is just correctly quoting the output of
a command, it shouldn't cause any compatibility issues.
Fixes#1211
When handling ShellCompDirectiveNoSpace we must still properly handle
descriptions. To do so we cannot simply use 'compadd', but must use
zsh's '_describe' function.
Also, when handling ShellCompDirectiveNoSpace we cannot assume that
only a single completion will be given to the script. In fact,
ValidArgsFunction can return multiple completions, even if they don't
match the 'toComplete' argument prefix. Therefore, we cannot use the
number of completions received in the completion script to determine
if we should activate the "no space" directive. Instead, we can leave
it all to the '_describe' function.
Fixes#1212
When handling ShellCompDirectiveNoFileComp we cannot base ourself on
the script receiving no valid completion. In fact,
ValidArgsFunction can return multiple completions, even if they don't
match the 'toComplete' argument prefix at all. Therefore, we cannot use
the number of completions received by the completion script to determine
if we should activate the "no file comp" directive. Instead, we can
check if the '_describe' function has found any completions.
Finally, it is important for the script to return the return code of the
called zsh functions (_describe, _arguments). This tells zsh if
completions were found or not, which if not, will trigger different
matching attempts, such as matching what the user typed with the the
content of possible completions (instead of just as the prefix).
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
The zsh completion script output by cobra is a stub completion function
which replaces itself with the actual completion function. This
technique enables cobra to define helper functions without splitting the
completion script into multiple files. However, the current
implementation forgets to call the actual completion function at the end
of the stub function, meaning that completion won't work the first time
it's invoked in a shell session. This commit is a fix for this problem.
Replace the current Zsh completion with a Zsh completion solution based
on Go completions. This allows to support custom completions (based
on Go completions), but also to standardize the behavior of completion
across all shells.
Also, add support to Go completions for the bash completion annotations:
BashCompFilenameExt (including Command.MarkFlagFilename() family)
- still supported by zsh
BashCompSubdirsInDir - now supported by zsh
BashCompOneRequiredFlag (including Command.MarkFlagRequired() family)
- now supported by zsh and fish
Finally, remove the suggestin of the = form of flag completion.
The = form is supported, but it will not be suggested to avoid having
duplicated suggestions.
- Moved some general function to a more generic shell_completions file.
- Added functions to mark flag as directory completion.
- Started making the global functions docs more generic (not bash
specific) and added compatibility matrix.
- If the flags are not bool the completion expects argument.
- You don't have to specify file extensions for file completion to
work.
- Allow multiple occurrences of flag if type is stringArray.
Need to verify that these assumption are correct :)
A very basic POC. Need to refactor to generate completion
structure before passing to the template to avoid repeated
computations.
What works:
* Real zsh completion (not built on bash)
* Basic flags (with long flag and optional shorthand)
* Basic filename completion indication (not with file extensions though)
What's missing:
* File extensions to filename completions
* Positional args
* Do we require handling only short flags?