Address problems pointed out in the comments.

pull/1717/head
Nariyasu Heseri 2020-12-04 12:30:51 +09:00
parent 26c0cd3f6d
commit 516d2c0d4c
1 changed files with 157 additions and 155 deletions

View File

@ -3,13 +3,14 @@
# cargo (Rust package manager) completion # 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 # 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 # Required for bash versions < 4.1
# Default bash version is 3.2 on latest macOS. See #6874 # Default bash version is 3.2 on latest macOS. See #6874
shopt -s extglob shopt -s extglob
command -v cargo >/dev/null 2>&1 &&
_cargo() { _cargo() {
local cur prev words cword local cur prev words cword
_get_comp_words_by_ref 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 if [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W "${opt___nocmd}" -- "$cur")) COMPREPLY=($(compgen -W "${opt___nocmd}" -- "$cur"))
elif [[ "$cur" == +* ]]; then elif [[ "$cur" == +* ]]; then
COMPREPLY=($(compgen -W "$(_toolchains)" -- "$cur")) COMPREPLY=($(compgen -W "$(__cargo_toolchains)" -- "$cur"))
else else
COMPREPLY=($(compgen -W "$__cargo_commands" -- "$cur")) COMPREPLY=($(compgen -W "$__cargo_commands" -- "$cur"))
fi fi
@ -118,19 +119,19 @@ command -v cargo >/dev/null 2>&1 &&
_filedir toml _filedir toml
;; ;;
--bin) --bin)
COMPREPLY=($(compgen -W "$(_bin_names)" -- "$cur")) COMPREPLY=($(compgen -W "$(__cargo_bin_names)" -- "$cur"))
;; ;;
--test) --test)
COMPREPLY=($(compgen -W "$(_test_names)" -- "$cur")) COMPREPLY=($(compgen -W "$(__cargo_test_names)" -- "$cur"))
;; ;;
--bench) --bench)
COMPREPLY=($(compgen -W "$(_benchmark_names)" -- "$cur")) COMPREPLY=($(compgen -W "$(__cargo_benchmark_names)" -- "$cur"))
;; ;;
--example) --example)
COMPREPLY=($(compgen -W "$(_get_examples)" -- "$cur")) COMPREPLY=($(compgen -W "$(__cargo_get_examples)" -- "$cur"))
;; ;;
--target) --target)
COMPREPLY=($(compgen -W "$(_get_targets)" -- "$cur")) COMPREPLY=($(compgen -W "$(__cargo_get_targets)" -- "$cur"))
;; ;;
--target-dir) --target-dir)
_filedir -d _filedir -d
@ -153,19 +154,20 @@ command -v cargo >/dev/null 2>&1 &&
# compopt does not work in bash version 3 # compopt does not work in bash version 3
return 0 return 0
} && }
complete -F _cargo cargo complete -F _cargo cargo
__cargo_commands=$(cargo --list 2>/dev/null | awk 'NR>1 {print $1}') __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 cargo locate-project --message-format plain 2>/dev/null
} }
# Extracts the values of "name" from the array given in $1 and shows them as # Extracts the values of "name" from the array given in $1 and shows them as
# command line options for completion # command line options for completion
_get_names_from_array() { __cargo_get_names_from_array() {
local manifest=$(_locate_manifest) local manifest=$(__cargo_locate_manifest)
if [[ -z $manifest ]]; then if [[ -z $manifest ]]; then
return 0 return 0
fi fi
@ -198,22 +200,22 @@ _get_names_from_array() {
} }
#Gets the bin names from the manifest file #Gets the bin names from the manifest file
_bin_names() { __cargo_bin_names() {
_get_names_from_array "bin" __cargo_get_names_from_array "bin"
} }
#Gets the test names from the manifest file #Gets the test names from the manifest file
_test_names() { __cargo_test_names() {
_get_names_from_array "test" __cargo_get_names_from_array "test"
} }
#Gets the bench names from the manifest file #Gets the bench names from the manifest file
_benchmark_names() { __cargo_benchmark_names() {
_get_names_from_array "bench" __cargo_get_names_from_array "bench"
} }
_get_examples() { __cargo_get_examples() {
local manifest=$(_locate_manifest) local manifest=$(__cargo_locate_manifest)
[ -z "$manifest" ] && return 0 [ -z "$manifest" ] && return 0
local files=("${manifest%/*}"/examples/*.rs) local files=("${manifest%/*}"/examples/*.rs)
@ -225,7 +227,7 @@ _get_examples() {
fi fi
} }
_get_targets() { __cargo_get_targets() {
local result=() local result=()
local targets=$(rustup target list) local targets=$(rustup target list)
while read line; do while read line; do
@ -236,7 +238,7 @@ _get_targets() {
echo "${result[@]}" echo "${result[@]}"
} }
_toolchains() { __cargo_toolchains() {
local result=() local result=()
local toolchains=$(rustup toolchain list) local toolchains=$(rustup toolchain list)
local channels="nightly|beta|stable|[0-9]\.[0-9]{1,2}\.[0-9]" local channels="nightly|beta|stable|[0-9]\.[0-9]{1,2}\.[0-9]"