Skip to content

[Diagnostics] Fix and adjust FixIt application #2941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions Sources/SwiftCompilerPluginMessageHandling/Diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,37 @@ extension PluginMessage.Diagnostic {
let text: String
switch $0 {
case .replace(let oldNode, let newNode):
// Replace the whole node including leading/trailing trivia, but if
// the trivia are the same, don't include them in the replacing range.
let leadingMatch = oldNode.leadingTrivia == newNode.leadingTrivia
let trailingMatch = oldNode.trailingTrivia == newNode.trailingTrivia
range = sourceManager.range(
of: oldNode,
from: .afterLeadingTrivia,
to: .beforeTrailingTrivia
from: leadingMatch ? .afterLeadingTrivia : .beforeLeadingTrivia,
to: trailingMatch ? .beforeTrailingTrivia : .afterTrailingTrivia
)
text = newNode.trimmedDescription
var newNode = newNode.detached
if leadingMatch {
newNode.leadingTrivia = []
}
if trailingMatch {
newNode.trailingTrivia = []
}
text = newNode.description
case .replaceLeadingTrivia(let token, let newTrivia):
guard token.leadingTrivia != newTrivia else {
return nil
}
range = sourceManager.range(
of: Syntax(token),
from: .beforeLeadingTrivia,
to: .afterLeadingTrivia
)
text = newTrivia.description
case .replaceTrailingTrivia(let token, let newTrivia):
guard token.trailingTrivia != newTrivia else {
return nil
}
range = sourceManager.range(
of: Syntax(token),
from: .beforeTrailingTrivia,
Expand Down
27 changes: 24 additions & 3 deletions Sources/SwiftDiagnostics/FixIt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ extension FixIt {
public var edits: [SourceEdit] {
var existingEdits = [SourceEdit]()
for change in changes {
let edit = change.edit
guard let edit = change.edit else {
continue
}
let isOverlapping = existingEdits.contains { edit.range.overlaps($0.range) }
if !isOverlapping {
// The edit overlaps with the previous edit. We can't apply both
Expand All @@ -111,21 +113,40 @@ extension FixIt {
}

private extension FixIt.Change {
var edit: SourceEdit {
var edit: SourceEdit? {
switch self {
case .replace(let oldNode, let newNode):
// Replace the whole node including leading/trailing trivia, but if
// the trivia are the same, don't include them in the replacing range.
let leadingMatch = oldNode.leadingTrivia == newNode.leadingTrivia
let trailingMatch = oldNode.trailingTrivia == newNode.trailingTrivia
let start = leadingMatch ? oldNode.positionAfterSkippingLeadingTrivia : oldNode.position
let end = trailingMatch ? oldNode.endPositionBeforeTrailingTrivia : oldNode.endPosition
var newNode = newNode.detached
if leadingMatch {
newNode.leadingTrivia = []
}
if trailingMatch {
newNode.trailingTrivia = []
}
return SourceEdit(
range: oldNode.position..<oldNode.endPosition,
range: start..<end,
replacement: newNode.description
)

case .replaceLeadingTrivia(let token, let newTrivia):
guard token.leadingTrivia != newTrivia else {
return nil
}
return SourceEdit(
range: token.position..<token.positionAfterSkippingLeadingTrivia,
replacement: newTrivia.description
)

case .replaceTrailingTrivia(let token, let newTrivia):
guard token.trailingTrivia != newTrivia else {
return nil
}
return SourceEdit(
range: token.endPositionBeforeTrailingTrivia..<token.endPosition,
replacement: newTrivia.description
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftIDEUtils/FixItApplier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public enum FixItApplier {
) -> String {
let messages = messages ?? diagnostics.compactMap { $0.fixIts.first?.message.message }

// FIXME: This assumes every fix-it is applied to a node in the 'tree', which is not guaranteed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we check this and ignore Fix-Its that apply to different trees? Although I do worry that macro authors will have made Fix-Its that apply to modified trees and that we might break them with this…

let edits =
diagnostics
.flatMap(\.fixIts)
Expand Down
8 changes: 4 additions & 4 deletions Tests/SwiftDiagnosticsTest/FixItTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import _SwiftSyntaxTestSupport

final class FixItTests: XCTestCase {
func testEditsForFixIt() throws {
let markedSource = "protocol 1️⃣Multi 2️⃣ident 3️⃣{}"
let markedSource = "protocol 1️⃣Multi2️⃣ 3️⃣ident 4️⃣{}"
let (markers, source) = extractMarkers(markedSource)
let positions = markers.mapValues { AbsolutePosition(utf8Offset: $0) }
XCTAssertEqual(positions.count, 3)
XCTAssertEqual(positions.count, 4)

let expectedEdits = [
SourceEdit(range: positions["1️⃣"]!..<positions["2️⃣"]!, replacement: "Multiident "),
SourceEdit(range: positions["2️⃣"]!..<positions["3️⃣"]!, replacement: ""),
SourceEdit(range: positions["1️⃣"]!..<positions["2️⃣"]!, replacement: "Multiident"),
SourceEdit(range: positions["3️⃣"]!..<positions["4️⃣"]!, replacement: ""),
]
let tree = Parser.parse(source: source)
let diags = ParseDiagnosticsGenerator.diagnostics(for: tree)
Expand Down