cstree/syntax/
resolved.rs

1//! Nodes, tokens, elements and their references which are guaranteed to belong to trees with
2//! associated [`Resolver`]s(lasso::Resolver).
3//!
4//! This means they can implement `Debug` and `Display` and be (de-)serializable by default.
5
6use std::{
7    fmt,
8    hash::Hash,
9    ops::{Deref, DerefMut},
10    sync::Arc as StdArc,
11};
12
13use text_size::{TextRange, TextSize};
14
15use crate::{
16    RawSyntaxKind,
17    Syntax,
18    green::GreenNode,
19    interning::{Resolver, TokenKey},
20    syntax::*,
21    traversal::*,
22    util::*,
23};
24
25/// Syntax tree node that is guaranteed to belong to a tree that contains an associated
26/// [`Resolver`](lasso::Resolver).
27/// # See also
28/// [`SyntaxNode`]
29/// [`SyntaxNode::new_root_with_resolver`]
30#[repr(transparent)]
31pub struct ResolvedNode<S: Syntax, D: 'static = ()> {
32    pub(super) syntax: SyntaxNode<S, D>,
33}
34
35impl<S: Syntax, D> ResolvedNode<S, D> {
36    /// # Safety:
37    /// `syntax` must belong to a tree that contains an associated inline resolver.
38    pub(super) unsafe fn coerce_ref(syntax: &SyntaxNode<S, D>) -> &Self {
39        unsafe { &*(syntax as *const _ as *const Self) }
40    }
41
42    /// Returns this node as a [`SyntaxNode`].
43    pub fn syntax(&self) -> &SyntaxNode<S, D> {
44        &self.syntax
45    }
46}
47
48impl<S: Syntax, D> Clone for ResolvedNode<S, D> {
49    fn clone(&self) -> Self {
50        Self {
51            syntax: self.syntax.clone(),
52        }
53    }
54}
55
56impl<S: Syntax, D> Deref for ResolvedNode<S, D> {
57    type Target = SyntaxNode<S, D>;
58
59    fn deref(&self) -> &Self::Target {
60        &self.syntax
61    }
62}
63
64impl<S: Syntax, D> DerefMut for ResolvedNode<S, D> {
65    fn deref_mut(&mut self) -> &mut Self::Target {
66        &mut self.syntax
67    }
68}
69
70impl<S: Syntax, D> PartialEq for ResolvedNode<S, D> {
71    fn eq(&self, other: &Self) -> bool {
72        self.syntax == other.syntax
73    }
74}
75impl<S: Syntax, D> Eq for ResolvedNode<S, D> {}
76impl<S: Syntax, D> Hash for ResolvedNode<S, D> {
77    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
78        self.syntax.hash(state);
79    }
80}
81
82/// Syntax tree token that is guaranteed to belong to a tree that contains an associated
83/// [`Resolver`](lasso::Resolver).
84/// # See also
85/// [`SyntaxToken`]
86#[repr(transparent)]
87pub struct ResolvedToken<S: Syntax, D: 'static = ()> {
88    syntax: SyntaxToken<S, D>,
89}
90
91impl<S: Syntax, D> ResolvedToken<S, D> {
92    /// # Safety:
93    /// `syntax` must belong to a tree that contains an associated inline resolver.
94    pub(super) unsafe fn coerce_ref(syntax: &SyntaxToken<S, D>) -> &Self {
95        unsafe { &*(syntax as *const _ as *const Self) }
96    }
97
98    /// Returns this token as a [`SyntaxToken`].
99    pub fn syntax(&self) -> &SyntaxToken<S, D> {
100        &self.syntax
101    }
102}
103
104impl<S: Syntax, D> Clone for ResolvedToken<S, D> {
105    fn clone(&self) -> Self {
106        Self {
107            syntax: self.syntax.clone(),
108        }
109    }
110}
111
112impl<S: Syntax, D> Deref for ResolvedToken<S, D> {
113    type Target = SyntaxToken<S, D>;
114
115    fn deref(&self) -> &Self::Target {
116        &self.syntax
117    }
118}
119
120impl<S: Syntax, D> DerefMut for ResolvedToken<S, D> {
121    fn deref_mut(&mut self) -> &mut Self::Target {
122        &mut self.syntax
123    }
124}
125
126impl<S: Syntax, D> PartialEq for ResolvedToken<S, D> {
127    fn eq(&self, other: &Self) -> bool {
128        self.syntax == other.syntax
129    }
130}
131impl<S: Syntax, D> Eq for ResolvedToken<S, D> {}
132impl<S: Syntax, D> Hash for ResolvedToken<S, D> {
133    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
134        self.syntax.hash(state);
135    }
136}
137
138/// An element of the tree that is guaranteed to belong to a tree that contains an associated
139/// [`Resolver`](lasso::Resolver), can be either a node or a token.
140///
141/// See also [`SyntaxElement`].
142pub type ResolvedElement<S, D = ()> = NodeOrToken<ResolvedNode<S, D>, ResolvedToken<S, D>>;
143
144impl<S: Syntax, D> From<ResolvedNode<S, D>> for ResolvedElement<S, D> {
145    fn from(node: ResolvedNode<S, D>) -> ResolvedElement<S, D> {
146        NodeOrToken::Node(node)
147    }
148}
149
150impl<S: Syntax, D> From<ResolvedToken<S, D>> for ResolvedElement<S, D> {
151    fn from(token: ResolvedToken<S, D>) -> ResolvedElement<S, D> {
152        NodeOrToken::Token(token)
153    }
154}
155
156impl<S: Syntax, D> ResolvedElement<S, D> {
157    #[allow(missing_docs)]
158    pub fn display(&self, resolver: &impl Resolver<TokenKey>) -> String {
159        match self {
160            NodeOrToken::Node(it) => it.display(resolver),
161            NodeOrToken::Token(it) => it.display(resolver),
162        }
163    }
164}
165
166/// A reference to an element of the tree that is guaranteed to belong to a tree that contains an
167/// associated [`Resolver`](lasso::Resolver), can be either a reference to a node or one to a token.
168/// # See also
169/// [`SyntaxElementRef`]
170pub type ResolvedElementRef<'a, S, D = ()> = NodeOrToken<&'a ResolvedNode<S, D>, &'a ResolvedToken<S, D>>;
171
172impl<'a, S: Syntax, D> ResolvedElementRef<'a, S, D> {
173    /// # Safety:
174    /// `syntax` must belong to a tree that contains an associated inline resolver.
175    pub(super) unsafe fn coerce_ref(syntax: SyntaxElementRef<'a, S, D>) -> Self {
176        unsafe {
177            match syntax {
178                NodeOrToken::Node(node) => Self::Node(ResolvedNode::coerce_ref(node)),
179                NodeOrToken::Token(token) => Self::Token(ResolvedToken::coerce_ref(token)),
180            }
181        }
182    }
183}
184
185impl<'a, S: Syntax, D> From<&'a ResolvedNode<S, D>> for ResolvedElementRef<'a, S, D> {
186    fn from(node: &'a ResolvedNode<S, D>) -> Self {
187        NodeOrToken::Node(node)
188    }
189}
190
191impl<'a, S: Syntax, D> From<&'a ResolvedToken<S, D>> for ResolvedElementRef<'a, S, D> {
192    fn from(token: &'a ResolvedToken<S, D>) -> Self {
193        NodeOrToken::Token(token)
194    }
195}
196
197impl<'a, S: Syntax, D> From<&'a ResolvedElement<S, D>> for ResolvedElementRef<'a, S, D> {
198    fn from(element: &'a ResolvedElement<S, D>) -> Self {
199        match element {
200            NodeOrToken::Node(it) => Self::Node(it),
201            NodeOrToken::Token(it) => Self::Token(it),
202        }
203    }
204}
205
206impl<S: Syntax, D> ResolvedNode<S, D> {
207    /// Uses the resolver associated with this tree to return an efficient representation of all
208    /// source text covered by this node, i.e. the combined text of all token leafs of the subtree
209    /// originating in this node.
210    #[inline]
211    pub fn text(&self) -> SyntaxText<'_, '_, dyn Resolver<TokenKey>, S, D> {
212        SyntaxText::new(self, &**self.resolver())
213    }
214}
215
216impl<S: Syntax, D> fmt::Debug for ResolvedNode<S, D> {
217    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
218        self.write_debug(&**self.resolver(), f, f.alternate())
219    }
220}
221
222impl<S: Syntax, D> fmt::Display for ResolvedNode<S, D> {
223    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
224        self.write_display(&**self.resolver(), f)
225    }
226}
227
228impl<S: Syntax, D> ResolvedToken<S, D> {
229    /// Uses the resolver associated with this tree to return the source text of this token.
230    #[inline]
231    pub fn text(&self) -> &str {
232        // one of the two must be present upon construction
233        self.static_text()
234            .or_else(|| self.green().text(&**self.resolver()))
235            .unwrap()
236    }
237}
238
239impl<S: Syntax, D> fmt::Debug for ResolvedToken<S, D> {
240    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
241        self.write_debug(&**self.resolver(), f)
242    }
243}
244
245impl<S: Syntax, D> fmt::Display for ResolvedToken<S, D> {
246    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
247        self.write_display(&**self.resolver(), f)
248    }
249}
250
251#[cfg(feature = "serialize")]
252impl<S, D> ResolvedNode<S, D>
253where
254    S: Syntax,
255{
256    /// Return an anonymous object that can be used to serialize this node,
257    /// including the data for each node.
258    pub fn as_serialize_with_data(&self) -> impl serde::Serialize + '_
259    where
260        D: serde::Serialize,
261    {
262        crate::serde_impls::SerializeWithData {
263            node:     self,
264            resolver: self.resolver().as_ref(),
265        }
266    }
267}
268
269/* It follows: wrapping all _traversal_ methods so they return `ResolvedXY`s */
270macro_rules! forward {
271    // safety: if we're starting from a `ResolvedXY`, then the tree must have a resolver
272    ($e:expr) => {
273        ($e).map(|e| unsafe { Self::coerce_ref(e) })
274    };
275}
276
277macro_rules! forward_as_elem {
278    // safety: if we're starting from a `ResolvedXY`, then the tree must have a resolver
279    ($e:expr) => {
280        ($e).map(|e| unsafe { ResolvedElementRef::coerce_ref(e) })
281    };
282}
283
284macro_rules! forward_token {
285    // safety: if we're starting from a `ResolvedXY`, then the tree must have a resolver
286    ($e:expr) => {
287        ($e).map(|e| unsafe { ResolvedToken::coerce_ref(e) })
288    };
289}
290
291macro_rules! forward_node {
292    // safety: if we're starting from a `ResolvedXY`, then the tree must have a resolver
293    ($e:expr) => {
294        ($e).map(|e| unsafe { ResolvedNode::coerce_ref(e) })
295    };
296}
297
298impl<S: Syntax, D> ResolvedNode<S, D> {
299    /// Returns the [`Resolver`] associated with this tree.
300    pub fn resolver(&self) -> &StdArc<dyn Resolver<TokenKey>> {
301        self.syntax.resolver().unwrap()
302    }
303
304    /// See [`SyntaxNode::new_root_with_resolver`].
305    #[inline]
306    pub fn new_root_with_resolver(green: GreenNode, resolver: impl Resolver<TokenKey> + 'static) -> Self {
307        SyntaxNode::new_root_with_resolver(green, resolver)
308    }
309
310    /// Always returns `Some(self)`.
311    ///
312    /// This method mostly exists to allow the convenience of being agnostic over [`SyntaxNode`] vs [`ResolvedNode`].
313    #[inline]
314    pub fn try_resolved(&self) -> Option<&ResolvedNode<S, D>> {
315        Some(self)
316    }
317
318    /// Always returns `self`.
319    ///
320    /// This method mostly exists to allow the convenience of being agnostic over [`SyntaxNode`] vs [`ResolvedNode`].
321    #[inline]
322    pub fn resolved(&self) -> &ResolvedNode<S, D> {
323        self
324    }
325
326    /// The root of the tree this node belongs to.
327    ///
328    /// If this node is the root, returns `self`.
329    #[inline]
330    pub fn root(&self) -> &SyntaxNode<S, D> {
331        unsafe { Self::coerce_ref(self.syntax.root()) }
332    }
333
334    /// The parent node of this node, except if this node is the root.
335    #[inline]
336    pub fn parent(&self) -> Option<&Self> {
337        forward!(self.syntax.parent())
338    }
339
340    /// Returns an iterator along the chain of parents of this node.
341    #[inline]
342    pub fn ancestors(&self) -> impl Iterator<Item = &Self> {
343        forward!(self.syntax.ancestors())
344    }
345
346    /// Returns an iterator over all nodes that are children of this node.
347    ///
348    /// If you want to also consider leafs, see [`children_with_tokens`](ResolvedNode::children_with_tokens).
349    #[inline]
350    pub fn children(&self) -> impl Iterator<Item = &Self> {
351        forward!(self.syntax.children())
352    }
353
354    /// Returns an iterator over child elements of this node, including tokens.
355    #[inline]
356    pub fn children_with_tokens(&self) -> impl Iterator<Item = ResolvedElementRef<'_, S, D>> {
357        forward_as_elem!(self.syntax.children_with_tokens())
358    }
359
360    /// The first child node of this node, if any.
361    ///
362    /// If you want to also consider leafs, see [`first_child_or_token`](ResolvedNode::first_child_or_token).
363    #[inline]
364    pub fn first_child(&self) -> Option<&ResolvedNode<S, D>> {
365        forward!(self.syntax.first_child())
366    }
367
368    /// The first child element of this node, if any, including tokens.
369    #[inline]
370    pub fn first_child_or_token(&self) -> Option<ResolvedElementRef<'_, S, D>> {
371        forward_as_elem!(self.syntax.first_child_or_token())
372    }
373
374    /// The last child node of this node, if any.
375    ///
376    /// If you want to also consider leafs, see [`last_child_or_token`](ResolvedNode::last_child_or_token).
377    #[inline]
378    pub fn last_child(&self) -> Option<&ResolvedNode<S, D>> {
379        forward!(self.syntax.last_child())
380    }
381
382    /// The last child element of this node, if any, including tokens.
383    #[inline]
384    pub fn last_child_or_token(&self) -> Option<ResolvedElementRef<'_, S, D>> {
385        forward_as_elem!(self.syntax.last_child_or_token())
386    }
387
388    /// The first child node of this node starting at the (n + 1)-st, if any.
389    /// Note that even if this method returns `Some`, the contained node may not actually be the (n +
390    /// 1)-st child, but the next child from there that is a node.
391    ///
392    /// If you want to also consider leafs, see [`next_child_or_token_after`](ResolvedNode::next_child_or_token_after).
393    #[inline]
394    pub fn next_child_after(&self, n: usize, offset: TextSize) -> Option<&ResolvedNode<S, D>> {
395        forward!(self.syntax.next_child_after(n, offset))
396    }
397
398    /// The first child element of this node starting at the (n + 1)-st, if any.
399    /// If this method returns `Some`, the contained node is the (n + 1)-st child of this node.
400    #[inline]
401    pub fn next_child_or_token_after(&self, n: usize, offset: TextSize) -> Option<ResolvedElementRef<'_, S, D>> {
402        forward_as_elem!(self.syntax.next_child_or_token_after(n, offset))
403    }
404
405    /// The last child node of this node up to the nth, if any.
406    /// Note that even if this method returns `Some`, the contained node may not actually be the (n -
407    /// 1)-st child, but the previous child from there that is a node.
408    ///
409    /// If you want to also consider leafs, see
410    /// [`prev_child_or_token_before`](ResolvedNode::prev_child_or_token_before).
411    #[inline]
412    pub fn prev_child_before(&self, n: usize, offset: TextSize) -> Option<&ResolvedNode<S, D>> {
413        forward!(self.syntax.prev_child_before(n, offset))
414    }
415
416    /// The last child node of this node up to the nth, if any.
417    /// If this method returns `Some`, the contained node is the (n - 1)-st child.
418    #[inline]
419    pub fn prev_child_or_token_before(&self, n: usize, offset: TextSize) -> Option<ResolvedElementRef<'_, S, D>> {
420        forward_as_elem!(self.syntax.prev_child_or_token_before(n, offset))
421    }
422
423    /// The node to the right of this one, i.e. the next child node (!) of this node's parent after this node.
424    ///
425    /// If you want to also consider leafs, see [`next_sibling_or_token`](ResolvedNode::next_sibling_or_token).
426    #[inline]
427    pub fn next_sibling(&self) -> Option<&ResolvedNode<S, D>> {
428        forward!(self.syntax.next_sibling())
429    }
430
431    /// The tree element to the right of this one, i.e. the next child of this node's parent after this node.
432    #[inline]
433    pub fn next_sibling_or_token(&self) -> Option<ResolvedElementRef<'_, S, D>> {
434        forward_as_elem!(self.syntax.next_sibling_or_token())
435    }
436
437    /// The node to the left of this one, i.e. the previous child node (!) of this node's parent before this node.
438    ///
439    /// If you want to also consider leafs, see [`prev_sibling_or_token`](ResolvedNode::prev_sibling_or_token).
440    #[inline]
441    pub fn prev_sibling(&self) -> Option<&ResolvedNode<S, D>> {
442        forward!(self.syntax.prev_sibling())
443    }
444
445    /// The tree element to the left of this one, i.e. the previous child of this node's parent before this node.
446    #[inline]
447    pub fn prev_sibling_or_token(&self) -> Option<ResolvedElementRef<'_, S, D>> {
448        forward_as_elem!(self.syntax.prev_sibling_or_token())
449    }
450
451    /// Return the leftmost token in the subtree of this node
452    #[inline]
453    pub fn first_token(&self) -> Option<&ResolvedToken<S, D>> {
454        forward_token!(self.syntax.first_token())
455    }
456
457    /// Return the rightmost token in the subtree of this node
458    #[inline]
459    pub fn last_token(&self) -> Option<&ResolvedToken<S, D>> {
460        forward_token!(self.syntax.last_token())
461    }
462
463    /// Returns an iterator over all sibling nodes of this node in the given `direction`, i.e. all of
464    /// this node's parent's child nodes (!) from this node on to the left or the right. The first
465    /// item in the iterator will always be this node.
466    ///
467    /// If you want to also consider leafs, see [`siblings_with_tokens`](ResolvedNode::siblings_with_tokens).
468    #[inline]
469    pub fn siblings(&self, direction: Direction) -> impl Iterator<Item = &ResolvedNode<S, D>> {
470        forward!(self.syntax.siblings(direction))
471    }
472
473    /// Returns an iterator over all siblings of this node in the given `direction`, i.e. all of this
474    /// node's parent's children from this node on to the left or the right.
475    /// The first item in the iterator will always be this node.
476    #[inline]
477    pub fn siblings_with_tokens(&self, direction: Direction) -> impl Iterator<Item = ResolvedElementRef<'_, S, D>> {
478        forward_as_elem!(self.syntax.siblings_with_tokens(direction))
479    }
480
481    /// Returns an iterator over all nodes (!) in the subtree starting at this node, including this node.
482    ///
483    /// If you want to also consider leafs, see [`descendants_with_tokens`](ResolvedNode::descendants_with_tokens).
484    #[inline]
485    pub fn descendants(&self) -> impl Iterator<Item = &ResolvedNode<S, D>> {
486        forward!(self.syntax.descendants())
487    }
488
489    /// Returns an iterator over all elements in the subtree starting at this node, including this node.
490    #[inline]
491    pub fn descendants_with_tokens(&self) -> impl Iterator<Item = ResolvedElementRef<'_, S, D>> {
492        forward_as_elem!(self.syntax.descendants_with_tokens())
493    }
494
495    /// Traverse the subtree rooted at the current node (including the current
496    /// node) in preorder, excluding tokens.
497    #[inline(always)]
498    pub fn preorder(&self) -> impl Iterator<Item = WalkEvent<&ResolvedNode<S, D>>> {
499        self.syntax
500            .preorder()
501            .map(|event| event.map(|node| unsafe { Self::coerce_ref(node) }))
502    }
503
504    /// Traverse the subtree rooted at the current node (including the current
505    /// node) in preorder, including tokens.
506    #[inline(always)]
507    pub fn preorder_with_tokens(&self) -> impl Iterator<Item = WalkEvent<ResolvedElementRef<'_, S, D>>> {
508        self.syntax
509            .preorder_with_tokens()
510            .map(|event| event.map(|elem| unsafe { ResolvedElementRef::coerce_ref(elem) }))
511    }
512
513    /// Find a token in the subtree corresponding to this node, which covers the offset.
514    /// Precondition: offset must be withing node's range.
515    pub fn token_at_offset(&self, offset: TextSize) -> TokenAtOffset<ResolvedToken<S, D>> {
516        self.syntax
517            .token_at_offset(offset)
518            .map(|token| ResolvedToken { syntax: token })
519    }
520
521    /// Return the deepest node or token in the current subtree that fully
522    /// contains the range. If the range is empty and is contained in two leaf
523    /// nodes, either one can be returned. Precondition: range must be contained
524    /// withing the current node
525    pub fn covering_element(&self, range: TextRange) -> ResolvedElementRef<'_, S, D> {
526        unsafe { ResolvedElementRef::coerce_ref(self.syntax.covering_element(range)) }
527    }
528}
529
530impl<S: Syntax, D> ResolvedToken<S, D> {
531    /// Returns the [`Resolver`] associated with this tree.
532    pub fn resolver(&self) -> &StdArc<dyn Resolver<TokenKey>> {
533        self.syntax.resolver().unwrap()
534    }
535
536    /// Always returns `Some(self)`.
537    ///
538    /// This method mostly exists to allow the convenience of being agnostic over [`SyntaxToken`] vs [`ResolvedToken`].
539    #[inline]
540    pub fn try_resolved(&self) -> Option<&ResolvedToken<S, D>> {
541        Some(self)
542    }
543
544    /// Always returns `self`.
545    ///
546    /// This method mostly exists to allow the convenience of being agnostic over [`SyntaxToken`] vs [`ResolvedToken`].
547    #[inline]
548    pub fn resolved(&self) -> &ResolvedToken<S, D> {
549        self
550    }
551
552    /// The parent node of this token.
553    #[inline]
554    pub fn parent(&self) -> &ResolvedNode<S, D> {
555        unsafe { ResolvedNode::coerce_ref(self.syntax.parent()) }
556    }
557
558    /// Returns an iterator along the chain of parents of this token.
559    #[inline]
560    pub fn ancestors(&self) -> impl Iterator<Item = &ResolvedNode<S, D>> {
561        forward_node!(self.syntax.ancestors())
562    }
563
564    /// The tree element to the right of this one, i.e. the next child of this token's parent after this token.
565    #[inline]
566    pub fn next_sibling_or_token(&self) -> Option<ResolvedElementRef<'_, S, D>> {
567        forward_as_elem!(self.syntax.next_sibling_or_token())
568    }
569
570    /// The tree element to the left of this one, i.e. the previous child of this token's parent after this token.
571    #[inline]
572    pub fn prev_sibling_or_token(&self) -> Option<ResolvedElementRef<'_, S, D>> {
573        forward_as_elem!(self.syntax.prev_sibling_or_token())
574    }
575
576    /// Returns an iterator over all siblings of this token in the given `direction`, i.e. all of this
577    /// token's parent's children from this token on to the left or the right.
578    /// The first item in the iterator will always be this token.
579    #[inline]
580    pub fn siblings_with_tokens(&self, direction: Direction) -> impl Iterator<Item = ResolvedElementRef<'_, S, D>> {
581        forward_as_elem!(self.syntax.siblings_with_tokens(direction))
582    }
583
584    /// Returns the next token in the tree.
585    /// This is not necessary a direct sibling of this token, but will always be further right in the tree.
586    pub fn next_token(&self) -> Option<&ResolvedToken<S, D>> {
587        forward!(self.syntax.next_token())
588    }
589
590    /// Returns the previous token in the tree.
591    /// This is not necessary a direct sibling of this token, but will always be further left in the tree.
592    pub fn prev_token(&self) -> Option<&ResolvedToken<S, D>> {
593        forward!(self.syntax.prev_token())
594    }
595}
596
597impl<S: Syntax, D> ResolvedElement<S, D> {
598    /// The range this element covers in the source text, in bytes.
599    #[inline]
600    pub fn text_range(&self) -> TextRange {
601        match self {
602            NodeOrToken::Node(it) => it.text_range(),
603            NodeOrToken::Token(it) => it.text_range(),
604        }
605    }
606
607    /// The internal representation of the kind of this element.
608    #[inline]
609    pub fn syntax_kind(&self) -> RawSyntaxKind {
610        match self {
611            NodeOrToken::Node(it) => it.syntax_kind(),
612            NodeOrToken::Token(it) => it.syntax_kind(),
613        }
614    }
615
616    /// The kind of this element in terms of your language.
617    #[inline]
618    pub fn kind(&self) -> S {
619        match self {
620            NodeOrToken::Node(it) => it.kind(),
621            NodeOrToken::Token(it) => it.kind(),
622        }
623    }
624
625    /// The parent node of this element, except if this element is the root.
626    #[inline]
627    pub fn parent(&self) -> Option<&ResolvedNode<S, D>> {
628        match self {
629            NodeOrToken::Node(it) => it.parent(),
630            NodeOrToken::Token(it) => Some(it.parent()),
631        }
632    }
633
634    /// Returns an iterator along the chain of parents of this node.
635    #[inline]
636    pub fn ancestors(&self) -> impl Iterator<Item = &ResolvedNode<S, D>> {
637        match self {
638            NodeOrToken::Node(it) => it.ancestors(),
639            NodeOrToken::Token(it) => it.parent().ancestors(),
640        }
641    }
642
643    /// Return the leftmost token in the subtree of this element.
644    #[inline]
645    pub fn first_token(&self) -> Option<&ResolvedToken<S, D>> {
646        match self {
647            NodeOrToken::Node(it) => it.first_token(),
648            NodeOrToken::Token(it) => Some(it),
649        }
650    }
651
652    /// Return the rightmost token in the subtree of this element.
653    #[inline]
654    pub fn last_token(&self) -> Option<&ResolvedToken<S, D>> {
655        match self {
656            NodeOrToken::Node(it) => it.last_token(),
657            NodeOrToken::Token(it) => Some(it),
658        }
659    }
660
661    /// The tree element to the right of this one, i.e. the next child of this element's parent after this element.
662    #[inline]
663    pub fn next_sibling_or_token(&self) -> Option<ResolvedElementRef<'_, S, D>> {
664        match self {
665            NodeOrToken::Node(it) => it.next_sibling_or_token(),
666            NodeOrToken::Token(it) => it.next_sibling_or_token(),
667        }
668    }
669
670    /// The tree element to the left of this one, i.e. the previous child of this element's parent after this element.
671    #[inline]
672    pub fn prev_sibling_or_token(&self) -> Option<ResolvedElementRef<'_, S, D>> {
673        match self {
674            NodeOrToken::Node(it) => it.prev_sibling_or_token(),
675            NodeOrToken::Token(it) => it.prev_sibling_or_token(),
676        }
677    }
678}
679
680impl<'a, S: Syntax, D> ResolvedElementRef<'a, S, D> {
681    /// The range this element covers in the source text, in bytes.
682    #[inline]
683    pub fn text_range(&self) -> TextRange {
684        match self {
685            NodeOrToken::Node(it) => it.text_range(),
686            NodeOrToken::Token(it) => it.text_range(),
687        }
688    }
689
690    /// The internal representation of the kind of this element.
691    #[inline]
692    pub fn syntax_kind(&self) -> RawSyntaxKind {
693        match self {
694            NodeOrToken::Node(it) => it.syntax_kind(),
695            NodeOrToken::Token(it) => it.syntax_kind(),
696        }
697    }
698
699    /// The kind of this element in terms of your language.
700    #[inline]
701    pub fn kind(&self) -> S {
702        match self {
703            NodeOrToken::Node(it) => it.kind(),
704            NodeOrToken::Token(it) => it.kind(),
705        }
706    }
707
708    /// The parent node of this element, except if this element is the root.
709    #[inline]
710    pub fn parent(&self) -> Option<&'a ResolvedNode<S, D>> {
711        match self {
712            NodeOrToken::Node(it) => it.parent(),
713            NodeOrToken::Token(it) => Some(it.parent()),
714        }
715    }
716
717    /// Returns an iterator along the chain of parents of this node.
718    #[inline]
719    pub fn ancestors(&self) -> impl Iterator<Item = &'a ResolvedNode<S, D>> + use<'a, S, D> {
720        match self {
721            NodeOrToken::Node(it) => it.ancestors(),
722            NodeOrToken::Token(it) => it.parent().ancestors(),
723        }
724    }
725
726    /// Return the leftmost token in the subtree of this element.
727    #[inline]
728    pub fn first_token(&self) -> Option<&'a ResolvedToken<S, D>> {
729        match self {
730            NodeOrToken::Node(it) => it.first_token(),
731            NodeOrToken::Token(it) => Some(it),
732        }
733    }
734
735    /// Return the rightmost token in the subtree of this element.
736    #[inline]
737    pub fn last_token(&self) -> Option<&'a ResolvedToken<S, D>> {
738        match self {
739            NodeOrToken::Node(it) => it.last_token(),
740            NodeOrToken::Token(it) => Some(it),
741        }
742    }
743
744    /// The tree element to the right of this one, i.e. the next child of this element's parent after this element.
745    #[inline]
746    pub fn next_sibling_or_token(&self) -> Option<ResolvedElementRef<'a, S, D>> {
747        match self {
748            NodeOrToken::Node(it) => it.next_sibling_or_token(),
749            NodeOrToken::Token(it) => it.next_sibling_or_token(),
750        }
751    }
752
753    /// The tree element to the left of this one, i.e. the previous child of this element's parent after this element.
754    #[inline]
755    pub fn prev_sibling_or_token(&self) -> Option<ResolvedElementRef<'a, S, D>> {
756        match self {
757            NodeOrToken::Node(it) => it.prev_sibling_or_token(),
758            NodeOrToken::Token(it) => it.prev_sibling_or_token(),
759        }
760    }
761}