1use 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#[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 pub(super) unsafe fn coerce_ref(syntax: &SyntaxNode<S, D>) -> &Self {
39 unsafe { &*(syntax as *const _ as *const Self) }
40 }
41
42 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#[repr(transparent)]
87pub struct ResolvedToken<S: Syntax, D: 'static = ()> {
88 syntax: SyntaxToken<S, D>,
89}
90
91impl<S: Syntax, D> ResolvedToken<S, D> {
92 pub(super) unsafe fn coerce_ref(syntax: &SyntaxToken<S, D>) -> &Self {
95 unsafe { &*(syntax as *const _ as *const Self) }
96 }
97
98 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
138pub 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
166pub 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 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 #[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 #[inline]
231 pub fn text(&self) -> &str {
232 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 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
269macro_rules! forward {
271 ($e:expr) => {
273 ($e).map(|e| unsafe { Self::coerce_ref(e) })
274 };
275}
276
277macro_rules! forward_as_elem {
278 ($e:expr) => {
280 ($e).map(|e| unsafe { ResolvedElementRef::coerce_ref(e) })
281 };
282}
283
284macro_rules! forward_token {
285 ($e:expr) => {
287 ($e).map(|e| unsafe { ResolvedToken::coerce_ref(e) })
288 };
289}
290
291macro_rules! forward_node {
292 ($e:expr) => {
294 ($e).map(|e| unsafe { ResolvedNode::coerce_ref(e) })
295 };
296}
297
298impl<S: Syntax, D> ResolvedNode<S, D> {
299 pub fn resolver(&self) -> &StdArc<dyn Resolver<TokenKey>> {
301 self.syntax.resolver().unwrap()
302 }
303
304 #[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 #[inline]
314 pub fn try_resolved(&self) -> Option<&ResolvedNode<S, D>> {
315 Some(self)
316 }
317
318 #[inline]
322 pub fn resolved(&self) -> &ResolvedNode<S, D> {
323 self
324 }
325
326 #[inline]
330 pub fn root(&self) -> &SyntaxNode<S, D> {
331 unsafe { Self::coerce_ref(self.syntax.root()) }
332 }
333
334 #[inline]
336 pub fn parent(&self) -> Option<&Self> {
337 forward!(self.syntax.parent())
338 }
339
340 #[inline]
342 pub fn ancestors(&self) -> impl Iterator<Item = &Self> {
343 forward!(self.syntax.ancestors())
344 }
345
346 #[inline]
350 pub fn children(&self) -> impl Iterator<Item = &Self> {
351 forward!(self.syntax.children())
352 }
353
354 #[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 #[inline]
364 pub fn first_child(&self) -> Option<&ResolvedNode<S, D>> {
365 forward!(self.syntax.first_child())
366 }
367
368 #[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 #[inline]
378 pub fn last_child(&self) -> Option<&ResolvedNode<S, D>> {
379 forward!(self.syntax.last_child())
380 }
381
382 #[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 #[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 #[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 #[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 #[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 #[inline]
427 pub fn next_sibling(&self) -> Option<&ResolvedNode<S, D>> {
428 forward!(self.syntax.next_sibling())
429 }
430
431 #[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 #[inline]
441 pub fn prev_sibling(&self) -> Option<&ResolvedNode<S, D>> {
442 forward!(self.syntax.prev_sibling())
443 }
444
445 #[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 #[inline]
453 pub fn first_token(&self) -> Option<&ResolvedToken<S, D>> {
454 forward_token!(self.syntax.first_token())
455 }
456
457 #[inline]
459 pub fn last_token(&self) -> Option<&ResolvedToken<S, D>> {
460 forward_token!(self.syntax.last_token())
461 }
462
463 #[inline]
469 pub fn siblings(&self, direction: Direction) -> impl Iterator<Item = &ResolvedNode<S, D>> {
470 forward!(self.syntax.siblings(direction))
471 }
472
473 #[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 #[inline]
485 pub fn descendants(&self) -> impl Iterator<Item = &ResolvedNode<S, D>> {
486 forward!(self.syntax.descendants())
487 }
488
489 #[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 #[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 #[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 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 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 pub fn resolver(&self) -> &StdArc<dyn Resolver<TokenKey>> {
533 self.syntax.resolver().unwrap()
534 }
535
536 #[inline]
540 pub fn try_resolved(&self) -> Option<&ResolvedToken<S, D>> {
541 Some(self)
542 }
543
544 #[inline]
548 pub fn resolved(&self) -> &ResolvedToken<S, D> {
549 self
550 }
551
552 #[inline]
554 pub fn parent(&self) -> &ResolvedNode<S, D> {
555 unsafe { ResolvedNode::coerce_ref(self.syntax.parent()) }
556 }
557
558 #[inline]
560 pub fn ancestors(&self) -> impl Iterator<Item = &ResolvedNode<S, D>> {
561 forward_node!(self.syntax.ancestors())
562 }
563
564 #[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 #[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 #[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 pub fn next_token(&self) -> Option<&ResolvedToken<S, D>> {
587 forward!(self.syntax.next_token())
588 }
589
590 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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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}