Clean alias-completion plugin

pull/1872/head
John Fred Fadrigalan 2021-04-20 15:09:18 +08:00
parent 6ef505c707
commit 3a4476aa6a
No known key found for this signature in database
GPG Key ID: 4C23960F72FC911F
2 changed files with 60 additions and 51 deletions

View File

@ -50,6 +50,7 @@ themes/purity
# plugins
#
plugins/available/alias-completion.plugin.bash
plugins/available/basher.plugin.bash
plugins/available/cmd-returned-notify.plugin.bash
plugins/available/docker-machine.plugin.bash
@ -104,6 +105,7 @@ aliases/available/vim.aliases.bash
aliases/available/git.aliases.bash
# tests
test/alias-completion.plugin.bats
test/test_helper.bash
# vendor init files

View File

@ -1,3 +1,4 @@
# shellcheck shell=bash
# Load after the other completions to understand what needs to be completed
# BASH_IT_LOAD_PRIORITY: 365
@ -29,12 +30,15 @@ function alias_completion {
((${#completions[@]} == 0)) && return 0
# create temporary file for wrapper functions and completions
local tmp_file; tmp_file="$(mktemp -t "${namespace}-${RANDOM}XXXXXX")" || return 1
local tmp_file
tmp_file="$(mktemp -t "${namespace}-${RANDOM}XXXXXX")" || return 1
local completion_loader; completion_loader="$(complete -p -D 2>/dev/null | sed -Ene 's/.* -F ([^ ]*).*/\1/p')"
local completion_loader
completion_loader="$(complete -p -D 2> /dev/null | sed -Ene 's/.* -F ([^ ]*).*/\1/p')"
# read in "<alias> '<aliased command>' '<command args>'" lines from defined aliases
local line; while read line; do
local line
while read -r line; do
eval "local alias_tokens; alias_tokens=($line)" 2> /dev/null || continue # some alias arg patterns cause an eval parse error
local alias_name="${alias_tokens[0]}" alias_cmd="${alias_tokens[1]}" alias_args="${alias_tokens[2]# }"
@ -42,16 +46,16 @@ function alias_completion {
# (leveraging that eval errs out if $alias_args contains unquoted shell metacharacters)
eval "local alias_arg_words; alias_arg_words=($alias_args)" 2> /dev/null || continue
# avoid expanding wildcards
read -a alias_arg_words <<< "$alias_args"
read -ra alias_arg_words <<< "$alias_args"
# skip alias if there is no completion function triggered by the aliased command
if [[ ! " ${completions[*]} " =~ " $alias_cmd " ]]; then
if [[ ! " ${completions[*]} " =~ $alias_cmd ]]; then
if [[ -n "$completion_loader" ]]; then
# force loading of completions for the aliased command
eval "$completion_loader $alias_cmd"
# 124 means completion loader was successful
[[ $? -eq 124 ]] || continue
completions+=($alias_cmd)
completions+=("$alias_cmd")
else
continue
fi
@ -60,9 +64,10 @@ function alias_completion {
# create a wrapper inserting the alias arguments if any
if [[ -n $alias_args ]]; then
local compl_func="${new_completion/#* -F /}"; compl_func="${compl_func%% *}"
local compl_func="${new_completion/#* -F /}"
compl_func="${compl_func%% *}"
# avoid recursive call loops by ignoring our own functions
if [[ "${compl_func#_$namespace::}" == $compl_func ]]; then
if [[ "${compl_func#_$namespace::}" == "$compl_func" ]]; then
local compl_wrapper="_${namespace}::${alias_name}"
echo "function $compl_wrapper {
local compl_word=\$2
@ -92,4 +97,6 @@ function alias_completion {
fi
done < <(alias -p | sed -Ene "s/$alias_regex/\2 '\3' '\4'/p")
source "$tmp_file" && command rm -f "$tmp_file"
}; alias_completion
}
alias_completion