Skip to content

Commit fc2e999

Browse files
committed
Fix missing newline for opening quote for multilines strings
1 parent 3192646 commit fc2e999

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,12 @@ open class BasicFormat: SyntaxRewriter {
393393
anchorPoints[token] = currentIndentationLevel
394394
}
395395

396+
if requiresTrailingNewline(token) {
397+
if !trailingTrivia.endsWithNewline && nextToken?.leadingTrivia.startsWithNewline == false {
398+
trailingTrivia += .newline
399+
}
400+
}
401+
396402
// Add a trailing space to the token unless
397403
// - it already ends with a whitespace or
398404
// - the next token will start starts with a newline after the rewrite

Tests/SwiftSyntaxBuilderTest/StringLiteralExprSyntaxTests.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,44 @@ final class StringLiteralExprSyntaxTests: XCTestCase {
334334
"""#
335335
)
336336
}
337+
338+
func testMultiStringOpeningQuote() {
339+
assertBuildResult(
340+
StringLiteralExprSyntax(openQuote: .multilineStringQuoteToken(), content: "a", closeQuote: .multilineStringQuoteToken()),
341+
#"""
342+
"""
343+
a
344+
"""
345+
"""#
346+
)
347+
348+
assertBuildResult(
349+
StringLiteralExprSyntax(
350+
openQuote: .multilineStringQuoteToken(),
351+
segments: StringLiteralSegmentsSyntax {
352+
.expressionSegment(
353+
ExpressionSegmentSyntax(
354+
expressions: TupleExprElementListSyntax {
355+
TupleExprElementSyntax(
356+
expression: StringLiteralExprSyntax(
357+
openQuote: .multilineStringQuoteToken(),
358+
content: "a",
359+
closeQuote: .multilineStringQuoteToken()
360+
)
361+
)
362+
}
363+
)
364+
)
365+
},
366+
closeQuote: .multilineStringQuoteToken()
367+
),
368+
#"""
369+
"""
370+
\("""
371+
a
372+
""")
373+
"""
374+
"""#
375+
)
376+
}
337377
}

0 commit comments

Comments
 (0)