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

Re-indenting multiline string literal with multiline tertiary expression fails #783

Open
dduan opened this issue Jul 26, 2024 · 2 comments

Comments

@dduan
Copy link
Contributor

dduan commented Jul 26, 2024

The following test fails. It involves a multi-line string literal that has a tertiary expression. The string literal needs re-indentation, but parts of the tertiary expression's indentation in the result has wrong indentation level.

  func testMultilineRawStringWithMultilineTertiaryThatNeedsReindentation() {
    let input =
    #"""
    f(
      """
      \(x ? "" :
      y)
      """)
    """#

    let expected =
    #"""
    f(
        """
        \(x ? "" :
        y)
        """)

    """#

    var configuration = Configuration.forTesting
    configuration.indentation = .spaces(4)
    assertPrettyPrintEqual(input: input, expected: expected, linelength: 80, configuration: configuration)
  }

Failure diagnostics:

failed - Pretty-printed result was not what was expected - Actual output (+) differed from expected output (-):
 f(
     """
     \(x ? "" :
-    y)
+  y)
     """)

Note that the formatted result is syntactically invalid.

@dduan
Copy link
Contributor Author

dduan commented Jul 26, 2024

rdar://132599415

@allevato
Copy link
Member

The dirty secret right now is that we don't do any formatting of expressions inside string interpolations. So what you're seeing is us just taking the verbatim text inside the interpolation (newlines and leading space included) and printing it back out, which doesn't work if the indentation of the outer string changes.

There were a couple reasons for that decision:

  • If we're formatting an interpolation inside a single-line string, we don't want to introduce any line breaks. We didn't have controls to disable those originally, but now we do (for #if conditions).
  • IIRC, the older representation of multiline strings was different and contained embedded newlines, making it harder to reason about. Now that there's a string segment per line and each line will be preceded by indentation that the pretty printer is aware of, it should be easier to have interpolation segments respect that.

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

No branches or pull requests

2 participants