From c2487be9620160829bc4d367db46eafa90ce5786 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 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Michał Woś --- README.md | 20 ++++++++++++++++++++ _gradle | 18 +++++++++++++++--- gradle-completion.bash | 14 ++++++++++++-- 3 files changed, 47 insertions(+), 5 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..71a896d 100644 --- a/_gradle +++ b/_gradle @@ -128,10 +128,19 @@ __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" ]]; then + # override default behaviours of this script + source "$project_root_dir"/.gradle-completion/_gradle + fi +} + __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 @@ -143,10 +152,9 @@ __gradle-completion-init() { } __gradle_tasks() { - local cache_dir cache_name gradle_build_file gradle_files_checksum project_root_dir + local cache_dir cache_name gradle_build_file gradle_files_checksum __gradle-init-cache-dir - __gradle-set-project-root-dir __gradle-set-build-file if [[ -f $gradle_build_file ]]; then __gradle-set-cache-name @@ -313,6 +321,7 @@ _gradle_dependency_configurations() { } _gradle() { + local project_root_dir local cur=${words[CURRENT]} local curcontext="$curcontext" state integer ret=1 @@ -385,6 +394,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