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

Fix indentation of the closing quotes of a multiline string literal #1262

Merged
merged 7 commits into from
Jan 22, 2022

Conversation

paul-dingemans
Copy link
Collaborator

Fix indentation below:

val bar = """
    some text
         some more text
"""

to

val bar = """
    some text
         some more text
    """

However, above might be a debatable choice. Therefore I left following TODO in a test:

        // TODO: What is the proper way to place the opening and closing quotes of a multi line string value? Note that
        //  the actual content of the multi line string is never been changed as required by ktlint maintainers
        //     val foo =
        //        """
        //        line1
        //        line2
        //        """
        //  or
        //     val foo = """
        //        line1
        //        line2
        //        """
        //  or
        //     val foo = """
        //     line1
        //     line2
        //     """
        //  or
        //     val foo = """
        //        line1
        //        line2
        //     """
        // First option would be most in line with function parameter. Second option is also acceptable. Third option
        // (previous behavior) is not logical as there is no continuation kind of indent visible. Last option is not in
        // sync with function parameter.

I would like your opinion about it, so that I can finalize this PR.

This changes also includes the fix for #1127 as it has a comparable problem with the closing quotes.

Closes pinterest#1127

Arranging the opening and closing quotes of multiline string literals should not be
limited to strings followed by the ".trimIndent()" clause.

In case the opening quotes have been placed at the start of the line, then the
closing quotes should also be placed at the start of the line.

In the reporter tests, the tab characters have replaced with an explicit placeholder
"{TAB}" to make them visible.
@romtsn
Copy link
Collaborator

romtsn commented Oct 31, 2021

Hm, I'm a bit confused, the description doesn't look relevant to the PR itself, or does it? Also, what part of the diff actually fixes the closing quote? I only see changes relevant to the opening quote if it's placed on a newline so far...

@@ -345,7 +345,7 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast {
val treeParent = n.treeParent
if (treeParent.elementType == STRING_TEMPLATE) {
val treeParentPsi = treeParent.psi as KtStringTemplateExpression
if (treeParentPsi.isMultiLine() && treeParentPsi.isFollowedByTrimIndent() && n.treePrev.text.isNotBlank()) {
if (treeParentPsi.isMultiLine() && n.treePrev.text.isNotBlank()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we remove the check for trimIndent?

@paul-dingemans
Copy link
Collaborator Author

Hm, I'm a bit confused, the description doesn't look relevant to the PR itself, or does it? Also, what part of the diff actually fixes the closing quote? I only see changes relevant to the opening quote if it's placed on a newline so far...

Sorry about the confusion. I should have stated more clearly that I need input before being able to solve this entirely. I mentioned a debatable choice in the initial description. In one of the other discussion about indentation, you mentioned that you like to align the closing quotes with the opening quotes. This PR was aiming to solve that in the end. Currently the following is allowed:

val foo = """
     line 1
     line 2
     """

How should the closing quotes be aligned in this case? Aligning would mean, the following.

val foo = """
     line 1
     line 2
               """

I can not see this as an acceptable solution.

Another solution, would be to wrap the opening quotes:

val foo = 
     """
     line 1
     line 2
     """

This is my preferred solution, but it can be considered as breaking with previous ktlint version.

The removal of the trimIndent function is possible since we decided that indentation in multiline raw string literals is never changed. Therefore it is not relevant whether this literal is followed by trimIndent, trimMargin, anything else or nothing at all. It has no relation with the goal of the PR and should have been requested in a separate PR.

If you answer my previous question about the placement of the closing quotes, then I will split this PR in separate PR's with a single change in each.

Paul Dingemans added 4 commits December 5, 2021 13:48
# Conflicts:
#	ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRuleTest.kt
…otes

# Conflicts:
#	ktlint-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/reporter/baseline/BaselineReporterTest.kt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants