|
| 1 | +#compdef {{ .App.HelpName }} |
| 2 | + |
| 3 | +# Copyright (c) 2021-present Fabien Potencier <fabien@symfony.com> |
| 4 | +# |
| 5 | +# This file is part of Symfony CLI project |
| 6 | +# |
| 7 | +# This program is free software: you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU Affero General Public License as |
| 9 | +# published by the Free Software Foundation, either version 3 of the |
| 10 | +# License, or (at your option) any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU Affero General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Affero General Public License |
| 18 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +# |
| 21 | +# zsh completions for {{ .App.HelpName }} |
| 22 | +# |
| 23 | +# References: |
| 24 | +# - https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/Console/Resources/completion.zsh |
| 25 | +# - https://github.com/posener/complete/blob/master/install/zsh.go |
| 26 | +# - https://stackoverflow.com/a/13547531 |
| 27 | +# |
| 28 | + |
| 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) |
| 60 | + shift words |
| 61 | + (( CURRENT-- )) |
| 62 | + _SF_CMD="_{{ .App.HelpName }}_console" _normal |
| 63 | + return |
| 64 | + ;; |
| 65 | + composer{{range $name := (.App.Command "php").Names }}|{{$name}}{{end}}) |
| 66 | + shift words |
| 67 | + (( CURRENT-- )) |
| 68 | + _normal |
| 69 | + return |
| 70 | + ;; |
| 71 | + {{range $i, $name := (.App.Command "local:run").Names }}{{if $i}}|{{end}}{{$name}}{{end}}) |
| 72 | + shift words |
| 73 | + (( CURRENT-- )) |
| 74 | + shift words |
| 75 | + (( CURRENT-- )) |
| 76 | + _normal |
| 77 | + return |
| 78 | + ;; |
| 79 | + esac; |
| 80 | + fi |
| 81 | + done |
| 82 | + |
| 83 | + while IFS='\n' read -r comp; do |
| 84 | + if [ -n "$comp" ]; then |
| 85 | + # We first need to escape any : as part of the completion itself. |
| 86 | + comp=${comp//:/\\:} |
| 87 | + completions+=${comp} |
| 88 | + fi |
| 89 | + done < <(COMP_LINE="$words" ${words[0]} ${_SF_CMD:-${words[1]}} self:autocomplete) |
| 90 | + |
| 91 | + # Let inbuilt _describe handle completions |
| 92 | + eval _describe "completions" completions $flagPrefix |
| 93 | +} |
| 94 | + |
| 95 | +compdef _complete_{{ .App.HelpName }} {{ .App.HelpName }} |
0 commit comments