@@ -8,62 +8,65 @@ _comp_cmd_java__classpath()
88}
99
1010# exact classpath determination
11- _java_find_classpath ()
11+ # @var[out] ret Array to store classpaths
12+ # @return 0 if at least one element is generated, or otherwise 1
13+ _comp_cmd_java__find_classpath ()
1214{
1315 local i
1416
15- classpath =
17+ ret =
1618
1719 # search first in current options
1820 for (( i = 1 ; i < cword; i++ )) ; do
1921 if [[ ${words[i]} == -@ (cp| classpath) ]]; then
20- classpath =${words[i + 1]}
22+ ret =${words[i + 1]}
2123 break
2224 fi
2325 done
2426
25- # default to environment
26- [[ ! $classpath ]] && classpath=${CLASSPATH-}
27-
28- # default to current directory
29- [[ ! $classpath ]] && classpath=.
27+ # fall back to environment, followed by current directory
28+ _comp_split -F : ret " ${ret:- ${CLASSPATH:- .} } "
3029}
3130
3231# exact sourcepath determination
33- _java_find_sourcepath ()
32+ # @var[out] ret Array to store sourcepaths
33+ # @return 0 if at least one element is generated, or otherwise 1
34+ _comp_cmd_java__find_sourcepath ()
3435{
3536 local i
3637
37- sourcepath =
38+ ret =
3839
3940 # search first in current options
4041 for (( i = 1 ; i < cword; i++ )) ; do
4142 if [[ ${words[i]} == -s ourcepath ]]; then
42- sourcepath =${words[i + 1]}
43+ ret =${words[i + 1]}
4344 break
4445 fi
4546 done
4647
47- # default to classpath
48- if [[ ! -v sourcepath ]]; then
49- local classpath
50- _java_find_classpath
51- sourcepath=$classpath
48+ # fall back to classpath
49+ if [[ ! $ret ]]; then
50+ _comp_cmd_java__find_classpath
51+ return
5252 fi
53+
54+ _comp_split -F : ret " $ret "
5355}
5456
5557# available classes completion
5658_comp_cmd_java__classes ()
5759{
58- local classpath i
60+ local ret i
5961
6062 # find which classpath to use
61- _java_find_classpath
63+ _comp_cmd_java__find_classpath
64+ local -a classpaths=(" ${ret[@]} " )
6265
6366 # convert package syntax to path syntax
6467 cur=${cur// .// }
6568 # parse each classpath element for classes
66- for i in ${classpath //:/ } ; do
69+ for i in " ${classpaths[@]} " ; do
6770 if [[ $i == * .@ (jar| zip) && -r $i ]]; then
6871 if type zipinfo & > /dev/null; then
6972 COMPREPLY+=($( zipinfo -1 " $i " " $cur *" 2> /dev/null |
@@ -103,27 +106,30 @@ _comp_cmd_java__classes()
103106# available packages completion
104107_comp_cmd_java__packages ()
105108{
106- local sourcepath i
109+ local ret i files
107110
108111 # find which sourcepath to use
109- _java_find_sourcepath
112+ _comp_cmd_java__find_sourcepath || return 0
113+ local -a sourcepaths=(" ${ret[@]} " )
110114
111115 # convert package syntax to path syntax
112- cur=${cur// .// }
116+ local cur=${cur// .// }
113117 # parse each sourcepath element for packages
114- for i in ${sourcepath //:/ } ; do
118+ for i in " ${sourcepaths[@]} " ; do
115119 if [[ -d $i ]]; then
116- COMPREPLY+=($( command ls -F -d " $i /$cur " * 2> /dev/null |
117- command sed -e ' s|^' " $i " ' /||' ) )
120+ _comp_expand_glob files ' "$i/$cur"*'
121+ (( ${# files[@]} )) || continue
122+ _comp_split -la COMPREPLY " $(
123+ command ls -F -d " ${files[@]} " 2> /dev/null |
124+ command sed -e ' s|^' " $i " ' /||'
125+ ) "
118126 fi
119127 done
120128 if (( ${# COMPREPLY[@]} != 0 )) ; then
121- # keep only packages
122- COMPREPLY=($( tr " " " \n" <<< " ${COMPREPLY[@]}" | command grep " /$" ) )
123- # remove packages extension
124- COMPREPLY=(${COMPREPLY[@]%/ } )
129+ # keep only packages with the package suffix `/` being removed
130+ _comp_split -l COMPREPLY " $( printf ' %s\n' " ${COMPREPLY[@]} " | command sed -n ' s,/$,,p' ) "
125131 # convert path syntax to package syntax
126- cur= " ${COMPREPLY[* ]// \/ / .} "
132+ (( ${ # COMPREPLY[@]} )) && COMPREPLY=( " ${COMPREPLY[@ ]// \/ / .} " )
127133 fi
128134}
129135
0 commit comments