1*5be3f65cSAndroid Build Coastguard Worker# ktfmt [](https://github.com/facebook/ktfmt/releases/) [](https://central.sonatype.com/artifact/com.facebook/ktfmt) [](https://github.com/facebook/ktfmt/actions/workflows/build_and_test.yml "GitHub Actions workflow status") [](https://slack-chats.kotlinlang.org/c/ktfmt) [](https://surveys.jetbrains.com/s3/kotlin-slack-sign-up) [](https://github.com/facebook/ktfmt/issues) 2*5be3f65cSAndroid Build Coastguard Worker 3*5be3f65cSAndroid Build Coastguard Worker`ktfmt` is a program that pretty-prints (formats) Kotlin code, based on 4*5be3f65cSAndroid Build Coastguard Worker[google-java-format](https://github.com/google/google-java-format). 5*5be3f65cSAndroid Build Coastguard Worker 6*5be3f65cSAndroid Build Coastguard WorkerThe minimum supported runtime version is JDK 11, released September 2018. 7*5be3f65cSAndroid Build Coastguard Worker 8*5be3f65cSAndroid Build Coastguard Worker## Demo 9*5be3f65cSAndroid Build Coastguard Worker 10*5be3f65cSAndroid Build Coastguard Worker|Before Formatting| Formatted by `ktfmt`| 11*5be3f65cSAndroid Build Coastguard Worker| ---- | ---- | 12*5be3f65cSAndroid Build Coastguard Worker|  |  | 13*5be3f65cSAndroid Build Coastguard Worker 14*5be3f65cSAndroid Build Coastguard WorkerFor comparison, the same code formatted by [`ktlint`](https://github.com/pinterest/ktlint) and 15*5be3f65cSAndroid Build Coastguard WorkerIntelliJ: 16*5be3f65cSAndroid Build Coastguard Worker 17*5be3f65cSAndroid Build Coastguard Worker| Formatted by `ktlint`|Formatted by IntelliJ| 18*5be3f65cSAndroid Build Coastguard Worker| ------ | --------| 19*5be3f65cSAndroid Build Coastguard Worker|  |  | 20*5be3f65cSAndroid Build Coastguard Worker 21*5be3f65cSAndroid Build Coastguard Worker## Playground 22*5be3f65cSAndroid Build Coastguard Worker 23*5be3f65cSAndroid Build Coastguard WorkerWe have a [live playground](https://facebook.github.io/ktfmt/) where you can easily see how ktfmt 24*5be3f65cSAndroid Build Coastguard Workerwould format your code. 25*5be3f65cSAndroid Build Coastguard WorkerGive it a try! https://facebook.github.io/ktfmt/ 26*5be3f65cSAndroid Build Coastguard Worker 27*5be3f65cSAndroid Build Coastguard Worker## Using the formatter 28*5be3f65cSAndroid Build Coastguard Worker 29*5be3f65cSAndroid Build Coastguard Worker### IntelliJ, Android Studio, and other JetBrains IDEs 30*5be3f65cSAndroid Build Coastguard Worker 31*5be3f65cSAndroid Build Coastguard WorkerA [ktfmt IntelliJ plugin](https://plugins.jetbrains.com/plugin/14912-ktfmt) is available from the 32*5be3f65cSAndroid Build Coastguard Workerplugin repository. To install it, go to your IDE's settings and select the `Plugins` category. Click 33*5be3f65cSAndroid Build Coastguard Workerthe `Marketplace` tab, search for the `ktfmt` plugin, and click the `Install` button. 34*5be3f65cSAndroid Build Coastguard Worker 35*5be3f65cSAndroid Build Coastguard WorkerThe plugin will be disabled by default. To enable it in the current project, go to 36*5be3f65cSAndroid Build Coastguard Worker`File → Settings... → ktfmt Settings` (or `IntelliJ IDEA → Preferences... → Editor → ktfmt Settings` 37*5be3f65cSAndroid Build Coastguard Workeron macOS) and check the `Enable ktfmt` checkbox. A notification will be presented when you first 38*5be3f65cSAndroid Build Coastguard Workeropen a project offering to do this for you. 39*5be3f65cSAndroid Build Coastguard Worker 40*5be3f65cSAndroid Build Coastguard WorkerTo enable it by default in new projects, use 41*5be3f65cSAndroid Build Coastguard Worker`File → New Project Settings → Preferences for new Projects → Editor → ktfmt Settings`. 42*5be3f65cSAndroid Build Coastguard Worker 43*5be3f65cSAndroid Build Coastguard WorkerWhen enabled, it will replace the normal `Reformat Code` action, which can be triggered from the 44*5be3f65cSAndroid Build Coastguard Worker`Code` menu or with the Ctrl-Alt-L (by default) keyboard shortcut. 45*5be3f65cSAndroid Build Coastguard Worker 46*5be3f65cSAndroid Build Coastguard WorkerTo configure IntelliJ to approximate ktfmt's formatting rules during code editing, you can edit your 47*5be3f65cSAndroid Build Coastguard Workerproject's 48*5be3f65cSAndroid Build Coastguard Worker[`.editorconfig` file](https://www.jetbrains.com/help/idea/configuring-code-style.html#editorconfig) 49*5be3f65cSAndroid Build Coastguard Workerto include the Kotlin section from one of the files inside [`docs/editorconfig`](docs/editorconfig). 50*5be3f65cSAndroid Build Coastguard Worker 51*5be3f65cSAndroid Build Coastguard Worker#### Share IntelliJ ktfmt settings 52*5be3f65cSAndroid Build Coastguard WorkerIn order to share the settings, make sure to commit the file `.idea/ktfmt.xml` into your codebase. 53*5be3f65cSAndroid Build Coastguard Worker 54*5be3f65cSAndroid Build Coastguard Worker### Installation 55*5be3f65cSAndroid Build Coastguard Worker 56*5be3f65cSAndroid Build Coastguard Worker#### Homebrew 57*5be3f65cSAndroid Build Coastguard Worker 58*5be3f65cSAndroid Build Coastguard WorkerIf you're a [Homebrew](https://brew.sh) user, you can install 59*5be3f65cSAndroid Build Coastguard Worker[ktfmt](https://formulae.brew.sh/formula/ktfmt) via: 60*5be3f65cSAndroid Build Coastguard Worker 61*5be3f65cSAndroid Build Coastguard Worker``` 62*5be3f65cSAndroid Build Coastguard Worker$ brew install ktfmt 63*5be3f65cSAndroid Build Coastguard Worker``` 64*5be3f65cSAndroid Build Coastguard Worker 65*5be3f65cSAndroid Build Coastguard Worker### from the command-line 66*5be3f65cSAndroid Build Coastguard Worker 67*5be3f65cSAndroid Build Coastguard Worker[Download the formatter](https://github.com/facebook/ktfmt/releases) and run it with: 68*5be3f65cSAndroid Build Coastguard Worker 69*5be3f65cSAndroid Build Coastguard Worker``` 70*5be3f65cSAndroid Build Coastguard Workerjava -jar /path/to/ktfmt-<VERSION>-jar-with-dependencies.jar [--kotlinlang-style | --google-style] [files...] 71*5be3f65cSAndroid Build Coastguard Worker``` 72*5be3f65cSAndroid Build Coastguard Worker 73*5be3f65cSAndroid Build Coastguard Worker`--kotlinlang-style` makes `ktfmt` use a block indent of 4 spaces instead of 2. 74*5be3f65cSAndroid Build Coastguard WorkerSee below for details. 75*5be3f65cSAndroid Build Coastguard Worker 76*5be3f65cSAndroid Build Coastguard Worker***Note:*** 77*5be3f65cSAndroid Build Coastguard Worker*There is no configurability as to the formatter's algorithm for formatting (apart from the 78*5be3f65cSAndroid Build Coastguard Workerdifferent styles). This is a deliberate design decision to unify our code formatting on a single 79*5be3f65cSAndroid Build Coastguard Workerformat.* 80*5be3f65cSAndroid Build Coastguard Worker 81*5be3f65cSAndroid Build Coastguard Worker### using Gradle 82*5be3f65cSAndroid Build Coastguard Worker 83*5be3f65cSAndroid Build Coastguard WorkerA [Gradle plugin (ktfmt-gradle)](https://github.com/cortinico/ktfmt-gradle) is available on the 84*5be3f65cSAndroid Build Coastguard WorkerGradle Plugin Portal. To set it up, just follow the instructions in the 85*5be3f65cSAndroid Build Coastguard Worker[How-to-use section](https://github.com/cortinico/ktfmt-gradle#how-to-use-). 86*5be3f65cSAndroid Build Coastguard Worker 87*5be3f65cSAndroid Build Coastguard WorkerAlternatively, you can use [Spotless](https://github.com/diffplug/spotless) with the 88*5be3f65cSAndroid Build Coastguard Worker[ktfmt Gradle plugin](https://github.com/diffplug/spotless/tree/main/plugin-gradle#ktfmt). 89*5be3f65cSAndroid Build Coastguard Worker 90*5be3f65cSAndroid Build Coastguard Worker### using Maven 91*5be3f65cSAndroid Build Coastguard Worker 92*5be3f65cSAndroid Build Coastguard WorkerConsider using [Spotless](https://github.com/diffplug/spotless) with the 93*5be3f65cSAndroid Build Coastguard Worker[ktfmt Maven plugin](https://github.com/diffplug/spotless/tree/main/plugin-maven#ktfmt). 94*5be3f65cSAndroid Build Coastguard Worker 95*5be3f65cSAndroid Build Coastguard Worker### using pre-commit hooks 96*5be3f65cSAndroid Build Coastguard Worker 97*5be3f65cSAndroid Build Coastguard WorkerA [pre-commit hook](https://pre-commit.com/hooks.html) is implemented in 98*5be3f65cSAndroid Build Coastguard Worker[language-formatters-pre-commit-hooks](https://github.com/macisamuele/language-formatters-pre-commit-hooks) 99*5be3f65cSAndroid Build Coastguard Worker 100*5be3f65cSAndroid Build Coastguard Worker## FAQ 101*5be3f65cSAndroid Build Coastguard Worker 102*5be3f65cSAndroid Build Coastguard Worker### `ktfmt` vs `ktlint` vs IntelliJ 103*5be3f65cSAndroid Build Coastguard Worker 104*5be3f65cSAndroid Build Coastguard Worker`ktfmt` uses google-java-format's underlying engine, and as such, many items on 105*5be3f65cSAndroid Build Coastguard Worker[google-java-format's FAQ](https://github.com/google/google-java-format/wiki/FAQ) apply to `ktfmt` 106*5be3f65cSAndroid Build Coastguard Workeras well. 107*5be3f65cSAndroid Build Coastguard Worker 108*5be3f65cSAndroid Build Coastguard WorkerIn particular, here are the principles that we try to adhere to: 109*5be3f65cSAndroid Build Coastguard Worker1. `ktfmt` ignores most existing formatting. It respects existing newlines in some places, but in 110*5be3f65cSAndroid Build Coastguard Worker general, its output is deterministic and is independent of the input code. 111*5be3f65cSAndroid Build Coastguard Worker2. `ktfmt` exposes no configuration options that govern formatting behavior. See 112*5be3f65cSAndroid Build Coastguard Worker https://github.com/google/google-java-format/wiki/FAQ#i-just-need-to-configure-it-a-bit-differently-how 113*5be3f65cSAndroid Build Coastguard Worker for the rationale. 114*5be3f65cSAndroid Build Coastguard Worker 1. For exposed configurations, like `style`, we aim to make sure that those are easily shared 115*5be3f65cSAndroid Build Coastguard Worker across your organization/codebase to avoid 116*5be3f65cSAndroid Build Coastguard Worker [bikeshedding discussions](https://thedecisionlab.com/biases/bikeshedding) about code format. 117*5be3f65cSAndroid Build Coastguard Worker 118*5be3f65cSAndroid Build Coastguard WorkerThese two properties make `ktfmt` a good fit in large Kotlin code bases, where consistency is very 119*5be3f65cSAndroid Build Coastguard Workerimportant. 120*5be3f65cSAndroid Build Coastguard Worker 121*5be3f65cSAndroid Build Coastguard WorkerWe created `ktfmt` because at the time `ktlint` and IntelliJ sometimes failed to produce 122*5be3f65cSAndroid Build Coastguard Workernice-looking code that fits in 100 columns, as can be seen in the [Demo](README.md#Demo) section. 123*5be3f65cSAndroid Build Coastguard Worker 124*5be3f65cSAndroid Build Coastguard Worker### `ktfmt` uses a 2-space indent; why not 4? any way to change that? 125*5be3f65cSAndroid Build Coastguard Worker 126*5be3f65cSAndroid Build Coastguard WorkerTwo reasons - 127*5be3f65cSAndroid Build Coastguard Worker1. Many of our projects use a mixture of Kotlin and Java, and we found the back-and-forth in styles 128*5be3f65cSAndroid Build Coastguard Worker to be distracting. 129*5be3f65cSAndroid Build Coastguard Worker2. From a pragmatic standpoint, the formatting engine behind google-java-format uses more whitespace 130*5be3f65cSAndroid Build Coastguard Worker and newlines than other formatters. Using an indentation of 4 spaces quickly reaches the maximal 131*5be3f65cSAndroid Build Coastguard Worker column width. 132*5be3f65cSAndroid Build Coastguard Worker 133*5be3f65cSAndroid Build Coastguard WorkerHowever, we do offer an alternative style for projects that absolutely cannot make the move to 134*5be3f65cSAndroid Build Coastguard Worker`ktfmt` because of 2-space: the style `--kotlinlang-style` changes block indents to 4-space. 135*5be3f65cSAndroid Build Coastguard Worker 136*5be3f65cSAndroid Build Coastguard Worker## Developer's Guide 137*5be3f65cSAndroid Build Coastguard Worker 138*5be3f65cSAndroid Build Coastguard Worker### Setup 139*5be3f65cSAndroid Build Coastguard Worker 140*5be3f65cSAndroid Build Coastguard Worker* Open `pom.xml` in IntelliJ. Choose "Open as a Project" 141*5be3f65cSAndroid Build Coastguard Worker* The IntelliJ project will unfortunately be broken on import. To fix, 142*5be3f65cSAndroid Build Coastguard Worker * Turn off ErrorProne by removing the compiler parameters in IntelliJ at the bottom of 143*5be3f65cSAndroid Build Coastguard Worker `Settings → Build, Execution, Deployment → Compiler → Java Compiler` (see 144*5be3f65cSAndroid Build Coastguard Worker https://github.com/google/google-java-format/issues/417) 145*5be3f65cSAndroid Build Coastguard Worker 146*5be3f65cSAndroid Build Coastguard Worker### Development 147*5be3f65cSAndroid Build Coastguard Worker 148*5be3f65cSAndroid Build Coastguard Worker* Currently, we mainly develop by adding tests to `FormatterTest.kt`. 149*5be3f65cSAndroid Build Coastguard Worker 150*5be3f65cSAndroid Build Coastguard Worker### Building on the Command Line 151*5be3f65cSAndroid Build Coastguard Worker 152*5be3f65cSAndroid Build Coastguard Worker* Run `mvn install` 153*5be3f65cSAndroid Build Coastguard Worker* Run `java -jar core/target/ktfmt-<VERSION>-jar-with-dependencies.jar` 154*5be3f65cSAndroid Build Coastguard Worker 155*5be3f65cSAndroid Build Coastguard Worker### Releasing 156*5be3f65cSAndroid Build Coastguard Worker 157*5be3f65cSAndroid Build Coastguard WorkerSee [RELEASING.md](RELEASING.md). 158*5be3f65cSAndroid Build Coastguard Worker 159*5be3f65cSAndroid Build Coastguard Worker## License 160*5be3f65cSAndroid Build Coastguard Worker 161*5be3f65cSAndroid Build Coastguard WorkerApache License 2.0 162