Skip to content

Commit 43f7095

Browse files
authored
Merge pull request #1033 from ahoppen/ahoppen/separate-non-punctuation
Make sure that any keyword or identifier inserted by a Fix-It is separated from the previous one by trivia
2 parents 734f534 + 271a54b commit 43f7095

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Sources/SwiftParserDiagnostics/DiagnosticExtensions.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import SwiftDiagnostics
14+
import SwiftBasicFormat
1415
import SwiftSyntax
1516

1617
extension FixIt {
@@ -91,6 +92,18 @@ extension FixIt.Changes {
9192
),
9293
.replaceLeadingTrivia(token: nextToken, newTrivia: [])
9394
]
95+
} else if let firstToken = node.firstToken(viewMode: .all),
96+
let previousToken = node.previousToken(viewMode: .fixedUp),
97+
!firstToken.tokenKind.isPunctuation,
98+
!previousToken.tokenKind.isPunctuation,
99+
firstToken.leadingTrivia.isEmpty,
100+
(previousToken.presence == .missing ? BasicFormat().visit(previousToken).trailingTrivia : previousToken.trailingTrivia).isEmpty {
101+
/// If neither this nor the previous token are punctionation make sure they
102+
/// are separated by a space.
103+
return [.replace(
104+
oldNode: Syntax(node),
105+
newNode: Syntax(presentNode).withLeadingTrivia(.space)
106+
)]
94107
} else {
95108
return [.replace(
96109
oldNode: Syntax(node),

Tests/SwiftParserTest/translated/ToplevelLibraryTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ final class ToplevelLibraryTests: XCTestCase {
4444
""",
4545
diagnostics: [
4646
DiagnosticSpec(message: "expected 'in', expression, and body in 'for' statement"),
47-
]
47+
], fixedSource: """
48+
for i in <#expression#> {}
49+
"""
4850
)
4951
}
5052
}

0 commit comments

Comments
 (0)