Skip to content

Commit cf08b3d

Browse files
committed
Trim to subprojects name only until a single one is chosen
Signed-off-by: Michał Woś <[email protected]> fix completion when single subproject is left Fix completion when starting with ':' remove todo cosmetics trim to subprojects - ported to zsh
1 parent fa72b79 commit cf08b3d

File tree

2 files changed

+97
-4
lines changed

2 files changed

+97
-4
lines changed

_gradle

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,51 @@ __gradle-completion-init() {
142142
return 0
143143
}
144144

145+
# Instead of printing all tasks for all subprojects, print only subprojects names to limit number of entries shown on the screen
146+
# Once a single subproject is choosen - return its all available tasks.
147+
__gradle-trim-tasks-to-subprojects() {
148+
if [[ "$1" =~ ^(.+:.*) ]]; then
149+
# do nothing if a subproject is already choosen
150+
cat
151+
else
152+
awk -F '[[:alnum:]_]:' '{
153+
# Split entries into (subprojects names) AND (tasks accessible from root project)
154+
if ($1 ~ /:/) {
155+
# keep only subproject name (skip task name part)
156+
c = split($1, arr, "\\\\:")
157+
158+
if (c == 3) {
159+
current = "\\:" arr[c-1] "\\:"
160+
} else {
161+
current = arr[c-1] "\\:"
162+
}
163+
# keep uniq names (it assumes that the input to this funtion is already sorted)
164+
if (m == 0 || current != module[m-1]) {
165+
module[m++] = current
166+
}
167+
module_task_line[mtl++] = $0
168+
} else {
169+
# otherwise store the whole line with description
170+
root_task_line[rtl++] = $0
171+
}
172+
} END {
173+
for(i=0; i<rtl; i++) {
174+
print root_task_line[i]
175+
}
176+
if (m > 1) {
177+
for(i=0; i<m; i++) {
178+
print module[i]
179+
}
180+
} else {
181+
# at most 1 subproject is choosen, show possible tasks
182+
for(i=0; i<mtl; i++) {
183+
print module_task_line[i]
184+
}
185+
}
186+
}'
187+
fi
188+
}
189+
145190
__gradle_tasks() {
146191
local cache_dir cache_name gradle_build_file gradle_files_checksum project_root_dir
147192

@@ -157,10 +202,12 @@ __gradle_tasks() {
157202
if [[ -f $cache_dir/$cache_name.md5 ]]; then
158203
local cached_checksum="$(cat $cache_dir/$cache_name.md5)"
159204
local -a cached_tasks
205+
160206
if [[ -z $cur ]]; then
161-
cached_tasks=(${(f)"$(cat $cache_dir/$cached_checksum)"})
207+
cached_tasks=(${(f)"$(cat $cache_dir/$cached_checksum | __gradle-trim-tasks-to-subprojects "" )"})
162208
else
163-
cached_tasks=(${(f)"$(grep "^${cur//:/\\\\:}" $cache_dir/$cached_checksum)"})
209+
cur_esc="${cur//:/\\\\:}"
210+
cached_tasks=(${(f)"$(grep "^${cur_esc}" $cache_dir/$cached_checksum | __gradle-trim-tasks-to-subprojects $cur)"})
164211
fi
165212
_describe 'all tasks' cached_tasks && ret=0
166213
else

gradle-completion.bash

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,50 @@ __gradle-short-options() {
179179
COMPREPLY=( $(compgen -W "$args" -- "$cur") )
180180
}
181181

182+
# Instead of printing all tasks for all subprojects, print only subprojects names to limit number of entries shown on the screen
183+
# Once a single subproject is choosen - return its all available tasks.
184+
__gradle-trim-tasks-to-subprojects() {
185+
if [[ "$1" =~ ^(.+:.*) ]]; then
186+
# do nothing if a subproject is already choosen
187+
cat
188+
else
189+
awk '{
190+
# Split entries into (subprojects names) AND (tasks accessible from root project)
191+
if ($1 ~ /:/) {
192+
# keep only subproject name (skip task name part)
193+
c = split($1, arr, ":")
194+
if (c == 3) {
195+
current = ":" arr[c-1] ":"
196+
} else {
197+
current = arr[c-1] ":"
198+
}
199+
# keep uniq names (it assumes that the input to this funtion is already sorted)
200+
if (m == 0 || current != module[m-1]) {
201+
module[m++] = current
202+
}
203+
module_task_line[mtl++] = $0
204+
} else {
205+
# otherwise store the whole line with description
206+
root_task_line[rtl++] = $0
207+
}
208+
} END {
209+
for(i=0; i<rtl; i++) {
210+
print root_task_line[i]
211+
}
212+
if (m > 1) {
213+
for(i=0; i<m; i++) {
214+
print module[i]
215+
}
216+
} else {
217+
# at most 1 subproject is choosen, show possible tasks
218+
for(i=0; i<mtl; i++) {
219+
print module_task_line[i]
220+
}
221+
}
222+
}'
223+
fi
224+
}
225+
182226
__gradle-tasks() {
183227
local cur
184228
_get_comp_words_by_ref -n : cur
@@ -196,10 +240,11 @@ __gradle-tasks() {
196240
local cached_checksum="$(cat "$cache_dir/$cache_name.md5")"
197241
local -a cached_tasks
198242
if [[ -z "$cur" ]]; then
199-
cached_tasks=( $(cat "$cache_dir/$cached_checksum") )
243+
cached_tasks=( $(cat "$cache_dir/$cached_checksum" | __gradle-trim-tasks-to-subprojects "$cur") )
200244
else
201-
cached_tasks=( $(grep "^$cur" "$cache_dir/$cached_checksum") )
245+
cached_tasks=( $(grep "^$cur" "$cache_dir/$cached_checksum" | __gradle-trim-tasks-to-subprojects "$cur") )
202246
fi
247+
203248
COMPREPLY=( $(compgen -W "${cached_tasks[*]}" -- "$cur") )
204249
else
205250
__gradle-notify-tasks-cache-build
@@ -300,6 +345,7 @@ __gradle-generate-tasks-cache() {
300345
# subproject tasks can be referenced implicitly from root project
301346
if [[ "$GRADLE_COMPLETION_UNQUALIFIED_TASKS" == "true" ]]; then
302347
local -a implicit_tasks=()
348+
# TODO make root_tasks or implicit_tasks (?) uniq somewhere below, to not store in cache unnecessary entries.
303349
implicit_tasks=( $(comm -23 <(printf "%s\n" "${subproject_tasks[@]}" | sort) <(printf "%s\n" "${root_tasks[@]}" | sort)) )
304350
for task in $(printf "%s\n" "${implicit_tasks[@]}"); do
305351
gradle_all_tasks+=( "$task" )

0 commit comments

Comments
 (0)