Skip to content

Commit b66915f

Browse files
committed
Deprecate intersect-style methods on ByteSourceRange/Range<AbsolutePosition>
We should use the native Range methods `clamped(to:)` and `overlaps` here. To match that naming scheme `intersectsOrTouches` has been renamed to `overlapsOrTouches`.
1 parent 866b35c commit b66915f

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

Release Notes/600.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
- Pull request: https://github.com/apple/swift-syntax/pull/2594
9393

9494
- `Range<AbsolutePosition>`
95-
- Description: `Range<AbsolutePosition>` gained a few convenience functions inspired from `ByteSourceRange`: `init(position:length:)`, `length`, `intersectsOrTouches`, `intersects`, `intersecting`
95+
- Description: `Range<AbsolutePosition>` gained a few convenience functions inspired from `ByteSourceRange`: `init(position:length:)`, `length`, `overlapsOrTouches`
9696
- Pull request: https://github.com/apple/swift-syntax/pull/2587
9797

9898
## API Behavior Changes

Sources/SwiftParser/IncrementalParseTransition.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,9 @@ public struct ConcurrentEdits: Sendable {
348348
var editToAdd = editToAdd
349349
var editIndicesMergedWithNewEdit: [Int] = []
350350
for (index, existingEdit) in concurrentEdits.enumerated() {
351-
if existingEdit.replacementRange.intersectsOrTouches(editToAdd.range) {
351+
if existingEdit.replacementRange.overlapsOrTouches(editToAdd.range) {
352352
let intersectionLength =
353-
existingEdit.replacementRange.intersecting(editToAdd.range)?.length ?? SourceLength(utf8Length: 0)
353+
existingEdit.replacementRange.clamped(to: editToAdd.range).length
354354
let replacement: [UInt8]
355355
replacement =
356356
existingEdit.replacement.prefix(

Sources/SwiftSyntax/Utils.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public extension Range<AbsolutePosition> {
3131
///
3232
/// If the intersection is empty, this returns a range starting at offset 0 and having length 0.
3333
func intersected(_ other: Range<AbsolutePosition>) -> Range<AbsolutePosition> {
34-
return self.intersecting(other) ?? AbsolutePosition(utf8Offset: 0)..<AbsolutePosition(utf8Offset: 0)
34+
return self.clamped(to: other)
3535
}
3636
}
3737

@@ -43,20 +43,29 @@ extension Range<AbsolutePosition> {
4343
self = position..<(position + length)
4444
}
4545

46+
// Returns `true` if the intersection between this range and `other` is non-empty or if the two ranges are directly
47+
/// adjacent to each other.
48+
public func overlapsOrTouches(_ other: Range<AbsolutePosition>) -> Bool {
49+
return self.upperBound >= other.lowerBound && self.lowerBound <= other.upperBound
50+
}
51+
4652
/// Returns `true` if the intersection between this range and `other` is non-empty or if the two ranges are directly
4753
/// adjacent to each other.
54+
@available(*, deprecated, renamed: "overlapsOrTouches(_:)")
4855
public func intersectsOrTouches(_ other: Range<AbsolutePosition>) -> Bool {
4956
return self.upperBound >= other.lowerBound && self.lowerBound <= other.upperBound
5057
}
5158

5259
/// Returns `true` if the intersection between this range and `other` is non-empty.
60+
@available(*, deprecated, renamed: "overlaps(_:)")
5361
public func intersects(_ other: Range<AbsolutePosition>) -> Bool {
5462
return self.upperBound > other.lowerBound && self.lowerBound < other.upperBound
5563
}
5664

5765
/// Returns the range for the overlapping region between two ranges.
5866
///
5967
/// If the intersection is empty, this returns `nil`.
68+
@available(*, deprecated, message: "Use clamped(to:) instead")
6069
public func intersecting(_ other: Range<AbsolutePosition>) -> Range<AbsolutePosition>? {
6170
let lowerBound = Swift.max(self.lowerBound, other.lowerBound)
6271
let upperBound = Swift.min(self.upperBound, other.upperBound)
@@ -114,11 +123,11 @@ public struct IncrementalEdit: Equatable, Sendable {
114123
}
115124

116125
public func intersectsOrTouchesRange(_ other: Range<AbsolutePosition>) -> Bool {
117-
return self.range.intersectsOrTouches(other)
126+
return self.range.overlapsOrTouches(other)
118127
}
119128

120129
public func intersectsRange(_ other: Range<AbsolutePosition>) -> Bool {
121-
return self.range.intersects(other)
130+
return self.range.overlaps(other)
122131
}
123132
}
124133

0 commit comments

Comments
 (0)