google-java-format

Google's Java formatter, in Google Java Style.

Formats
Java
In ur.toml
[format.java] style = "google-java-format"
Its options
https://github.com/google/google-java-format

Formatted

Cart.java, as written
import java.util.List;
class Cart { int subtotal(List<Item> items) { int sum = 0; for (Item item : items) { sum += item.price() * item.quantity(); } return sum; }
    String label(Item item){return item.name()+" x"+item.quantity();} }
ur format --style google-java-format Cart.java
import java.util.List;

class Cart {
  int subtotal(List<Item> items) {
    int sum = 0;
    for (Item item : items) {
      sum += item.price() * item.quantity();
    }
    return sum;
  }

  String label(Item item) {
    return item.name() + " x" + item.quantity();
  }
}

Try it in the playground →

Defaults

The shared options, as google-java-format sets them when nothing configures it. Its own names for them are read from its configuration files, but ur.toml sets them by the shared name only.

OptionDefaultgoogle-java-format calls it
width100
indent.width2
indent.style
line-ending"auto"
trailing-comma.add"preserve"
trailing-comma.forces-breakfalse
quotes"preserve"
target

Its own options

Set in a table named for it, such as [format.google-java-format], or in a language's table, as google-java-format = { ... }.

OptionDefaultWhat it does
aospfalseFormat in the Android Open Source Project's style, indented four spaces, in place of Google's. Takes true or false.
fix-imports-onlyfalseOnly sort the imports and remove unused ones, leaving the rest as written. Takes true or false.
skip-sorting-importsfalseLeave the imports in the order written. Takes true or false.
skip-removing-unused-importsfalseKeep imports that nothing uses. Takes true or false.
skip-reflowing-long-stringsfalseLeave string literals that run past the line width unbroken. Takes true or false.
skip-javadoc-formattingfalseLeave Javadoc comments as written. Takes true or false.