swiftlang / swift-format

Formatting technology for Swift source code
Apache License 2.0
2.52k stars 229 forks source link

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

Open dduan opened 3 months ago

dduan commented 3 months ago

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 commented 3 months ago

rdar://132599415

allevato commented 3 months ago

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: