Parent directory

symfony6.plugin.zsh

2749 bytes
 1#compdef console
 2
 3# This file is part of the Symfony package.
 4#
 5# (c) Fabien Potencier <fabien@symfony.com>
 6#
 7# For the full copyright and license information, please view
 8# https://symfony.com/doc/current/contributing/code/license.html
 9
10#
11# zsh completions for console
12#
13# References:
14#   - https://github.com/spf13/cobra/blob/master/zsh_completions.go
15#   - https://github.com/symfony/symfony/blob/5.4/src/Symfony/Component/Console/Resources/completion.bash
16#
17_sf_console() {
18    local lastParam flagPrefix requestComp out comp
19    local -a completions
20
21    # The user could have moved the cursor backwards on the command-line.
22    # We need to trigger completion from the $CURRENT location, so we need
23    # to truncate the command-line ($words) up to the $CURRENT location.
24    # (We cannot use $CURSOR as its value does not work when a command is an alias.)
25    words=("${=words[1,CURRENT]}") lastParam=${words[-1]}
26
27    # For zsh, when completing a flag with an = (e.g., console -n=<TAB>)
28    # completions must be prefixed with the flag
29    setopt local_options BASH_REMATCH
30    if [[ "${lastParam}" =~ '-.*=' ]]; then
31        # We are dealing with a flag with an =
32        flagPrefix="-P ${BASH_REMATCH}"
33    fi
34
35    # Prepare the command to obtain completions
36    requestComp="${words[0]} ${words[1]} _complete --no-interaction -szsh -a1 -c$((CURRENT-1))" i=""
37    for w in ${words[@]}; do
38        w=$(printf -- '%b' "$w")
39        # remove quotes from typed values
40        quote="${w:0:1}"
41        if [ "$quote" = \' ]; then
42            w="${w%\'}"
43            w="${w#\'}"
44        elif [ "$quote" = \" ]; then
45            w="${w%\"}"
46            w="${w#\"}"
47        fi
48        # empty values are ignored
49        if [ ! -z "$w" ]; then
50            i="${i}-i${w} "
51        fi
52    done
53
54    # Ensure at least 1 input
55    if [ "${i}" = "" ]; then
56        requestComp="${requestComp} -i\" \""
57    else
58        requestComp="${requestComp} ${i}"
59    fi
60
61    # Use eval to handle any environment variables and such
62    out=$(eval ${requestComp} 2>/dev/null)
63
64    while IFS='\n' read -r comp; do
65        if [ -n "$comp" ]; then
66            # If requested, completions are returned with a description.
67            # The description is preceded by a TAB character.
68            # For zsh's _describe, we need to use a : instead of a TAB.
69            # We first need to escape any : as part of the completion itself.
70            comp=${comp//:/\\:}
71            local tab=$(printf '\t')
72            comp=${comp//$tab/:}
73            completions+=${comp}
74        fi
75    done < <(printf "%s\n" "${out[@]}")
76
77    # Let inbuilt _describe handle completions
78    eval _describe "completions" completions $flagPrefix
79    return $?
80}
81
82compdef _sf_console console