Fork me on GitHub

About


ktfmt is a program that pretty-prints (formats) Kotlin code.

It always produces the same result, regardless of how the code looks initially, freeing developers to focus on essence.

It is by design non-customizable in order to promote consistency.


Usage


A ktfmt plugin that augments the Reformat Code action (⌥⌘L) is available from JetBrains Marketplace.

Install

Spotless (details):

# build.gradle.kts
plugins { id("com.diffplug.spotless") }

// version and style are optional
spotless { kotlin { ktfmt('0.47').kotlinlangStyle() } }

or,

ktfmt-gradle (details):

# build.gradle.kts
plugins { id("com.ncorti.ktfmt.gradle") }

ktfmt { kotlinLangStyle() }

Spotless (details):

# pom.xml
<configuration>
  <kotlin>
    <ktfmt>
      <version>0.47</version>
    </ktfmt>
  </kotlin>
</configuration>

Download the jar from Maven Central and invoke it using java:

$ wget https://repo1.maven.org/maven2/com/facebook/ktfmt/0.47/ktfmt-0.47-jar-with-dependencies.jar
$ java -jar ktfmt-0.47-jar-with-dependencies.jar [--kotlinlang-style] [files...]

Try It Online

Running v0.47