Skip to content

Commit

Permalink
Auto merge of #6644 - ehuss:bashcomp-updates, r=dwijnand
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
bors committed Feb 8, 2019
2 parents b296129 + 5773aaa commit 1b17388
Showing 1 changed file with 63 additions and 52 deletions.
115 changes: 63 additions & 52 deletions src/etc/cargo.bashcomp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,84 @@ _cargo()

# Skip past - and + options to find the command.
local nwords=${#words[@]}
local cmd_i cmd
local cmd_i cmd dd_i
for (( cmd_i=1; cmd_i<$nwords; cmd_i++ ));
do
if [[ ! "${words[$cmd_i]}" =~ ^[+-] ]]; then
cmd="${words[$cmd_i]}"
break
fi
done
# Find the location of the -- separator.
for (( dd_i=1; dd_i<$nwords-1; dd_i++ ));
do
if [[ "${words[$dd_i]}" = "--" ]]; then
break
fi
done

local vcs='git hg none'
local vcs='git hg none pijul fossil'
local color='auto always never'
local msg_format='human json'
local msg_format='human json short'

local opt_help='-h --help'
local opt_verbose='-v --verbose'
local opt_quiet='-q --quiet'
local opt_color='--color'
local opt_common="$opt_help $opt_verbose $opt_quiet $opt_color"
local opt_pkg_spec='-p --package --all --exclude'
local opt_pkg='-p --package'
local opt_feat='--features --all-features --no-default-features'
local opt_mani='--manifest-path'
local opt_jobs='-j --jobs'
local opt_force='-f --force'
local opt_test='--test --bench'
local opt_lock='--frozen --locked'
local opt_targets="--lib --bin --bins --example --examples --test --tests --bench --benches --all-targets"

local opt___nocmd="$opt_common -V --version --list"
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"
local opt__build="$opt_common $opt_pkg $opt_feat $opt_mani $opt_lock $opt_jobs $opt_test --message-format --target --lib --bin --example --release"
local opt__check="$opt_common $opt_pkg $opt_feat $opt_mani $opt_lock $opt_jobs $opt_test --message-format --target --lib --bin --example --release"
local opt__clean="$opt_common $opt_pkg $opt_mani $opt_lock --target --release"
local opt__doc="$opt_common $opt_pkg $opt_feat $opt_mani $opt_lock $opt_jobs --message-format --bin --lib --target --open --no-deps --release"
local opt___nocmd="$opt_common -V --version --list --explain"
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"
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"
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"
local opt__clean="$opt_common $opt_pkg $opt_mani $opt_lock --target --release --doc --target-dir"
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"
local opt__fetch="$opt_common $opt_mani $opt_lock"
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"
local opt__generate_lockfile="${opt__fetch}"
local opt__git_checkout="$opt_common $opt_lock --reference --url"
local opt__help="$opt_help"
local opt__init="$opt_common $opt_lock --bin --lib --name --vcs"
local opt__install="$opt_common $opt_feat $opt_jobs $opt_lock $opt_force --bin --branch --debug --example --git --list --path --rev --root --tag --vers"
local opt__init="$opt_common $opt_lock --bin --lib --name --vcs --edition --registry"
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"
local opt__locate_project="$opt_mani -h --help"
local opt__login="$opt_common $opt_lock --host"
local opt__metadata="$opt_common $opt_feat $opt_mani $opt_lock --format-version --no-deps"
local opt__new="$opt_common $opt_lock --vcs --bin --lib --name"
local opt__owner="$opt_common $opt_lock -a --add -r --remove -l --list --index --token"
local opt__package="$opt_common $opt_mani $opt_lock $opt_jobs --allow-dirty -l --list --no-verify --no-metadata"
local opt__login="$opt_common $opt_lock --host --registry"
local opt__metadata="$opt_common $opt_feat $opt_mani $opt_lock --format-version=1 --no-deps"
local opt__new="$opt_common $opt_lock --vcs --bin --lib --name --edition --registry"
local opt__owner="$opt_common $opt_lock -a --add -r --remove -l --list --index --token --registry"
local opt__package="$opt_common $opt_mani $opt_feat $opt_lock $opt_jobs --allow-dirty -l --list --no-verify --no-metadata --target --target-dir"
local opt__pkgid="${opt__fetch} $opt_pkg"
local opt__publish="$opt_common $opt_mani $opt_lock $opt_jobs --allow-dirty --dry-run --host --token --no-verify"
local opt__read_manifest="$opt_help $opt_verbose $opt_mani $opt_color --no-deps"
local opt__run="$opt_common $opt_feat $opt_mani $opt_lock $opt_jobs --message-format --target --bin --example --release"
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"
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"
local opt__search="$opt_common $opt_lock --host --limit"
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"
local opt__uninstall="$opt_common $opt_lock --bin --root"
local opt__update="$opt_common $opt_pkg $opt_mani $opt_lock --aggressive --precise"
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"
local opt__read_manifest="$opt_help $opt_quiet $opt_verbose $opt_mani $opt_color "
local opt__run="$opt_common $opt_pkg $opt_feat $opt_mani $opt_lock $opt_jobs --message-format --target --bin --example --release --target-dir"
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"
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"
local opt__search="$opt_common $opt_lock --host --limit --index --limit --registry"
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"
local opt__uninstall="$opt_common $opt_lock $opt_pkg_spec --bin --root"
local opt__update="$opt_common $opt_pkg_spec $opt_mani $opt_lock --aggressive --precise --dry-run"
local opt__verify_project="${opt__fetch}"
local opt__version="$opt_help $opt_verbose $opt_color"
local opt__yank="$opt_common $opt_lock --vers --undo --index --token"
local opt__yank="$opt_common $opt_lock --vers --undo --index --token --registry"
local opt__libtest="--help --include-ignored --ignored --test --bench --list --logfile --nocapture --test-threads --skip -q --quiet --exact --color --format"

if [[ $cmd_i -ge $nwords-1 ]]; then
if [[ $cword -gt $dd_i ]]; then
# Completion after -- separator.
if [[ "${cmd}" = @(test|bench) ]]; then
COMPREPLY=( $( compgen -W "${opt__libtest}" -- "$cur" ) )
else
# Fallback to filename completion, useful with `cargo run`.
_filedir
fi
elif [[ $cword -le $cmd_i ]]; then
# Completion before or at the command.
if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W "${opt___nocmd}" -- "$cur" ) )
Expand Down Expand Up @@ -104,12 +123,20 @@ _cargo()
--target)
COMPREPLY=( $( compgen -W "$(_get_targets)" -- "$cur" ) )
;;
--target-dir)
_filedir -d
;;
help)
COMPREPLY=( $( compgen -W "$__cargo_commands" -- "$cur" ) )
;;
*)
local opt_var=opt__${cmd//-/_}
COMPREPLY=( $( compgen -W "${!opt_var}" -- "$cur" ) )
if [[ -z "${!opt_var}" ]]; then
# Fallback to filename completion.
_filedir
else
COMPREPLY=( $( compgen -W "${!opt_var}" -- "$cur" ) )
fi
;;
esac
fi
Expand Down Expand Up @@ -197,31 +224,15 @@ _get_examples(){
}

_get_targets(){
local CURRENT_PATH
if [ `uname -o` == "Cygwin" -a -f "$PWD"/Cargo.toml ]; then
CURRENT_PATH=$PWD
else
CURRENT_PATH=$(_locate_manifest)
fi
if [[ -z "$CURRENT_PATH" ]]; then
return 1
fi
local TARGETS=()
local FIND_PATHS=( "/" )
local FIND_PATH LINES LINE
while [[ "$CURRENT_PATH" != "/" ]]; do
FIND_PATHS+=( "$CURRENT_PATH" )
CURRENT_PATH=$(dirname $CURRENT_PATH)
done
for FIND_PATH in ${FIND_PATHS[@]}; do
if [[ -f "$FIND_PATH"/.cargo/config ]]; then
LINES=( `grep "$FIND_PATH"/.cargo/config -e "^\[target\."` )
for LINE in ${LINES[@]}; do
TARGETS+=(`sed 's/^\[target\.\(.*\)\]$/\1/' <<< $LINE`)
done
local result=()
local targets=$(rustup target list)
while read line
do
if [[ "$line" =~ default|installed ]]; then
result+=("${line%% *}")
fi
done
echo "${TARGETS[@]}"
done <<< "$targets"
echo "${result[@]}"
}

_toolchains(){
Expand Down

0 comments on commit 1b17388

Please sign in to comment.