Skip to content

Commit e49abe0

Browse files
committed
Fix wrong foratting of multiline string literal
1 parent 3c73cc8 commit e49abe0

10 files changed

+37
-21
lines changed

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,13 @@ open class BasicFormat: SyntaxRewriter {
171171
}
172172
}
173173

174-
return false
174+
let syntaxToken: Syntax = Syntax(token)
175+
switch syntaxToken.keyPathInParent {
176+
case \StringLiteralExprSyntax.closeQuote:
177+
return token.tokenKind == .multilineStringQuote
178+
default:
179+
return false
180+
}
175181
}
176182

177183
open func requiresWhitespace(between first: TokenSyntax?, and second: TokenSyntax?) -> Bool {
@@ -249,9 +255,10 @@ open class BasicFormat: SyntaxRewriter {
249255
case \ExpressionSegmentSyntax.backslash,
250256
\ExpressionSegmentSyntax.rightParen,
251257
\DeclNameArgumentSyntax.colon,
252-
\StringLiteralExprSyntax.openQuote,
253258
\RegexLiteralExprSyntax.openSlash:
254259
return false
260+
case \StringLiteralExprSyntax.openQuote:
261+
return first?.tokenKind == .multilineStringQuote
255262
default:
256263
break
257264
}

Sources/SwiftParserDiagnostics/DiagnosticExtensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ extension FixIt.MultiNodeChange {
167167
}
168168
}
169169

170-
if let previousToken = node.previousToken(viewMode: .all),
170+
if let previousToken = node.previousToken(viewMode: .fixedUp),
171171
previousToken.presence == .present,
172172
let firstToken = node.firstToken(viewMode: .all),
173173
previousToken.trailingTrivia.allSatisfy({ $0.isWhitespace }),

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,12 +1174,10 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
11741174
}
11751175
if case .stringSegment(let segment) = node.segments.last {
11761176
if let invalidContent = segment.unexpectedBeforeContent?.onlyToken(where: { $0.trailingTrivia.contains(where: { $0.isBackslash }) }) {
1177-
let leadingTrivia = segment.content.leadingTrivia
1178-
let trailingTrivia = segment.content.trailingTrivia
11791177
let fixIt = FixIt(
11801178
message: .removeBackslash,
11811179
changes: [
1182-
.makePresent(segment.content, leadingTrivia: leadingTrivia, trailingTrivia: trailingTrivia),
1180+
.makePresent(segment.content),
11831181
.makeMissing(invalidContent, transferTrivia: false),
11841182
]
11851183
)

Tests/SwiftParserTest/ExpressionTests.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,8 @@ final class ExpressionTests: XCTestCase {
577577
)
578578
],
579579
fixedSource: ##"""
580-
"""""""
580+
""""
581+
"""
581582
"""##
582583
)
583584

@@ -589,7 +590,8 @@ final class ExpressionTests: XCTestCase {
589590
DiagnosticSpec(message: #"expected '"""' to end string literal"#, fixIts: [#"insert '"""'"#])
590591
],
591592
fixedSource: ##"""
592-
""""""""
593+
"""""
594+
"""
593595
"""##
594596
)
595597

@@ -626,7 +628,8 @@ final class ExpressionTests: XCTestCase {
626628
DiagnosticSpec(message: ##"expected '"""#' to end string literal"##, fixIts: [##"insert '"""#'"##])
627629
],
628630
fixedSource: ##"""
629-
#""""""#
631+
#"""
632+
"""#
630633
"""##
631634
)
632635

@@ -638,7 +641,8 @@ final class ExpressionTests: XCTestCase {
638641
DiagnosticSpec(message: ##"expected '"""#' to end string literal"##, fixIts: [##"insert '"""#'"##])
639642
],
640643
fixedSource: ##"""
641-
#"""a"""#
644+
#"""a
645+
"""#
642646
"""##
643647
)
644648

Tests/SwiftParserTest/translated/AvailabilityQueryTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ final class AvailabilityQueryTests: XCTestCase {
100100
DiagnosticSpec(message: "expected ',' joining parts of a multi-clause condition", fixIts: ["replace '&&' with ','"])
101101
],
102102
fixedSource: """
103-
if #available(OSX 10.51, *) , #available(OSX 10.52, *) {
103+
if #available(OSX 10.51, *), #available(OSX 10.52, *) {
104104
}
105105
"""
106106
)
@@ -433,7 +433,7 @@ final class AvailabilityQueryTests: XCTestCase {
433433
DiagnosticSpec(message: "expected ',' joining platforms in availability condition", fixIts: ["replace '||' with ','"])
434434
],
435435
fixedSource: """
436-
if #available(OSX 10.51 , iOS 8.0) {
436+
if #available(OSX 10.51, iOS 8.0) {
437437
}
438438
"""
439439
)

Tests/SwiftParserTest/translated/AvailabilityQueryUnavailabilityTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ final class AvailabilityQueryUnavailabilityTests: XCTestCase {
103103
)
104104
],
105105
fixedSource: """
106-
if #unavailable(OSX 10.51) , #unavailable(OSX 10.52) {
106+
if #unavailable(OSX 10.51), #unavailable(OSX 10.52) {
107107
}
108108
"""
109109
)
@@ -446,7 +446,7 @@ final class AvailabilityQueryUnavailabilityTests: XCTestCase {
446446
)
447447
],
448448
fixedSource: """
449-
if #unavailable(OSX 10.51 , iOS 8.0) {
449+
if #unavailable(OSX 10.51, iOS 8.0) {
450450
}
451451
"""
452452
)

Tests/SwiftParserTest/translated/MultilineErrorsTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,8 @@ final class MultilineErrorsTests: XCTestCase {
603603
],
604604
fixedSource: ##"""
605605
_ = """
606-
foo\"""
606+
foo\
607+
"""
607608
"""##
608609
)
609610
}

Tests/SwiftParserTest/translated/MultilinePoundDiagnosticArgRdar41154797Tests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ final class MultilinePoundDiagnosticArgRdar41154797Tests: XCTestCase {
2525
DiagnosticSpec(message: "expected ')' to end macro expansion", fixIts: ["insert ')'"]),
2626
],
2727
fixedSource: ##"""
28-
#error("""""")
28+
#error("""
29+
""")
2930
"""##
3031
)
3132
}

Tests/SwiftParserTest/translated/StringLiteralEofTests.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ final class StringLiteralEofTests: XCTestCase {
106106
fixedSource: #"""
107107
// NOTE: DO NOT add a newline at EOF.
108108
_ = """
109-
foo"""
109+
foo
110+
"""
110111
"""#
111112
)
112113
}
@@ -126,7 +127,8 @@ final class StringLiteralEofTests: XCTestCase {
126127
fixedSource: ##"""
127128
_ = """
128129
foo
129-
\(<#expression#>)"""
130+
\(<#expression#>)
131+
"""
130132
"""##
131133
// FIXME: The closing delimiter should be put on the new line
132134
)
@@ -148,7 +150,8 @@ final class StringLiteralEofTests: XCTestCase {
148150
fixedSource: ##"""
149151
_ = """
150152
foo
151-
\("bar")"""
153+
\("bar")
154+
"""
152155
"""##
153156
)
154157
}
@@ -169,7 +172,8 @@ final class StringLiteralEofTests: XCTestCase {
169172
fixedSource: ##"""
170173
_ = """
171174
\("bar"
172-
baz)"""
175+
baz)
176+
"""
173177
"""##
174178
)
175179
}

Tests/SwiftParserTest/translated/UnclosedStringInterpolationTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ final class UnclosedStringInterpolationTests: XCTestCase {
126126
fixedSource: ##"""
127127
_ = """
128128
\(
129-
"""""")"""
129+
""" """)
130+
"""
130131
"""##
131132
)
132133
}

0 commit comments

Comments
 (0)