Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(make): Use awk to extract target names #1069

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
175 changes: 88 additions & 87 deletions completions/make
Original file line number Diff line number Diff line change
@@ -1,91 +1,92 @@
# bash completion for GNU make -*- shell-script -*-

# TODO: rename per API conventions, rework to use vars rather than outputting(?)
_make_target_extract_script()
# Extract the valid target names starting with PREFIX from the output of
# `make -npq'
# @param mode If this is `-d', the directory names already specified in
# PREFIX are omitted in the output
# @param prefix Prefix of the target names
_comp_cmd_make__extract_targets()
{
local mode="$1"
shift

local prefix="$1"
local prefix_pat=$(command sed 's/[][\,.*^$(){}?+|/]/\\&/g' <<<"$prefix")
local basename=${prefix##*/}
local dirname_len=$((${#prefix} - ${#basename}))
# Avoid expressions like '\(.\{0\}\)', FreeBSD sed doesn't like them:
# > sed: 1: ...: RE error: empty (sub)expression
local dirname_re=
((dirname_len > 0)) && dirname_re="\(.\{${dirname_len}\}\)"

if [[ ! $dirname_re ]]; then
local output="\1"
elif [[ $mode == -d ]]; then
# display mode, only output current path component to the next slash
local output="\2"
else
# completion mode, output full path to the next slash
local output="\1\2"
fi

cat <<EOF
1,/^# * Make data base/ d; # skip any makefile output
/^# * Finished Make data base/,/^# * Make data base/{
d; # skip any makefile output
}
/^# * Variables/,/^# * Files/ d; # skip until files section
/^# * Not a target/,/^$/ d; # skip not target blocks
/^${prefix_pat}/,/^$/! d; # skip anything user dont want

# The stuff above here describes lines that are not
# explicit targets or not targets other than special ones
# The stuff below here decides whether an explicit target
# should be output.

/^# * File is an intermediate prerequisite/ {
s/^.*$//;x; # unhold target
d; # delete line
}

/^$/ { # end of target block
x; # unhold target
/^$/d; # dont print blanks
s|^${dirname_re-}\(.\{${#basename}\}[^:]*\):.*$|${output}|p;
d; # hide any bugs
}

# This pattern includes a literal tab character as \t is not a portable
# representation and fails with BSD sed
/^[^# :%]\{1,\}:/ { # found target block
/^\.PHONY:/ d; # special target
/^\.SUFFIXES:/ d; # special target
/^\.DEFAULT:/ d; # special target
/^\.PRECIOUS:/ d; # special target
/^\.INTERMEDIATE:/ d; # special target
/^\.SECONDARY:/ d; # special target
/^\.SECONDEXPANSION:/ d; # special target
/^\.DELETE_ON_ERROR:/ d; # special target
/^\.IGNORE:/ d; # special target
/^\.LOW_RESOLUTION_TIME:/ d; # special target
/^\.SILENT:/ d; # special target
/^\.EXPORT_ALL_VARIABLES:/ d; # special target
/^\.NOTPARALLEL:/ d; # special target
/^\.ONESHELL:/ d; # special target
/^\.POSIX:/ d; # special target
/^\.NOEXPORT:/ d; # special target
/^\.MAKE:/ d; # special target
EOF

# don't complete with hidden targets unless we are doing a partial completion
if [[ ! ${prefix_pat} || ${prefix_pat} == */ ]]; then
cat <<EOF
/^${prefix_pat}[^a-zA-Z0-9]/d; # convention for hidden tgt
EOF
fi

cat <<EOF
h; # hold target
d; # delete line
}

EOF
local mode=$1
local -x prefix=$2

# display mode, only output current path component to the next slash
local -x prefix_replace=$prefix
[[ $mode == -d && $prefix == */* ]] &&
prefix_replace=${prefix##*/}

awk '
BEGIN {
prefix = ENVIRON["prefix"];
prefix_replace = ENVIRON["prefix_replace"];
is_target_block = 0;
target = "";
}
function starts_with(str, prefix) {
akinomyoga marked this conversation as resolved.
Show resolved Hide resolved
return substr(str, 1, length(prefix)) == prefix;
}

NR == 1, /^# +Make data base/ { next; } # skip any makefile output
/^# +Finished Make data base/,/^# +Make data base/ { next; } # skip any makefile output
/^# +Variables/, /^# +Files/ { next; } # skip until files section
/^# +Not a target/, /^$/ { next; } # skip not target blocks

# The stuff above here describes lines that are not
# explicit targets or not targets other than special ones
# The stuff below here decides whether an explicit target
# should be output.

starts_with($0, prefix) { is_target_block = 1; }
!is_target_block { next; }
akinomyoga marked this conversation as resolved.
Show resolved Hide resolved

/^# +File is an intermediate prerequisite/ { # cancel the block
is_target_block = 0;
target = "";
next;
}

/^$/ { # end of target block
is_target_block = 0;
if (target != "") {
print target;
target = "";
}
next;
}

# only process the targets the user wants.
/^[^#\t:%]+:/ { # found target block
if (/^\.PHONY:/ ) next; # special target
if (/^\.SUFFIXES:/ ) next; # special target
if (/^\.DEFAULT:/ ) next; # special target
if (/^\.PRECIOUS:/ ) next; # special target
if (/^\.INTERMEDIATE:/ ) next; # special target
if (/^\.SECONDARY:/ ) next; # special target
if (/^\.SECONDEXPANSION:/ ) next; # special target
if (/^\.DELETE_ON_ERROR:/ ) next; # special target
if (/^\.IGNORE:/ ) next; # special target
if (/^\.LOW_RESOLUTION_TIME:/ ) next; # special target
if (/^\.SILENT:/ ) next; # special target
if (/^\.EXPORT_ALL_VARIABLES:/) next; # special target
if (/^\.NOTPARALLEL:/ ) next; # special target
if (/^\.ONESHELL:/ ) next; # special target
if (/^\.POSIX:/ ) next; # special target
if (/^\.NOEXPORT:/ ) next; # special target
if (/^\.MAKE:/ ) next; # special target

# dont complete with hidden targets unless we are doing a partial completion
if (prefix == "" || prefix ~ /\/$/)
if (substr($0, length(prefix) + 1, 1) ~ /[^a-zA-Z0-9]/)
next;

target = $0;
sub(/:.*/, "", target);
if (prefix_replace != prefix)
target = prefix_replace substr(target, 1 + length(prefix))

next;
}
'
}

# Truncate the non-unique filepaths in COMPREPLY to only generate unique
Expand Down Expand Up @@ -224,11 +225,11 @@ _comp_cmd_make()
# mode=-d # display-only mode
# fi

local IFS=$' \t\n' script=$(_make_target_extract_script $mode "$cur")
local IFS=$' \t\n'
COMPREPLY=($(LC_ALL=C \
$1 -npq __BASH_MAKE_COMPLETION__=1 \
${makef+"${makef[@]}"} "${makef_dir[@]}" .DEFAULT 2>/dev/null |
command sed -ne "$script"))
_comp_cmd_make__extract_targets "$mode" "$cur"))

_comp_cmd_make__truncate_non_unique_paths

Expand Down