Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mathjeff opened this issue Sep 24, 2020 · 8 comments · Fixed by #1877
Closed

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

mathjeff opened this issue Sep 24, 2020 · 8 comments · Fixed by #1877

Comments

@mathjeff
Copy link

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
Copy link
Author

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

@Tapchicoma
Copy link
Collaborator

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?

@romtsn
Copy link
Collaborator

romtsn commented Sep 25, 2020

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.

@Tapchicoma
Copy link
Collaborator

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.

@shashachu
Copy link
Contributor

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.

@paul-dingemans
Copy link
Collaborator

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

@romtsn
Copy link
Collaborator

romtsn commented Dec 1, 2021

@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
Copy link
Collaborator

@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 #1230 has been merged.

@paul-dingemans paul-dingemans added this to the 0.49.0 milestone Mar 16, 2023
paul-dingemans added a commit that referenced this issue Mar 21, 2023
…al` code style

This rule forces multiline string templates which are post-fixed with `.trimIndent()` to be formatted consistently. The opening and closing `"""` are placed on separate lines and the indentation of the content of the template is aligned with the `"""`.

Closes #925
paul-dingemans added a commit that referenced this issue Mar 22, 2023
…al` code style (#1877)

This rule forces multiline string templates which are post-fixed with `.trimIndent()` to be formatted consistently. The opening and closing `"""` are placed on separate lines and the indentation of the content of the template is aligned with the `"""`.

Closes #925
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants