• Home
Name Date Size #Lines LOC

..--

.github/workflows/03-May-2024-11092

core/03-May-2024-13,23110,764

docs/03-May-2024-377369

ktfmt_idea_plugin/03-May-2024-1,448964

online_formatter/03-May-2024-627418

website/03-May-2024-17,32116,059

.gitignoreD03-May-2024249 2619

.gitmodulesD03-May-2024130 43

Android.bpD03-May-20241.7 KiB5650

CODE_OF_CONDUCT.mdD03-May-20243.3 KiB7757

CONTRIBUTING.mdD03-May-20241.3 KiB3425

LICENSED03-May-202411.2 KiB207172

MANIFEST.mfD03-May-202462 32

METADATAD03-May-2024259 1412

MODULE_LICENSE_APACHE2D03-May-20240

OWNERSD03-May-202445 32

README.mdD03-May-20245.6 KiB12579

RELEASING.mdD03-May-2024705 119

generate_includes_file.pyD03-May-20242.9 KiB9659

ktfmt.pyD03-May-20244.2 KiB146105

pom.xmlD03-May-20242.8 KiB8978

prepare_upgrade.pyD03-May-20247.2 KiB258184

version.txtD03-May-20245 21

README.md

1[![](https://github.com/facebookincubator/ktfmt/workflows/build/badge.svg)](https://github.com/facebookincubator/ktfmt/actions?query=workflow%3Abuild)
2
3# ktfmt
4
5`ktfmt` is a program that pretty-prints (formats) Kotlin code, based on [google-java-format](https://github.com/google/google-java-format).
6
7**Note** that `ktfmt` still has some rough edges which we're constantly working on fixing.
8
9The minimum supported runtime version is JDK 11, released September 2018.
10
11## Demo
12
13|Before Formatting| Formatted by `ktfmt`|
14| ---- | ---- |
15| ![Original](docs/images/before.png) | ![ktfmt](docs/images/ktfmt.png) |
16
17For comparison, the same code formatted by [`ktlint`](https://github.com/pinterest/ktlint) and IntelliJ:
18
19| Formatted by `ktlint`|Formatted by IntelliJ|
20| ------ | --------|
21| ![ktlint](docs/images/ktlint.png) | ![IntelliJ](docs/images/intellij.png) |
22
23## Using the formatter
24
25### IntelliJ, Android Studio, and other JetBrains IDEs
26
27A
28[ktfmt IntelliJ plugin](https://plugins.jetbrains.com/plugin/14912-ktfmt)
29is available from the plugin repository. To install it, go to your IDE's
30settings and select the `Plugins` category. Click the `Marketplace` tab, search
31for the `ktfmt` plugin, and click the `Install` button.
32
33The plugin will be disabled by default. To enable it in the current project, go
34to `File→Settings...→ktfmt Settings` (or `IntelliJ
35IDEA→Preferences...→Editor→ktfmt Settings` on macOS) and
36check the `Enable ktfmt` checkbox. (A notification will be
37presented when you first open a project offering to do this for you.)
38
39To enable it by default in new projects, use `File→New Project Settings→Preferences for new Projects→Editor→ktfmt Settings`.
40
41When enabled, it will replace the normal `Reformat Code` action, which can be
42triggered from the `Code` menu or with the Ctrl-Alt-L (by default) keyboard
43shortcut.
44
45To configure IntelliJ to approximate ktfmt's formatting rules during code editing,
46you can edit your project's
47[`.editorconfig` file](https://www.jetbrains.com/help/idea/configuring-code-style.html#editorconfig)
48to include the Kotlin section from one of the files inside
49[`docs/editorconfig`](docs/editorconfig).
50Not all of ktfmt's rules can be represented as IntelliJ editor settings, so you will still
51need to run ktfmt. Alternately, that file can be used as a reference to manually change
52the project's code style settings.
53
54### from the command-line
55
56[Download the formatter](https://github.com/facebookincubator/ktfmt/releases)
57and run it with:
58
59```
60java -jar /path/to/ktfmt-<VERSION>-jar-with-dependencies.jar [--dropbox-style] [files...]
61```
62
63`--dropbox-style` makes `ktfmt` use a block indent of 4 spaces instead of 2. See below for details.
64
65***Note:*** *There is no configurability as to the formatter's algorithm for
66formatting (apart from `--dropbox-style`). This is a deliberate design decision to unify our code
67formatting on a single format.*
68
69### using Gradle
70
71A [Gradle plugin (ktfmt-gradle)](https://github.com/cortinico/ktfmt-gradle) is available on the Gradle Plugin Portal. To set it up, just follow the instructions in the [How-to-use section](https://github.com/cortinico/ktfmt-gradle#how-to-use-).
72
73Alternatively, you can use [Spotless](https://github.com/diffplug/spotless) with the [ktfmt Gradle plugin](https://github.com/diffplug/spotless/tree/main/plugin-gradle#ktfmt).
74
75### using Maven
76
77Consider using [Spotless](https://github.com/diffplug/spotless) with the [ktfmt Maven plugin](https://github.com/diffplug/spotless/tree/main/plugin-maven#ktfmt).
78
79## FAQ
80
81### `ktfmt` vs `ktlint` vs IntelliJ
82
83`ktfmt` uses google-java-format's underlying engine, and as such, many items on [google-java-format's FAQ](https://github.com/google/google-java-format/wiki/FAQ) apply to `ktfmt` as well.
84
85In particular,
861. `ktfmt` ignores most existing formatting. It respects existing newlines in some places, but in general, its output is deterministic and is independent of the input code.
872. `ktfmt` exposes no configuration options that govern formatting behavior. See https://github.com/google/google-java-format/wiki/FAQ#i-just-need-to-configure-it-a-bit-differently-how for the rationale.
88
89These two properties make `ktfmt` a good fit in large Kotlin code bases, where consistency is very important.
90
91We created `ktfmt` because `ktlint` and IntelliJ sometime fail to produce nice-looking code that fits in 100 columns, as can be seen in the [Demo](README.md#Demo) section.
92
93### `ktfmt` uses a 2-space indent; why not 4? any way to change that?
94
95Two reasons -
961. Many of our projects use a mixture of Kotlin and Java, and we found the back-and-forth in styles to be distracting.
972. From a pragmatic standpoint, the formatting engine behind google-java-format uses more whitespace and newlines than other formatters. Using an indentation of 4 spaces quickly reaches the maximal column width.
98
99However, we do offer an escape-hatch for projects that absolutely cannot make the move to `ktfmt` because of 2-space: the `--dropbox-style` flag changes block indents to 4-space.
100
101## Developer's Guide
102
103### Setup
104
105* Open `pom.xml` in IntelliJ. Choose "Open as a Project"
106* The IntelliJ project will unfortunately be broken on import. To fix,
107    * Turn off ErrorProne by removing the compiler parameters in IntelliJ at the bottom of "Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler" (see https://github.com/google/google-java-format/issues/417)
108
109### Development
110
111* Currently, we mainly develop by adding tests to `FormatterTest.kt`.
112
113### Building on the Command Line
114
115* Run `mvn install`
116* Run `java -jar core/target/ktfmt-<VERSION>-jar-with-dependencies.jar`
117
118### Releasing
119
120See [RELEASING.md](RELEASING.md).
121
122## License
123
124Apache License 2.0
125