Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Java completion for Java files



Awesome! Thank you so much!

On Fri, 16 Feb 2024 at 20:18, Oliver Kiddle <opk@xxxxxxx> wrote:
[ moved to zsh-workers ]

On 5 Feb, Henri Tremblay wrote:
> Java now supports (since Java 10 I think) command lines like 
>
> java Math.java 
>
> or 
>
> java src/main/java/org/me/Math.java. 
>
> It will then just compile and launch that java file. But zsh won't autocomplete
> for that. It only wants a jar, class file or whatever.

The following adds completion for that. This does add --source to the
options completed because that's needed to limit completion to files
when it's present. However, there does appear to be many new options
not covered by the completion. I'd guess that most javac options are now
valid with java too.

The JEP330 link indicates that with --source, files needn't have a .java
extension but broadening the file completion in that case is probably
unhelpful (seems the feature primarily exists to support use with a
shebang).

Oliver

diff --git a/Completion/Unix/Command/_java b/Completion/Unix/Command/_java
index b2352df7b..325413d87 100644
--- a/Completion/Unix/Command/_java
+++ b/Completion/Unix/Command/_java
@@ -82,7 +82,10 @@ java)
     '(- 1)-X-[non-standard java option]:option' \
     '(- 1)-jar[specify a program encapsulated as jar]:jar:_files -g \*.jar\(-.\)' \
     '-splash\:-[show splash screen with specified image]:image:_files' \
-    '(-):class:_java_class -m main ${(kv)opt_args[(i)(-classpath|-cp)]}' \
+    '--source=[set the version of the source in source-file mode]:version' \
+    '(-):arg: _alternative
+      "classes\:class\:{ (( ! $+opt_args[--source] )) && _java_class -m main ${(kv)opt_args[(i)(-classpath|-cp)]} }"
+      "files\:source file\:_files -g \*.java\(-.\)"' \
     '*::args:= _normal' \
      && return 0
   ;;


Messages sorted by: Reverse Date, Date, Thread, Author