Skip to content

Commit 38332ef

Browse files
authored
Merge pull request #16 from tucksaun/feat/console-completion-template-override
refacto: allow autocompletion templates to be overridden
2 parents 4bda854 + fb31b77 commit 38332ef

File tree

4 files changed

+7
-104
lines changed

4 files changed

+7
-104
lines changed

completion_installer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import (
1616
"github.com/symfony-cli/terminal"
1717
)
1818

19-
// completionTemplates holds our shell completions templates.
19+
// CompletionTemplates holds our shell completions templates.
2020
//
2121
//go:embed resources/completion.*
22-
var completionTemplates embed.FS
22+
var CompletionTemplates embed.FS
2323

2424
var shellAutoCompleteInstallCommand = &Command{
2525
Category: "self",
@@ -110,7 +110,7 @@ Add this to the end of your shell configuration file (e.g. <info>"{{ call .RcFil
110110
shell = guessShell()
111111
}
112112

113-
templates, err := template.ParseFS(completionTemplates, "resources/*")
113+
templates, err := template.ParseFS(CompletionTemplates, "resources/*")
114114
if err != nil {
115115
return errors.WithStack(err)
116116
}

resources/completion.bash

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,7 @@
1818
# Bash completions for the CLI binary
1919
#
2020
# References:
21-
# - https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/Console/Resources/completion.bash
2221
# - https://github.com/posener/complete/blob/master/install/bash.go
23-
# - https://github.com/scop/bash-completion/blob/master/completions/sudo
2422
#
2523

26-
# this wrapper function allows us to let Symfony knows how to call the
27-
# `bin/console` using the Symfony CLI binary (to ensure the right env and PHP
28-
# versions are used)
29-
_{{ .App.HelpName }}_console() {
30-
# shellcheck disable=SC2068
31-
{{ .CurrentBinaryInvocation }} console $@
32-
}
33-
34-
_complete_{{ .App.HelpName }}() {
35-
36-
# Use the default completion for shell redirect operators.
37-
for w in '>' '>>' '&>' '<'; do
38-
if [[ $w = "${COMP_WORDS[COMP_CWORD-1]}" ]]; then
39-
compopt -o filenames
40-
COMPREPLY=($(compgen -f -- "${COMP_WORDS[COMP_CWORD]}"))
41-
return 0
42-
fi
43-
done
44-
45-
for (( i=1; i <= COMP_CWORD; i++ )); do
46-
if [[ "${COMP_WORDS[i]}" != -* ]]; then
47-
case "${COMP_WORDS[i]}" in
48-
console|php|pecl|composer|run|local:run)
49-
_SF_CMD="_{{ .App.HelpName }}_console" _command_offset $i
50-
return
51-
esac;
52-
fi
53-
done
54-
55-
IFS=$'\n' COMPREPLY=( $(COMP_LINE="${COMP_LINE}" COMP_POINT="${COMP_POINT}" COMP_DEBUG="$COMP_DEBUG" {{ .CurrentBinaryPath }} self:autocomplete) )
56-
}
57-
58-
complete -F _complete_{{ .App.HelpName }} {{ .App.HelpName }}
24+
complete -C "{{ .CurrentBinaryPath }} self:autocomplete" {{ .App.HelpName }}

resources/completion.fish

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
# Fish completions for the CLI binary
1919
#
2020
# References:
21-
# - https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/Console/Resources/completion.fish
2221
# - https://github.com/posener/complete/blob/master/install/fish.go
23-
# - https://github.com/fish-shell/fish-shell/blob/master/share/completions/sudo.fish
2422
#
2523

2624
function __complete_{{ .App.HelpName }}
@@ -30,13 +28,4 @@ function __complete_{{ .App.HelpName }}
3028
{{ .CurrentBinaryInvocation }} self:autocomplete
3129
end
3230

33-
# this wrapper function allows us to call Symfony autocompletion letting it
34-
# knows how to call the `bin/console` using the Symfony CLI binary (to ensure
35-
# the right env and PHP versions are used)
36-
function __complete_{{ .App.HelpName }}_console
37-
set -x _SF_CMD "{{ .CurrentBinaryInvocation }}" "console"
38-
_sf_console
39-
end
40-
41-
complete -f -c '{{ .App.HelpName }}' -n "__fish_seen_subcommand_from console" -a '(__complete_{{ .App.HelpName }}_console)' -f
42-
complete -f -c '{{ .App.HelpName }}' -n "not __fish_seen_subcommand_from console php pecl composer run local:run" -a '(__complete_{{ .App.HelpName }})'
31+
complete -f -c '{{ .App.HelpName }}' -a '(__complete_{{ .App.HelpName }})'

resources/completion.zsh

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,60 +21,8 @@
2121
# zsh completions for {{ .App.HelpName }}
2222
#
2323
# References:
24-
# - https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/Console/Resources/completion.zsh
2524
# - https://github.com/posener/complete/blob/master/install/zsh.go
26-
# - https://stackoverflow.com/a/13547531
2725
#
2826

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

Comments
 (0)