ktlint icon indicating copy to clipboard operation
ktlint copied to clipboard

auto-indent bodies of multiline strings with .trimIndent()

Open mathjeff opened this issue 4 years ago • 8 comments

We had some code that looks like this:

    params.apply {
        task.description =
                """
                Creates a maven repository that includes just the libraries compiled in
                this project.
                Group: ${if (mavenGroup != "") mavenGroup else "All"}
                """.trimIndent()
        task.from(supportRepoOut)
    }

and we asked ktlint to format it and ktlint correctly pointed out that the quotation marks in the multiline string need to be deindented by 4 spaces. However, ktlint didn't deindent the body of the string too

Observed Behavior

ktlint formatted this code into this:

    params.apply {
        task.description =
            """
                Creates a maven repository that includes just the libraries compiled in
                this project.
                Group: ${if (mavenGroup != "") mavenGroup else "All"}
             """.trimIndent()
        task.from(supportRepoOut)
    }

Expected Behavior

We would like ktlint to notice the .trimIndent() that follows the multiline string, and then deindent the body of the string to match, to get:

    params.apply {
        task.description =
            """
            Creates a maven repository that includes just the libraries compiled in
            this project.
            Group: ${if (mavenGroup != "") mavenGroup else "All"}
             """.trimIndent()
        task.from(supportRepoOut)
    }

Your Environment

We're seeing this when upgrading from ktlint 0.36.0 to 0.39.0 The code in question: https://android-review.googlesource.com/c/platform/frameworks/support/+/1431946/1/buildSrc/src/main/kotlin/androidx/build/Release.kt#92

mathjeff avatar Sep 24 '20 18:09 mathjeff

Also note that this isn't a super high priority for us

mathjeff avatar Sep 24 '20 19:09 mathjeff

Quite specific case, probably Ktlint should while formatting move multiline string content as well when it uses .trimIndent()

@romtsn what do you think about this?

Tapchicoma avatar Sep 24 '20 20:09 Tapchicoma

I am not sure we'd like to introduce that, as the idea of trimIndent is about having a free-form text within the quotes... Although it might be convenient to re-indent the contents of trimIndent if, and only if both starting and ending quotes got re-indented by the same offset. Otherwise, it's generally unclear how to indent stuff within the quotes.

romtsn avatar Sep 25 '20 20:09 romtsn

Imho ktlint should handle correctly this specific edge-case - on formatting raw string if it ends with .trimIndent() and both """ are indented - content of the raw string should be indented on the same amount.

Tapchicoma avatar Sep 28 '20 07:09 Tapchicoma

I might be misremembering, but I think we recently reverted some code that would touch the contents of raw strings for the same reasoning that @romtsn said - it's a raw string so we assume we should leave the contents alone.

shashachu avatar Sep 28 '20 17:09 shashachu

In PR this is resolved. Indentation of multiline strings is improved.

paul-dingemans avatar Jan 09 '21 17:01 paul-dingemans

@paul-dingemans made some improvements to string templates, but we aren't going to touch the contents of them. Perhaps, this would make a good third-party rule or in a new-to-be-defined-upcoming-contrib-ruleset

romtsn avatar Dec 01 '21 14:12 romtsn

@paul-dingemans made some improvements to string templates, but we aren't going to touch the contents of them. Perhaps, this would make a good third-party rule or in a new-to-be-defined-upcoming-contrib-ruleset

Actually I have developed such a rule a while ago. I can however not release it until https://github.com/pinterest/ktlint/pull/1230 has been merged.

paul-dingemans avatar Dec 20 '21 19:12 paul-dingemans