Skip to content

Conversation

@ahoppen
Copy link
Member

@ahoppen ahoppen commented Jul 29, 2023

The underlying issues of the malformatted multi-line string literal are that

  1. We are setting an anchorPoint for empty lines. Anchor points are meant to be starting points at which the user didn’t provide any manual indentation relative to which we should format the rest of the tree. But for empty lines it’s not like the user didn’t provide any indentation. There simply wasn’t anything to indent.
  2. leadingTrivia.trimmingTrailingWhitespaceBeforeNewline was not considering whether the token text itself contained a newline in isBeforeNewline.

Now, to detect if the token is followed by a newline, we should check if its text is empty, which is the case for empty string literals. Unfortunately, this is also the case for missing identifiers, which also have an empty text but whose empty text will be replace by a placeholder by PresentMaker. To distinguish between the two, I merged PresentMaker and MissingNodesBasicFormatter so that the BasicFormat also performs the text replacement to placeholders.

Fixes #1959

@ahoppen ahoppen requested a review from bnbarham July 29, 2023 16:35
@ahoppen
Copy link
Member Author

ahoppen commented Jul 29, 2023

@swift-ci Please test

)
}

func testMultilineStringLiteralWithBlackLines() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func testMultilineStringLiteralWithBlackLines() {
func testMultilineStringLiteralWithBlankLines() {

I made a typo in the test method name.

Copy link
Member Author

Choose a reason for hiding this comment

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

Haha, thanks for pointing it out.

@ahoppen ahoppen force-pushed the ahoppen/present-making-formatter branch from baafe5c to 441b025 Compare July 29, 2023 18:03
assertFormatted(
source: #"""
assertionFailure("""
First line
Copy link
Contributor

Choose a reason for hiding this comment

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

The blank line is gone. The code is the same as the test code above testMultilineStringLiteral().

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, copying code from GitHub apparently drops empty lines 🤯 Thanks for catching it. I updated the test case

@kishikawakatsumi
Copy link
Contributor

In the following test case (no spaces in the empty lines), an indent is inserted in the first blank line. This is not identical to the original string.

assertFormatted(
  source: #"""
    assertionFailure("""


      First line
      Second line
      """)
    """#,
  expected: #"""
    assertionFailure("""


      First line
      Second line
      """)
    """#
)

Actual Result:

assertionFailure("""
␣␣␣␣

  First line
  Second line
  """)

@ahoppen ahoppen force-pushed the ahoppen/present-making-formatter branch from 441b025 to 530547d Compare July 31, 2023 21:56
@ahoppen
Copy link
Member Author

ahoppen commented Jul 31, 2023

In the following test case (no spaces in the empty lines), an indent is inserted in the first blank line. This is not identical to the original string.

Fixed

@ahoppen ahoppen force-pushed the ahoppen/present-making-formatter branch from 530547d to 3961b9c Compare July 31, 2023 21:58
@ahoppen
Copy link
Member Author

ahoppen commented Jul 31, 2023

@swift-ci Please test

@kishikawakatsumi
Copy link
Contributor

Running the following test case with the latest commit 3961b9c produces incorrectly indented results.

assertFormatted(
  source: #"""
    assertionFailure("""

      \("""
        First Line
      """)
    """)
    """#,
  expected: ...
)

Actual result:

assertionFailure("""

      \("""
    First Line
  """)
    """)

@ahoppen
Copy link
Member Author

ahoppen commented Aug 1, 2023

@swift-ci Please test

…tting bug in string literals

The underlying issues of the malformatted multi-line string literal are that

1. We are setting an `anchorPoint` for empty lines. Anchor points are meant to be starting points at which the user didn’t provide any manual indentation relative to which we should format the rest of the tree. But for empty lines it’s not like the user didn’t provide any indentation. There simply wasn’t anything to indent.
2. `leadingTrivia.trimmingTrailingWhitespaceBeforeNewline` was not considering whether the token text itself contained a newline in `isBeforeNewline`.

Now, to detect if the token is followed by a newline, we should check if its text is empty, which is the case for empty string literals. Unfortunately, this is also the case for missing identifiers, which also have an empty text but whose empty text will be replace by a placeholder by `PresentMaker`. To distinguish between the two, I merged `PresentMaker` and `MissingNodesBasicFormatter` so that the `BasicFormat` also performs the text replacement to placeholders.

Fixes swiftlang#1959

Co-Authored-By: kishikawa katsumi <[email protected]>
@ahoppen ahoppen force-pushed the ahoppen/present-making-formatter branch from 3961b9c to 653ae4b Compare August 1, 2023 18:20
@ahoppen
Copy link
Member Author

ahoppen commented Aug 1, 2023

Fixed as well. I can’t think of any more ways to break formatting of multi-line string literals but if you do find more, please let me know @kishikawakatsumi. And thanks for the thorough testing.

@ahoppen
Copy link
Member Author

ahoppen commented Aug 1, 2023

@swift-ci Please test

@ahoppen ahoppen force-pushed the ahoppen/present-making-formatter branch from 653ae4b to 0387483 Compare August 1, 2023 21:01
@ahoppen
Copy link
Member Author

ahoppen commented Aug 1, 2023

@swift-ci Please test

1 similar comment
@ahoppen
Copy link
Member Author

ahoppen commented Aug 1, 2023

@swift-ci Please test

@ahoppen ahoppen merged commit 91d33db into swiftlang:main Aug 2, 2023
@ahoppen ahoppen deleted the ahoppen/present-making-formatter branch August 4, 2025 10:49
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.

BasicFormat outputs incomplete indentation when formatting multiline string literals containing blank lines

4 participants