type_sitter_lib/node/
mod.rs1use crate::raw;
2#[cfg(feature = "yak-sitter")]
3use crate::PointRange;
4use crate::{InputEdit, Point, Range};
5pub use cursor::*;
6pub use incorrect_kind::*;
7pub use parser::*;
8use std::fmt::Debug;
9use std::hash::Hash;
10#[cfg(not(feature = "yak-sitter"))]
11use std::str::Utf8Error;
12pub use tree::*;
13pub use unwrap_and_flatten_multi::*;
14pub use wrappers::*;
15
16mod cursor;
17mod incorrect_kind;
19mod parser;
20mod tree;
21mod unwrap_and_flatten_multi;
23mod wrappers;
25
26pub trait Node<'tree>: Debug + Clone + Copy + PartialEq + Eq + Hash {
36 type WithLifetime<'a>: Node<'a>;
38
39 const KIND: &'static str;
45
46 fn try_from_raw(node: raw::Node<'tree>) -> NodeResult<'tree, Self>;
50
51 #[inline]
56 unsafe fn from_raw_unchecked(node: raw::Node<'tree>) -> Self {
57 Self::try_from_raw(node).expect("from_raw_unchecked failed")
58 }
59
60 fn raw(&self) -> &raw::Node<'tree>;
64
65 fn raw_mut(&mut self) -> &mut raw::Node<'tree>;
69
70 fn into_raw(self) -> raw::Node<'tree>;
74
75 #[inline]
79 fn upcast(self) -> UntypedNode<'tree> {
80 UntypedNode::new(self.into_raw())
81 }
82
83 #[cfg(feature = "yak-sitter")]
86 fn text(&self) -> &'tree str {
87 self.raw().text()
88 }
89
90 #[inline]
92 #[cfg(not(feature = "yak-sitter"))]
93 fn utf8_text<'a>(&self, source: &'a [u8]) -> Result<&'a str, Utf8Error> {
94 self.raw().utf8_text(source)
95 }
96
97 #[inline]
99 #[cfg(not(feature = "yak-sitter"))]
100 fn utf16_text<'a>(&self, source: &'a [u16]) -> &'a [u16] {
101 self.raw().utf16_text(source)
102 }
103
104 #[inline]
108 fn prefixes(&self) -> impl Iterator<Item = UntypedNode<'tree>> {
109 Prefixes::new(*self.raw())
110 }
111
112 #[inline]
116 fn suffixes(&self) -> impl Iterator<Item = UntypedNode<'tree>> {
117 Suffixes::new(*self.raw())
118 }
119
120 #[inline]
122 fn walk(&self) -> TreeCursor<'tree> {
123 TreeCursor(self.raw().walk())
124 }
125
126 #[inline]
128 fn parent(&self) -> Option<UntypedNode<'tree>> {
129 self.raw().parent().map(UntypedNode::from)
130 }
131
132 #[inline]
134 fn next_named_sibling(&self) -> Option<UntypedNode<'tree>> {
135 self.raw().next_named_sibling().map(UntypedNode::from)
136 }
137
138 #[inline]
140 fn prev_named_sibling(&self) -> Option<UntypedNode<'tree>> {
141 self.raw().prev_named_sibling().map(UntypedNode::from)
142 }
143
144 #[inline]
146 fn named_child_count(&self) -> usize {
147 self.raw().named_child_count()
148 }
149
150 #[inline]
152 fn to_sexp(&self) -> String {
153 self.raw().to_sexp()
154 }
155
156 #[inline]
158 fn kind(&self) -> &'static str {
159 self.raw().kind()
160 }
161
162 #[inline]
164 fn is_named(&self) -> bool {
165 self.raw().is_named()
166 }
167
168 #[inline]
170 fn has_changes(&self) -> bool {
171 self.raw().has_changes()
172 }
173
174 #[inline]
177 fn has_error(&self) -> bool {
178 self.raw().has_error()
179 }
180
181 #[inline]
183 fn start_byte(&self) -> usize {
184 self.raw().start_byte()
185 }
186
187 #[inline]
189 fn end_byte(&self) -> usize {
190 self.raw().end_byte()
191 }
192
193 #[inline]
195 fn start_position(&self) -> Point {
196 self.raw().start_position()
197 }
198
199 #[inline]
201 fn end_position(&self) -> Point {
202 self.raw().end_position()
203 }
204
205 #[inline]
207 fn range(&self) -> Range {
208 self.raw().range()
209 }
210
211 #[inline]
213 fn byte_range(&self) -> std::ops::Range<usize> {
214 self.raw().byte_range()
215 }
216
217 #[inline]
219 #[cfg(feature = "yak-sitter")]
220 fn position_range(&self) -> PointRange {
221 self.raw().position_range()
222 }
223
224 #[inline]
227 fn edit(&mut self, edit: &InputEdit) {
228 self.raw_mut().edit(edit)
229 }
230 }
232
233pub trait HasOptionalChild<'tree>: Node<'tree> {
239 type Child: Node<'tree>;
240
241 #[inline]
243 fn child(&self) -> Option<NodeResult<'tree, Self::Child>> {
244 optional_child(self)
245 }
246}
247
248pub trait HasChild<'tree>: Node<'tree> {
254 type Child: Node<'tree>;
255
256 #[inline]
258 fn child(&self) -> NodeResult<'tree, Self::Child> {
259 optional_child(self).expect(
260 "required child not present, there should at least be a MISSING node in its place",
261 )
262 }
263}
264
265#[inline]
266fn optional_child<'tree, Child: Node<'tree>>(
267 this: &impl Node<'tree>,
268) -> Option<NodeResult<'tree, Child>> {
269 (0..this.raw().named_child_count())
270 .map(|i| this.raw().named_child(i).unwrap())
271 .filter(|n| !n.is_extra())
272 .next()
273 .map(Child::try_from_raw)
274}
275
276pub trait HasChildren<'tree>: Node<'tree> {
282 type Child: Node<'tree>;
283
284 #[inline]
286 fn children<'a>(
287 &self,
288 c: &'a mut TreeCursor<'tree>,
289 ) -> impl Iterator<Item = NodeResult<'tree, Self::Child>> + 'a
290 where
291 Self: 'a,
292 {
293 self.raw()
294 .named_children(&mut c.0)
295 .filter(|n| !n.is_extra())
296 .map(Self::Child::try_from_raw)
297 }
298}
299
300struct Prefixes<'tree> {
301 cursor: raw::TreeCursor<'tree>,
302 end: raw::Node<'tree>,
303}
304
305struct Suffixes<'tree> {
306 cursor: raw::TreeCursor<'tree>,
307}
308
309impl<'tree> Prefixes<'tree> {
310 fn new(raw: raw::Node<'tree>) -> Self {
311 let Some(parent) = raw.parent() else {
312 return Self {
313 cursor: raw.walk(),
314 end: raw,
315 };
316 };
317
318 let mut cursor = parent.walk();
319 cursor.goto_first_child();
320
321 'outer: loop {
322 if cursor.node() == raw {
323 break Self {
324 cursor: raw.walk(),
325 end: raw,
326 };
327 }
328
329 if cursor.node().is_extra() {
330 let mut cursor2 = cursor.clone();
331 while cursor2.node().is_extra() {
332 if !cursor2.goto_next_sibling() {
333 break 'outer Self {
334 cursor: raw.walk(),
335 end: raw,
336 };
337 }
338
339 if cursor2.node() == raw {
340 break 'outer Self { cursor, end: raw };
341 }
342 }
343 }
344
345 if !cursor.goto_next_sibling() {
346 break Self {
347 cursor: raw.walk(),
348 end: raw,
349 };
350 }
351 }
352 }
353}
354
355impl<'tree> Suffixes<'tree> {
356 fn new(raw: raw::Node<'tree>) -> Self {
357 let Some(parent) = raw.parent() else {
358 return Self { cursor: raw.walk() };
359 };
360
361 let mut cursor = parent.walk();
362 cursor.goto_first_child();
363
364 while cursor.node() != raw {
365 let next = cursor.goto_next_sibling();
366 assert!(next, "node not found in parent");
367 }
368
369 Self { cursor }
370 }
371}
372
373impl<'tree> Iterator for Prefixes<'tree> {
374 type Item = UntypedNode<'tree>;
375
376 fn next(&mut self) -> Option<Self::Item> {
377 if self.cursor.node() == self.end {
378 return None;
379 }
380
381 let result = UntypedNode::new(self.cursor.node());
382 debug_assert!(
383 self.cursor.node().is_extra(),
384 "node before our iteration target isn't an extra, but we thought it would be"
385 );
386 let next = self.cursor.goto_next_sibling();
387 assert!(
388 next,
389 "node (that we've been iterating the prefixes of) not found in parent"
390 );
391 Some(result)
392 }
393}
394
395impl<'tree> Iterator for Suffixes<'tree> {
396 type Item = UntypedNode<'tree>;
397
398 fn next(&mut self) -> Option<Self::Item> {
399 if !self.cursor.goto_next_sibling() || !self.cursor.node().is_extra() {
400 return None;
401 }
402
403 Some(UntypedNode::new(self.cursor.node()))
404 }
405}