Add command sensitive completion.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Sat, 25 Feb 2017 21:44:22 +0200
changeset 839 e22ea617339d
parent 838 98cdbead06be
child 840 92bd6fcf51da
Add command sensitive completion.
.bash_completion.d/gradle
--- a/.bash_completion.d/gradle	Wed Feb 22 16:09:02 2017 +0200
+++ b/.bash_completion.d/gradle	Sat Feb 25 21:44:22 2017 +0200
@@ -1,5 +1,17 @@
 # -*- mode: sh; sh-shell-file: bash -*-
 
+_gradle_cmd()
+{
+  for ((i = $COMP_CWORD - 1; i > 0; i -= 1)); do
+    case "${COMP_WORDS[i]}" in
+      -*) continue ;;
+      *" "*) continue ;;
+    esac
+    echo "${COMP_WORDS[i]}"
+    return
+  done
+}
+
 _gradle()
 {
   local _opts="-? -h --help -A --dep-tasks -C --cache -D --system-prop -I --init-script -P --project-prop -S --full-stacktrace -a --no-rebuild --all \
@@ -8,30 +20,61 @@
      --continue --daemon --foreground --no-color --no-daemon --offline --parallel --parallel-threads --project-cache-dir --recompile-scripts --refresh --refresh-dependencies \
      --rerun-tasks --stop"
   local _help_cmds='components dependencies dependencyInsight buildEnvironment help model projects properties tasks wrapper'
-  local _java_cmds='assemble build buildDependents buildNeeded classes compileJava processResources clean cleanTest jar testClasses compileTestJava processTestResources javadoc check test uploadArchives'
+  local _java_cmds='assemble build buildDependents buildNeeded classes compileJava processResources cleanTest jar testClasses compileTestJava processTestResources javadoc check test uploadArchives'
   local _application_cmds='run startScripts installApp distZip distTar'
-  local _other_cmd='install interactive'
+  local _other_cmd='clean init install interactive'
+  declare -a _extra
   local cur prev
   cur=${COMP_WORDS[COMP_CWORD]}
   [[ $COMP_CWORD != 0 ]] && prev=${COMP_WORDS[COMP_CWORD-1]}
-  case "$prev" in
-    -I|--init-script|-b|--build-file|-c|--settings-file|-e|--embedded)
-      COMPREPLY=( $(compgen -f -- "$cur") )
-      return ;;
-    -g|--gradle-user-home|-p|--project-dir|--project-cache-dir)
-      COMPREPLY=( $(compgen -d -- "$cur") )
-      return ;;
+
+  # Perform default option completion for command.
+  if [[ -z "$curr" ]]; then
+      case "$prev" in
+        help)
+          COMPREPLY=( --task )
+          return ;;
+        init)
+          COMPREPLY=( --type )
+          return ;;
+        wrapper)
+          COMPREPLY=( --gradle-version )
+          return ;;
+        dependencies)
+          COMPREPLY=( --configuration )
+          return ;;
+      esac
+  fi
+
+  # Add or perform command specific completion.
+  local cmd="$(_gradle_cmd)"
+  case "$cmd" in
     help)
-      COMPREPLY=( $(compgen -W "--task" -- "$cur") )
-      return ;;
+      case "$prev" in
+        --task)
+          COMPREPLY=( $(compgen -W '$_help_cmds $_java_cmds $_application_cmds $_other_cmd'  -- $cur) )
+          return ;;
+      esac
+      _extra+=( --task )
+      ;;
+    init)
+      case "$prev" in
+        --type)
+          COMPREPLY=( $(compgen -W "basic groovy-library java-library pom scala-library" -- "$cur") )
+          return ;;
+        --test-framework)
+          COMPREPLY=( $(compgen -W "spock testng" -- "$cur") )
+          return ;;
+      esac
+      _extra+=( --type --test-framework )
+      ;;
     wrapper)
-      COMPREPLY=( $(compgen -W "--gradle-version" -- "$cur") )
-      return ;;
+      _extra+=( --gradle-version )
+      ;;
     dependencies)
-      COMPREPLY=( $(compgen -W "--configuration" -- "$cur") )
-      return ;;
-    --configuration)
-      local _configs="archives              - Configuration for archive artifacts.
+      case "$prev" in
+        --configuration)
+          local _configs="archives              - Configuration for archive artifacts.
 compile               - Compile time dependencies.
 compileClasspath      - Compile classpath, used when compiling source.
 compileOnly           - Compile time only dependencies, not used at runtime.
@@ -43,22 +86,27 @@
 testCompileClasspath  - Compile classpath for source set 'test'.
 testCompileOnly       - Compile dependencies for source set 'test'.
 testRuntime           - Runtime dependencies for source set 'test'."
-      local IFS=$'\n'
-      COMPREPLY=( $(compgen -W '${_configs}' -- "$cur") )
-      if [[ ${#COMPREPLY[@]} -le 1 ]]; then
-          COMPREPLY=( ${COMPREPLY[0]%% *-*} )
-      fi
+          local IFS=$'\n'
+          COMPREPLY=( $(compgen -W '${_configs}' -- "$cur") )
+          if [[ ${#COMPREPLY[@]} -le 1 ]]; then
+              COMPREPLY=( ${COMPREPLY[0]%% *-*} )
+          fi
+          return ;;
+      esac
+      _extra+=( --configuration )
+      ;;
+  esac
+
+  # Perform generic completion.
+  case "$prev" in
+    -I|--init-script|-b|--build-file|-c|--settings-file|-e|--embedded)
+      COMPREPLY=( $(compgen -f -- "$cur") )
       return ;;
-    --task)
-      COMPREPLY=( $(compgen -W '$_help_cmds $_java_cmds $_application_cmds $_other_cmd'  -- $cur) )
+    -g|--gradle-user-home|-p|--project-dir|--project-cache-dir)
+      COMPREPLY=( $(compgen -d -- "$cur") )
       return ;;
   esac
-  # colonprefixes=${cur%"${cur##*:}"}
-  COMPREPLY=( $(compgen -W '$_opts $_help_cmds $_java_cmds $_application_cmds $_other_cmd'  -- $cur) )
-  # local i=${#COMPREPLY[*]}
-  # while [ $((--i)) -ge 0 ]; do
-  #    COMPREPLY[$i]=${COMPREPLY[$i]#"$colonprefixes"}
-  # done
-  return 0
+  COMPREPLY=( $(compgen -W '$_extra $_opts $_help_cmds $_java_cmds $_application_cmds $_other_cmd' -- $cur) )
+  compopt +o nospace
 } &&
     complete -F _gradle gradle ./gradlew