diff options
Diffstat (limited to 'crates/ra_syntax/src/syntax_node.rs')
-rw-r--r-- | crates/ra_syntax/src/syntax_node.rs | 331 |
1 files changed, 120 insertions, 211 deletions
diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs index e57813a94..cf680e66a 100644 --- a/crates/ra_syntax/src/syntax_node.rs +++ b/crates/ra_syntax/src/syntax_node.rs | |||
@@ -7,14 +7,13 @@ | |||
7 | //! modules just wraps its API. | 7 | //! modules just wraps its API. |
8 | 8 | ||
9 | use std::{ | 9 | use std::{ |
10 | borrow::Borrow, | ||
11 | fmt::{self, Write}, | 10 | fmt::{self, Write}, |
12 | iter::successors, | 11 | iter::successors, |
13 | ops::RangeInclusive, | 12 | ops::RangeInclusive, |
14 | }; | 13 | }; |
15 | 14 | ||
16 | use ra_parser::ParseError; | 15 | use ra_parser::ParseError; |
17 | use rowan::{GreenNodeBuilder, TransparentNewType}; | 16 | use rowan::GreenNodeBuilder; |
18 | 17 | ||
19 | use crate::{ | 18 | use crate::{ |
20 | syntax_error::{SyntaxError, SyntaxErrorKind}, | 19 | syntax_error::{SyntaxError, SyntaxErrorKind}, |
@@ -33,86 +32,8 @@ pub enum InsertPosition<T> { | |||
33 | After(T), | 32 | After(T), |
34 | } | 33 | } |
35 | 34 | ||
36 | /// Marker trait for CST and AST nodes | 35 | #[derive(PartialEq, Eq, Hash, Clone)] |
37 | pub trait SyntaxNodeWrapper: TransparentNewType<Repr = rowan::SyntaxNode> {} | 36 | pub struct SyntaxNode(pub(crate) rowan::cursor::SyntaxNode); |
38 | impl<T: TransparentNewType<Repr = rowan::SyntaxNode>> SyntaxNodeWrapper for T {} | ||
39 | |||
40 | /// An owning smart pointer for CST or AST node. | ||
41 | #[derive(PartialEq, Eq, Hash)] | ||
42 | pub struct TreeArc<T: SyntaxNodeWrapper>(pub(crate) rowan::TreeArc<T>); | ||
43 | |||
44 | impl<T: SyntaxNodeWrapper> Borrow<T> for TreeArc<T> { | ||
45 | fn borrow(&self) -> &T { | ||
46 | &*self | ||
47 | } | ||
48 | } | ||
49 | |||
50 | impl<T> TreeArc<T> | ||
51 | where | ||
52 | T: SyntaxNodeWrapper, | ||
53 | { | ||
54 | pub(crate) fn cast<U>(this: TreeArc<T>) -> TreeArc<U> | ||
55 | where | ||
56 | U: SyntaxNodeWrapper, | ||
57 | { | ||
58 | TreeArc(rowan::TreeArc::cast(this.0)) | ||
59 | } | ||
60 | } | ||
61 | |||
62 | impl<T> std::ops::Deref for TreeArc<T> | ||
63 | where | ||
64 | T: SyntaxNodeWrapper, | ||
65 | { | ||
66 | type Target = T; | ||
67 | fn deref(&self) -> &T { | ||
68 | self.0.deref() | ||
69 | } | ||
70 | } | ||
71 | |||
72 | impl<T> PartialEq<T> for TreeArc<T> | ||
73 | where | ||
74 | T: SyntaxNodeWrapper, | ||
75 | T: PartialEq<T>, | ||
76 | { | ||
77 | fn eq(&self, other: &T) -> bool { | ||
78 | let t: &T = self; | ||
79 | t == other | ||
80 | } | ||
81 | } | ||
82 | |||
83 | impl<T> Clone for TreeArc<T> | ||
84 | where | ||
85 | T: SyntaxNodeWrapper, | ||
86 | { | ||
87 | fn clone(&self) -> TreeArc<T> { | ||
88 | TreeArc(self.0.clone()) | ||
89 | } | ||
90 | } | ||
91 | |||
92 | impl<T> fmt::Debug for TreeArc<T> | ||
93 | where | ||
94 | T: SyntaxNodeWrapper, | ||
95 | T: fmt::Debug, | ||
96 | { | ||
97 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||
98 | fmt::Debug::fmt(&self.0, fmt) | ||
99 | } | ||
100 | } | ||
101 | |||
102 | #[derive(PartialEq, Eq, Hash)] | ||
103 | #[repr(transparent)] | ||
104 | pub struct SyntaxNode(pub(crate) rowan::SyntaxNode); | ||
105 | unsafe impl TransparentNewType for SyntaxNode { | ||
106 | type Repr = rowan::SyntaxNode; | ||
107 | } | ||
108 | |||
109 | impl ToOwned for SyntaxNode { | ||
110 | type Owned = TreeArc<SyntaxNode>; | ||
111 | fn to_owned(&self) -> TreeArc<SyntaxNode> { | ||
112 | let ptr = TreeArc(self.0.to_owned()); | ||
113 | TreeArc::cast(ptr) | ||
114 | } | ||
115 | } | ||
116 | 37 | ||
117 | impl fmt::Debug for SyntaxNode { | 38 | impl fmt::Debug for SyntaxNode { |
118 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | 39 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
@@ -133,9 +54,9 @@ pub enum Direction { | |||
133 | } | 54 | } |
134 | 55 | ||
135 | impl SyntaxNode { | 56 | impl SyntaxNode { |
136 | pub(crate) fn new(green: GreenNode) -> TreeArc<SyntaxNode> { | 57 | pub(crate) fn new(green: GreenNode) -> SyntaxNode { |
137 | let ptr = TreeArc(rowan::SyntaxNode::new(green, None)); | 58 | let inner = rowan::cursor::SyntaxNode::new_root(green); |
138 | TreeArc::cast(ptr) | 59 | SyntaxNode(inner) |
139 | } | 60 | } |
140 | 61 | ||
141 | pub fn kind(&self) -> SyntaxKind { | 62 | pub fn kind(&self) -> SyntaxKind { |
@@ -143,47 +64,47 @@ impl SyntaxNode { | |||
143 | } | 64 | } |
144 | 65 | ||
145 | pub fn range(&self) -> TextRange { | 66 | pub fn range(&self) -> TextRange { |
146 | self.0.range() | 67 | self.0.text_range() |
147 | } | 68 | } |
148 | 69 | ||
149 | pub fn text(&self) -> SyntaxText { | 70 | pub fn text(&self) -> SyntaxText { |
150 | SyntaxText::new(self) | 71 | SyntaxText::new(self) |
151 | } | 72 | } |
152 | 73 | ||
153 | pub fn parent(&self) -> Option<&SyntaxNode> { | 74 | pub fn parent(&self) -> Option<SyntaxNode> { |
154 | self.0.parent().map(SyntaxNode::from_repr) | 75 | self.0.parent().map(SyntaxNode) |
155 | } | 76 | } |
156 | 77 | ||
157 | pub fn first_child(&self) -> Option<&SyntaxNode> { | 78 | pub fn first_child(&self) -> Option<SyntaxNode> { |
158 | self.0.first_child().map(SyntaxNode::from_repr) | 79 | self.0.first_child().map(SyntaxNode) |
159 | } | 80 | } |
160 | 81 | ||
161 | pub fn first_child_or_token(&self) -> Option<SyntaxElement> { | 82 | pub fn first_child_or_token(&self) -> Option<SyntaxElement> { |
162 | self.0.first_child_or_token().map(SyntaxElement::from) | 83 | self.0.first_child_or_token().map(SyntaxElement::new) |
163 | } | 84 | } |
164 | 85 | ||
165 | pub fn last_child(&self) -> Option<&SyntaxNode> { | 86 | pub fn last_child(&self) -> Option<SyntaxNode> { |
166 | self.0.last_child().map(SyntaxNode::from_repr) | 87 | self.0.last_child().map(SyntaxNode) |
167 | } | 88 | } |
168 | 89 | ||
169 | pub fn last_child_or_token(&self) -> Option<SyntaxElement> { | 90 | pub fn last_child_or_token(&self) -> Option<SyntaxElement> { |
170 | self.0.last_child_or_token().map(SyntaxElement::from) | 91 | self.0.last_child_or_token().map(SyntaxElement::new) |
171 | } | 92 | } |
172 | 93 | ||
173 | pub fn next_sibling(&self) -> Option<&SyntaxNode> { | 94 | pub fn next_sibling(&self) -> Option<SyntaxNode> { |
174 | self.0.next_sibling().map(SyntaxNode::from_repr) | 95 | self.0.next_sibling().map(SyntaxNode) |
175 | } | 96 | } |
176 | 97 | ||
177 | pub fn next_sibling_or_token(&self) -> Option<SyntaxElement> { | 98 | pub fn next_sibling_or_token(&self) -> Option<SyntaxElement> { |
178 | self.0.next_sibling_or_token().map(SyntaxElement::from) | 99 | self.0.next_sibling_or_token().map(SyntaxElement::new) |
179 | } | 100 | } |
180 | 101 | ||
181 | pub fn prev_sibling(&self) -> Option<&SyntaxNode> { | 102 | pub fn prev_sibling(&self) -> Option<SyntaxNode> { |
182 | self.0.prev_sibling().map(SyntaxNode::from_repr) | 103 | self.0.prev_sibling().map(SyntaxNode) |
183 | } | 104 | } |
184 | 105 | ||
185 | pub fn prev_sibling_or_token(&self) -> Option<SyntaxElement> { | 106 | pub fn prev_sibling_or_token(&self) -> Option<SyntaxElement> { |
186 | self.0.prev_sibling_or_token().map(SyntaxElement::from) | 107 | self.0.prev_sibling_or_token().map(SyntaxElement::new) |
187 | } | 108 | } |
188 | 109 | ||
189 | pub fn children(&self) -> SyntaxNodeChildren { | 110 | pub fn children(&self) -> SyntaxNodeChildren { |
@@ -195,18 +116,18 @@ impl SyntaxNode { | |||
195 | } | 116 | } |
196 | 117 | ||
197 | pub fn first_token(&self) -> Option<SyntaxToken> { | 118 | pub fn first_token(&self) -> Option<SyntaxToken> { |
198 | self.0.first_token().map(SyntaxToken::from) | 119 | self.0.first_token().map(SyntaxToken) |
199 | } | 120 | } |
200 | 121 | ||
201 | pub fn last_token(&self) -> Option<SyntaxToken> { | 122 | pub fn last_token(&self) -> Option<SyntaxToken> { |
202 | self.0.last_token().map(SyntaxToken::from) | 123 | self.0.last_token().map(SyntaxToken) |
203 | } | 124 | } |
204 | 125 | ||
205 | pub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode> { | 126 | pub fn ancestors(&self) -> impl Iterator<Item = SyntaxNode> { |
206 | successors(Some(self), |&node| node.parent()) | 127 | successors(Some(self.clone()), |node| node.parent()) |
207 | } | 128 | } |
208 | 129 | ||
209 | pub fn descendants(&self) -> impl Iterator<Item = &SyntaxNode> { | 130 | pub fn descendants(&self) -> impl Iterator<Item = SyntaxNode> { |
210 | self.preorder().filter_map(|event| match event { | 131 | self.preorder().filter_map(|event| match event { |
211 | WalkEvent::Enter(node) => Some(node), | 132 | WalkEvent::Enter(node) => Some(node), |
212 | WalkEvent::Leave(_) => None, | 133 | WalkEvent::Leave(_) => None, |
@@ -220,8 +141,8 @@ impl SyntaxNode { | |||
220 | }) | 141 | }) |
221 | } | 142 | } |
222 | 143 | ||
223 | pub fn siblings(&self, direction: Direction) -> impl Iterator<Item = &SyntaxNode> { | 144 | pub fn siblings(&self, direction: Direction) -> impl Iterator<Item = SyntaxNode> { |
224 | successors(Some(self), move |&node| match direction { | 145 | successors(Some(self.clone()), move |node| match direction { |
225 | Direction::Next => node.next_sibling(), | 146 | Direction::Next => node.next_sibling(), |
226 | Direction::Prev => node.prev_sibling(), | 147 | Direction::Prev => node.prev_sibling(), |
227 | }) | 148 | }) |
@@ -231,29 +152,29 @@ impl SyntaxNode { | |||
231 | &self, | 152 | &self, |
232 | direction: Direction, | 153 | direction: Direction, |
233 | ) -> impl Iterator<Item = SyntaxElement> { | 154 | ) -> impl Iterator<Item = SyntaxElement> { |
234 | let me: SyntaxElement = self.into(); | 155 | let me: SyntaxElement = self.clone().into(); |
235 | successors(Some(me), move |el| match direction { | 156 | successors(Some(me), move |el| match direction { |
236 | Direction::Next => el.next_sibling_or_token(), | 157 | Direction::Next => el.next_sibling_or_token(), |
237 | Direction::Prev => el.prev_sibling_or_token(), | 158 | Direction::Prev => el.prev_sibling_or_token(), |
238 | }) | 159 | }) |
239 | } | 160 | } |
240 | 161 | ||
241 | pub fn preorder(&self) -> impl Iterator<Item = WalkEvent<&SyntaxNode>> { | 162 | pub fn preorder(&self) -> impl Iterator<Item = WalkEvent<SyntaxNode>> { |
242 | self.0.preorder().map(|event| match event { | 163 | self.0.preorder().map(|event| match event { |
243 | WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode::from_repr(n)), | 164 | WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode(n)), |
244 | WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode::from_repr(n)), | 165 | WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode(n)), |
245 | }) | 166 | }) |
246 | } | 167 | } |
247 | 168 | ||
248 | pub fn preorder_with_tokens(&self) -> impl Iterator<Item = WalkEvent<SyntaxElement>> { | 169 | pub fn preorder_with_tokens(&self) -> impl Iterator<Item = WalkEvent<SyntaxElement>> { |
249 | self.0.preorder_with_tokens().map(|event| match event { | 170 | self.0.preorder_with_tokens().map(|event| match event { |
250 | WalkEvent::Enter(n) => WalkEvent::Enter(n.into()), | 171 | WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxElement::new(n)), |
251 | WalkEvent::Leave(n) => WalkEvent::Leave(n.into()), | 172 | WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxElement::new(n)), |
252 | }) | 173 | }) |
253 | } | 174 | } |
254 | 175 | ||
255 | pub fn memory_size_of_subtree(&self) -> usize { | 176 | pub fn memory_size_of_subtree(&self) -> usize { |
256 | self.0.memory_size_of_subtree() | 177 | 0 |
257 | } | 178 | } |
258 | 179 | ||
259 | pub fn debug_dump(&self) -> String { | 180 | pub fn debug_dump(&self) -> String { |
@@ -290,11 +211,11 @@ impl SyntaxNode { | |||
290 | /// | 211 | /// |
291 | /// This is a type-unsafe low-level editing API, if you need to use it, | 212 | /// This is a type-unsafe low-level editing API, if you need to use it, |
292 | /// prefer to create a type-safe abstraction on top of it instead. | 213 | /// prefer to create a type-safe abstraction on top of it instead. |
293 | pub fn insert_children<'a>( | 214 | pub fn insert_children( |
294 | &self, | 215 | &self, |
295 | position: InsertPosition<SyntaxElement<'_>>, | 216 | position: InsertPosition<SyntaxElement>, |
296 | to_insert: impl Iterator<Item = SyntaxElement<'a>>, | 217 | to_insert: impl Iterator<Item = SyntaxElement>, |
297 | ) -> TreeArc<SyntaxNode> { | 218 | ) -> SyntaxNode { |
298 | let mut delta = TextUnit::default(); | 219 | let mut delta = TextUnit::default(); |
299 | let to_insert = to_insert.map(|element| { | 220 | let to_insert = to_insert.map(|element| { |
300 | delta += element.text_len(); | 221 | delta += element.text_len(); |
@@ -303,7 +224,7 @@ impl SyntaxNode { | |||
303 | 224 | ||
304 | let old_children = self.0.green().children(); | 225 | let old_children = self.0.green().children(); |
305 | 226 | ||
306 | let new_children = match position { | 227 | let new_children = match &position { |
307 | InsertPosition::First => { | 228 | InsertPosition::First => { |
308 | to_insert.chain(old_children.iter().cloned()).collect::<Box<[_]>>() | 229 | to_insert.chain(old_children.iter().cloned()).collect::<Box<[_]>>() |
309 | } | 230 | } |
@@ -312,7 +233,7 @@ impl SyntaxNode { | |||
312 | } | 233 | } |
313 | InsertPosition::Before(anchor) | InsertPosition::After(anchor) => { | 234 | InsertPosition::Before(anchor) | InsertPosition::After(anchor) => { |
314 | let take_anchor = if let InsertPosition::After(_) = position { 1 } else { 0 }; | 235 | let take_anchor = if let InsertPosition::After(_) = position { 1 } else { 0 }; |
315 | let split_at = self.position_of_child(anchor) + take_anchor; | 236 | let split_at = self.position_of_child(anchor.clone()) + take_anchor; |
316 | let (before, after) = old_children.split_at(split_at); | 237 | let (before, after) = old_children.split_at(split_at); |
317 | before | 238 | before |
318 | .iter() | 239 | .iter() |
@@ -330,13 +251,13 @@ impl SyntaxNode { | |||
330 | /// | 251 | /// |
331 | /// This is a type-unsafe low-level editing API, if you need to use it, | 252 | /// This is a type-unsafe low-level editing API, if you need to use it, |
332 | /// prefer to create a type-safe abstraction on top of it instead. | 253 | /// prefer to create a type-safe abstraction on top of it instead. |
333 | pub fn replace_children<'a>( | 254 | pub fn replace_children( |
334 | &self, | 255 | &self, |
335 | to_delete: RangeInclusive<SyntaxElement<'_>>, | 256 | to_delete: RangeInclusive<SyntaxElement>, |
336 | to_insert: impl Iterator<Item = SyntaxElement<'a>>, | 257 | to_insert: impl Iterator<Item = SyntaxElement>, |
337 | ) -> TreeArc<SyntaxNode> { | 258 | ) -> SyntaxNode { |
338 | let start = self.position_of_child(*to_delete.start()); | 259 | let start = self.position_of_child(to_delete.start().clone()); |
339 | let end = self.position_of_child(*to_delete.end()); | 260 | let end = self.position_of_child(to_delete.end().clone()); |
340 | let old_children = self.0.green().children(); | 261 | let old_children = self.0.green().children(); |
341 | 262 | ||
342 | let new_children = old_children[..start] | 263 | let new_children = old_children[..start] |
@@ -348,7 +269,7 @@ impl SyntaxNode { | |||
348 | self.with_children(new_children) | 269 | self.with_children(new_children) |
349 | } | 270 | } |
350 | 271 | ||
351 | fn with_children(&self, new_children: Box<[rowan::GreenElement]>) -> TreeArc<SyntaxNode> { | 272 | fn with_children(&self, new_children: Box<[rowan::GreenElement]>) -> SyntaxNode { |
352 | let len = new_children.iter().map(|it| it.text_len()).sum::<TextUnit>(); | 273 | let len = new_children.iter().map(|it| it.text_len()).sum::<TextUnit>(); |
353 | let new_node = GreenNode::new(rowan::SyntaxKind(self.kind() as u16), new_children); | 274 | let new_node = GreenNode::new(rowan::SyntaxKind(self.kind() as u16), new_children); |
354 | let new_file_node = self.replace_with(new_node); | 275 | let new_file_node = self.replace_with(new_node); |
@@ -364,7 +285,7 @@ impl SyntaxNode { | |||
364 | fn position_of_child(&self, child: SyntaxElement) -> usize { | 285 | fn position_of_child(&self, child: SyntaxElement) -> usize { |
365 | self.children_with_tokens() | 286 | self.children_with_tokens() |
366 | .position(|it| it == child) | 287 | .position(|it| it == child) |
367 | .expect("elemetn is not a child of current element") | 288 | .expect("element is not a child of current element") |
368 | } | 289 | } |
369 | } | 290 | } |
370 | 291 | ||
@@ -377,11 +298,11 @@ fn to_green_element(element: SyntaxElement) -> rowan::GreenElement { | |||
377 | } | 298 | } |
378 | } | 299 | } |
379 | 300 | ||
380 | #[derive(Clone, Copy, PartialEq, Eq, Hash)] | 301 | #[derive(Clone, PartialEq, Eq, Hash)] |
381 | pub struct SyntaxToken<'a>(pub(crate) rowan::SyntaxToken<'a>); | 302 | pub struct SyntaxToken(pub(crate) rowan::cursor::SyntaxToken); |
382 | 303 | ||
383 | //FIXME: always output text | 304 | //FIXME: always output text |
384 | impl<'a> fmt::Debug for SyntaxToken<'a> { | 305 | impl fmt::Debug for SyntaxToken { |
385 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | 306 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
386 | write!(fmt, "{:?}@{:?}", self.kind(), self.range())?; | 307 | write!(fmt, "{:?}@{:?}", self.kind(), self.range())?; |
387 | if self.text().len() < 25 { | 308 | if self.text().len() < 25 { |
@@ -398,60 +319,54 @@ impl<'a> fmt::Debug for SyntaxToken<'a> { | |||
398 | } | 319 | } |
399 | } | 320 | } |
400 | 321 | ||
401 | impl<'a> fmt::Display for SyntaxToken<'a> { | 322 | impl fmt::Display for SyntaxToken { |
402 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | 323 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
403 | fmt::Display::fmt(self.text(), fmt) | 324 | fmt::Display::fmt(self.text(), fmt) |
404 | } | 325 | } |
405 | } | 326 | } |
406 | 327 | ||
407 | impl<'a> From<rowan::SyntaxToken<'a>> for SyntaxToken<'a> { | 328 | impl SyntaxToken { |
408 | fn from(t: rowan::SyntaxToken<'a>) -> Self { | ||
409 | SyntaxToken(t) | ||
410 | } | ||
411 | } | ||
412 | |||
413 | impl<'a> SyntaxToken<'a> { | ||
414 | pub fn kind(&self) -> SyntaxKind { | 329 | pub fn kind(&self) -> SyntaxKind { |
415 | self.0.kind().0.into() | 330 | self.0.kind().0.into() |
416 | } | 331 | } |
417 | 332 | ||
418 | pub fn text(&self) -> &'a SmolStr { | 333 | pub fn text(&self) -> &SmolStr { |
419 | self.0.text() | 334 | self.0.text() |
420 | } | 335 | } |
421 | 336 | ||
422 | pub fn range(&self) -> TextRange { | 337 | pub fn range(&self) -> TextRange { |
423 | self.0.range() | 338 | self.0.text_range() |
424 | } | 339 | } |
425 | 340 | ||
426 | pub fn parent(&self) -> &'a SyntaxNode { | 341 | pub fn parent(&self) -> SyntaxNode { |
427 | SyntaxNode::from_repr(self.0.parent()) | 342 | SyntaxNode(self.0.parent()) |
428 | } | 343 | } |
429 | 344 | ||
430 | pub fn next_sibling_or_token(&self) -> Option<SyntaxElement<'a>> { | 345 | pub fn next_sibling_or_token(&self) -> Option<SyntaxElement> { |
431 | self.0.next_sibling_or_token().map(SyntaxElement::from) | 346 | self.0.next_sibling_or_token().map(SyntaxElement::new) |
432 | } | 347 | } |
433 | 348 | ||
434 | pub fn prev_sibling_or_token(&self) -> Option<SyntaxElement<'a>> { | 349 | pub fn prev_sibling_or_token(&self) -> Option<SyntaxElement> { |
435 | self.0.prev_sibling_or_token().map(SyntaxElement::from) | 350 | self.0.prev_sibling_or_token().map(SyntaxElement::new) |
436 | } | 351 | } |
437 | 352 | ||
438 | pub fn siblings_with_tokens( | 353 | pub fn siblings_with_tokens( |
439 | &self, | 354 | &self, |
440 | direction: Direction, | 355 | direction: Direction, |
441 | ) -> impl Iterator<Item = SyntaxElement<'a>> { | 356 | ) -> impl Iterator<Item = SyntaxElement> { |
442 | let me: SyntaxElement = (*self).into(); | 357 | let me: SyntaxElement = self.clone().into(); |
443 | successors(Some(me), move |el| match direction { | 358 | successors(Some(me), move |el| match direction { |
444 | Direction::Next => el.next_sibling_or_token(), | 359 | Direction::Next => el.next_sibling_or_token(), |
445 | Direction::Prev => el.prev_sibling_or_token(), | 360 | Direction::Prev => el.prev_sibling_or_token(), |
446 | }) | 361 | }) |
447 | } | 362 | } |
448 | 363 | ||
449 | pub fn next_token(&self) -> Option<SyntaxToken<'a>> { | 364 | pub fn next_token(&self) -> Option<SyntaxToken> { |
450 | self.0.next_token().map(SyntaxToken::from) | 365 | self.0.next_token().map(SyntaxToken) |
451 | } | 366 | } |
452 | 367 | ||
453 | pub fn prev_token(&self) -> Option<SyntaxToken<'a>> { | 368 | pub fn prev_token(&self) -> Option<SyntaxToken> { |
454 | self.0.prev_token().map(SyntaxToken::from) | 369 | self.0.prev_token().map(SyntaxToken) |
455 | } | 370 | } |
456 | 371 | ||
457 | pub(crate) fn replace_with(&self, new_token: GreenToken) -> GreenNode { | 372 | pub(crate) fn replace_with(&self, new_token: GreenToken) -> GreenNode { |
@@ -459,13 +374,25 @@ impl<'a> SyntaxToken<'a> { | |||
459 | } | 374 | } |
460 | } | 375 | } |
461 | 376 | ||
462 | #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)] | 377 | #[derive(Debug, PartialEq, Eq, Hash, Clone)] |
463 | pub enum SyntaxElement<'a> { | 378 | pub enum SyntaxElement { |
464 | Node(&'a SyntaxNode), | 379 | Node(SyntaxNode), |
465 | Token(SyntaxToken<'a>), | 380 | Token(SyntaxToken), |
466 | } | 381 | } |
467 | 382 | ||
468 | impl<'a> fmt::Display for SyntaxElement<'a> { | 383 | impl From<SyntaxNode> for SyntaxElement { |
384 | fn from(node: SyntaxNode) -> Self { | ||
385 | SyntaxElement::Node(node) | ||
386 | } | ||
387 | } | ||
388 | |||
389 | impl From<SyntaxToken> for SyntaxElement { | ||
390 | fn from(token: SyntaxToken) -> Self { | ||
391 | SyntaxElement::Token(token) | ||
392 | } | ||
393 | } | ||
394 | |||
395 | impl fmt::Display for SyntaxElement { | ||
469 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | 396 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
470 | match self { | 397 | match self { |
471 | SyntaxElement::Node(it) => fmt::Display::fmt(it, fmt), | 398 | SyntaxElement::Node(it) => fmt::Display::fmt(it, fmt), |
@@ -474,7 +401,14 @@ impl<'a> fmt::Display for SyntaxElement<'a> { | |||
474 | } | 401 | } |
475 | } | 402 | } |
476 | 403 | ||
477 | impl<'a> SyntaxElement<'a> { | 404 | impl SyntaxElement { |
405 | pub(crate) fn new(el: rowan::cursor::SyntaxElement) -> Self { | ||
406 | match el { | ||
407 | rowan::cursor::SyntaxElement::Node(it) => SyntaxElement::Node(SyntaxNode(it)), | ||
408 | rowan::cursor::SyntaxElement::Token(it) => SyntaxElement::Token(SyntaxToken(it)), | ||
409 | } | ||
410 | } | ||
411 | |||
478 | pub fn kind(&self) -> SyntaxKind { | 412 | pub fn kind(&self) -> SyntaxKind { |
479 | match self { | 413 | match self { |
480 | SyntaxElement::Node(it) => it.kind(), | 414 | SyntaxElement::Node(it) => it.kind(), |
@@ -482,99 +416,74 @@ impl<'a> SyntaxElement<'a> { | |||
482 | } | 416 | } |
483 | } | 417 | } |
484 | 418 | ||
485 | pub fn as_node(&self) -> Option<&'a SyntaxNode> { | 419 | pub fn as_node(&self) -> Option<&SyntaxNode> { |
486 | match self { | 420 | match self { |
487 | SyntaxElement::Node(node) => Some(*node), | 421 | SyntaxElement::Node(node) => Some(node), |
488 | SyntaxElement::Token(_) => None, | 422 | SyntaxElement::Token(_) => None, |
489 | } | 423 | } |
490 | } | 424 | } |
491 | 425 | ||
492 | pub fn as_token(&self) -> Option<SyntaxToken<'a>> { | 426 | pub fn as_token(&self) -> Option<&SyntaxToken> { |
493 | match self { | 427 | match self { |
494 | SyntaxElement::Node(_) => None, | 428 | SyntaxElement::Node(_) => None, |
495 | SyntaxElement::Token(token) => Some(*token), | 429 | SyntaxElement::Token(token) => Some(token), |
496 | } | 430 | } |
497 | } | 431 | } |
498 | 432 | ||
499 | pub fn next_sibling_or_token(&self) -> Option<SyntaxElement<'a>> { | 433 | pub fn next_sibling_or_token(&self) -> Option<SyntaxElement> { |
500 | match self { | 434 | match self { |
501 | SyntaxElement::Node(it) => it.next_sibling_or_token(), | 435 | SyntaxElement::Node(it) => it.next_sibling_or_token(), |
502 | SyntaxElement::Token(it) => it.next_sibling_or_token(), | 436 | SyntaxElement::Token(it) => it.next_sibling_or_token(), |
503 | } | 437 | } |
504 | } | 438 | } |
505 | 439 | ||
506 | pub fn prev_sibling_or_token(&self) -> Option<SyntaxElement<'a>> { | 440 | pub fn prev_sibling_or_token(&self) -> Option<SyntaxElement> { |
507 | match self { | 441 | match self { |
508 | SyntaxElement::Node(it) => it.prev_sibling_or_token(), | 442 | SyntaxElement::Node(it) => it.prev_sibling_or_token(), |
509 | SyntaxElement::Token(it) => it.prev_sibling_or_token(), | 443 | SyntaxElement::Token(it) => it.prev_sibling_or_token(), |
510 | } | 444 | } |
511 | } | 445 | } |
512 | 446 | ||
513 | pub fn ancestors(&self) -> impl Iterator<Item = &'a SyntaxNode> { | 447 | pub fn ancestors(&self) -> impl Iterator<Item = SyntaxNode> { |
514 | match self { | 448 | match self { |
515 | SyntaxElement::Node(it) => it, | 449 | SyntaxElement::Node(it) => it.clone(), |
516 | SyntaxElement::Token(it) => it.parent(), | 450 | SyntaxElement::Token(it) => it.parent(), |
517 | } | 451 | } |
518 | .ancestors() | 452 | .ancestors() |
519 | } | 453 | } |
520 | 454 | ||
521 | fn text_len(&self) -> TextUnit { | 455 | pub fn range(&self) -> TextRange { |
522 | match self { | 456 | match self { |
523 | SyntaxElement::Node(node) => node.0.green().text_len(), | 457 | SyntaxElement::Node(it) => it.range(), |
524 | SyntaxElement::Token(token) => TextUnit::of_str(token.0.text()), | 458 | SyntaxElement::Token(it) => it.range(), |
525 | } | ||
526 | } | ||
527 | } | ||
528 | |||
529 | impl<'a> From<rowan::SyntaxElement<'a>> for SyntaxElement<'a> { | ||
530 | fn from(el: rowan::SyntaxElement<'a>) -> Self { | ||
531 | match el { | ||
532 | rowan::SyntaxElement::Node(n) => SyntaxElement::Node(SyntaxNode::from_repr(n)), | ||
533 | rowan::SyntaxElement::Token(t) => SyntaxElement::Token(t.into()), | ||
534 | } | 459 | } |
535 | } | 460 | } |
536 | } | ||
537 | |||
538 | impl<'a> From<&'a SyntaxNode> for SyntaxElement<'a> { | ||
539 | fn from(node: &'a SyntaxNode) -> SyntaxElement<'a> { | ||
540 | SyntaxElement::Node(node) | ||
541 | } | ||
542 | } | ||
543 | |||
544 | impl<'a> From<SyntaxToken<'a>> for SyntaxElement<'a> { | ||
545 | fn from(token: SyntaxToken<'a>) -> SyntaxElement<'a> { | ||
546 | SyntaxElement::Token(token) | ||
547 | } | ||
548 | } | ||
549 | 461 | ||
550 | impl<'a> SyntaxElement<'a> { | 462 | fn text_len(&self) -> TextUnit { |
551 | pub fn range(&self) -> TextRange { | ||
552 | match self { | 463 | match self { |
553 | SyntaxElement::Node(it) => it.range(), | 464 | SyntaxElement::Node(node) => node.0.green().text_len(), |
554 | SyntaxElement::Token(it) => it.range(), | 465 | SyntaxElement::Token(token) => TextUnit::of_str(token.0.text()), |
555 | } | 466 | } |
556 | } | 467 | } |
557 | } | 468 | } |
558 | 469 | ||
559 | #[derive(Debug)] | 470 | #[derive(Clone, Debug)] |
560 | pub struct SyntaxNodeChildren<'a>(rowan::SyntaxNodeChildren<'a>); | 471 | pub struct SyntaxNodeChildren(rowan::cursor::SyntaxNodeChildren); |
561 | 472 | ||
562 | impl<'a> Iterator for SyntaxNodeChildren<'a> { | 473 | impl Iterator for SyntaxNodeChildren { |
563 | type Item = &'a SyntaxNode; | 474 | type Item = SyntaxNode; |
564 | 475 | fn next(&mut self) -> Option<SyntaxNode> { | |
565 | fn next(&mut self) -> Option<&'a SyntaxNode> { | 476 | self.0.next().map(SyntaxNode) |
566 | self.0.next().map(SyntaxNode::from_repr) | ||
567 | } | 477 | } |
568 | } | 478 | } |
569 | 479 | ||
570 | #[derive(Debug)] | 480 | #[derive(Clone, Debug)] |
571 | pub struct SyntaxElementChildren<'a>(rowan::SyntaxElementChildren<'a>); | 481 | pub struct SyntaxElementChildren(rowan::cursor::SyntaxElementChildren); |
572 | |||
573 | impl<'a> Iterator for SyntaxElementChildren<'a> { | ||
574 | type Item = SyntaxElement<'a>; | ||
575 | 482 | ||
576 | fn next(&mut self) -> Option<SyntaxElement<'a>> { | 483 | impl Iterator for SyntaxElementChildren { |
577 | self.0.next().map(SyntaxElement::from) | 484 | type Item = SyntaxElement; |
485 | fn next(&mut self) -> Option<SyntaxElement> { | ||
486 | self.0.next().map(SyntaxElement::new) | ||
578 | } | 487 | } |
579 | } | 488 | } |
580 | 489 | ||
@@ -601,7 +510,7 @@ impl SyntaxTreeBuilder { | |||
601 | if cfg!(debug_assertions) { | 510 | if cfg!(debug_assertions) { |
602 | crate::validation::validate_block_structure(&node); | 511 | crate::validation::validate_block_structure(&node); |
603 | } | 512 | } |
604 | Parse::new(node, errors) | 513 | Parse::new(node.0.green().clone(), errors) |
605 | } | 514 | } |
606 | 515 | ||
607 | pub fn token(&mut self, kind: SyntaxKind, text: SmolStr) { | 516 | pub fn token(&mut self, kind: SyntaxKind, text: SmolStr) { |