Kotlin Formatting (ktfmt)

Elide comes with a built-in Kotlin formatter. It is ktfmt, but compiled into the Elide binary so it starts up faster and saves time, especially with smaller projects. You can use it via elide ktfmt -- .... 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 ktfmt:
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 ktfmt exits 0 even when files need formatting, so pass --set-exit-if-changed after -- if you need CI to fail on unformatted files.

> Argfile mixing: The original ktfmt only accepts an @argfile when it is the sole argument. elide ktfmt lifts this restriction — flags and argfile references may be combined freely (e.g. elide ktfmt -- --google-style @kt-sources.txt).

Examples

Fix an incorrectly formatted Kotlin file:

kotlin
// Hello.kt
fun   main()
{
      println("Hello!");
}
bash
 elide ktfmt -- -replace Hello.kt && cat Hello.kt
terminaloutput
Done formatting ./Hello.kt
fun main() {
  println("Hello!")
}

Format files and list which ones were modified:

bash
 elide ktfmt --list-files -- @kt-sources.txt
terminaloutput
src/main/Foo.kt
src/main/Bar.kt
✅  Formatted 1302 Kotlin sources

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

bash
 elide ktfmt --list-files -- -n @kt-sources.txt
terminaloutput
src/main/Foo.kt
src/main/Bar.kt
❌  Format-checking failed after 1302 Kotlin sources

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

bash
 elide ktfmt --list-diffs -- -n @kt-sources.txt
terminaloutput
--- a/src/main/Foo.kt
+++ b/src/main/Foo.kt
@@ -10,7 +10,7 @@
   val x = 1
+  val y = 2
-    val y = 2
   println(x + y)
❌  Format-checking failed after 1302 Kotlin sources

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

bash
 elide ktfmt --list-diffs=5 -- -n @kt-sources.txt

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

bash
 elide ktfmt --list-diffs -- -n --set-exit -if -changed @kt-sources.txt