Skip to content

Commit 1b17388

Browse files
committed
Auto merge of #6644 - ehuss:bashcomp-updates, r=dwijnand
Some updates to bash completion. - Be smarter about editing an existing command (currently completion only worked well at the end of the line). - Update for new command line options and commands. - Support libtest completions. - Fallback to filename completion in some circumstances. - Use rustup to complete `--target`.
2 parents b296129 + 5773aaa commit 1b17388

File tree

1 file changed

+63
-52
lines changed

1 file changed

+63
-52
lines changed

src/etc/cargo.bashcomp.sh

Lines changed: 63 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,65 +8,84 @@ _cargo()
88

99
# Skip past - and + options to find the command.
1010
local nwords=${#words[@]}
11-
local cmd_i cmd
11+
local cmd_i cmd dd_i
1212
for (( cmd_i=1; cmd_i<$nwords; cmd_i++ ));
1313
do
1414
if [[ ! "${words[$cmd_i]}" =~ ^[+-] ]]; then
1515
cmd="${words[$cmd_i]}"
1616
break
1717
fi
1818
done
19+
# Find the location of the -- separator.
20+
for (( dd_i=1; dd_i<$nwords-1; dd_i++ ));
21+
do
22+
if [[ "${words[$dd_i]}" = "--" ]]; then
23+
break
24+
fi
25+
done
1926

20-
local vcs='git hg none'
27+
local vcs='git hg none pijul fossil'
2128
local color='auto always never'
22-
local msg_format='human json'
29+
local msg_format='human json short'
2330

2431
local opt_help='-h --help'
2532
local opt_verbose='-v --verbose'
2633
local opt_quiet='-q --quiet'
2734
local opt_color='--color'
2835
local opt_common="$opt_help $opt_verbose $opt_quiet $opt_color"
36+
local opt_pkg_spec='-p --package --all --exclude'
2937
local opt_pkg='-p --package'
3038
local opt_feat='--features --all-features --no-default-features'
3139
local opt_mani='--manifest-path'
3240
local opt_jobs='-j --jobs'
3341
local opt_force='-f --force'
3442
local opt_test='--test --bench'
3543
local opt_lock='--frozen --locked'
44+
local opt_targets="--lib --bin --bins --example --examples --test --tests --bench --benches --all-targets"
3645

37-
local opt___nocmd="$opt_common -V --version --list"
38-
local opt__bench="$opt_common $opt_pkg $opt_feat $opt_mani $opt_lock $opt_jobs $opt_test --message-format --target --lib --bin --example --no-run"
39-
local opt__build="$opt_common $opt_pkg $opt_feat $opt_mani $opt_lock $opt_jobs $opt_test --message-format --target --lib --bin --example --release"
40-
local opt__check="$opt_common $opt_pkg $opt_feat $opt_mani $opt_lock $opt_jobs $opt_test --message-format --target --lib --bin --example --release"
41-
local opt__clean="$opt_common $opt_pkg $opt_mani $opt_lock --target --release"
42-
local opt__doc="$opt_common $opt_pkg $opt_feat $opt_mani $opt_lock $opt_jobs --message-format --bin --lib --target --open --no-deps --release"
46+
local opt___nocmd="$opt_common -V --version --list --explain"
47+
local opt__bench="$opt_common $opt_pkg_spec $opt_feat $opt_mani $opt_lock $opt_jobs $opt_test $opt_targets --message-format --target --no-run --no-fail-fast --target-dir"
48+
local opt__build="$opt_common $opt_pkg_spec $opt_feat $opt_mani $opt_lock $opt_jobs $opt_test $opt_targets --message-format --target --release --target-dir"
49+
local opt__check="$opt_common $opt_pkg_spec $opt_feat $opt_mani $opt_lock $opt_jobs $opt_test $opt_targets --message-format --target --release --profile --target-dir"
50+
local opt__clean="$opt_common $opt_pkg $opt_mani $opt_lock --target --release --doc --target-dir"
51+
local opt__doc="$opt_common $opt_pkg_spec $opt_feat $opt_mani $opt_lock $opt_jobs --message-format --bin --bins --lib --target --open --no-deps --release --document-private-items --target-dir"
4352
local opt__fetch="$opt_common $opt_mani $opt_lock"
53+
local opt__fix="$opt_common $opt_pkg_spec $opt_feat $opt_mani $opt_jobs $opt_targets $opt_lock --release --target --message-format --prepare-for --broken-code --edition --edition-idioms --allow-no-vcs --allow-dirty --allow-staged --profile --target-dir"
4454
local opt__generate_lockfile="${opt__fetch}"
4555
local opt__git_checkout="$opt_common $opt_lock --reference --url"
4656
local opt__help="$opt_help"
47-
local opt__init="$opt_common $opt_lock --bin --lib --name --vcs"
48-
local opt__install="$opt_common $opt_feat $opt_jobs $opt_lock $opt_force --bin --branch --debug --example --git --list --path --rev --root --tag --vers"
57+
local opt__init="$opt_common $opt_lock --bin --lib --name --vcs --edition --registry"
58+
local opt__install="$opt_common $opt_feat $opt_jobs $opt_lock $opt_force --bin --bins --branch --debug --example --examples --git --list --path --rev --root --tag --version --registry --target"
4959
local opt__locate_project="$opt_mani -h --help"
50-
local opt__login="$opt_common $opt_lock --host"
51-
local opt__metadata="$opt_common $opt_feat $opt_mani $opt_lock --format-version --no-deps"
52-
local opt__new="$opt_common $opt_lock --vcs --bin --lib --name"
53-
local opt__owner="$opt_common $opt_lock -a --add -r --remove -l --list --index --token"
54-
local opt__package="$opt_common $opt_mani $opt_lock $opt_jobs --allow-dirty -l --list --no-verify --no-metadata"
60+
local opt__login="$opt_common $opt_lock --host --registry"
61+
local opt__metadata="$opt_common $opt_feat $opt_mani $opt_lock --format-version=1 --no-deps"
62+
local opt__new="$opt_common $opt_lock --vcs --bin --lib --name --edition --registry"
63+
local opt__owner="$opt_common $opt_lock -a --add -r --remove -l --list --index --token --registry"
64+
local opt__package="$opt_common $opt_mani $opt_feat $opt_lock $opt_jobs --allow-dirty -l --list --no-verify --no-metadata --target --target-dir"
5565
local opt__pkgid="${opt__fetch} $opt_pkg"
56-
local opt__publish="$opt_common $opt_mani $opt_lock $opt_jobs --allow-dirty --dry-run --host --token --no-verify"
57-
local opt__read_manifest="$opt_help $opt_verbose $opt_mani $opt_color --no-deps"
58-
local opt__run="$opt_common $opt_feat $opt_mani $opt_lock $opt_jobs --message-format --target --bin --example --release"
59-
local opt__rustc="$opt_common $opt_pkg $opt_feat $opt_mani $opt_lock $opt_jobs $opt_test --message-format --profile --target --lib --bin --example --release"
60-
local opt__rustdoc="$opt_common $opt_pkg $opt_feat $opt_mani $opt_lock $opt_jobs $opt_test --message-format --target --lib --bin --example --release --open"
61-
local opt__search="$opt_common $opt_lock --host --limit"
62-
local opt__test="$opt_common $opt_pkg $opt_feat $opt_mani $opt_lock $opt_jobs $opt_test --message-format --all --doc --target --lib --bin --example --no-run --release --no-fail-fast"
63-
local opt__uninstall="$opt_common $opt_lock --bin --root"
64-
local opt__update="$opt_common $opt_pkg $opt_mani $opt_lock --aggressive --precise"
66+
local opt__publish="$opt_common $opt_mani $opt_feat $opt_lock $opt_jobs --allow-dirty --dry-run --host --token --no-verify --index --registry --target --target-dir"
67+
local opt__read_manifest="$opt_help $opt_quiet $opt_verbose $opt_mani $opt_color "
68+
local opt__run="$opt_common $opt_pkg $opt_feat $opt_mani $opt_lock $opt_jobs --message-format --target --bin --example --release --target-dir"
69+
local opt__rustc="$opt_common $opt_pkg $opt_feat $opt_mani $opt_lock $opt_jobs $opt_test $opt_targets --message-format --profile --target --release --target-dir"
70+
local opt__rustdoc="$opt_common $opt_pkg $opt_feat $opt_mani $opt_lock $opt_jobs $opt_test $opt_targets --message-format --target --release --open --target-dir"
71+
local opt__search="$opt_common $opt_lock --host --limit --index --limit --registry"
72+
local opt__test="$opt_common $opt_pkg_spec $opt_feat $opt_mani $opt_lock $opt_jobs $opt_test $opt_targets --message-format --doc --target --no-run --release --no-fail-fast --target-dir"
73+
local opt__uninstall="$opt_common $opt_lock $opt_pkg_spec --bin --root"
74+
local opt__update="$opt_common $opt_pkg_spec $opt_mani $opt_lock --aggressive --precise --dry-run"
6575
local opt__verify_project="${opt__fetch}"
6676
local opt__version="$opt_help $opt_verbose $opt_color"
67-
local opt__yank="$opt_common $opt_lock --vers --undo --index --token"
77+
local opt__yank="$opt_common $opt_lock --vers --undo --index --token --registry"
78+
local opt__libtest="--help --include-ignored --ignored --test --bench --list --logfile --nocapture --test-threads --skip -q --quiet --exact --color --format"
6879

69-
if [[ $cmd_i -ge $nwords-1 ]]; then
80+
if [[ $cword -gt $dd_i ]]; then
81+
# Completion after -- separator.
82+
if [[ "${cmd}" = @(test|bench) ]]; then
83+
COMPREPLY=( $( compgen -W "${opt__libtest}" -- "$cur" ) )
84+
else
85+
# Fallback to filename completion, useful with `cargo run`.
86+
_filedir
87+
fi
88+
elif [[ $cword -le $cmd_i ]]; then
7089
# Completion before or at the command.
7190
if [[ "$cur" == -* ]]; then
7291
COMPREPLY=( $( compgen -W "${opt___nocmd}" -- "$cur" ) )
@@ -104,12 +123,20 @@ _cargo()
104123
--target)
105124
COMPREPLY=( $( compgen -W "$(_get_targets)" -- "$cur" ) )
106125
;;
126+
--target-dir)
127+
_filedir -d
128+
;;
107129
help)
108130
COMPREPLY=( $( compgen -W "$__cargo_commands" -- "$cur" ) )
109131
;;
110132
*)
111133
local opt_var=opt__${cmd//-/_}
112-
COMPREPLY=( $( compgen -W "${!opt_var}" -- "$cur" ) )
134+
if [[ -z "${!opt_var}" ]]; then
135+
# Fallback to filename completion.
136+
_filedir
137+
else
138+
COMPREPLY=( $( compgen -W "${!opt_var}" -- "$cur" ) )
139+
fi
113140
;;
114141
esac
115142
fi
@@ -197,31 +224,15 @@ _get_examples(){
197224
}
198225

199226
_get_targets(){
200-
local CURRENT_PATH
201-
if [ `uname -o` == "Cygwin" -a -f "$PWD"/Cargo.toml ]; then
202-
CURRENT_PATH=$PWD
203-
else
204-
CURRENT_PATH=$(_locate_manifest)
205-
fi
206-
if [[ -z "$CURRENT_PATH" ]]; then
207-
return 1
208-
fi
209-
local TARGETS=()
210-
local FIND_PATHS=( "/" )
211-
local FIND_PATH LINES LINE
212-
while [[ "$CURRENT_PATH" != "/" ]]; do
213-
FIND_PATHS+=( "$CURRENT_PATH" )
214-
CURRENT_PATH=$(dirname $CURRENT_PATH)
215-
done
216-
for FIND_PATH in ${FIND_PATHS[@]}; do
217-
if [[ -f "$FIND_PATH"/.cargo/config ]]; then
218-
LINES=( `grep "$FIND_PATH"/.cargo/config -e "^\[target\."` )
219-
for LINE in ${LINES[@]}; do
220-
TARGETS+=(`sed 's/^\[target\.\(.*\)\]$/\1/' <<< $LINE`)
221-
done
227+
local result=()
228+
local targets=$(rustup target list)
229+
while read line
230+
do
231+
if [[ "$line" =~ default|installed ]]; then
232+
result+=("${line%% *}")
222233
fi
223-
done
224-
echo "${TARGETS[@]}"
234+
done <<< "$targets"
235+
echo "${result[@]}"
225236
}
226237

227238
_toolchains(){

0 commit comments

Comments
 (0)