Clean alias completion plugin (#1872)
* Sort plugins/available * Clean alias-completion plugin * Fix shellcheck warning. SC1090 * Change shellcheck source to /dev/null for explicity * Disable shellcheck warning SC2162pull/1875/head
parent
31bea24da6
commit
7b8dbd39bc
|
|
@ -50,15 +50,16 @@ themes/purity
|
||||||
|
|
||||||
# plugins
|
# plugins
|
||||||
#
|
#
|
||||||
|
plugins/available/alias-completion.plugin.bash
|
||||||
plugins/available/basher.plugin.bash
|
plugins/available/basher.plugin.bash
|
||||||
plugins/available/cmd-returned-notify.plugin.bash
|
plugins/available/cmd-returned-notify.plugin.bash
|
||||||
plugins/available/docker-machine.plugin.bash
|
plugins/available/docker-machine.plugin.bash
|
||||||
plugins/available/git.plugin.bash
|
plugins/available/git.plugin.bash
|
||||||
plugins/available/go.plugin.bash
|
plugins/available/go.plugin.bash
|
||||||
plugins/available/goenv.plugin.bash
|
plugins/available/goenv.plugin.bash
|
||||||
plugins/available/history.plugin.bash
|
|
||||||
plugins/available/history-search.plugin.bash
|
plugins/available/history-search.plugin.bash
|
||||||
plugins/available/history-substring-search.plugin.bash
|
plugins/available/history-substring-search.plugin.bash
|
||||||
|
plugins/available/history.plugin.bash
|
||||||
plugins/available/xterm.plugin.bash
|
plugins/available/xterm.plugin.bash
|
||||||
|
|
||||||
# completions
|
# completions
|
||||||
|
|
@ -105,6 +106,7 @@ aliases/available/vim.aliases.bash
|
||||||
aliases/available/git.aliases.bash
|
aliases/available/git.aliases.bash
|
||||||
|
|
||||||
# tests
|
# tests
|
||||||
|
test/plugins/alias-completion.plugin.bats
|
||||||
test/test_helper.bash
|
test/test_helper.bash
|
||||||
|
|
||||||
# vendor init files
|
# vendor init files
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
# shellcheck shell=bash
|
||||||
# Load after the other completions to understand what needs to be completed
|
# Load after the other completions to understand what needs to be completed
|
||||||
# BASH_IT_LOAD_PRIORITY: 365
|
# BASH_IT_LOAD_PRIORITY: 365
|
||||||
|
|
||||||
|
|
@ -17,54 +18,61 @@ about-plugin 'Automatic completion of aliases'
|
||||||
|
|
||||||
# Automatically add completion for all aliases to commands having completion functions
|
# Automatically add completion for all aliases to commands having completion functions
|
||||||
function alias_completion {
|
function alias_completion {
|
||||||
local namespace="alias_completion"
|
local namespace="alias_completion"
|
||||||
|
|
||||||
# parse function based completion definitions, where capture group 2 => function and 3 => trigger
|
# parse function based completion definitions, where capture group 2 => function and 3 => trigger
|
||||||
local compl_regex='complete( +[^ ]+)* -F ([^ ]+) ("[^"]+"|[^ ]+)'
|
local compl_regex='complete( +[^ ]+)* -F ([^ ]+) ("[^"]+"|[^ ]+)'
|
||||||
# parse alias definitions, where capture group 1 => trigger, 2 => command, 3 => command arguments
|
# parse alias definitions, where capture group 1 => trigger, 2 => command, 3 => command arguments
|
||||||
local alias_regex="alias( -- | )([^=]+)='(\"[^\"]+\"|[^ ]+)(( +[^ ]+)*)'"
|
local alias_regex="alias( -- | )([^=]+)='(\"[^\"]+\"|[^ ]+)(( +[^ ]+)*)'"
|
||||||
|
|
||||||
# create array of function completion triggers, keeping multi-word triggers together
|
# create array of function completion triggers, keeping multi-word triggers together
|
||||||
eval "local completions=($(complete -p | sed -Ene "/$compl_regex/s//'\3'/p"))"
|
eval "local completions=($(complete -p | sed -Ene "/$compl_regex/s//'\3'/p"))"
|
||||||
(( ${#completions[@]} == 0 )) && return 0
|
((${#completions[@]} == 0)) && return 0
|
||||||
|
|
||||||
# create temporary file for wrapper functions and completions
|
# 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
|
# read in "<alias> '<aliased command>' '<command args>'" lines from defined aliases
|
||||||
local line; while read line; do
|
local line
|
||||||
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]# }"
|
|
||||||
|
|
||||||
# skip aliases to pipes, boolean control structures and other command lists
|
# shellcheck disable=SC2162
|
||||||
# (leveraging that eval errs out if $alias_args contains unquoted shell metacharacters)
|
# some aliases do have backslashes that needs to be interpreted
|
||||||
eval "local alias_arg_words; alias_arg_words=($alias_args)" 2>/dev/null || continue
|
while read line; do
|
||||||
# avoid expanding wildcards
|
eval "local alias_tokens; alias_tokens=($line)" 2> /dev/null || continue # some alias arg patterns cause an eval parse error
|
||||||
read -a alias_arg_words <<< "$alias_args"
|
local alias_name="${alias_tokens[0]}" alias_cmd="${alias_tokens[1]}" alias_args="${alias_tokens[2]# }"
|
||||||
|
|
||||||
# skip alias if there is no completion function triggered by the aliased command
|
# skip aliases to pipes, boolean control structures and other command lists
|
||||||
if [[ ! " ${completions[*]} " =~ " $alias_cmd " ]]; then
|
# (leveraging that eval errs out if $alias_args contains unquoted shell metacharacters)
|
||||||
if [[ -n "$completion_loader" ]]; then
|
eval "local alias_arg_words; alias_arg_words=($alias_args)" 2> /dev/null || continue
|
||||||
# force loading of completions for the aliased command
|
# avoid expanding wildcards
|
||||||
eval "$completion_loader $alias_cmd"
|
read -ra alias_arg_words <<< "$alias_args"
|
||||||
# 124 means completion loader was successful
|
|
||||||
[[ $? -eq 124 ]] || continue
|
|
||||||
completions+=($alias_cmd)
|
|
||||||
else
|
|
||||||
continue
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
local new_completion="$(complete -p "$alias_cmd" 2>/dev/null)"
|
|
||||||
|
|
||||||
# create a wrapper inserting the alias arguments if any
|
# skip alias if there is no completion function triggered by the aliased command
|
||||||
if [[ -n $alias_args ]]; then
|
if [[ ! " ${completions[*]} " =~ $alias_cmd ]]; then
|
||||||
local compl_func="${new_completion/#* -F /}"; compl_func="${compl_func%% *}"
|
if [[ -n "$completion_loader" ]]; then
|
||||||
# avoid recursive call loops by ignoring our own functions
|
# force loading of completions for the aliased command
|
||||||
if [[ "${compl_func#_$namespace::}" == $compl_func ]]; then
|
eval "$completion_loader $alias_cmd"
|
||||||
local compl_wrapper="_${namespace}::${alias_name}"
|
# 124 means completion loader was successful
|
||||||
echo "function $compl_wrapper {
|
[[ $? -eq 124 ]] || continue
|
||||||
|
completions+=("$alias_cmd")
|
||||||
|
else
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
local new_completion="$(complete -p "$alias_cmd" 2> /dev/null)"
|
||||||
|
|
||||||
|
# create a wrapper inserting the alias arguments if any
|
||||||
|
if [[ -n $alias_args ]]; then
|
||||||
|
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
|
||||||
|
local compl_wrapper="_${namespace}::${alias_name}"
|
||||||
|
echo "function $compl_wrapper {
|
||||||
local compl_word=\$2
|
local compl_word=\$2
|
||||||
local prec_word=\$3
|
local prec_word=\$3
|
||||||
# check if prec_word is the alias itself. if so, replace it
|
# check if prec_word is the alias itself. if so, replace it
|
||||||
|
|
@ -81,15 +89,18 @@ function alias_completion {
|
||||||
(( COMP_POINT += \${#COMP_LINE} ))
|
(( COMP_POINT += \${#COMP_LINE} ))
|
||||||
$compl_func \"$alias_cmd\" \"\$compl_word\" \"\$prec_word\"
|
$compl_func \"$alias_cmd\" \"\$compl_word\" \"\$prec_word\"
|
||||||
}" >> "$tmp_file"
|
}" >> "$tmp_file"
|
||||||
new_completion="${new_completion/ -F $compl_func / -F $compl_wrapper }"
|
new_completion="${new_completion/ -F $compl_func / -F $compl_wrapper }"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# replace completion trigger by alias
|
# replace completion trigger by alias
|
||||||
if [[ -n $new_completion ]]; then
|
if [[ -n $new_completion ]]; then
|
||||||
new_completion="${new_completion% *} $alias_name"
|
new_completion="${new_completion% *} $alias_name"
|
||||||
echo "$new_completion" >> "$tmp_file"
|
echo "$new_completion" >> "$tmp_file"
|
||||||
fi
|
fi
|
||||||
done < <(alias -p | sed -Ene "s/$alias_regex/\2 '\3' '\4'/p")
|
done < <(alias -p | sed -Ene "s/$alias_regex/\2 '\3' '\4'/p")
|
||||||
source "$tmp_file" && command rm -f "$tmp_file"
|
# shellcheck source=/dev/null
|
||||||
}; alias_completion
|
source "$tmp_file" && command rm -f "$tmp_file"
|
||||||
|
}
|
||||||
|
|
||||||
|
alias_completion
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue