aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/syntax_node/syntax_text.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/syntax_node/syntax_text.rs')
-rw-r--r--crates/ra_syntax/src/syntax_node/syntax_text.rs144
1 files changed, 144 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/syntax_node/syntax_text.rs b/crates/ra_syntax/src/syntax_node/syntax_text.rs
new file mode 100644
index 000000000..84e5b231a
--- /dev/null
+++ b/crates/ra_syntax/src/syntax_node/syntax_text.rs
@@ -0,0 +1,144 @@
1use std::{fmt, ops};
2
3use crate::{SyntaxNode, TextRange, TextUnit};
4
5#[derive(Clone)]
6pub struct SyntaxText<'a> {
7 node: &'a SyntaxNode,
8 range: TextRange,
9}
10
11impl<'a> SyntaxText<'a> {
12 pub(crate) fn new(node: &'a SyntaxNode) -> SyntaxText<'a> {
13 SyntaxText { node, range: node.range() }
14 }
15
16 pub fn chunks(&self) -> impl Iterator<Item = &'a str> {
17 let range = self.range;
18 self.node.descendants().filter_map(move |node| {
19 let text = node.leaf_text()?;
20 let range = range.intersection(&node.range())?;
21 let range = range - node.range().start();
22 Some(&text[range])
23 })
24 }
25
26 pub fn push_to(&self, buf: &mut String) {
27 self.chunks().for_each(|it| buf.push_str(it));
28 }
29
30 pub fn to_string(&self) -> String {
31 self.chunks().collect()
32 }
33
34 pub fn contains(&self, c: char) -> bool {
35 self.chunks().any(|it| it.contains(c))
36 }
37
38 pub fn find(&self, c: char) -> Option<TextUnit> {
39 let mut acc: TextUnit = 0.into();
40 for chunk in self.chunks() {
41 if let Some(pos) = chunk.find(c) {
42 let pos: TextUnit = (pos as u32).into();
43 return Some(acc + pos);
44 }
45 acc += TextUnit::of_str(chunk);
46 }
47 None
48 }
49
50 pub fn len(&self) -> TextUnit {
51 self.range.len()
52 }
53
54 pub fn slice(&self, range: impl SyntaxTextSlice) -> SyntaxText<'a> {
55 let range = range.restrict(self.range).unwrap_or_else(|| {
56 panic!("invalid slice, range: {:?}, slice: {:?}", self.range, range)
57 });
58 SyntaxText { node: self.node, range }
59 }
60
61 pub fn char_at(&self, offset: impl Into<TextUnit>) -> Option<char> {
62 let mut start: TextUnit = 0.into();
63 let offset = offset.into();
64 for chunk in self.chunks() {
65 let end = start + TextUnit::of_str(chunk);
66 if start <= offset && offset < end {
67 let off: usize = u32::from(offset - start) as usize;
68 return Some(chunk[off..].chars().next().unwrap());
69 }
70 start = end;
71 }
72 None
73 }
74}
75
76impl<'a> fmt::Debug for SyntaxText<'a> {
77 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78 fmt::Debug::fmt(&self.to_string(), f)
79 }
80}
81
82impl<'a> fmt::Display for SyntaxText<'a> {
83 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
84 fmt::Display::fmt(&self.to_string(), f)
85 }
86}
87
88pub trait SyntaxTextSlice: fmt::Debug {
89 fn restrict(&self, range: TextRange) -> Option<TextRange>;
90}
91
92impl SyntaxTextSlice for TextRange {
93 fn restrict(&self, range: TextRange) -> Option<TextRange> {
94 self.intersection(&range)
95 }
96}
97
98impl SyntaxTextSlice for ops::RangeTo<TextUnit> {
99 fn restrict(&self, range: TextRange) -> Option<TextRange> {
100 if !range.contains_inclusive(self.end) {
101 return None;
102 }
103 Some(TextRange::from_to(range.start(), self.end))
104 }
105}
106
107impl SyntaxTextSlice for ops::RangeFrom<TextUnit> {
108 fn restrict(&self, range: TextRange) -> Option<TextRange> {
109 if !range.contains_inclusive(self.start) {
110 return None;
111 }
112 Some(TextRange::from_to(self.start, range.end()))
113 }
114}
115
116impl SyntaxTextSlice for ops::Range<TextUnit> {
117 fn restrict(&self, range: TextRange) -> Option<TextRange> {
118 TextRange::from_to(self.start, self.end).restrict(range)
119 }
120}
121
122impl From<SyntaxText<'_>> for String {
123 fn from(text: SyntaxText) -> String {
124 text.to_string()
125 }
126}
127
128impl PartialEq<str> for SyntaxText<'_> {
129 fn eq(&self, mut rhs: &str) -> bool {
130 for chunk in self.chunks() {
131 if !rhs.starts_with(chunk) {
132 return false;
133 }
134 rhs = &rhs[chunk.len()..];
135 }
136 rhs.is_empty()
137 }
138}
139
140impl PartialEq<&'_ str> for SyntaxText<'_> {
141 fn eq(&self, rhs: &&str) -> bool {
142 self == *rhs
143 }
144}