-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake-completion-enhanced.bash
More file actions
69 lines (64 loc) · 2 KB
/
make-completion-enhanced.bash
File metadata and controls
69 lines (64 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
_make_completion_enhanced() {
local cur target cache_dir cache
cache_dir="$HOME/.cache/make-completion-enhanced"
mkdir -p "$cache_dir"
cache="$cache_dir/$(pwd | tr '/' '_').cache"
cur="${COMP_WORDS[COMP_CWORD]}"
target="${COMP_WORDS[1]}"
if [[ ! -f "$cache" || Makefile -nt "$cache" ]]; then
awk '
BEGIN { tgt="__global__" }
/^## TARGET / { tgt=$3 }
/^## PARAM / {
name=$3; sub(":", "", name)
vals=""
for (i=4;i<=NF;i++) if ($i !~ /TYPE=|REQUIRED|DEFAULT=/) vals=vals" "$i
print name"|"tgt"|"vals
}
/^## ARGS / {
pos=$3; sub(":", "", pos)
vals=""
for (i=4;i<=NF;i++) vals=vals" "$i
print "__args_"pos"__|"tgt"|"vals
}
/^## OPT / {
name=$3; sub(":", "", name)
vals=""
for (i=4;i<=NF;i++) vals=vals" "$i
print name"|"tgt"|"vals
}' Makefile > "$cache"
fi
if [[ $COMP_CWORD -eq 1 ]]; then
COMPREPLY=( $(compgen -W "$(awk -F: '/^[a-zA-Z0-9_.-]+:/{print $1}' Makefile)" -- "$cur") )
return
fi
if [[ "$cur" == -* ]]; then
COMPREPLY=( $(awk -F'|' -v t="$target" '
($2=="__global__"||$2==t) && $1~/^-/ {
if ($3!="" && $1~/^--/) { split($3,v," "); for(i in v) print $1"="v[i] }
print $1
}
' "$cache" | compgen -W "$(cat)" -- "$cur") )
return
fi
local prev="${COMP_WORDS[COMP_CWORD-1]}"
if [[ "$prev" == -* ]]; then
local opt_vals
opt_vals=$(awk -F'|' -v t="$target" -v opt="$prev" '
($2=="__global__"||$2==t) && $1==opt && $3!="" { print $3 }
' "$cache")
if [[ -n "$opt_vals" ]]; then
COMPREPLY=( $(compgen -W "$opt_vals" -- "$cur") )
return
fi
fi
local pos=$(( COMP_CWORD - 1 ))
COMPREPLY=( $(awk -F'|' -v t="$target" -v pos="$pos" '
($2=="__global__"||$2==t){
split($3,v," ")
if ($1=="__args_"pos"__") { for(i in v) print v[i] }
else if ($1!~/^__args_/ && $1!~/^-/) { for(i in v) print $1"="v[i] }
}
' "$cache" | compgen -W "$(cat)" -- "$cur") )
}
complete -F _make_completion_enhanced make