swiftlang / swift-format

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

swift-format breaks compilation by wrongly reformatting nested multi-line string blocks #864

Open ktoso opened 4 hours ago

ktoso commented 4 hours ago

swift-format breaks compilation when reformatting code with """ includes other """ multiline strings, like these:


    let initSyntax: DeclSyntax = """
      public init(_ enumValue: \(raw: name), environment: JNIEnvironment? = nil) {
        let _environment = if let environment {
          environment
        } else {
          try! JavaVirtualMachine.shared().environment()
        }
        let classObj = try! JavaClass<Self>(in: _environment)
        switch enumValue {
      \(raw: enumFields.map {
      return """
          case .\($0.getName()):
            if let \($0.getName()) = classObj.\($0.getName()) {
              self = \($0.getName())
            } else {
              fatalError("Enum value \($0.getName()) was unexpectedly nil, please re-run Java2Swift on the most updated Java class") 
            }
      """
    }.joined(separator: "\n"))
        }
      }
      """ <<<< this gets formatted wrong

results in:

/__w/swift-java/swift-java/Sources/Java2SwiftLib/JavaTranslator.swift:586:5: error: insufficient indentation of line in multi-line string literal
584 |             }
585 |       """
586 |     }.joined(separator: "\n"))
    |     |- error: insufficient indentation of line in multi-line string literal
    |     `- note: change indentation of this line to match closing delimiter
587 |         }
588 |       }
589 |       """
    |       `- note: should match space here
590 | 

Like here: https://github.com/swiftlang/swift-java/actions/runs/11511458930/job/32044929995?pr=109

Version:

ahoppen commented 4 hours ago

Synced to Apple’s issue tracker as rdar://138608242