From 552cd89851a331811fcf40888b22b63960215703 Mon Sep 17 00:00:00 2001 From: cornfeedhobo Date: Fri, 21 Feb 2020 13:09:08 -0600 Subject: [PATCH] enhance makefile completion - shortcircuit early when nothing is found --- completion/available/makefile.completion.bash | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/completion/available/makefile.completion.bash b/completion/available/makefile.completion.bash index d53e18fb..021dd484 100644 --- a/completion/available/makefile.completion.bash +++ b/completion/available/makefile.completion.bash @@ -4,12 +4,16 @@ # Loosely adapted from http://stackoverflow.com/a/38415982/1472048 _makecomplete() { + COMPREPLY=() + # https://www.gnu.org/software/make/manual/html_node/Makefile-Names.html local files=() while IFS='' read -r line; do files+=("$line") done < <(find . -maxdepth 1 -regextype posix-extended -regex '.*(GNU)?[Mm]akefile$' -printf '%f\n') + [ "${#files[@]}" -eq 0 ] && return 0 + # collect all targets local targets=() for f in "${files[@]}" ; do @@ -18,11 +22,13 @@ _makecomplete() { done < <(grep -oE '^[a-zA-Z0-9_-]+:([^=]|$)' "$f" | cut -d':' -f1) done - # flatten the array for completion - COMPREPLY=() + [ "${#targets[@]}" -eq 0 ] && return 0 + + # use the targets for completion while IFS='' read -r line ; do COMPREPLY+=("$line") done < <(compgen -W "$(tr ' ' '\n' <<<"${targets[@]}" | sort -u)" -- "${COMP_WORDS[COMP_CWORD]}") + return 0 }