aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r--crates/ra_syntax/src/syntax_node.rs134
1 files changed, 67 insertions, 67 deletions
diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs
index 1ca1c992b..bcfacde72 100644
--- a/crates/ra_syntax/src/syntax_node.rs
+++ b/crates/ra_syntax/src/syntax_node.rs
@@ -96,13 +96,24 @@ unsafe impl TransparentNewType for SyntaxNode {
96 type Repr = rowan::SyntaxNode<RaTypes>; 96 type Repr = rowan::SyntaxNode<RaTypes>;
97} 97}
98 98
99impl SyntaxNode { 99impl ToOwned for SyntaxNode {
100 pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> TreeArc<SyntaxNode> { 100 type Owned = TreeArc<SyntaxNode>;
101 let ptr = TreeArc(rowan::SyntaxNode::new(green, errors)); 101 fn to_owned(&self) -> TreeArc<SyntaxNode> {
102 let ptr = TreeArc(self.0.to_owned());
102 TreeArc::cast(ptr) 103 TreeArc::cast(ptr)
103 } 104 }
104} 105}
105 106
107impl fmt::Debug for SyntaxNode {
108 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
109 write!(fmt, "{:?}@{:?}", self.kind(), self.range())?;
110 if has_short_text(self.kind()) {
111 write!(fmt, " \"{}\"", self.text())?;
112 }
113 Ok(())
114 }
115}
116
106#[derive(Debug, Clone, Copy, PartialEq, Eq)] 117#[derive(Debug, Clone, Copy, PartialEq, Eq)]
107pub enum Direction { 118pub enum Direction {
108 Next, 119 Next,
@@ -110,24 +121,73 @@ pub enum Direction {
110} 121}
111 122
112impl SyntaxNode { 123impl SyntaxNode {
124 pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> TreeArc<SyntaxNode> {
125 let ptr = TreeArc(rowan::SyntaxNode::new(green, errors));
126 TreeArc::cast(ptr)
127 }
128
129 pub fn kind(&self) -> SyntaxKind {
130 self.0.kind()
131 }
132
133 pub fn range(&self) -> TextRange {
134 self.0.range()
135 }
136
137 pub fn text(&self) -> SyntaxText {
138 SyntaxText::new(self)
139 }
140
141 pub fn is_leaf(&self) -> bool {
142 self.0.is_leaf()
143 }
144
113 pub fn leaf_text(&self) -> Option<&SmolStr> { 145 pub fn leaf_text(&self) -> Option<&SmolStr> {
114 self.0.leaf_text() 146 self.0.leaf_text()
115 } 147 }
148
149 pub fn parent(&self) -> Option<&SyntaxNode> {
150 self.0.parent().map(SyntaxNode::from_repr)
151 }
152
153 pub fn first_child(&self) -> Option<&SyntaxNode> {
154 self.0.first_child().map(SyntaxNode::from_repr)
155 }
156
157 pub fn last_child(&self) -> Option<&SyntaxNode> {
158 self.0.last_child().map(SyntaxNode::from_repr)
159 }
160
161 pub fn next_sibling(&self) -> Option<&SyntaxNode> {
162 self.0.next_sibling().map(SyntaxNode::from_repr)
163 }
164
165 pub fn prev_sibling(&self) -> Option<&SyntaxNode> {
166 self.0.prev_sibling().map(SyntaxNode::from_repr)
167 }
168
169 pub fn children(&self) -> SyntaxNodeChildren {
170 SyntaxNodeChildren(self.0.children())
171 }
172
116 pub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode> { 173 pub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode> {
117 crate::algo::generate(Some(self), |&node| node.parent()) 174 crate::algo::generate(Some(self), |&node| node.parent())
118 } 175 }
176
119 pub fn descendants(&self) -> impl Iterator<Item = &SyntaxNode> { 177 pub fn descendants(&self) -> impl Iterator<Item = &SyntaxNode> {
120 self.preorder().filter_map(|event| match event { 178 self.preorder().filter_map(|event| match event {
121 WalkEvent::Enter(node) => Some(node), 179 WalkEvent::Enter(node) => Some(node),
122 WalkEvent::Leave(_) => None, 180 WalkEvent::Leave(_) => None,
123 }) 181 })
124 } 182 }
183
125 pub fn siblings(&self, direction: Direction) -> impl Iterator<Item = &SyntaxNode> { 184 pub fn siblings(&self, direction: Direction) -> impl Iterator<Item = &SyntaxNode> {
126 crate::algo::generate(Some(self), move |&node| match direction { 185 crate::algo::generate(Some(self), move |&node| match direction {
127 Direction::Next => node.next_sibling(), 186 Direction::Next => node.next_sibling(),
128 Direction::Prev => node.prev_sibling(), 187 Direction::Prev => node.prev_sibling(),
129 }) 188 })
130 } 189 }
190
131 pub fn preorder(&self) -> impl Iterator<Item = WalkEvent<&SyntaxNode>> { 191 pub fn preorder(&self) -> impl Iterator<Item = WalkEvent<&SyntaxNode>> {
132 self.0.preorder().map(|event| match event { 192 self.0.preorder().map(|event| match event {
133 WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode::from_repr(n)), 193 WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode::from_repr(n)),
@@ -135,6 +195,10 @@ impl SyntaxNode {
135 }) 195 })
136 } 196 }
137 197
198 pub fn memory_size_of_subtree(&self) -> usize {
199 self.0.memory_size_of_subtree()
200 }
201
138 pub fn debug_dump(&self) -> String { 202 pub fn debug_dump(&self) -> String {
139 let mut errors: Vec<_> = match self.ancestors().find_map(SourceFile::cast) { 203 let mut errors: Vec<_> = match self.ancestors().find_map(SourceFile::cast) {
140 Some(file) => file.errors(), 204 Some(file) => file.errors(),
@@ -178,17 +242,7 @@ impl SyntaxNode {
178 242
179 buf 243 buf
180 } 244 }
181}
182 245
183impl ToOwned for SyntaxNode {
184 type Owned = TreeArc<SyntaxNode>;
185 fn to_owned(&self) -> TreeArc<SyntaxNode> {
186 let ptr = TreeArc(self.0.to_owned());
187 TreeArc::cast(ptr)
188 }
189}
190
191impl SyntaxNode {
192 pub(crate) fn root_data(&self) -> &Vec<SyntaxError> { 246 pub(crate) fn root_data(&self) -> &Vec<SyntaxError> {
193 self.0.root_data() 247 self.0.root_data()
194 } 248 }
@@ -196,60 +250,6 @@ impl SyntaxNode {
196 pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode { 250 pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode {
197 self.0.replace_self(replacement) 251 self.0.replace_self(replacement)
198 } 252 }
199
200 pub fn kind(&self) -> SyntaxKind {
201 self.0.kind()
202 }
203
204 pub fn range(&self) -> TextRange {
205 self.0.range()
206 }
207
208 pub fn text(&self) -> SyntaxText {
209 SyntaxText::new(self)
210 }
211
212 pub fn is_leaf(&self) -> bool {
213 self.0.is_leaf()
214 }
215
216 pub fn parent(&self) -> Option<&SyntaxNode> {
217 self.0.parent().map(SyntaxNode::from_repr)
218 }
219
220 pub fn first_child(&self) -> Option<&SyntaxNode> {
221 self.0.first_child().map(SyntaxNode::from_repr)
222 }
223
224 pub fn last_child(&self) -> Option<&SyntaxNode> {
225 self.0.last_child().map(SyntaxNode::from_repr)
226 }
227
228 pub fn next_sibling(&self) -> Option<&SyntaxNode> {
229 self.0.next_sibling().map(SyntaxNode::from_repr)
230 }
231
232 pub fn prev_sibling(&self) -> Option<&SyntaxNode> {
233 self.0.prev_sibling().map(SyntaxNode::from_repr)
234 }
235
236 pub fn children(&self) -> SyntaxNodeChildren {
237 SyntaxNodeChildren(self.0.children())
238 }
239
240 pub fn memory_size_of_subtree(&self) -> usize {
241 self.0.memory_size_of_subtree()
242 }
243}
244
245impl fmt::Debug for SyntaxNode {
246 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
247 write!(fmt, "{:?}@{:?}", self.kind(), self.range())?;
248 if has_short_text(self.kind()) {
249 write!(fmt, " \"{}\"", self.text())?;
250 }
251 Ok(())
252 }
253} 253}
254 254
255#[derive(Debug)] 255#[derive(Debug)]