symfony6.plugin.zsh 2.7 KB

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