Skip to content

Commit

Permalink
Fix zsh completion not working on the first time in a shell session (s…
Browse files Browse the repository at this point in the history
…pf13#1237)

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.
  • Loading branch information
midchildan authored Oct 4, 2020
1 parent 40d34bc commit f64bfa1
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions zsh_completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ _%[1]s()
_describe "completions" completions $(echo $flagPrefix)
fi
}
# don't run the completion function when being source-ed or eval-ed
if [ "$funcstack[1]" = "_%[1]s" ]; then
_%[1]s
fi
`, name, compCmd,
ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs))
Expand Down

0 comments on commit f64bfa1

Please sign in to comment.