enhance makefile completion and make shellcheck happy

pull/1502/head
cornfeedhobo 2020-02-21 12:56:00 -06:00
parent b70c02c4ae
commit 9d97532f8e
No known key found for this signature in database
GPG Key ID: 724357093F994B26
1 changed files with 13 additions and 7 deletions

View File

@ -5,18 +5,24 @@
_makecomplete() {
# https://www.gnu.org/software/make/manual/html_node/Makefile-Names.html
local files=( $(find . -maxdepth 1 -regextype posix-extended -regex '.*(GNU)?[Mm]akefile$' -printf '%f ') )
local files=()
while IFS='' read -r line; do
files+=("$line")
done < <(find . -maxdepth 1 -regextype posix-extended -regex '.*(GNU)?[Mm]akefile$' -printf '%f\n')
# collect all targets
local targets=''
for f in ${files[@]} ; do
for t in $(grep -oE '^[a-zA-Z0-9_-]+:([^=]|$)' $f | cut -d':' -f1) ; do
targets+="$t\n"
done
local targets=()
for f in "${files[@]}" ; do
while IFS='' read -r line ; do
targets+=("$line")
done < <(grep -oE '^[a-zA-Z0-9_-]+:([^=]|$)' "$f" | cut -d':' -f1)
done
# flatten the array for completion
COMPREPLY=($(compgen -W "$(echo -e "$targets" | head -c -1 | sort -u)" -- ${COMP_WORDS[COMP_CWORD]}))
COMPREPLY=()
while IFS='' read -r line ; do
COMPREPLY+=("$line")
done < <(compgen -W "$(tr ' ' '\n' <<<"${targets[@]}" | sort -u)" -- "${COMP_WORDS[COMP_CWORD]}")
return 0
}