Add cargo,completion.bash to clean_files.txt & run lint.

pull/1717/head
Nariyasu Heseri 2020-12-06 01:05:48 +09:00
parent 9e9edb9593
commit 2663071f14
2 changed files with 208 additions and 207 deletions

View File

@ -31,3 +31,4 @@ themes/powerline
# completions # completions
# #
completion/available/rustup.completion.bash completion/available/rustup.completion.bash
completion/available/cargo.completion.bash

View File

@ -3,260 +3,260 @@
# 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
if !_command_exists cargo; then if ! _command_exists cargo; then
return 0 return 0
fi 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
_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
COMPREPLY=() COMPREPLY=()
# Skip past - and + options to find the command. # Skip past - and + options to find the command.
local nwords=${#words[@]} local nwords=${#words[@]}
local cmd_i cmd dd_i local cmd_i cmd dd_i
for ((cmd_i = 1; cmd_i < $nwords; cmd_i++)); do for ((cmd_i = 1; cmd_i < $nwords; cmd_i++)); do
if [[ ! "${words[$cmd_i]}" =~ ^[+-] ]]; then if [[ ! "${words[$cmd_i]}" =~ ^[+-] ]]; then
cmd="${words[$cmd_i]}" cmd="${words[$cmd_i]}"
break break
fi fi
done done
# Find the location of the -- separator. # Find the location of the -- separator.
for ((dd_i = 1; dd_i < $nwords - 1; dd_i++)); do for ((dd_i = 1; dd_i < $nwords - 1; dd_i++)); do
if [[ "${words[$dd_i]}" = "--" ]]; then if [[ "${words[$dd_i]}" = "--" ]]; then
break break
fi fi
done done
local vcs='git hg none pijul fossil' local vcs='git hg none pijul fossil'
local color='auto always never' local color='auto always never'
local msg_format='human json short' local msg_format='human json short'
local opt_help='-h --help' local opt_help='-h --help'
local opt_verbose='-v --verbose' local opt_verbose='-v --verbose'
local opt_quiet='-q --quiet' local opt_quiet='-q --quiet'
local opt_color='--color' local opt_color='--color'
local opt_common="$opt_help $opt_verbose $opt_quiet $opt_color" local opt_common="$opt_help $opt_verbose $opt_quiet $opt_color"
local opt_pkg_spec='-p --package --all --exclude --workspace' local opt_pkg_spec='-p --package --all --exclude --workspace'
local opt_pkg='-p --package' local opt_pkg='-p --package'
local opt_feat='--features --all-features --no-default-features' local opt_feat='--features --all-features --no-default-features'
local opt_mani='--manifest-path' local opt_mani='--manifest-path'
local opt_jobs='-j --jobs' local opt_jobs='-j --jobs'
local opt_force='-f --force' local opt_force='-f --force'
local opt_sync='-s --sync' local opt_sync='-s --sync'
local opt_lock='--frozen --locked --offline' local opt_lock='--frozen --locked --offline'
local opt_targets="--lib --bin --bins --example --examples --test --tests --bench --benches --all-targets" local opt_targets="--lib --bin --bins --example --examples --test --tests --bench --benches --all-targets"
local opt___nocmd="$opt_common -V --version --list --explain" 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_targets --message-format --target --no-run --no-fail-fast --target-dir" local opt__bench="$opt_common $opt_pkg_spec $opt_feat $opt_mani $opt_lock $opt_jobs $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_targets --message-format --target --release --profile --target-dir" local opt__build="$opt_common $opt_pkg_spec $opt_feat $opt_mani $opt_lock $opt_jobs $opt_targets --message-format --target --release --profile --target-dir"
local opt__check="$opt_common $opt_pkg_spec $opt_feat $opt_mani $opt_lock $opt_jobs $opt_targets --message-format --target --release --profile --target-dir" local opt__check="$opt_common $opt_pkg_spec $opt_feat $opt_mani $opt_lock $opt_jobs $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 --profile" local opt__clean="$opt_common $opt_pkg $opt_mani $opt_lock --target --release --doc --target-dir --profile"
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 --profile" 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 --profile"
local opt__fetch="$opt_common $opt_mani $opt_lock --target" local opt__fetch="$opt_common $opt_mani $opt_lock --target"
local opt__fix="$opt_common $opt_pkg_spec $opt_feat $opt_mani $opt_jobs $opt_targets $opt_lock --release --target --message-format --broken-code --edition --edition-idioms --allow-no-vcs --allow-dirty --allow-staged --profile --target-dir" local opt__fix="$opt_common $opt_pkg_spec $opt_feat $opt_mani $opt_jobs $opt_targets $opt_lock --release --target --message-format --broken-code --edition --edition-idioms --allow-no-vcs --allow-dirty --allow-staged --profile --target-dir"
local opt__generate_lockfile="$opt_common $opt_mani $opt_lock" local opt__generate_lockfile="$opt_common $opt_mani $opt_lock"
local opt__help="$opt_help" local opt__help="$opt_help"
local opt__init="$opt_common $opt_lock --bin --lib --name --vcs --edition --registry" 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 --profile --no-track" 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 --profile --no-track"
local opt__locate_project="$opt_common $opt_mani $opt_lock --message-format --workspace" local opt__locate_project="$opt_common $opt_mani $opt_lock --message-format --workspace"
local opt__login="$opt_common $opt_lock --registry" local opt__login="$opt_common $opt_lock --registry"
local opt__metadata="$opt_common $opt_feat $opt_mani $opt_lock --format-version=1 --no-deps --filter-platform" local opt__metadata="$opt_common $opt_feat $opt_mani $opt_lock --format-version=1 --no-deps --filter-platform"
local opt__new="$opt_common $opt_lock --vcs --bin --lib --name --edition --registry" 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__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__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_common $opt_mani $opt_lock $opt_pkg" local opt__pkgid="$opt_common $opt_mani $opt_lock $opt_pkg"
local opt__publish="$opt_common $opt_mani $opt_feat $opt_lock $opt_jobs --allow-dirty --dry-run --token --no-verify --index --registry --target --target-dir" local opt__publish="$opt_common $opt_mani $opt_feat $opt_lock $opt_jobs --allow-dirty --dry-run --token --no-verify --index --registry --target --target-dir"
local opt__read_manifest="$opt_help $opt_quiet $opt_verbose $opt_mani $opt_color $opt_lock --no-deps" local opt__read_manifest="$opt_help $opt_quiet $opt_verbose $opt_mani $opt_color $opt_lock --no-deps"
local opt__run="$opt_common $opt_pkg $opt_feat $opt_mani $opt_lock $opt_jobs --message-format --target --bin --example --release --target-dir --profile" local opt__run="$opt_common $opt_pkg $opt_feat $opt_mani $opt_lock $opt_jobs --message-format --target --bin --example --release --target-dir --profile"
local opt__rustc="$opt_common $opt_pkg $opt_feat $opt_mani $opt_lock $opt_jobs $opt_targets -L --crate-type --extern --message-format --profile --target --release --target-dir" local opt__rustc="$opt_common $opt_pkg $opt_feat $opt_mani $opt_lock $opt_jobs $opt_targets -L --crate-type --extern --message-format --profile --target --release --target-dir"
local opt__rustdoc="$opt_common $opt_pkg $opt_feat $opt_mani $opt_lock $opt_jobs $opt_targets --message-format --target --release --open --target-dir --profile" local opt__rustdoc="$opt_common $opt_pkg $opt_feat $opt_mani $opt_lock $opt_jobs $opt_targets --message-format --target --release --open --target-dir --profile"
local opt__search="$opt_common $opt_lock --limit --index --registry" local opt__search="$opt_common $opt_lock --limit --index --registry"
local opt__test="$opt_common $opt_pkg_spec $opt_feat $opt_mani $opt_lock $opt_jobs $opt_targets --message-format --doc --target --no-run --release --no-fail-fast --target-dir --profile" local opt__test="$opt_common $opt_pkg_spec $opt_feat $opt_mani $opt_lock $opt_jobs $opt_targets --message-format --doc --target --no-run --release --no-fail-fast --target-dir --profile"
local opt__tree="$opt_common $opt_pkg_spec $opt_feat $opt_mani $opt_lock --target -i --invert --prefix --no-dedupe --duplicates -d --charset -f --format -e --edges" local opt__tree="$opt_common $opt_pkg_spec $opt_feat $opt_mani $opt_lock --target -i --invert --prefix --no-dedupe --duplicates -d --charset -f --format -e --edges"
local opt__uninstall="$opt_common $opt_lock $opt_pkg --bin --root" local opt__uninstall="$opt_common $opt_lock $opt_pkg --bin --root"
local opt__update="$opt_common $opt_mani $opt_lock $opt_pkg --aggressive --precise --dry-run" local opt__update="$opt_common $opt_mani $opt_lock $opt_pkg --aggressive --precise --dry-run"
local opt__vendor="$opt_common $opt_mani $opt_lock $opt_sync --no-delete --respect-source-config --versioned-dirs" local opt__vendor="$opt_common $opt_mani $opt_lock $opt_sync --no-delete --respect-source-config --versioned-dirs"
local opt__verify_project="$opt_common $opt_mani $opt_lock" local opt__verify_project="$opt_common $opt_mani $opt_lock"
local opt__version="$opt_common $opt_lock" local opt__version="$opt_common $opt_lock"
local opt__yank="$opt_common $opt_lock --vers --undo --index --token --registry" 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" local opt__libtest="--help --include-ignored --ignored --test --bench --list --logfile --nocapture --test-threads --skip -q --quiet --exact --color --format"
if [[ $cword -gt $dd_i ]]; then if [[ $cword -gt $dd_i ]]; then
# Completion after -- separator. # Completion after -- separator.
if [[ "${cmd}" = @(test|bench) ]]; then if [[ "${cmd}" = @(test|bench) ]]; then
COMPREPLY=($(compgen -W "${opt__libtest}" -- "$cur")) COMPREPLY=($(compgen -W "${opt__libtest}" -- "$cur"))
else else
# Fallback to filename completion, useful with `cargo run`. # Fallback to filename completion, useful with `cargo run`.
_filedir _filedir
fi fi
elif [[ $cword -le $cmd_i ]]; then elif [[ $cword -le $cmd_i ]]; then
# Completion before or at the command. # Completion before or at the command.
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 "$(__cargo_toolchains)" -- "$cur")) COMPREPLY=($(compgen -W "$(__cargo_toolchains)" -- "$cur"))
else else
COMPREPLY=($(compgen -W "$__cargo_commands" -- "$cur")) COMPREPLY=($(compgen -W "$__cargo_commands" -- "$cur"))
fi fi
else else
case "${prev}" in case "${prev}" in
--vcs) --vcs)
COMPREPLY=($(compgen -W "$vcs" -- "$cur")) COMPREPLY=($(compgen -W "$vcs" -- "$cur"))
;; ;;
--color) --color)
COMPREPLY=($(compgen -W "$color" -- "$cur")) COMPREPLY=($(compgen -W "$color" -- "$cur"))
;; ;;
--message-format) --message-format)
COMPREPLY=($(compgen -W "$msg_format" -- "$cur")) COMPREPLY=($(compgen -W "$msg_format" -- "$cur"))
;; ;;
--manifest-path) --manifest-path)
_filedir toml _filedir toml
;; ;;
--bin) --bin)
COMPREPLY=($(compgen -W "$(__cargo_bin_names)" -- "$cur")) COMPREPLY=($(compgen -W "$(__cargo_bin_names)" -- "$cur"))
;; ;;
--test) --test)
COMPREPLY=($(compgen -W "$(__cargo_test_names)" -- "$cur")) COMPREPLY=($(compgen -W "$(__cargo_test_names)" -- "$cur"))
;; ;;
--bench) --bench)
COMPREPLY=($(compgen -W "$(__cargo_benchmark_names)" -- "$cur")) COMPREPLY=($(compgen -W "$(__cargo_benchmark_names)" -- "$cur"))
;; ;;
--example) --example)
COMPREPLY=($(compgen -W "$(__cargo_get_examples)" -- "$cur")) COMPREPLY=($(compgen -W "$(__cargo_get_examples)" -- "$cur"))
;; ;;
--target) --target)
COMPREPLY=($(compgen -W "$(__cargo_get_targets)" -- "$cur")) COMPREPLY=($(compgen -W "$(__cargo_get_targets)" -- "$cur"))
;; ;;
--target-dir) --target-dir)
_filedir -d _filedir -d
;; ;;
help) help)
COMPREPLY=($(compgen -W "$__cargo_commands" -- "$cur")) COMPREPLY=($(compgen -W "$__cargo_commands" -- "$cur"))
;; ;;
*) *)
local opt_var=opt__${cmd//-/_} local opt_var=opt__${cmd//-/_}
if [[ -z "${!opt_var}" ]]; then if [[ -z "${!opt_var}" ]]; then
# Fallback to filename completion. # Fallback to filename completion.
_filedir _filedir
else else
COMPREPLY=($(compgen -W "${!opt_var}" -- "$cur")) COMPREPLY=($(compgen -W "${!opt_var}" -- "$cur"))
fi fi
;; ;;
esac esac
fi fi
# 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}')
__cargo_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
__cargo_get_names_from_array() { __cargo_get_names_from_array() {
local manifest=$(__cargo_locate_manifest) local manifest=$(__cargo_locate_manifest)
if [[ -z $manifest ]]; then if [[ -z $manifest ]]; then
return 0 return 0
fi fi
local last_line local last_line
local -a names local -a names
local in_block=false local in_block=false
local block_name=$1 local block_name=$1
while read line; do while read line; do
if [[ $last_line == "[[$block_name]]" ]]; then if [[ $last_line == "[[$block_name]]" ]]; then
in_block=true in_block=true
else else
if [[ $last_line =~ .*\[\[.* ]]; then if [[ $last_line =~ .*\[\[.* ]]; then
in_block=false in_block=false
fi fi
fi fi
if [[ $in_block == true ]]; then if [[ $in_block == true ]]; then
if [[ $line =~ .*name.*\= ]]; then if [[ $line =~ .*name.*\= ]]; then
line=${line##*=} line=${line##*=}
line=${line%%\"} line=${line%%\"}
line=${line##*\"} line=${line##*\"}
names+=($line) names+=($line)
fi fi
fi fi
last_line=$line last_line=$line
done <$manifest done < $manifest
echo "${names[@]}" echo "${names[@]}"
} }
#Gets the bin names from the manifest file #Gets the bin names from the manifest file
__cargo_bin_names() { __cargo_bin_names() {
__cargo_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
__cargo_test_names() { __cargo_test_names() {
__cargo_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
__cargo_benchmark_names() { __cargo_benchmark_names() {
__cargo_get_names_from_array "bench" __cargo_get_names_from_array "bench"
} }
__cargo_get_examples() { __cargo_get_examples() {
local manifest=$(__cargo_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)
local names=("${files[@]##*/}") local names=("${files[@]##*/}")
local names=("${names[@]%.*}") local names=("${names[@]%.*}")
# "*" means no examples found # "*" means no examples found
if [[ "${names[@]}" != "*" ]]; then if [[ "${names[@]}" != "*" ]]; then
echo "${names[@]}" echo "${names[@]}"
fi fi
} }
__cargo_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
if [[ "$line" =~ default|installed ]]; then if [[ "$line" =~ default|installed ]]; then
result+=("${line%% *}") result+=("${line%% *}")
fi fi
done <<<"$targets" done <<< "$targets"
echo "${result[@]}" echo "${result[@]}"
} }
__cargo_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]"
local date="[0-9]{4}-[0-9]{2}-[0-9]{2}" local date="[0-9]{4}-[0-9]{2}-[0-9]{2}"
while read line; do while read line; do
# Strip " (default)" # Strip " (default)"
line=${line%% *} line=${line%% *}
if [[ "$line" =~ ^($channels)(-($date))?(-.*) ]]; then if [[ "$line" =~ ^($channels)(-($date))?(-.*) ]]; then
if [[ -z ${BASH_REMATCH[3]} ]]; then if [[ -z ${BASH_REMATCH[3]} ]]; then
result+=("+${BASH_REMATCH[1]}") result+=("+${BASH_REMATCH[1]}")
else else
# channel-date # channel-date
result+=("+${BASH_REMATCH[1]}-${BASH_REMATCH[3]}") result+=("+${BASH_REMATCH[1]}-${BASH_REMATCH[3]}")
fi fi
result+=("+$line") result+=("+$line")
else else
result+=("+$line") result+=("+$line")
fi fi
done <<<"$toolchains" done <<< "$toolchains"
echo "${result[@]}" echo "${result[@]}"
} }