ktlint
ktlint copied to clipboard
auto-indent bodies of multiline strings with .trimIndent()
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
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.
@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
@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.