ktfmt

repository·main·Indexed 22 days ago

https://github.com/facebook/ktfmt

A deterministic Kotlin code formatter based on google-java-format. It provides a highly consistent formatting style to minimize code style debates in large projects. It supports multiple styles including --google-style and --kotlinlang-style, and can be used via CLI, IntelliJ/Android Studio plugins, Gradle, Maven, or as an AWS Lambda online formatter. It also supports limited .editorconfig overrides for properties like max_line_length and indent_size.

Tokens
4.6K
Snippets
14
Records
32
Agent score
75%

What's inside ktfmt

  1. How ktfmt handles lambda line breaks

    main

    By default, ktfmt respects user-authored line breaks inside lambda bodies. This preserves the semantic structure of DSLs (like Jetpack Compose or Gradle scripts) without forcing a specific layout.

    • A multi-line lambda in the source remains multi-line.
    • A single-line lambda in the source remains single-line.
    // Multi-line in source is preserved
    App {
      SelectableCard {
        Button { Text("Click me") }
      }
    }
    
    // Single-line in source is also preserved
    dependencies { implementation(libs.androidx.activity) }
    val state = remember { mutableStateOf(false) }
  2. Comparison: ktfmt vs ktlint vs IntelliJ

    main

    Unlike ktlint or standard IntelliJ formatting, ktfmt is based on google-java-format and follows these principles:

    1. Deterministic Output: It ignores most existing formatting. The output is independent of the input code, ensuring a single consistent style across large codebases.
    2. Minimal Configuration: It does not expose many configuration options to prevent 'bikeshedding' (endless debates over minor style differences). The primary configuration is the choice between --google-style and --kotlinlang-style.
    3. Column Width: ktfmt is designed to handle code that fits well within 100 columns, addressing issues where other formatters might struggle with line wrapping.
  3. Configure IDE versions for the ktfmt plugin

    main

    The plugin's compatibility with different IntelliJ/Android Studio versions is defined in ktfmt/gradle/libs.versions.toml using the following keys:

    • intellijPlatform.pluginConfiguration.ideaVersion.sinceBuild: Sets the minimum supported version. The source code must be compatible with this build and must not reference classes introduced in later builds.
    • intellijPlatform.pluginConfiguration.ideaVersion.untilBuild: Sets the maximum supported version.
  4. Test the plugin with a local IDE installation

    main

    If you want to test the plugin against a specific local installation of IntelliJ or Android Studio instead of a Gradle-downloaded version:

    1. In build.gradle.kts, set intellij.alternativeIdePath to the path of your local IDE installation.
    2. Ensure the plugin is enabled in that IDE. You can find it by pressing Cmd-shift-A (macOS) and typing 'ktfmt', then looking for Preferences.
  5. Build the ktfmt demo website for deployment

    main

    To build the website for deployment, you need to set the KTFMT_TMP_DIR and KTFMT_WEBSITE_OUTPUT_DIR environment variables before running the gulp build command. The following steps use a temporary directory for the build process.

    npm install .
    KTFMT_TMP_DIR=$(mktemp -d)
    KTFMT_WEBSITE_OUTPUT_DIR=$KTFMT_TMP_DIR gulp build-website
    
    # Clean up:
    rm -rf "$KTFMT_TMP_DIR"
  6. Install ktfmt plugin for IntelliJ and Android Studio

    main

    To use ktfmt directly within JetBrains IDEs:

    1. Open IDE Settings (or Preferences on macOS).
    2. Select Plugins and go to the Marketplace tab.
    3. Search for and install the ktfmt plugin.
    4. Enable it: The plugin is disabled by default. Go to File → Settings... → ktfmt Settings (or IntelliJ IDEA → Preferences → Editor → ktfmt Settings on macOS) and check Enable ktfmt.

    Note: Once enabled, it replaces the standard Reformat Code action (Ctrl-Alt-L).

    Sharing settings: To ensure team consistency, commit the .idea/ktfmt.xml file to your version control system.