Skip to content

Commit c5aea9e

Browse files
committed
Fix multiline string indentation
1 parent 13ddedb commit c5aea9e

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

Sources/SwiftBasicFormat/Trivia+Indented.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
import SwiftSyntax
1414

1515
extension Trivia {
16-
func indented(indentation: TriviaPiece) -> Trivia {
16+
func indented(indentation: TriviaPiece, hasContentNewline: Bool = false) -> Trivia {
1717
var indentedPieces: [TriviaPiece] = []
1818
for (index, piece) in self.enumerated() {
1919
let nextPiece = index < pieces.count - 1 ? pieces[index + 1] : nil
2020
indentedPieces.append(piece)
21-
if piece.isNewline {
21+
if piece.isNewline || hasContentNewline && index == 0 {
2222
switch (nextPiece, indentation) {
2323
case (.spaces(let nextPieceSpaces)?, .spaces(let indentationSpaces)):
2424
if nextPieceSpaces < indentationSpaces {

Sources/SwiftBasicFormat/generated/BasicFormat.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ open class BasicFormat: SyntaxRewriter {
4444
indentationLevel -= 1
4545
}
4646
}
47-
47+
4848
open override func visit(_ node: TokenSyntax) -> TokenSyntax {
4949
var leadingTrivia = node.leadingTrivia
5050
var trailingTrivia = node.trailingTrivia
@@ -57,8 +57,12 @@ open class BasicFormat: SyntaxRewriter {
5757
if let keyPath = getKeyPath(Syntax(node)), requiresLeadingNewline(keyPath), !(leadingTrivia.first?.isNewline ?? false) {
5858
leadingTrivia = .newline + leadingTrivia
5959
}
60-
leadingTrivia = leadingTrivia.indented(indentation: indentation)
61-
trailingTrivia = trailingTrivia.indented(indentation: indentation)
60+
61+
leadingTrivia = leadingTrivia.indented(indentation: indentation,
62+
hasContentNewline: hasTrailingNewlineInContent(lastRewrittenToken)
63+
)
64+
trailingTrivia = trailingTrivia.indented(indentation: indentation)
65+
6266
let rewritten = TokenSyntax(
6367
node.tokenKind,
6468
leadingTrivia: leadingTrivia,
@@ -70,6 +74,15 @@ open class BasicFormat: SyntaxRewriter {
7074
putNextTokenOnNewLine = false
7175
return rewritten
7276
}
77+
78+
open func hasTrailingNewlineInContent(_ node: TokenSyntax?) -> Bool {
79+
switch node?.tokenKind {
80+
case .stringSegment(let text):
81+
return text.hasSuffix("\n")
82+
default:
83+
return false
84+
}
85+
}
7386

7487
open func shouldIndent(_ keyPath: AnyKeyPath) -> Bool {
7588
switch keyPath {
@@ -176,7 +189,7 @@ open class BasicFormat: SyntaxRewriter {
176189
return false
177190
}
178191
}
179-
192+
180193
/// If this returns a value that is not `nil`, it overrides the default
181194
/// trailing space behavior of a token.
182195
open func requiresTrailingSpace(_ keyPath: AnyKeyPath) -> Bool? {

Tests/SwiftSyntaxBuilderTest/StringLiteralTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ final class StringLiteralTests: XCTestCase {
249249
func testMultiStringLiteralInIfExpr() {
250250
let buildable = ExprSyntax(
251251
#"""
252-
if nonNilErrors.count == errors.count {
252+
if true {
253253
assertionFailure("""
254254
Error validating child at index
255255
Node did not satisfy any node choice requirement.
@@ -262,7 +262,7 @@ final class StringLiteralTests: XCTestCase {
262262
AssertBuildResult(
263263
buildable,
264264
#"""
265-
if nonNilErrors.count == errors.count {
265+
if true {
266266
assertionFailure("""
267267
Error validating child at index
268268
Node did not satisfy any node choice requirement.
@@ -287,7 +287,7 @@ final class StringLiteralTests: XCTestCase {
287287
}
288288
"""#
289289
)
290-
290+
291291
AssertBuildResult(
292292
buildable,
293293
#"""

0 commit comments

Comments
 (0)