# Formatting

> Format Java and Kotlin with `elide format`, which runs the bundled google-java-format (or the Netflix jfmt dialect) and ktfmt, or run `elide javaformat` or `elide ktfmt` alone.

Elide bundles google-java-format 1.37.0, Netflix jfmt 0.9.0 and ktfmt 0.65. `elide format` runs the Java and Kotlin
formatters; `elide javaformat` and `elide ktfmt` run one.

```bash
elide format -- src
elide javaformat -- Hello.java src/main/java
elide ktfmt -- Hello.kt src/main/kotlin
```

<img src="https://elide.help/assets/demos/format.gif" alt="Formatting Java and Kotlin source files with Elide" width="1591" height="1128" loading="lazy" decoding="async" class="rounded-lg border border-border my-6 w-full" />

Files and directories go after `--` and are rewritten in place. Each file is reported as unchanged, rewritten, would
be rewritten, or failed to parse. `javaformat` also accepts `-i`:

```bash
elide javaformat -- -i Hello.java
```

## Check formatting

`-n` or `--dry-run` after `--` writes nothing and exits 1 if a file would change. `--set-exit-if-changed` is
accepted; there is no `--check`.

```bash
elide format -- -n src
elide javaformat -- -n src/main/java
elide ktfmt -- -n src/main/kotlin
```

In check mode, two Elide options (before `--`) report what would change:

```bash
elide format --list-files -- -n src
elide format --list-diffs -- -n src
elide format --list-diffs=5 -- -n src
```

| Option | Output |
| --- | --- |
| `--list-files` | Paths that would change, one per line on stdout. |
| `--list-diffs=N` | Unified diffs when at most `N` files fail, otherwise paths. Use `=N`, not a separate value. |

Both work on `javaformat` and `ktfmt` too.

## Formatter flags

Everything after `--` is passed to the formatter, including argument files and style switches (`--google-style`,
`--kotlinlang-style`, `--aosp`):

```bash
elide ktfmt -- --google-style @kotlin-sources.txt
```

`elide javaformat --help` and `elide ktfmt --help` show Elide's options and how to show the formatter's help.

## jfmt dialect

`--jfmt` formats Java with [jfmt](https://github.com/Netflix/jfmt) instead of google-java-format: 4-space blocks,
8-space continuations, no column limit, braces around every statement body, and static imports after ordinary ones.

```bash
elide javaformat -- --jfmt src/main/java
elide format -- --jfmt -n src
```

jfmt is a fixed style, so `--jfmt` cannot be combined with `--aosp`, `--fix-imports-only`, the `--skip-*` flags, or
`--lines`, `--offset` and `--length`. jfmt's own wildcard-import expansion and qualified-name shortening are not run.
