Skip to content

Commit 5927a1d

Browse files
committed
Auto merge of #5111 - ehuss:bashc-toolchains, r=alexcrichton
Support +toolchain rustup override in bash completions. Fixes #5107
2 parents 078f333 + 6965495 commit 5927a1d

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

src/etc/cargo.bashcomp.sh

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
command -v cargo >/dev/null 2>&1 &&
22
_cargo()
33
{
4-
local cur prev words cword cmd
4+
local cur prev words cword
55
_get_comp_words_by_ref cur prev words cword
66

77
COMPREPLY=()
88

9-
cmd=${words[1]}
9+
# Skip past - and + options to find the command.
10+
local nwords=${#words[@]}
11+
local cmd_i cmd
12+
for (( cmd_i=1; cmd_i<$nwords; cmd_i++ ));
13+
do
14+
if [[ ! "${words[$cmd_i]}" =~ ^[+-] ]]; then
15+
cmd="${words[$cmd_i]}"
16+
break
17+
fi
18+
done
1019

1120
local vcs='git hg none'
1221
local color='auto always never'
@@ -57,13 +66,16 @@ _cargo()
5766
local opt__version="$opt_help $opt_verbose $opt_color"
5867
local opt__yank="$opt_common $opt_lock --vers --undo --index --token"
5968

60-
if [[ $cword -eq 1 ]]; then
69+
if [[ $cmd_i -ge $nwords-1 ]]; then
70+
# Completion before or at the command.
6171
if [[ "$cur" == -* ]]; then
6272
COMPREPLY=( $( compgen -W "${opt___nocmd}" -- "$cur" ) )
73+
elif [[ "$cur" == +* ]]; then
74+
COMPREPLY=( $( compgen -W "$(_toolchains)" -- "$cur" ) )
6375
else
6476
COMPREPLY=( $( compgen -W "$__cargo_commands" -- "$cur" ) )
6577
fi
66-
elif [[ $cword -ge 2 ]]; then
78+
else
6779
case "${prev}" in
6880
--vcs)
6981
COMPREPLY=( $( compgen -W "$vcs" -- "$cur" ) )
@@ -208,4 +220,27 @@ _get_targets(){
208220
done
209221
echo "${TARGETS[@]}"
210222
}
223+
224+
_toolchains(){
225+
local result=()
226+
local toolchains=$(rustup toolchain list)
227+
local channels="nightly|beta|stable|[0-9]\.[0-9]{1,2}\.[0-9]"
228+
local date="[0-9]{4}-[0-9]{2}-[0-9]{2}"
229+
while read line
230+
do
231+
# Strip " (default)"
232+
line=${line%% *}
233+
if [[ "$line" =~ ^($channels)(-($date))?(-.*) ]]; then
234+
if [[ -z ${BASH_REMATCH[3]} ]]; then
235+
result+=("+${BASH_REMATCH[1]}")
236+
else
237+
# channel-date
238+
result+=("+${BASH_REMATCH[1]}-${BASH_REMATCH[3]}")
239+
fi
240+
result+=("+$line")
241+
fi
242+
done <<< "$toolchains"
243+
echo "${result[@]}"
244+
}
245+
211246
# vim:ft=sh

0 commit comments

Comments
 (0)