|
1 | 1 | command -v cargo >/dev/null 2>&1 &&
|
2 | 2 | _cargo()
|
3 | 3 | {
|
4 |
| - local cur prev words cword cmd |
| 4 | + local cur prev words cword |
5 | 5 | _get_comp_words_by_ref cur prev words cword
|
6 | 6 |
|
7 | 7 | COMPREPLY=()
|
8 | 8 |
|
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 |
10 | 19 |
|
11 | 20 | local vcs='git hg none'
|
12 | 21 | local color='auto always never'
|
@@ -57,13 +66,16 @@ _cargo()
|
57 | 66 | local opt__version="$opt_help $opt_verbose $opt_color"
|
58 | 67 | local opt__yank="$opt_common $opt_lock --vers --undo --index --token"
|
59 | 68 |
|
60 |
| - if [[ $cword -eq 1 ]]; then |
| 69 | + if [[ $cmd_i -ge $nwords-1 ]]; then |
| 70 | + # Completion before or at the command. |
61 | 71 | if [[ "$cur" == -* ]]; then
|
62 | 72 | COMPREPLY=( $( compgen -W "${opt___nocmd}" -- "$cur" ) )
|
| 73 | + elif [[ "$cur" == +* ]]; then |
| 74 | + COMPREPLY=( $( compgen -W "$(_toolchains)" -- "$cur" ) ) |
63 | 75 | else
|
64 | 76 | COMPREPLY=( $( compgen -W "$__cargo_commands" -- "$cur" ) )
|
65 | 77 | fi
|
66 |
| - elif [[ $cword -ge 2 ]]; then |
| 78 | + else |
67 | 79 | case "${prev}" in
|
68 | 80 | --vcs)
|
69 | 81 | COMPREPLY=( $( compgen -W "$vcs" -- "$cur" ) )
|
@@ -208,4 +220,27 @@ _get_targets(){
|
208 | 220 | done
|
209 | 221 | echo "${TARGETS[@]}"
|
210 | 222 | }
|
| 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 | +
|
211 | 246 | # vim:ft=sh
|
0 commit comments