Skip to content

Commit

Permalink
fix: [fixup] fix subdir
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Dec 19, 2022
1 parent 9bb711a commit 0ef11ca
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions completions/make
Original file line number Diff line number Diff line change
Expand Up @@ -177,42 +177,50 @@ _make()
${makef+"${makef[@]}"} "${makef_dir[@]}" .DEFAULT 2>/dev/null |
command sed -ne "$script"))

# discard the files under subdirectories unless the path is unique
# under each subdirectory and instead generate the subdirectory path.
# For example, when there are two candidates, "abc/def" and "abc/xyz",
# we generate "abc/" instead of generating both candidates directly.
# When there is only one candidate "abc/def", we generate the full path
# "abc/def".
local prefix=$cur
[[ $mode == -d ]] && prefix=
if ((${#COMPREPLY[@]} > 0)); then
local -A processed paths
# collect the possible completions including the directory names in
# `paths' and count the number of children of each subdirectory in
# `nchild'.
local -A paths nchild
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))
while [[ ! ${paths[$path]+set} ]] &&
paths[$path]=1 &&
[[ $path == "$prefix"*/* ]]; do
path=${path%/*}
if [[ ! ${nchild[$path]+set} ]]; then
((nchild[\$path] = 1))
else
((paths[\$path]++))
((nchild[\$path]++))
fi
[[ $path == "$prefix"*/* ]]
do
path=${path%/*}
done
done

COMPREPLY=()
local nreply=0 target
local nreply=0
for target in "${!paths[@]}"; do
# generate only the paths that do not have a unique child and
# whose all parent and ancestor directories have a unique
# child.
((${nchild[$target]-0} == 1)) && continue
local path=$target
while [[ $path == "$prefix"*/* ]]; do
path=${path%/*}
((paths[\$path] != 1)) && continue 2
((${nchild[$path]-0} == 1)) || continue 2
done

if [[ ${processed[$target]+set} ]]; then
COMPREPLY[nreply++]=${processed[$target]}
elif ((paths[\$target] > 1)); then
COMPREPLY[nreply++]=$target/
fi
# suffix `/' when the target path is a subdiretory, which has
# at least one child.
COMPREPLY[nreply++]=$target${nchild[$target]+/}
done
fi

Expand Down

0 comments on commit 0ef11ca

Please sign in to comment.