Address problems pointed out in the comments.
parent
26c0cd3f6d
commit
516d2c0d4c
|
|
@ -3,13 +3,14 @@
|
|||
# cargo (Rust package manager) completion
|
||||
# This script is taken and modified from https://github.com/rust-lang/cargo/blob/master/src/etc/cargo.bashcomp.sh
|
||||
|
||||
source $HOME/.cargo/env
|
||||
if ! _command_exists cargo; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Required for bash versions < 4.1
|
||||
# Default bash version is 3.2 on latest macOS. See #6874
|
||||
shopt -s extglob
|
||||
|
||||
command -v cargo >/dev/null 2>&1 &&
|
||||
_cargo() {
|
||||
local cur prev words cword
|
||||
_get_comp_words_by_ref cur prev words cword
|
||||
|
|
@ -99,7 +100,7 @@ command -v cargo >/dev/null 2>&1 &&
|
|||
if [[ "$cur" == -* ]]; then
|
||||
COMPREPLY=($(compgen -W "${opt___nocmd}" -- "$cur"))
|
||||
elif [[ "$cur" == +* ]]; then
|
||||
COMPREPLY=($(compgen -W "$(_toolchains)" -- "$cur"))
|
||||
COMPREPLY=($(compgen -W "$(__cargo_toolchains)" -- "$cur"))
|
||||
else
|
||||
COMPREPLY=($(compgen -W "$__cargo_commands" -- "$cur"))
|
||||
fi
|
||||
|
|
@ -118,19 +119,19 @@ command -v cargo >/dev/null 2>&1 &&
|
|||
_filedir toml
|
||||
;;
|
||||
--bin)
|
||||
COMPREPLY=($(compgen -W "$(_bin_names)" -- "$cur"))
|
||||
COMPREPLY=($(compgen -W "$(__cargo_bin_names)" -- "$cur"))
|
||||
;;
|
||||
--test)
|
||||
COMPREPLY=($(compgen -W "$(_test_names)" -- "$cur"))
|
||||
COMPREPLY=($(compgen -W "$(__cargo_test_names)" -- "$cur"))
|
||||
;;
|
||||
--bench)
|
||||
COMPREPLY=($(compgen -W "$(_benchmark_names)" -- "$cur"))
|
||||
COMPREPLY=($(compgen -W "$(__cargo_benchmark_names)" -- "$cur"))
|
||||
;;
|
||||
--example)
|
||||
COMPREPLY=($(compgen -W "$(_get_examples)" -- "$cur"))
|
||||
COMPREPLY=($(compgen -W "$(__cargo_get_examples)" -- "$cur"))
|
||||
;;
|
||||
--target)
|
||||
COMPREPLY=($(compgen -W "$(_get_targets)" -- "$cur"))
|
||||
COMPREPLY=($(compgen -W "$(__cargo_get_targets)" -- "$cur"))
|
||||
;;
|
||||
--target-dir)
|
||||
_filedir -d
|
||||
|
|
@ -153,19 +154,20 @@ command -v cargo >/dev/null 2>&1 &&
|
|||
# compopt does not work in bash version 3
|
||||
|
||||
return 0
|
||||
} &&
|
||||
}
|
||||
|
||||
complete -F _cargo cargo
|
||||
|
||||
__cargo_commands=$(cargo --list 2>/dev/null | awk 'NR>1 {print $1}')
|
||||
|
||||
_locate_manifest() {
|
||||
__cargo_locate_manifest() {
|
||||
cargo locate-project --message-format plain 2>/dev/null
|
||||
}
|
||||
|
||||
# Extracts the values of "name" from the array given in $1 and shows them as
|
||||
# command line options for completion
|
||||
_get_names_from_array() {
|
||||
local manifest=$(_locate_manifest)
|
||||
__cargo_get_names_from_array() {
|
||||
local manifest=$(__cargo_locate_manifest)
|
||||
if [[ -z $manifest ]]; then
|
||||
return 0
|
||||
fi
|
||||
|
|
@ -198,22 +200,22 @@ _get_names_from_array() {
|
|||
}
|
||||
|
||||
#Gets the bin names from the manifest file
|
||||
_bin_names() {
|
||||
_get_names_from_array "bin"
|
||||
__cargo_bin_names() {
|
||||
__cargo_get_names_from_array "bin"
|
||||
}
|
||||
|
||||
#Gets the test names from the manifest file
|
||||
_test_names() {
|
||||
_get_names_from_array "test"
|
||||
__cargo_test_names() {
|
||||
__cargo_get_names_from_array "test"
|
||||
}
|
||||
|
||||
#Gets the bench names from the manifest file
|
||||
_benchmark_names() {
|
||||
_get_names_from_array "bench"
|
||||
__cargo_benchmark_names() {
|
||||
__cargo_get_names_from_array "bench"
|
||||
}
|
||||
|
||||
_get_examples() {
|
||||
local manifest=$(_locate_manifest)
|
||||
__cargo_get_examples() {
|
||||
local manifest=$(__cargo_locate_manifest)
|
||||
[ -z "$manifest" ] && return 0
|
||||
|
||||
local files=("${manifest%/*}"/examples/*.rs)
|
||||
|
|
@ -225,7 +227,7 @@ _get_examples() {
|
|||
fi
|
||||
}
|
||||
|
||||
_get_targets() {
|
||||
__cargo_get_targets() {
|
||||
local result=()
|
||||
local targets=$(rustup target list)
|
||||
while read line; do
|
||||
|
|
@ -236,7 +238,7 @@ _get_targets() {
|
|||
echo "${result[@]}"
|
||||
}
|
||||
|
||||
_toolchains() {
|
||||
__cargo_toolchains() {
|
||||
local result=()
|
||||
local toolchains=$(rustup toolchain list)
|
||||
local channels="nightly|beta|stable|[0-9]\.[0-9]{1,2}\.[0-9]"
|
||||
|
|
|
|||
Loading…
Reference in New Issue