6a0116f
# This file is part of the Symfony package.
6a0116f
#
6a0116f
# (c) Fabien Potencier <fabien@symfony.com>
6a0116f
#
6a0116f
# For the full copyright and license information, please view
6a0116f
# https://symfony.com/doc/current/contributing/code/license.html
6a0116f
6a0116f
_sf_composer() {
6a0116f
    # Use newline as only separator to allow space in completion values
6a0116f
    IFS=$'\n'
6a0116f
    local sf_cmd="${COMP_WORDS[0]}"
6a0116f
6a0116f
    # for an alias, get the real script behind it
0c41f6e
    sf_cmd_type=$(type -t $sf_cmd)
0c41f6e
    if [[ $sf_cmd_type == "alias" ]]; then
6a0116f
        sf_cmd=$(alias $sf_cmd | sed -E "s/alias $sf_cmd='(.*)'/\1/")
0c41f6e
    elif [[ $sf_cmd_type == "file" ]]; then
6384f21
        sf_cmd=$(type -p $sf_cmd)
6a0116f
    fi
6a0116f
0c41f6e
    if [[ $sf_cmd_type != "function" && ! -x $sf_cmd ]]; then
6a0116f
        return 1
6a0116f
    fi
6a0116f
6a0116f
    local cur prev words cword
6a0116f
    _get_comp_words_by_ref -n := cur prev words cword
6a0116f
474ee51
    local completecmd=("$sf_cmd" "_complete" "--no-interaction" "-sbash" "-c$cword" "-S2.7.6")
6a0116f
    for w in ${words[@]}; do
6a0116f
        w=$(printf -- '%b' "$w")
6a0116f
        # remove quotes from typed values
6a0116f
        quote="${w:0:1}"
6a0116f
        if [ "$quote" == \' ]; then
6a0116f
            w="${w%\'}"
6a0116f
            w="${w#\'}"
6a0116f
        elif [ "$quote" == \" ]; then
6a0116f
            w="${w%\"}"
6a0116f
            w="${w#\"}"
6a0116f
        fi
6a0116f
        # empty values are ignored
6a0116f
        if [ ! -z "$w" ]; then
6a0116f
            completecmd+=("-i$w")
6a0116f
        fi
6a0116f
    done
6a0116f
6a0116f
    local sfcomplete
6a0116f
    if sfcomplete=$(${completecmd[@]} 2>&1;; then
6a0116f
        local quote suggestions
6a0116f
        quote=${cur:0:1}
6a0116f
6a0116f
        # Use single quotes by default if suggestions contains backslash (FQCN)
6a0116f
        if [ "$quote" == '' ] && [[ "$sfcomplete" =~ \\ ]]; then
6a0116f
            quote=\'
6a0116f
        fi
6a0116f
6a0116f
        if [ "$quote" == \' ]; then
6a0116f
            # single quotes: no additional escaping (does not accept ' in values)
6a0116f
            suggestions=$(for s in $sfcomplete; do printf $'%q%q%q\n' "$quote" "$s" "$quote"; done)
6a0116f
        elif [ "$quote" == \" ]; then
6a0116f
            # double quotes: double escaping for \ $ ` "
6a0116f
            suggestions=$(for s in $sfcomplete; do
6a0116f
                s=${s//\\/\\\\}
6a0116f
                s=${s//\$/\\\$}
6a0116f
                s=${s//\`/\\\`}
6a0116f
                s=${s//\"/\\\"}
6a0116f
                printf $'%q%q%q\n' "$quote" "$s" "$quote";
6a0116f
            done)
6a0116f
        else
6a0116f
            # no quotes: double escaping
6a0116f
            suggestions=$(for s in $sfcomplete; do printf $'%q\n' $(printf '%q' "$s"); done)
6a0116f
        fi
6a0116f
        COMPREPLY=($(IFS=$'\n' compgen -W "$suggestions" -- $(printf -- "%q" "$cur")))
6a0116f
        __ltrim_colon_completions "$cur"
6a0116f
    else
6a0116f
        if [[ "$sfcomplete" != *"Command \"_complete\" is not defined."* ]]; then
6a0116f
            >&2 echo
6a0116f
            >&2 echo $sfcomplete
6a0116f
        fi
6a0116f
6a0116f
        return 1
6a0116f
    fi
6a0116f
}
6a0116f
6a0116f
complete -F _sf_composer composer