aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/syntax_text.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/syntax_text.rs')
-rw-r--r--crates/ra_syntax/src/syntax_text.rs25
1 files changed, 16 insertions, 9 deletions
diff --git a/crates/ra_syntax/src/syntax_text.rs b/crates/ra_syntax/src/syntax_text.rs
index 939f2c02f..6902a04a2 100644
--- a/crates/ra_syntax/src/syntax_text.rs
+++ b/crates/ra_syntax/src/syntax_text.rs
@@ -16,29 +16,36 @@ impl<'a> SyntaxText<'a> {
16 SyntaxText { node, range: node.range() } 16 SyntaxText { node, range: node.range() }
17 } 17 }
18 18
19 pub fn chunks(&self) -> impl Iterator<Item = &'a str> { 19 pub fn chunks(&self) -> impl Iterator<Item = SmolStr> {
20 let range = self.range; 20 let range = self.range;
21 self.node.descendants_with_tokens().filter_map(move |el| match el { 21 self.node.descendants_with_tokens().filter_map(move |el| match el {
22 SyntaxElement::Token(t) => { 22 SyntaxElement::Token(t) => {
23 let text = t.text(); 23 let text = t.text();
24 let range = range.intersection(&t.range())?; 24 let range = range.intersection(&t.range())?;
25 let range = range - t.range().start(); 25 let res = if range == t.range() {
26 Some(&text[range]) 26 t.text().clone()
27 } else {
28 let range = range - t.range().start();
29 text[range].into()
30 };
31 Some(res)
27 } 32 }
28 SyntaxElement::Node(_) => None, 33 SyntaxElement::Node(_) => None,
29 }) 34 })
30 } 35 }
31 36
32 pub fn push_to(&self, buf: &mut String) { 37 pub fn push_to(&self, buf: &mut String) {
33 self.chunks().for_each(|it| buf.push_str(it)); 38 self.chunks().for_each(|it| buf.push_str(it.as_str()));
34 } 39 }
35 40
36 pub fn to_string(&self) -> String { 41 pub fn to_string(&self) -> String {
37 self.chunks().collect() 42 let mut buf = String::new();
43 self.push_to(&mut buf);
44 buf
38 } 45 }
39 46
40 pub fn to_smol_string(&self) -> SmolStr { 47 pub fn to_smol_string(&self) -> SmolStr {
41 self.chunks().collect() 48 self.to_string().into()
42 } 49 }
43 50
44 pub fn contains(&self, c: char) -> bool { 51 pub fn contains(&self, c: char) -> bool {
@@ -52,7 +59,7 @@ impl<'a> SyntaxText<'a> {
52 let pos: TextUnit = (pos as u32).into(); 59 let pos: TextUnit = (pos as u32).into();
53 return Some(acc + pos); 60 return Some(acc + pos);
54 } 61 }
55 acc += TextUnit::of_str(chunk); 62 acc += TextUnit::of_str(chunk.as_str());
56 } 63 }
57 None 64 None
58 } 65 }
@@ -97,7 +104,7 @@ impl<'a> SyntaxText<'a> {
97 let mut start: TextUnit = 0.into(); 104 let mut start: TextUnit = 0.into();
98 let offset = offset.into(); 105 let offset = offset.into();
99 for chunk in self.chunks() { 106 for chunk in self.chunks() {
100 let end = start + TextUnit::of_str(chunk); 107 let end = start + TextUnit::of_str(chunk.as_str());
101 if start <= offset && offset < end { 108 if start <= offset && offset < end {
102 let off: usize = u32::from(offset - start) as usize; 109 let off: usize = u32::from(offset - start) as usize;
103 return Some(chunk[off..].chars().next().unwrap()); 110 return Some(chunk[off..].chars().next().unwrap());
@@ -129,7 +136,7 @@ impl From<SyntaxText<'_>> for String {
129impl PartialEq<str> for SyntaxText<'_> { 136impl PartialEq<str> for SyntaxText<'_> {
130 fn eq(&self, mut rhs: &str) -> bool { 137 fn eq(&self, mut rhs: &str) -> bool {
131 for chunk in self.chunks() { 138 for chunk in self.chunks() {
132 if !rhs.starts_with(chunk) { 139 if !rhs.starts_with(chunk.as_str()) {
133 return false; 140 return false;
134 } 141 }
135 rhs = &rhs[chunk.len()..]; 142 rhs = &rhs[chunk.len()..];