/bash_completion.d/java

http://github.com/brinkman83/bashrc · #! · 260 lines · 228 code · 32 blank · 0 comment · 0 complexity · 647e9c6c186f11d0b5fbe882b5b10e10 MD5 · raw file

  1. # bash completion for java, javac and javadoc
  2. # available path elements completion
  3. have java && {
  4. _java_path()
  5. {
  6. cur=${cur##*:}
  7. _filedir '@(jar|zip)'
  8. }
  9. # exact classpath determination
  10. _java_find_classpath()
  11. {
  12. local i
  13. # search first in current options
  14. for (( i=1; i < COMP_CWORD; i++ )); do
  15. if [[ "${COMP_WORDS[i]}" == -@(cp|classpath) ]]; then
  16. classpath=${COMP_WORDS[i+1]}
  17. break
  18. fi
  19. done
  20. # default to environment
  21. [ -z "$classpath" ] && classpath=$CLASSPATH
  22. # default to current directory
  23. [ -z "$classpath" ] && classpath=.
  24. }
  25. # exact sourcepath determination
  26. _java_find_sourcepath()
  27. {
  28. local i
  29. # search first in current options
  30. for (( i=1; i < COMP_CWORD; i++ )); do
  31. if [[ "${COMP_WORDS[i]}" == -sourcepath ]]; then
  32. sourcepath=${COMP_WORDS[i+1]}
  33. break
  34. fi
  35. done
  36. # default to classpath
  37. if [ -z "$sourcepath" ]; then
  38. _java_find_classpath
  39. sourcepath=$classpath
  40. fi
  41. }
  42. # available classes completion
  43. _java_classes()
  44. {
  45. local classpath i
  46. # find which classpath to use
  47. _java_find_classpath
  48. # convert package syntax to path syntax
  49. cur=${cur//.//}
  50. # parse each classpath element for classes
  51. for i in ${classpath//:/ }; do
  52. if [ -r $i ] && [[ "$i" == *.@(jar|zip) ]]; then
  53. if type zipinfo &>/dev/null; then
  54. COMPREPLY=( "${COMPREPLY[@]}" $( zipinfo -1 \
  55. "$i" "$cur*" 2>/dev/null | \
  56. grep '^[^$]*\.class$' ) )
  57. else
  58. COMPREPLY=( "${COMPREPLY[@]}" $( jar tf "$i" \
  59. "$cur" | grep '^[^$]*\.class$' ) )
  60. fi
  61. elif [ -d $i ]; then
  62. i=${i%/}
  63. # See Debian bug #496828
  64. COMPREPLY=( "${COMPREPLY[@]}" $( find "$i" -type f \
  65. -maxdepth 1 -path "$i/$cur*.class" 2>/dev/null | \
  66. grep -v "\\$" | sed -e "s|^$i/||" ) )
  67. # FIXME: if we have foo.class and foo/, the completion
  68. # returns "foo/"... how to give precedence to files
  69. # over directories?
  70. fi
  71. done
  72. # remove class extension
  73. COMPREPLY=( ${COMPREPLY[@]%.class} )
  74. # convert path syntax to package syntax
  75. COMPREPLY=( ${COMPREPLY[@]//\//.} )
  76. }
  77. # available packages completion
  78. _java_packages()
  79. {
  80. local sourcepath i
  81. # find which sourcepath to use
  82. _java_find_sourcepath
  83. # convert package syntax to path syntax
  84. cur=${cur//.//}
  85. # parse each sourcepath element for packages
  86. for i in ${sourcepath//:/ }; do
  87. if [ -d $i ]; then
  88. COMPREPLY=( "${COMPREPLY[@]}" $( command ls -F -d \
  89. $i/$cur* 2>/dev/null | sed -e 's|^'$i'/||' ) )
  90. fi
  91. done
  92. # keep only packages
  93. COMPREPLY=( $( echo "${COMPREPLY[@]}" | tr " " "\n" | grep "/$" ) )
  94. # remove packages extension
  95. COMPREPLY=( ${COMPREPLY[@]%/} )
  96. # convert path syntax to package syntax
  97. cur=${COMPREPLY[@]//\//.}
  98. }
  99. # java completion
  100. #
  101. _java()
  102. {
  103. local cur prev i
  104. COMPREPLY=()
  105. cur=`_get_cword`
  106. prev=${COMP_WORDS[COMP_CWORD-1]}
  107. for ((i=1; i < $COMP_CWORD; i++)); do
  108. case ${COMP_WORDS[$i]} in
  109. -cp|-classpath)
  110. ((i++)) # skip the classpath string.
  111. ;;
  112. -*)
  113. # this is an option, not a class/jarfile name.
  114. ;;
  115. *)
  116. # once we've seen a class, just do filename completion
  117. _filedir
  118. return 0
  119. ;;
  120. esac
  121. done
  122. case $prev in
  123. -@(cp|classpath))
  124. _java_path
  125. return 0
  126. ;;
  127. esac
  128. if [[ "$cur" == -* ]]; then
  129. # relevant options completion
  130. COMPREPLY=( $( compgen -W '-client -hotspot -server -classic \
  131. -cp -classpath -D -verbose -verbose:class \
  132. -verbose:gc -version:jni -version \
  133. -showversion -? -help -X -jar \
  134. -ea -enableassertions -da -disableassertions \
  135. -esa -enablesystemassertions \
  136. -dsa -disablesystemassertions ' -- "$cur" ) )
  137. else
  138. if [[ "$prev" == -jar ]]; then
  139. # jar file completion
  140. _filedir jar
  141. else
  142. # classes completion
  143. _java_classes
  144. fi
  145. fi
  146. }
  147. complete -F _java $filenames java
  148. }
  149. have javadoc &&
  150. _javadoc()
  151. {
  152. COMPREPLY=()
  153. local cur prev
  154. cur=`_get_cword`
  155. prev=${COMP_WORDS[COMP_CWORD-1]}
  156. case $prev in
  157. -@(overview|helpfile|stylesheetfile))
  158. _filedir
  159. return 0
  160. ;;
  161. -d)
  162. _filedir -d
  163. return 0
  164. ;;
  165. -@(classpath|bootclasspath|docletpath|sourcepath|extdirs))
  166. _java_path
  167. return 0
  168. ;;
  169. esac
  170. if [[ "$cur" == -* ]]; then
  171. # relevant options completion
  172. COMPREPLY=( $( compgen -W '-overview -public -protected \
  173. -package -private -help -doclet -docletpath \
  174. -sourcepath -classpath -exclude -subpackages \
  175. -breakiterator -bootclasspath -source -extdirs \
  176. -verbose -locale -encoding -J -d -use -version \
  177. -author -docfilessubdirs -splitindex \
  178. -windowtitle -doctitle -header -footer -bottom \
  179. -link -linkoffline -excludedocfilessubdir \
  180. -group -nocomment -nodeprecated -noqualifier \
  181. -nosince -nodeprecatedlist -notree -noindex \
  182. -nohelp -nonavbar -quiet -serialwarn -tag \
  183. -taglet -tagletpath -charset -helpfile \
  184. -linksource -stylesheetfile -docencoding' -- "$cur" ) )
  185. else
  186. # source files completion
  187. _filedir java
  188. # packages completion
  189. _java_packages
  190. fi
  191. } &&
  192. complete -F _javadoc $filenames javadoc
  193. have javac &&
  194. _javac()
  195. {
  196. COMPREPLY=()
  197. local cur prev
  198. cur=`_get_cword`
  199. prev=${COMP_WORDS[COMP_CWORD-1]}
  200. case $prev in
  201. -d)
  202. _filedir -d
  203. return 0
  204. ;;
  205. -@(classpath|bootclasspath|sourcepath|extdirs))
  206. _java_path
  207. return 0
  208. ;;
  209. esac
  210. if [[ "$cur" == -* ]]; then
  211. # relevant options completion
  212. COMPREPLY=( $( compgen -W '-g -g:none -g:lines -g:vars\
  213. -g:source -O -nowarn -verbose -deprecation -classpath\
  214. -sourcepath -bootclasspath -extdirs -d -encoding -source\
  215. -target -help' -- "$cur" ) )
  216. else
  217. # source files completion
  218. _filedir java
  219. fi
  220. } &&
  221. complete -F _javac $filenames javac
  222. # Local variables:
  223. # mode: shell-script
  224. # sh-basic-offset: 4
  225. # sh-indent-comment: t
  226. # indent-tabs-mode: nil
  227. # End:
  228. # ex: ts=4 sw=4 et filetype=sh