@@ -179,6 +179,50 @@ __gradle-short-options() {
179
179
COMPREPLY=( $( compgen -W " $args " -- " $cur " ) )
180
180
}
181
181
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
+
182
226
__gradle-tasks () {
183
227
local cur
184
228
_get_comp_words_by_ref -n : cur
@@ -196,10 +240,11 @@ __gradle-tasks() {
196
240
local cached_checksum=" $( cat " $cache_dir /$cache_name .md5" ) "
197
241
local -a cached_tasks
198
242
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 " ) )
200
244
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 " ) )
202
246
fi
247
+
203
248
COMPREPLY=( $( compgen -W " ${cached_tasks[*]} " -- " $cur " ) )
204
249
else
205
250
__gradle-notify-tasks-cache-build
@@ -300,6 +345,7 @@ __gradle-generate-tasks-cache() {
300
345
# subproject tasks can be referenced implicitly from root project
301
346
if [[ " $GRADLE_COMPLETION_UNQUALIFIED_TASKS " == " true" ]]; then
302
347
local -a implicit_tasks=()
348
+ # TODO make root_tasks or implicit_tasks (?) uniq somewhere below, to not store in cache unnecessary entries.
303
349
implicit_tasks=( $( comm -23 <( printf " %s\n" " ${subproject_tasks[@]} " | sort) <( printf " %s\n" " ${root_tasks[@]} " | sort) ) )
304
350
for task in $( printf " %s\n" " ${implicit_tasks[@]} " ) ; do
305
351
gradle_all_tasks+=( " $task " )
0 commit comments