Unnamed repository; edit this file 'description' to name the repository.
Merge rust-analyzer/text-size#1
1: Add extend_to for TextRange r=edwin0cheng a=edwin0cheng
~~This PR added a `convex_hull` function for joining to two `TextUnit` together.~~
This PR added a `extend_to` function for joining to two `TextUnit` together.
Co-authored-by: Edwin Cheng <[email protected]>
| -rw-r--r-- | lib/text-size/src/lib.rs | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/text-size/src/lib.rs b/lib/text-size/src/lib.rs index c00954c55c..bd8e820e29 100644 --- a/lib/text-size/src/lib.rs +++ b/lib/text-size/src/lib.rs @@ -301,6 +301,15 @@ impl TextRange { // BREAK: pass by value #[inline(always)] + /// The smallest range that contains both ranges + pub fn extend_to(&self, other: &TextRange) -> TextRange { + let start = self.start().min(other.start()); + let end = self.end().max(other.end()); + TextRange::from_to(start, end) + } + + // BREAK: pass by value + #[inline(always)] pub fn contains(&self, offset: TextUnit) -> bool { self.start() <= offset && offset < self.end() } @@ -434,6 +443,13 @@ mod tests { } #[test] + fn check_extend_to() { + assert_eq!(r(1, 2).extend_to(&r(2, 3)), r(1, 3)); + assert_eq!(r(1, 5).extend_to(&r(2, 3)), r(1, 5)); + assert_eq!(r(1, 2).extend_to(&r(4, 5)), r(1, 5)); + } + + #[test] fn check_contains() { assert!(!r(1, 3).contains(0.into())); assert!(r(1, 3).contains(1.into())); |