From 89cfbafcaca1304c7a8000c7f04d5d4ebe7ce26c Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Sun, 20 Jun 2021 09:33:49 +0900 Subject: [PATCH] fix(make): show unique targets in subdirs --- completions/make | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/completions/make b/completions/make index c96d8fc9d18..9876f1f526c 100644 --- a/completions/make +++ b/completions/make @@ -46,7 +46,7 @@ _make_target_extract_script() /^$/ { # end of target block x; # unhold target /^$/d; # dont print blanks - s|^${dirname_re-}\(.\{${#basename}\}[^:/]*/\{0,1\}\)[^:]*:.*$|${output}|p; + s|^${dirname_re-}\(.\{${#basename}\}[^:]*\):.*$|${output}|p; d; # hide any bugs } @@ -164,6 +164,45 @@ _make() ${makef+"${makef[@]}"} "${makef_dir[@]}" .DEFAULT 2>/dev/null | command sed -ne "$script")) + local prefix=$cur + [[ $mode == -d ]] && prefix= + if ((${#COMPREPLY[@]} > 0)); then + local -A processed paths + local target + for target in "${COMPREPLY[@]}"; do + local path=${target%/} + [[ ${processed[$path]+set} ]] && continue + processed[$path]=$target + + while + if [[ ! ${paths[$path]+set} ]]; then + ((paths[\$path] = 1)) + else + ((paths[\$path]++)) + fi + [[ $path == "$prefix"*/* ]] + do + path=${path%/*} + done + done + + COMPREPLY=() + local nreply=0 target + for target in "${!paths[@]}"; do + local path=$target + while [[ $path == "$prefix"*/* ]]; do + path=${path%/*} + ((paths[\$path] != 1)) && continue 2 + done + + if [[ ${processed[$target]+set} ]]; then + COMPREPLY[nreply++]=${processed[$target]} + elif ((paths[\$target] > 1)); then + COMPREPLY[nreply++]=$target/ + fi + done + fi + if [[ $mode != -d ]]; then # Completion will occur if there is only one suggestion # so set options for completion based on the first one