Skip to content

Commit a63ff17

Browse files
bb010gjpgrayson
authored andcommitted
Zsh refine completion for passthrough options
Ensure `zparseopts` is loaded from `zsh/zutil`. Pass through options after the `CURRENT` option. Use shell builtins (not `seq`) & optimize.
1 parent e9f794e commit a63ff17

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

completion/stgit.zsh

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,38 +1207,42 @@ __stg_add_args_trailers() {
12071207
}
12081208

12091209
__stg_complete_git_opts() {
1210-
local gitcmd short long
1211-
gitcmd=$1
1210+
local git_cmd short long i
1211+
git_cmd=$1
12121212
short=$2
12131213
long=$3
12141214

12151215
# Parse short and long options (e.g. -O and --diff-opt) from $words.
1216-
declare -a gitopts
1217-
function {
1218-
zparseopts -E -D ${short}+:=gitopts -${long}+:=gitopts 2>/dev/null
1219-
} $words
1216+
declare -a git_opts git_opts_after
1217+
zmodload -F 'zsh/zutil' 'b:zparseopts'
1218+
() { zparseopts -E -D -a git_opts ${short}+: -${long}+: 2>/dev/null; } \
1219+
${(@)words[1,CURRENT]}
1220+
() { zparseopts -E -D -a git_opts_after ${short}+: -${long}+: 2>/dev/null; } \
1221+
${(@)words[CURRENT+1,-1]}
12201222

12211223
# Compose git command line
1222-
words=('git' ${(@)__stg_C_args} ${gitcmd})
1224+
words=('git' ${(@)__stg_C_args} ${git_cmd})
12231225

1224-
# The option values are at the even indexes in the gitopts array.
1226+
# The option values are at the even indexes in the git_opts array.
12251227
# The last option is skipped because it is the partially specified one being
12261228
# completed.
1227-
for i in $(seq 2 2 $(($#gitopts - 2))); do
1229+
for i in {2..2..$(($#git_opts - 2))}; do
12281230
# Need to strip any leading '=' because zparseopts will parse, for example,
12291231
# `--diff-opt=foo` as ('--diff-opt' '=foo').
1230-
words+=${gitopts[i]#=}
1232+
words+=(${git_opts[i]#=})
12311233
done
12321234

12331235
# If the user has not started typing the value, prime it with '-' to force
12341236
# completing only the options (and not arguments) to the git command.
1235-
if [ -z "$PREFIX" -a -z "$SUFFIX" ]; then
1236-
PREFIX='-'
1237-
fi
1238-
words+="$PREFIX$SUFFIX"
1237+
: ${SUFFIX:-${PREFIX:=-}}
1238+
words+=("$PREFIX$SUFFIX")
12391239
(( CURRENT = $#words ))
12401240

1241-
_message -e "git-$gitcmd-option" "git $gitcmd" &&
1241+
for i in {2..2..$#git_opts_after}; do
1242+
words+=(${git_opts_after[i]#=})
1243+
done
1244+
1245+
_message -e "git-$git_cmd-option" "git $git_cmd" &&
12421246
_dispatch git git $commands[git]
12431247
}
12441248

0 commit comments

Comments
 (0)