Skip to content

Commit a6162a5

Browse files
committed
SwiftSyntax: Conform AbsolutePosition to Strideable
1 parent fdaf1be commit a6162a5

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

Sources/SwiftIDEUtils/FixItApplier.swift

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public enum FixItApplier {
6868
source.replaceSubrange(startIndex..<endIndex, with: edit.replacement)
6969

7070
edits = edits.compactMap { remainingEdit -> SourceEdit? in
71-
if remainingEdit.replacementRange.overlaps(edit.replacementRange) {
71+
if remainingEdit.range.overlaps(edit.range) {
7272
// The edit overlaps with the previous edit. We can't apply both
7373
// without conflicts. Apply the one that's listed first and drop the
7474
// later edit.
@@ -78,12 +78,10 @@ public enum FixItApplier {
7878
// If the remaining edit starts after or at the end of the edit that we just applied,
7979
// shift it by the current edit's difference in length.
8080
if edit.endUtf8Offset <= remainingEdit.startUtf8Offset {
81-
let startPosition = AbsolutePosition(
82-
utf8Offset: remainingEdit.startUtf8Offset - edit.replacementRange.count + edit.replacementLength.utf8Length
83-
)
84-
let endPosition = AbsolutePosition(
85-
utf8Offset: remainingEdit.endUtf8Offset - edit.replacementRange.count + edit.replacementLength.utf8Length
86-
)
81+
let shift = edit.replacementLength.utf8Length - edit.range.count
82+
let startPosition = AbsolutePosition(utf8Offset: remainingEdit.startUtf8Offset + shift)
83+
let endPosition = AbsolutePosition(utf8Offset: remainingEdit.endUtf8Offset + shift)
84+
8785
return SourceEdit(range: startPosition..<endPosition, replacement: remainingEdit.replacement)
8886
}
8987

@@ -103,8 +101,4 @@ private extension SourceEdit {
103101
var endUtf8Offset: Int {
104102
return range.upperBound.utf8Offset
105103
}
106-
107-
var replacementRange: Range<Int> {
108-
return startUtf8Offset..<endUtf8Offset
109-
}
110104
}

Sources/SwiftSyntax/AbsolutePosition.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ public struct AbsolutePosition: Comparable, Hashable, Sendable {
2121
self.utf8Offset = utf8Offset
2222
}
2323

24+
public static func < (lhs: AbsolutePosition, rhs: AbsolutePosition) -> Bool {
25+
return lhs.utf8Offset < rhs.utf8Offset
26+
}
27+
}
28+
29+
extension AbsolutePosition: Strideable {
2430
public func advanced(by offset: Int) -> AbsolutePosition {
25-
return AbsolutePosition(utf8Offset: self.utf8Offset + offset)
31+
AbsolutePosition(utf8Offset: self.utf8Offset + offset)
2632
}
2733

28-
public static func < (lhs: AbsolutePosition, rhs: AbsolutePosition) -> Bool {
29-
return lhs.utf8Offset < rhs.utf8Offset
34+
public func distance(to other: AbsolutePosition) -> Int {
35+
self.utf8Offset.distance(to: other.utf8Offset)
3036
}
3137
}

0 commit comments

Comments
 (0)