aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r--crates/ra_syntax/src/text_utils.rs10
-rw-r--r--crates/ra_syntax/src/yellow/syntax_text.rs14
2 files changed, 5 insertions, 19 deletions
diff --git a/crates/ra_syntax/src/text_utils.rs b/crates/ra_syntax/src/text_utils.rs
index 417d43e1b..7aaf4c223 100644
--- a/crates/ra_syntax/src/text_utils.rs
+++ b/crates/ra_syntax/src/text_utils.rs
@@ -1,15 +1,5 @@
1use crate::TextRange; 1use crate::TextRange;
2 2
3pub fn intersect(r1: TextRange, r2: TextRange) -> Option<TextRange> {
4 let start = r1.start().max(r2.start());
5 let end = r1.end().min(r2.end());
6 if start <= end {
7 Some(TextRange::from_to(start, end))
8 } else {
9 None
10 }
11}
12
13pub fn replace_range(mut text: String, range: TextRange, replace_with: &str) -> String { 3pub fn replace_range(mut text: String, range: TextRange, replace_with: &str) -> String {
14 let start = u32::from(range.start()) as usize; 4 let start = u32::from(range.start()) as usize;
15 let end = u32::from(range.end()) as usize; 5 let end = u32::from(range.end()) as usize;
diff --git a/crates/ra_syntax/src/yellow/syntax_text.rs b/crates/ra_syntax/src/yellow/syntax_text.rs
index 31db0fdab..08dbe57a2 100644
--- a/crates/ra_syntax/src/yellow/syntax_text.rs
+++ b/crates/ra_syntax/src/yellow/syntax_text.rs
@@ -1,10 +1,6 @@
1use std::{fmt, ops}; 1use std::{fmt, ops};
2 2
3use ra_text_edit::text_utils::contains_offset_nonstrict; 3use crate::{SyntaxNode, TextRange, TextUnit};
4use crate::{
5 text_utils::intersect,
6 SyntaxNode, TextRange, TextUnit,
7};
8 4
9#[derive(Clone)] 5#[derive(Clone)]
10pub struct SyntaxText<'a> { 6pub struct SyntaxText<'a> {
@@ -23,7 +19,7 @@ impl<'a> SyntaxText<'a> {
23 let range = self.range; 19 let range = self.range;
24 self.node.descendants().filter_map(move |node| { 20 self.node.descendants().filter_map(move |node| {
25 let text = node.leaf_text()?; 21 let text = node.leaf_text()?;
26 let range = intersect(range, node.range())?; 22 let range = range.intersection(&node.range())?;
27 let range = range - node.range().start(); 23 let range = range - node.range().start();
28 Some(&text[range]) 24 Some(&text[range])
29 }) 25 })
@@ -92,13 +88,13 @@ pub trait SyntaxTextSlice: fmt::Debug {
92 88
93impl SyntaxTextSlice for TextRange { 89impl SyntaxTextSlice for TextRange {
94 fn restrict(&self, range: TextRange) -> Option<TextRange> { 90 fn restrict(&self, range: TextRange) -> Option<TextRange> {
95 intersect(*self, range) 91 self.intersection(&range)
96 } 92 }
97} 93}
98 94
99impl SyntaxTextSlice for ops::RangeTo<TextUnit> { 95impl SyntaxTextSlice for ops::RangeTo<TextUnit> {
100 fn restrict(&self, range: TextRange) -> Option<TextRange> { 96 fn restrict(&self, range: TextRange) -> Option<TextRange> {
101 if !contains_offset_nonstrict(range, self.end) { 97 if !range.contains_inclusive(self.end) {
102 return None; 98 return None;
103 } 99 }
104 Some(TextRange::from_to(range.start(), self.end)) 100 Some(TextRange::from_to(range.start(), self.end))
@@ -107,7 +103,7 @@ impl SyntaxTextSlice for ops::RangeTo<TextUnit> {
107 103
108impl SyntaxTextSlice for ops::RangeFrom<TextUnit> { 104impl SyntaxTextSlice for ops::RangeFrom<TextUnit> {
109 fn restrict(&self, range: TextRange) -> Option<TextRange> { 105 fn restrict(&self, range: TextRange) -> Option<TextRange> {
110 if !contains_offset_nonstrict(range, self.start) { 106 if !range.contains_inclusive(self.start) {
111 return None; 107 return None;
112 } 108 }
113 Some(TextRange::from_to(self.start, range.end())) 109 Some(TextRange::from_to(self.start, range.end()))