|
21 | 21 | # zsh completions for {{ .App.HelpName }} |
22 | 22 | # |
23 | 23 | # References: |
24 | | -# - https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/Console/Resources/completion.zsh |
25 | 24 | # - https://github.com/posener/complete/blob/master/install/zsh.go |
26 | | -# - https://stackoverflow.com/a/13547531 |
27 | 25 | # |
28 | 26 |
|
29 | | -# this wrapper function allows us to let Symfony knows how to call the |
30 | | -# `bin/console` using the Symfony CLI binary (to ensure the right env and PHP |
31 | | -# versions are used) |
32 | | -_{{ .App.HelpName }}_console() { |
33 | | - # shellcheck disable=SC2068 |
34 | | - {{ .CurrentBinaryInvocation }} console $@ |
35 | | -} |
36 | | - |
37 | | -_complete_{{ .App.HelpName }}() { |
38 | | - local lastParam flagPrefix requestComp out comp |
39 | | - local -a completions |
40 | | - |
41 | | - # The user could have moved the cursor backwards on the command-line. |
42 | | - # We need to trigger completion from the $CURRENT location, so we need |
43 | | - # to truncate the command-line ($words) up to the $CURRENT location. |
44 | | - # (We cannot use $CURSOR as its value does not work when a command is an alias.) |
45 | | - words=("${=words[1,CURRENT]}") lastParam=${words[-1]} |
46 | | - |
47 | | - # For zsh, when completing a flag with an = (e.g., {{ .App.HelpName }} -n=<TAB>) |
48 | | - # completions must be prefixed with the flag |
49 | | - setopt local_options BASH_REMATCH |
50 | | - if [[ "${lastParam}" =~ '-.*=' ]]; then |
51 | | - # We are dealing with a flag with an = |
52 | | - flagPrefix="-P ${BASH_REMATCH}" |
53 | | - fi |
54 | | - |
55 | | - # detect if we are in a wrapper command and need to "forward" completion to it |
56 | | - for ((i = 1; i <= $#words; i++)); do |
57 | | - if [[ "${words[i]}" != -* ]]; then |
58 | | - case "${words[i]}" in |
59 | | - console|php|pecl|composer|run|local:run) |
60 | | - shift words |
61 | | - (( CURRENT-- )) |
62 | | - _SF_CMD="_{{ .App.HelpName }}_console" _normal |
63 | | - return |
64 | | - esac; |
65 | | - fi |
66 | | - done |
67 | | - |
68 | | - while IFS='\n' read -r comp; do |
69 | | - if [ -n "$comp" ]; then |
70 | | - # We first need to escape any : as part of the completion itself. |
71 | | - comp=${comp//:/\\:} |
72 | | - completions+=${comp} |
73 | | - fi |
74 | | - done < <(COMP_LINE="$words" ${words[0]} ${_SF_CMD:-${words[1]}} self:autocomplete) |
75 | | - |
76 | | - # Let inbuilt _describe handle completions |
77 | | - eval _describe "completions" completions $flagPrefix |
78 | | -} |
79 | | - |
80 | | -compdef _complete_{{ .App.HelpName }} {{ .App.HelpName }} |
| 27 | +autoload -U +X bashcompinit && bashcompinit |
| 28 | +complete -o nospace -C "{{ .CurrentBinaryInvocation }} self:autocomplete" {{ .App.HelpName }} |
0 commit comments