facebook / ktfmt

A program that reformats Kotlin source code to comply with the common community standard for Kotlin code conventions.
https://facebook.github.io/ktfmt/
Apache License 2.0
932 stars 78 forks source link

Adding a space before a comment makes method chaining appear as multi-line in Gradle #517

Open robifr opened 2 months ago

robifr commented 2 months ago

Adding a comment with a single space (the space is important) at the end of a method chain in a Gradle build script will cause the code to be incorrectly formatted, making it appear as multi-line or folded instead of staying on one line.

Expected behavior: Place this code at the very end of build.gradle.kts. Even an empty file containing only this code can reproduce the bug.

tasks.named("preBuild").dependsOn("test")

//

\ Actual behavior: Inside build.gradle.kts

tasks
    .named("preBuild")
    .dependsOn(
        "test")

// Removing this comment will make the formatting work as expected.

\ I tested this on an Android project using Spotless with Gradle.

My root build.gradle.kts

allprojects {
  apply<SpotlessPlugin>()
  extensions.configure<SpotlessExtension> {
    kotlinGradle {
      target("*.gradle.kts")
      ktfmt()
    }  
    ...
}
$ ./gradlew spotlessApply