From 47e5238a5a385f57cacf2de1fe0ea31046b09e0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Wo=C5=9B?= Date: Wed, 4 Mar 2020 16:07:31 +0100 Subject: [PATCH] Allow to override functions to change default behaviour of gradle completion per project --- README.md | 20 ++++++++++++++++++++ _gradle | 15 +++++++++++++-- gradle-completion.bash | 14 ++++++++++++-- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a82b2bc..415ec1c 100644 --- a/README.md +++ b/README.md @@ -155,6 +155,26 @@ export GRADLE_COMPLETION_UNQUALIFIED_TASKS="true" You may need to invalidate the cache using the cache config above or by executing `touch build.gradle`. + +#### Overriding default completion behoviour per project +If you need to customize this tool for one of your projects, just create `.gradle-completion` directory in your project root. +Then place there empty `gradle-completion.bash` (for bash) and/or `_gradle` (for zsh) and fill it with redefined original functions only. +Example content of `/.gradle-completion/gradle-completion.bash`: + +```bash +# Overrides default function that generates completion cache +__gradle-generate-tasks-cache() { + __gradle-set-files-checksum + + "${project_root_dir}"/gradlew initCompletionCache --completionCachePath="$cache_dir/$gradle_files_checksum" --no-scan + echo "$gradle_files_checksum" >| "$cache_dir/$cache_name.md5" +} + +``` + +But do not hesitate to make PR to this repo to support more generic features straight away. + + ## Troubleshooting If zsh completion isn't working, first try checking your `$fpath` with `echo $fpath`. diff --git a/_gradle b/_gradle index 10efa24..81a0862 100644 --- a/_gradle +++ b/_gradle @@ -130,8 +130,10 @@ __gradle-generate-tasks-cache() { __gradle-completion-init() { local cache_dir cache_name gradle_build_file gradle_files_checksum project_root_dir - __gradle-init-cache-dir + __gradle-set-project-root-dir + __gradle-source-project-overrides + __gradle-init-cache-dir __gradle-set-build-file if [[ -f $gradle_build_file ]]; then __gradle-set-cache-name @@ -146,7 +148,6 @@ __gradle_tasks() { local cache_dir cache_name gradle_build_file gradle_files_checksum project_root_dir __gradle-init-cache-dir - __gradle-set-project-root-dir __gradle-set-build-file if [[ -f $gradle_build_file ]]; then __gradle-set-cache-name @@ -312,6 +313,13 @@ _gradle_dependency_configurations() { _describe -t 'dependency configurations' "dependency configuration" configurations } +__gradle-source-project-overrides() { + if [[ -f "$project_root_dir/.gradle-completion/gradle-completion.bash" ]]; then + # override default behaviours of this script + source "$project_root_dir"/.gradle-completion/_gradle + fi +} + _gradle() { local cur=${words[CURRENT]} local curcontext="$curcontext" state @@ -385,6 +393,9 @@ _gradle() { {-x,--exclude-task}'[Specify a task to be excluded from execution.]' \ '(-)*:: :->task-or-option' && ret=0 + __gradle-set-project-root-dir + __gradle-source-project-overrides + if [[ $words[CURRENT] != -* && $state != "argument-expected" ]]; then __gradle_tasks && ret=0 else diff --git a/gradle-completion.bash b/gradle-completion.bash index d4c3ad0..3b42bd1 100644 --- a/gradle-completion.bash +++ b/gradle-completion.bash @@ -184,7 +184,6 @@ __gradle-tasks() { _get_comp_words_by_ref -n : cur __gradle-init-cache-dir - __gradle-set-project-root-dir __gradle-set-build-file if [[ -f "$gradle_build_file" ]]; then __gradle-set-cache-name @@ -310,14 +309,22 @@ __gradle-generate-tasks-cache() { echo "$gradle_files_checksum" >| "$cache_dir/$cache_name.md5" } +__gradle-source-project-overrides() { + if [[ -f "$project_root_dir/.gradle-completion/gradle-completion.bash" ]]; then + # override default behaviours of this script + source "$project_root_dir"/.gradle-completion/gradle-completion.bash + fi +} + __gradle-completion-init() { local cache_dir cache_name gradle_build_file gradle_files_checksum project_root_dir local OLDIFS="$IFS" local IFS=$'\n' - __gradle-init-cache-dir __gradle-set-project-root-dir + __gradle-source-project-overrides + __gradle-init-cache-dir __gradle-set-build-file if [[ -f "$gradle_build_file" ]]; then __gradle-set-cache-name @@ -342,6 +349,9 @@ _gradle() { local OLDIFS="$IFS" local IFS=$'\n' + __gradle-set-project-root-dir + __gradle-source-project-overrides + if [[ ${cur} == --* ]]; then __gradle-long-options elif [[ ${cur} == -D* ]]; then