Java Formatting (Google Java Format)

Elide comes with a built-in Java formatter. It is Google Java Format, but compiled into the Elide binary so it starts up faster and saves time, especially with smaller projects. Use it via elide javaformat -- .... It is fully compatible and drop-in ready.

Elide-specific options

These options are passed before the -- separator and are handled by Elide, not forwarded to google-java-format:
OptionDescription
—list-filesPrint the path of each file that fails the check (check mode) or was modified by the formatter (replace mode), one per line to stdout. —list-diffs takes precedence when it is active (check mode); in replace mode —list-diffs is ignored, so this listing is still produced.
—list-diffs[=N]Print a unified diff for each file that would change in check mode (colored when stdout is a TTY and neither NO_COLOR nor CI is set, plain otherwise). Only valid with -n/—dry-run; ignored in replace mode. When N is given, diffs are only printed if the number of failing files is at most N; otherwise falls back to —list-files behaviour. Omit N or pass 0 for no limit. The limit must be written with an equals sign (—list-diffs=5), not as a separate argument.
> Note: --list-files and --list-diffs only affect output, not the exit code. In check mode google-java-format exits 0 even when files need formatting, so pass --set-exit-if-changed after -- if you need CI to fail on unformatted files.

Examples

Fix an incorrectly formatted Java file:

java
// Hello.java
public class Hello {
public static void main(String[ ] args) 
{
    System.out.println("Hello!");}
}
bash
 elide javaformat -- -replace Hello.java && cat Hello.java
terminaloutput
public class Hello {
  public static void main(String[] args) {
    System.out.println("Hello!");
  }
}

Format files and list which ones were modified:

bash
 elide javaformat --list-files -- -i @java-sources.txt
terminaloutput
src/main/Foo.java
src/main/Bar.java
✅  Formatted 842 Java sources

Check formatting without modifying files, listing which files need changes:

bash
 elide javaformat --list-files -- -n @java-sources.txt
terminaloutput
src/main/Foo.java
src/main/Bar.java
❌  Format-checking failed after 842 Java sources

Show a colored diff of what would change (useful in CI to surface formatting issues):

bash
 elide javaformat --list-diffs -- -n @java-sources.txt
terminaloutput
--- a/src/main/Foo.java
+++ b/src/main/Foo.java
@@ -5,7 +5,7 @@
   public static void main(String[] args) {
+    System.out.println("Hello!");
-      System.out.println("Hello!");
   }
❌  Format-checking failed after 842 Java sources

Limit diff output to runs with at most 5 failing files (falls back to listing files if more fail):

bash
 elide javaformat --list-diffs=5 -- -n @java-sources.txt

CI-ready: show diffs and fail the step when files need formatting:

bash
 elide javaformat --list-diffs -- -n --set-exit -if -changed @java-sources.txt