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

Created on 24 Sep 2020  路  6Comments  路  Source: pinterest/ktlint

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

bug question

Most helpful comment

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.

All 6 comments

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

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?

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.

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.

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.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

artnc picture artnc  路  3Comments

pkubowicz picture pkubowicz  路  4Comments

filipedelimabrito picture filipedelimabrito  路  3Comments

ZakTaccardi picture ZakTaccardi  路  4Comments

DanteAndroid picture DanteAndroid  路  3Comments