PageRenderTime 121ms CodeModel.GetById 92ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

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