Skip to content

Commit c1682c7

Browse files
committed
Fix zsh completion when worktree names have spaces
1 parent 06c80d8 commit c1682c7

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

completions/_wt_completion

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@
33
# AUTOCOMPLETION FOR ZSH
44
# Reference: https://zsh.sourceforge.io/Doc/Release/Completion-Widgets.html
55

6-
# wt list: list all the available worktrees
7-
# | awk '{ print $1; }': grab the first column of the output
8-
# | tr "\n" " ": replace line break character with space to put the worktrees on single line
9-
# separated by space
10-
11-
list="$(wt list | awk '{ print $1; }' | tr "\n" " ")"
6+
# Declare an associative array named `opts`
127
declare -A opts
138

9+
# Split the worktree names into an array, line-by-line.
10+
list=("${(@f)$(wt names)}")
11+
1412
# Create associative array with key same as its value
1513
# Completion keywords are taken as keys of arrays and the possible matches are their values
16-
# shwordsplit: iterate over a string separated by space (like sh/bash)
17-
setopt shwordsplit
1814
for item in $list; do
19-
base="$(basename -- "$item")"
20-
opts+=(["$base"]="$base")
15+
# Escape every element's special characters
16+
item=$(printf '%q' "$item")
17+
opts+=(["$item"]="$item")
2118
done
22-
unsetopt shwordsplit
2319

20+
# Add the keys of `opts` as completion options for the `wt` command.
21+
# `-Q` quotes the completion options.
22+
# `-a` specifies that the options are taken from an associative array.
2423
compadd -Qa opts

0 commit comments

Comments
 (0)