8000 Make the extension of FixIt.Change public · swiftlang/swift-syntax@cdac3b1 · GitHub
[go: up one dir, main page]

Skip to content

Commit cdac3b1

Browse files
committed
Make the extension of FixIt.Change public
Finetue the code
1 parent e5962c4 commit cdac3b1

File tree

2 files changed

+52
-48
lines changed

2 files changed

+52
-48
lines changed

Sources/SwiftDiagnostics/FixIt.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,48 @@ public struct FixIt {
4646
self.changes = changes
4747
}
4848
}
49+
50+
public extension FixIt.Change {
51+
struct Edit: Equatable {
52+
public var startPosition: AbsolutePosition
53+
public var endPosition: AbsolutePosition
54+
public let replacement: String
55+
56+
public var replacementLength: Int {
57+
return replacement.utf8.count
58+
}
59+
60+
public var replacementRange: Range<AbsolutePosition> {
61+
return startPosition..<endPosition
62+
}
63+
64+
public var replacementRangeCount: Int {
65+
return replacementRange.upperBound.utf8Offset - replacementRange.lowerBound.utf8Offset
66+
}
67+
}
68+
69+
var edit: Edit {
70+
switch self {
71+
case .replace(let oldNode, let newNode):
72+
return Edit(
73+
startPosition: oldNode.position,
74+
endPosition: oldNode.endPosition,
75+
replacement: newNode.description
76+
)
77+
78+
case .replaceLeadingTrivia(let token, let newTrivia):
79+
return Edit(
80+
startPosition: token.position,
81+
endPosition: token.positionAfterSkippingLeadingTrivia,
82+
replacement: newTrivia.description
83+
)
84+
85+
case .replaceTrailingTrivia(let token, let newTrivia):
86+
return Edit(
87+
startPosition: token.endPositionBeforeTrailingTrivia,
88+
endPosition: token.endPosition,
89+
replacement: newTrivia.description
90+
)
91+
}
92+
}
93+
}

Sources/_SwiftSyntaxTestSupport/FixItApplier.swift

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,6 @@ import SwiftDiagnostics
1414
import SwiftSyntax
1515

1616
public enum FixItApplier {
17-
struct Edit: Equatable {
18-
var startUtf8Offset: Int
19-
var endUtf8Offset: Int
20-
let replacement: String
21-
22-
var replacementLength: Int {
23-
return replacement.utf8.count
24-
}
25-
26-
var replacementRange: Range<Int> {
27-
return startUtf8Offset..<endUtf8Offset
28-
}
29-
}
30-
3117
/// Applies selected or all Fix-Its from the provided diagnostics to a given syntax tree.
3218
///
3319
/// - Parameters:
@@ -50,18 +36,18 @@ public enum FixItApplier {
5036
.filter { messages.contains($0.message.message) }
5137
.flatMap(\.changes)
5238

53-
var edits: [Edit] = changes.map(\.edit)
39+
var edits: [FixIt.Change.Edit] = changes.map(\.edit)
5440
var source = tree.description
5541

5642
while let edit = edits.first {
5743
edits = Array(edits.dropFirst())
5844

59-
let startIndex = source.utf8.index(source.utf8.startIndex, offsetBy: edit.startUtf8Offset)
60-
let endIndex = source.utf8.index(source.utf8.startIndex, offsetBy: edit.endUtf8Offset)
45+
let startIndex = source.utf8.index(source.utf8.startIndex, offsetBy: edit.startPosition.utf8Offset)
46+
let endIndex = source.utf8.index(source.utf8.startIndex, offsetBy: edit.endPosition.utf8Offset)
6147

6248
source.replaceSubrange(startIndex..<endIndex, with: edit.replacement)
6349

64-
edits = edits.compactMap { remainingEdit -> FixItApplier.Edit? in
50+
edits = edits.compactMap { remainingEdit -> FixIt.Change.Edit? in
6551
var remainingEdit = remainingEdit
6652

6753
if remainingEdit.replacementRange.overlaps(edit.replacementRange) {
@@ -73,9 +59,9 @@ public enum FixItApplier {
7359

7460
// If the remaining edit starts after or at the end of the edit that we just applied,
7561
// shift it by the current edit's difference in length.
76-
if edit.endUtf8Offset <= remainingEdit.startUtf8Offset {
77-
remainingEdit.startUtf8Offset = remainingEdit.startUtf8Offset - edit.replacementRange.count + edit.replacementLength
78-
remainingEdit.endUtf8Offset = remainingEdit.endUtf8Offset - edit.replacementRange.count + edit.replacementLength
62+
if edit.endPosition.utf8Offset <= remainingEdit.startPosition.utf8Offset {
63+
remainingEdit.startPosition = AbsolutePosition(utf8Offset: remainingEdit.startPosition.utf8Offset - edit.replacementRangeCount + edit.replacementLength)
64+
remainingEdit.endPosition = AbsolutePosition(utf8Offset: remainingEdit.endPosition.utf8Offset - edit.replacementRangeCount + edit.replacementLength)
7965
}
8066

8167
return remainingEdit
@@ -85,30 +71,3 @@ public enum FixItApplier {
8571
return source
8672
}
8773
}
88-
89-
fileprivate extension FixIt.Change {
90-
var edit: FixItApplier.Edit {
91-
switch self {
92-
case .replace(let oldNode, let newNode):
93-
return FixItApplier.Edit(
94-
startUtf8Offset: oldNode.position.utf8Offset,
95-
endUtf8Offset: oldNode.endPosition.utf8Offset,
96-
replacement: newNode.description
97-
)
98-
99-
case .replaceLeadingTrivia(let token, let newTrivia):
100-
return FixItApplier.Edit(
101-
startUtf8Offset: token.position.utf8Offset,
102-
endUtf8Offset: token.positionAfterSkippingLeadingTrivia.utf8Offset,
103-
replacement: newTrivia.description
104-
)
105-
106-
case .replaceTrailingTrivia(let token, let newTrivia):
107-
return FixItApplier.Edit(
108-
startUtf8Offset: token.endPositionBeforeTrailingTrivia.utf8Offset,
109-
endUtf8Offset: token.endPosition.utf8Offset,
110-
replacement: newTrivia.description
111-
)
112-
}
113-
}
114-
}

0 commit comments

Comments
 (0)
0