-
Notifications
You must be signed in to change notification settings - Fork 61
/
cursor.rs
1284 lines (1141 loc) · 39.4 KB
/
cursor.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Implementation of the cursors -- API for convenient access to syntax trees.
//!
//! Functional programmers will recognize that this module implements a zipper
//! for a purely functional (green) tree.
//!
//! A cursor node (`SyntaxNode`) points to a `GreenNode` and a parent
//! `SyntaxNode`. This allows cursor to provide iteration over both ancestors
//! and descendants, as well as a cheep access to absolute offset of the node in
//! file.
//!
//! By default `SyntaxNode`s are immutable, but you can get a mutable copy of
//! the tree by calling `clone_for_update`. Mutation is based on interior
//! mutability and doesn't need `&mut`. You can have two `SyntaxNode`s pointing
//! at different parts of the same tree; mutations via the first node will be
//! reflected in the other.
// Implementation notes:
//
// The implementation is utterly and horribly unsafe. This whole module is an
// unsafety boundary. It is believed that the API here is, in principle, sound,
// but the implementation might have bugs.
//
// The core type is `NodeData` -- a heap-allocated reference counted object,
// which points to a green node or a green token, and to the parent `NodeData`.
// Publicly-exposed `SyntaxNode` and `SyntaxToken` own a reference to
// `NodeData`.
//
// `NodeData`s are transient, and are created and destroyed during tree
// traversals. In general, only currently referenced nodes and their ancestors
// are alive at any given moment.
//
// More specifically, `NodeData`'s ref count is equal to the number of
// outstanding `SyntaxNode` and `SyntaxToken` plus the number of children with
// non-zero ref counts. For example, if the user has only a single `SyntaxNode`
// pointing somewhere in the middle of the tree, then all `NodeData` on the path
// from that point towards the root have ref count equal to one.
//
// `NodeData` which doesn't have a parent (is a root) owns the corresponding
// green node or token, and is responsible for freeing it.
//
// That's mostly it for the immutable subset of the API. Mutation is fun though,
// you'll like it!
//
// Mutability is a run-time property of a tree of `NodeData`. The whole tree is
// either mutable or immutable. `clone_for_update` clones the whole tree of
// `NodeData`s, making it mutable (note that the green tree is re-used).
//
// If the tree is mutable, then all live `NodeData` are additionally liked to
// each other via intrusive liked lists. Specifically, there are two pointers to
// siblings, as well as a pointer to the first child. Note that only live nodes
// are considered. If the user only has `SyntaxNode`s for the first and last
// children of some particular node, then their `NodeData` will point at each
// other.
//
// The links are used to propagate mutations across the tree. Specifically, each
// `NodeData` remembers it's index in parent. When the node is detached from or
// attached to the tree, we need to adjust the indices of all subsequent
// siblings. That's what makes the `for c in node.children() { c.detach() }`
// pattern work despite the apparent iterator invalidation.
//
// This code is encapsulated into the sorted linked list (`sll`) module.
//
// The actual mutation consist of functionally "mutating" (creating a
// structurally shared copy) the green node, and then re-spinning the tree. This
// is a delicate process: `NodeData` point directly to the green nodes, so we
// must make sure that those nodes don't move. Additionally, during mutation a
// node might become or might stop being a root, so we must take care to not
// double free / leak its green node.
//
// Because we can change green nodes using only shared references, handing out
// references into green nodes in the public API would be unsound. We don't do
// that, but we do use such references internally a lot. Additionally, for
// tokens the underlying green token actually is immutable, so we can, and do
// return `&str`.
//
// Invariants [must not leak outside of the module]:
// - Mutability is the property of the whole tree. Intermixing elements that
// differ in mutability is not allowed.
// - Mutability property is persistent.
// - References to the green elements' data are not exposed into public API
// when the tree is mutable.
// - TBD
use std::{
borrow::Cow,
cell::Cell,
fmt,
hash::{Hash, Hasher},
iter,
mem::{self, ManuallyDrop},
ops::Range,
ptr, slice,
};
use countme::Count;
use crate::{
green::{GreenChild, GreenElementRef, GreenNodeData, GreenTokenData, SyntaxKind},
sll,
utility_types::Delta,
Direction, GreenNode, GreenToken, NodeOrToken, SyntaxText, TextRange, TextSize, TokenAtOffset,
WalkEvent,
};
enum Green {
Node { ptr: Cell<ptr::NonNull<GreenNodeData>> },
Token { ptr: ptr::NonNull<GreenTokenData> },
}
struct _SyntaxElement;
struct NodeData {
_c: Count<_SyntaxElement>,
rc: Cell<u32>,
parent: Cell<Option<ptr::NonNull<NodeData>>>,
index: Cell<u32>,
green: Green,
/// Invariant: never changes after NodeData is created.
mutable: bool,
/// Absolute offset for immutable nodes, unused for mutable nodes.
offset: TextSize,
// The following links only have meaning when `mutable` is true.
first: Cell<*const NodeData>,
/// Invariant: never null if mutable.
next: Cell<*const NodeData>,
/// Invariant: never null if mutable.
prev: Cell<*const NodeData>,
}
unsafe impl sll::Elem for NodeData {
fn prev(&self) -> &Cell<*const Self> {
&self.prev
}
fn next(&self) -> &Cell<*const Self> {
&self.next
}
fn key(&self) -> &Cell<u32> {
&self.index
}
}
pub type SyntaxElement = NodeOrToken<SyntaxNode, SyntaxToken>;
pub struct SyntaxNode {
ptr: ptr::NonNull<NodeData>,
}
impl Clone for SyntaxNode {
#[inline]
fn clone(&self) -> Self {
self.data().inc_rc();
SyntaxNode { ptr: self.ptr }
}
}
impl Drop for SyntaxNode {
#[inline]
fn drop(&mut self) {
if self.data().dec_rc() {
unsafe { free(self.ptr) }
}
}
}
#[derive(Debug)]
pub struct SyntaxToken {
ptr: ptr::NonNull<NodeData>,
}
impl Clone for SyntaxToken {
#[inline]
fn clone(&self) -> Self {
self.data().inc_rc();
SyntaxToken { ptr: self.ptr }
}
}
impl Drop for SyntaxToken {
#[inline]
fn drop(&mut self) {
if self.data().dec_rc() {
unsafe { free(self.ptr) }
}
}
}
#[inline(never)]
unsafe fn free(mut data: ptr::NonNull<NodeData>) {
loop {
debug_assert_eq!(data.as_ref().rc.get(), 0);
debug_assert!(data.as_ref().first.get().is_null());
let node = Box::from_raw(data.as_ptr());
match node.parent.take() {
Some(parent) => {
debug_assert!(parent.as_ref().rc.get() > 0);
if node.mutable {
sll::unlink(&parent.as_ref().first, &*node)
}
if parent.as_ref().dec_rc() {
data = parent;
} else {
break;
}
}
None => {
match &node.green {
Green::Node { ptr } => {
let _ = GreenNode::from_raw(ptr.get());
}
Green::Token { ptr } => {
let _ = GreenToken::from_raw(*ptr);
}
}
break;
}
}
}
}
impl NodeData {
#[inline]
fn new(
parent: Option<SyntaxNode>,
index: u32,
offset: TextSize,
green: Green,
mutable: bool,
) -> ptr::NonNull<NodeData> {
let parent = ManuallyDrop::new(parent);
let res = NodeData {
_c: Count::new(),
rc: Cell::new(1),
parent: Cell::new(parent.as_ref().map(|it| it.ptr)),
index: Cell::new(index),
green,
mutable,
offset,
first: Cell::new(ptr::null()),
next: Cell::new(ptr::null()),
prev: Cell::new(ptr::null()),
};
unsafe {
if mutable {
let res_ptr: *const NodeData = &res;
match sll::init((*res_ptr).parent().map(|it| &it.first), res_ptr.as_ref().unwrap())
{
sll::AddToSllResult::AlreadyInSll(node) => {
if cfg!(debug_assertions) {
assert_eq!((*node).index(), (*res_ptr).index());
match ((*node).green(), (*res_ptr).green()) {
(NodeOrToken::Node(lhs), NodeOrToken::Node(rhs)) => {
assert!(ptr::eq(lhs, rhs))
}
(NodeOrToken::Token(lhs), NodeOrToken::Token(rhs)) => {
assert!(ptr::eq(lhs, rhs))
}
it => {
panic!("node/token confusion: {:?}", it)
}
}
}
ManuallyDrop::into_inner(parent);
let res = node as *mut NodeData;
(*res).inc_rc();
return ptr::NonNull::new_unchecked(res);
}
it => {
let res = Box::into_raw(Box::new(res));
it.add_to_sll(res);
return ptr::NonNull::new_unchecked(res);
}
}
}
ptr::NonNull::new_unchecked(Box::into_raw(Box::new(res)))
}
}
#[inline]
fn inc_rc(&self) {
let rc = match self.rc.get().checked_add(1) {
Some(it) => it,
None => std::process::abort(),
};
self.rc.set(rc)
}
#[inline]
fn dec_rc(&self) -> bool {
let rc = self.rc.get() - 1;
self.rc.set(rc);
rc == 0
}
#[inline]
fn key(&self) -> (ptr::NonNull<()>, TextSize) {
let ptr = match &self.green {
Green::Node { ptr } => ptr.get().cast(),
Green::Token { ptr } => ptr.cast(),
};
(ptr, self.offset())
}
#[inline]
fn parent_node(&self) -> Option<SyntaxNode> {
let parent = self.parent()?;
debug_assert!(matches!(parent.green, Green::Node { .. }));
parent.inc_rc();
Some(SyntaxNode { ptr: ptr::NonNull::from(parent) })
}
#[inline]
fn parent(&self) -> Option<&NodeData> {
self.parent.get().map(|it| unsafe { &*it.as_ptr() })
}
#[inline]
fn green(&self) -> GreenElementRef<'_> {
match &self.green {
Green::Node { ptr } => GreenElementRef::Node(unsafe { &*ptr.get().as_ptr() }),
Green::Token { ptr } => GreenElementRef::Token(unsafe { &*ptr.as_ref() }),
}
}
#[inline]
fn green_siblings(&self) -> slice::Iter<GreenChild> {
match &self.parent().map(|it| &it.green) {
Some(Green::Node { ptr }) => unsafe { &*ptr.get().as_ptr() }.children().raw,
Some(Green::Token { .. }) => {
debug_assert!(false);
[].iter()
}
None => [].iter(),
}
}
#[inline]
fn index(&self) -> u32 {
self.index.get()
}
#[inline]
fn offset(&self) -> TextSize {
if self.mutable {
self.offset_mut()
} else {
self.offset
}
}
#[cold]
fn offset_mut(&self) -> TextSize {
let mut res = TextSize::from(0);
let mut node = self;
while let Some(parent) = node.parent() {
let green = parent.green().into_node().unwrap();
res += green.children().raw.nth(node.index() as usize).unwrap().rel_offset();
node = parent;
}
res
}
#[inline]
fn text_range(&self) -> TextRange {
let offset = self.offset();
let len = self.green().text_len();
TextRange::at(offset, len)
}
#[inline]
fn kind(&self) -> SyntaxKind {
self.green().kind()
}
fn next_sibling(&self) -> Option<SyntaxNode> {
let mut siblings = self.green_siblings().enumerate();
let index = self.index() as usize;
siblings.nth(index);
siblings.find_map(|(index, child)| {
child.as_ref().into_node().and_then(|green| {
let parent = self.parent_node()?;
let offset = parent.offset() + child.rel_offset();
Some(SyntaxNode::new_child(green, parent, index as u32, offset))
})
})
}
fn prev_sibling(&self) -> Option<SyntaxNode> {
let mut rev_siblings = self.green_siblings().enumerate().rev();
let index = rev_siblings.len().checked_sub(self.index() as usize + 1)?;
rev_siblings.nth(index);
rev_siblings.find_map(|(index, child)| {
child.as_ref().into_node().and_then(|green| {
let parent = self.parent_node()?;
let offset = parent.offset() + child.rel_offset();
Some(SyntaxNode::new_child(green, parent, index as u32, offset))
})
})
}
fn next_sibling_or_token(&self) -> Option<SyntaxElement> {
let mut siblings = self.green_siblings().enumerate();
let index = self.index() as usize + 1;
siblings.nth(index).and_then(|(index, child)| {
let parent = self.parent_node()?;
let offset = parent.offset() + child.rel_offset();
Some(SyntaxElement::new(child.as_ref(), parent, index as u32, offset))
})
}
fn prev_sibling_or_token(&self) -> Option<SyntaxElement> {
let mut siblings = self.green_siblings().enumerate();
let index = self.index().checked_sub(1)? as usize;
siblings.nth(index).and_then(|(index, child)| {
let parent = self.parent_node()?;
let offset = parent.offset() + child.rel_offset();
Some(SyntaxElement::new(child.as_ref(), parent, index as u32, offset))
})
}
fn detach(&self) {
assert!(self.mutable);
assert!(self.rc.get() > 0);
let parent_ptr = match self.parent.take() {
Some(parent) => parent,
None => return,
};
unsafe {
sll::adjust(self, self.index() + 1, Delta::Sub(1));
let parent = parent_ptr.as_ref();
sll::unlink(&parent.first, self);
// Add strong ref to green
match self.green().to_owned() {
NodeOrToken::Node(it) => {
GreenNode::into_raw(it);
}
NodeOrToken::Token(it) => {
GreenToken::into_raw(it);
}
}
match parent.green() {
NodeOrToken::Node(green) => {
let green = green.remove_child(self.index() as usize);
parent.respine(green)
}
NodeOrToken::Token(_) => unreachable!(),
}
if parent.dec_rc() {
free(parent_ptr)
}
}
}
fn attach_child(&self, index: usize, child: &NodeData) {
assert!(self.mutable && child.mutable && child.parent().is_none());
assert!(self.rc.get() > 0 && child.rc.get() > 0);
unsafe {
child.index.set(index as u32);
child.parent.set(Some(self.into()));
self.inc_rc();
if !self.first.get().is_null() {
sll::adjust(&*self.first.get(), index as u32, Delta::Add(1));
}
match sll::link(&self.first, child) {
sll::AddToSllResult::AlreadyInSll(_) => {
panic!("Child already in sorted linked list")
}
it => it.add_to_sll(child),
}
match self.green() {
NodeOrToken::Node(green) => {
// Child is root, so it ownes the green node. Steal it!
let child_green = match &child.green {
Green::Node { ptr } => GreenNode::from_raw(ptr.get()).into(),
Green::Token { ptr } => GreenToken::from_raw(*ptr).into(),
};
let green = green.insert_child(index, child_green);
self.respine(green);
}
NodeOrToken::Token(_) => unreachable!(),
}
}
}
unsafe fn respine(&self, mut new_green: GreenNode) {
let mut node = self;
loop {
let old_green = match &node.green {
Green::Node { ptr } => ptr.replace(ptr::NonNull::from(&*new_green)),
Green::Token { .. } => unreachable!(),
};
match node.parent() {
Some(parent) => match parent.green() {
NodeOrToken::Node(parent_green) => {
new_green =
parent_green.replace_child(node.index() as usize, new_green.into());
node = parent;
}
_ => unreachable!(),
},
None => {
mem::forget(new_green);
let _ = GreenNode::from_raw(old_green);
break;
}
}
}
}
}
impl SyntaxNode {
pub fn new_root(green: GreenNode) -> SyntaxNode {
let green = GreenNode::into_raw(green);
let green = Green::Node { ptr: Cell::new(green) };
SyntaxNode { ptr: NodeData::new(None, 0, 0.into(), green, false) }
}
pub fn new_root_mut(green: GreenNode) -> SyntaxNode {
let green = GreenNode::into_raw(green);
let green = Green::Node { ptr: Cell::new(green) };
SyntaxNode { ptr: NodeData::new(None, 0, 0.into(), green, true) }
}
fn new_child(
green: &GreenNodeData,
parent: SyntaxNode,
index: u32,
offset: TextSize,
) -> SyntaxNode {
let mutable = parent.data().mutable;
let green = Green::Node { ptr: Cell::new(green.into()) };
SyntaxNode { ptr: NodeData::new(Some(parent), index, offset, green, mutable) }
}
pub fn clone_for_update(&self) -> SyntaxNode {
assert!(!self.data().mutable);
match self.parent() {
Some(parent) => {
let parent = parent.clone_for_update();
SyntaxNode::new_child(self.green_ref(), parent, self.data().index(), self.offset())
}
None => SyntaxNode::new_root_mut(self.green_ref().to_owned()),
}
}
pub fn clone_subtree(&self) -> SyntaxNode {
SyntaxNode::new_root(self.green().into())
}
#[inline]
fn data(&self) -> &NodeData {
unsafe { self.ptr.as_ref() }
}
pub fn replace_with(&self, replacement: GreenNode) -> GreenNode {
assert_eq!(self.kind(), replacement.kind());
match &self.parent() {
None => replacement,
Some(parent) => {
let new_parent = parent
.green_ref()
.replace_child(self.data().index() as usize, replacement.into());
parent.replace_with(new_parent)
}
}
}
#[inline]
pub fn kind(&self) -> SyntaxKind {
self.data().kind()
}
#[inline]
fn offset(&self) -> TextSize {
self.data().offset()
}
#[inline]
pub fn text_range(&self) -> TextRange {
self.data().text_range()
}
#[inline]
pub fn index(&self) -> usize {
self.data().index() as usize
}
#[inline]
pub fn text(&self) -> SyntaxText {
SyntaxText::new(self.clone())
}
#[inline]
pub fn green(&self) -> Cow<'_, GreenNodeData> {
let green_ref = self.green_ref();
match self.data().mutable {
false => Cow::Borrowed(green_ref),
true => Cow::Owned(green_ref.to_owned()),
}
}
#[inline]
fn green_ref(&self) -> &GreenNodeData {
self.data().green().into_node().unwrap()
}
#[inline]
pub fn parent(&self) -> Option<SyntaxNode> {
self.data().parent_node()
}
#[inline]
pub fn ancestors(&self) -> impl Iterator<Item = SyntaxNode> {
iter::successors(Some(self.clone()), SyntaxNode::parent)
}
#[inline]
pub fn children(&self) -> SyntaxNodeChildren {
SyntaxNodeChildren::new(self.clone())
}
#[inline]
pub fn children_with_tokens(&self) -> SyntaxElementChildren {
SyntaxElementChildren::new(self.clone())
}
pub fn first_child(&self) -> Option<SyntaxNode> {
self.green_ref().children().raw.enumerate().find_map(|(index, child)| {
child.as_ref().into_node().map(|green| {
SyntaxNode::new_child(
green,
self.clone(),
index as u32,
self.offset() + child.rel_offset(),
)
})
})
}
pub fn last_child(&self) -> Option<SyntaxNode> {
self.green_ref().children().raw.enumerate().rev().find_map(|(index, child)| {
child.as_ref().into_node().map(|green| {
SyntaxNode::new_child(
green,
self.clone(),
index as u32,
self.offset() + child.rel_offset(),
)
})
})
}
pub fn first_child_or_token(&self) -> Option<SyntaxElement> {
self.green_ref().children().raw.next().map(|child| {
SyntaxElement::new(child.as_ref(), self.clone(), 0, self.offset() + child.rel_offset())
})
}
pub fn last_child_or_token(&self) -> Option<SyntaxElement> {
self.green_ref().children().raw.enumerate().next_back().map(|(index, child)| {
SyntaxElement::new(
child.as_ref(),
self.clone(),
index as u32,
self.offset() + child.rel_offset(),
)
})
}
pub fn next_sibling(&self) -> Option<SyntaxNode> {
self.data().next_sibling()
}
pub fn prev_sibling(&self) -> Option<SyntaxNode> {
self.data().prev_sibling()
}
pub fn next_sibling_or_token(&self) -> Option<SyntaxElement> {
self.data().next_sibling_or_token()
}
pub fn prev_sibling_or_token(&self) -> Option<SyntaxElement> {
self.data().prev_sibling_or_token()
}
pub fn first_token(&self) -> Option<SyntaxToken> {
self.first_child_or_token()?.first_token()
}
pub fn last_token(&self) -> Option<SyntaxToken> {
self.last_child_or_token()?.last_token()
}
#[inline]
pub fn siblings(&self, direction: Direction) -> impl Iterator<Item = SyntaxNode> {
iter::successors(Some(self.clone()), move |node| match direction {
Direction::Next => node.next_sibling(),
Direction::Prev => node.prev_sibling(),
})
}
#[inline]
pub fn siblings_with_tokens(
&self,
direction: Direction,
) -> impl Iterator<Item = SyntaxElement> {
let me: SyntaxElement = self.clone().into();
iter::successors(Some(me), move |el| match direction {
Direction::Next => el.next_sibling_or_token(),
Direction::Prev => el.prev_sibling_or_token(),
})
}
#[inline]
pub fn descendants(&self) -> impl Iterator<Item = SyntaxNode> {
self.preorder().filter_map(|event| match event {
WalkEvent::Enter(node) => Some(node),
WalkEvent::Leave(_) => None,
})
}
#[inline]
pub fn descendants_with_tokens(&self) -> impl Iterator<Item = SyntaxElement> {
self.preorder_with_tokens().filter_map(|event| match event {
WalkEvent::Enter(it) => Some(it),
WalkEvent::Leave(_) => None,
})
}
#[inline]
pub fn preorder(&self) -> Preorder {
Preorder::new(self.clone())
}
#[inline]
pub fn preorder_with_tokens(&self) -> PreorderWithTokens {
PreorderWithTokens::new(self.clone())
}
pub fn token_at_offset(&self, offset: TextSize) -> TokenAtOffset<SyntaxToken> {
// TODO: this could be faster if we first drill-down to node, and only
// then switch to token search. We should also replace explicit
// recursion with a loop.
let range = self.text_range();
assert!(
range.start() <= offset && offset <= range.end(),
"Bad offset: range {:?} offset {:?}",
range,
offset
);
if range.is_empty() {
return TokenAtOffset::None;
}
let mut children = self.children_with_tokens().filter(|child| {
let child_range = child.text_range();
!child_range.is_empty()
&& (child_range.start() <= offset && offset <= child_range.end())
});
let left = children.next().unwrap();
let right = children.next();
assert!(children.next().is_none());
if let Some(right) = right {
match (left.token_at_offset(offset), right.token_at_offset(offset)) {
(TokenAtOffset::Single(left), TokenAtOffset::Single(right)) => {
TokenAtOffset::Between(left, right)
}
_ => unreachable!(),
}
} else {
left.token_at_offset(offset)
}
}
pub fn covering_element(&self, range: TextRange) -> SyntaxElement {
let mut res: SyntaxElement = self.clone().into();
loop {
assert!(
res.text_range().contains_range(range),
"Bad range: node range {:?}, range {:?}",
res.text_range(),
range,
);
res = match &res {
NodeOrToken::Token(_) => return res,
NodeOrToken::Node(node) => match node.child_or_token_at_range(range) {
Some(it) => it,
None => return res,
},
};
}
}
pub fn child_or_token_at_range(&self, range: TextRange) -> Option<SyntaxElement> {
let rel_range = range - self.offset();
self.green_ref().child_at_range(rel_range).map(|(index, rel_offset, green)| {
SyntaxElement::new(green, self.clone(), index as u32, self.offset() + rel_offset)
})
}
pub fn splice_children(&self, to_delete: Range<usize>, to_insert: Vec<SyntaxElement>) {
assert!(self.data().mutable, "immutable tree: {}", self);
for (i, child) in self.children_with_tokens().enumerate() {
if to_delete.contains(&i) {
child.detach();
}
}
let mut index = to_delete.start;
for child in to_insert {
self.attach_child(index, child);
index += 1;
}
}
pub fn detach(&self) {
assert!(self.data().mutable, "immutable tree: {}", self);
self.data().detach()
}
fn attach_child(&self, index: usize, child: SyntaxElement) {
assert!(self.data().mutable, "immutable tree: {}", self);
child.detach();
let data = match &child {
NodeOrToken::Node(it) => it.data(),
NodeOrToken::Token(it) => it.data(),
};
self.data().attach_child(index, data)
}
}
impl SyntaxToken {
fn new(
green: &GreenTokenData,
parent: SyntaxNode,
index: u32,
offset: TextSize,
) -> SyntaxToken {
let mutable = parent.data().mutable;
let green = Green::Token { ptr: green.into() };
SyntaxToken { ptr: NodeData::new(Some(parent), index, offset, green, mutable) }
}
#[inline]
fn data(&self) -> &NodeData {
unsafe { self.ptr.as_ref() }
}
pub fn replace_with(&self, replacement: GreenToken) -> GreenNode {
assert_eq!(self.kind(), replacement.kind());
let parent = self.parent().unwrap();
let me: u32 = self.data().index();
let new_parent = parent.green_ref().replace_child(me as usize, replacement.into());
parent.replace_with(new_parent)
}
#[inline]
pub fn kind(&self) -> SyntaxKind {
self.data().kind()
}
#[inline]
pub fn text_range(&self) -> TextRange {
self.data().text_range()
}
#[inline]
pub fn index(&self) -> usize {
self.data().index() as usize
}
#[inline]
pub fn text(&self) -> &str {
match self.data().green().as_token() {
Some(it) => it.text(),
None => {
debug_assert!(
false,
"corrupted tree: a node thinks it is a token: {:?}",
self.data().green().as_node().unwrap().to_string()
);
""
}
}
}
#[inline]
pub fn green(&self) -> &GreenTokenData {
self.data().green().into_token().unwrap()
}
#[inline]
pub fn parent(&self) -> Option<SyntaxNode> {
self.data().parent_node()
}
#[inline]
pub fn ancestors(&self) -> impl Iterator<Item = SyntaxNode> {
std::iter::successors(self.parent(), SyntaxNode::parent)
}
pub fn next_sibling_or_token(&self) -> Option<SyntaxElement> {
self.data().next_sibling_or_token()
}
pub fn prev_sibling_or_token(&self) -> Option<SyntaxElement> {
self.data().prev_sibling_or_token()
}
#[inline]
pub fn siblings_with_tokens(
&self,
direction: Direction,
) -> impl Iterator<Item = SyntaxElement> {
let me: SyntaxElement = self.clone().into();
iter::successors(Some(me), move |el| match direction {
Direction::Next => el.next_sibling_or_token(),
Direction::Prev => el.prev_sibling_or_token(),
})
}
pub fn next_token(&self) -> Option<SyntaxToken> {
match self.next_sibling_or_token() {
Some(element) => element.first_token(),
None => self
.ancestors()
.find_map(|it| it.next_sibling_or_token())
.and_then(|element| element.first_token()),
}
}
pub fn prev_token(&self) -> Option<SyntaxToken> {
match self.prev_sibling_or_token() {
Some(element) => element.last_token(),
None => self
.ancestors()
.find_map(|it| it.prev_sibling_or_token())
.and_then(|element| element.last_token()),
}
}
pub fn detach(&self) {
assert!(self.data().mutable, "immutable tree: {}", self);
self.data().detach()
}
}
impl SyntaxElement {
fn new(
element: GreenElementRef<'_>,
parent: SyntaxNode,
index: u32,
offset: TextSize,
) -> SyntaxElement {
match element {
NodeOrToken::Node(node) => {
SyntaxNode::new_child(node, parent, index as u32, offset).into()
}
NodeOrToken::Token(token) => {
SyntaxToken::new(token, parent, index as u32, offset).into()
}
}
}
#[inline]
pub fn text_range(&self) -> TextRange {
match self {
NodeOrToken::Node(it) => it.text_range(),
NodeOrToken::Token(it) => it.text_range(),
}
}
#[inline]
pub fn index(&self) -> usize {
match self {
NodeOrToken::Node(it) => it.index(),
NodeOrToken::Token(it) => it.index(),
}
}
#[inline]
pub fn kind(&self) -> SyntaxKind {
match self {
NodeOrToken::Node(it) => it.kind(),
NodeOrToken::Token(it) => it.kind(),
}
}
#[inline]
pub fn parent(&self) -> Option<SyntaxNode> {
match self {
NodeOrToken::Node(it) => it.parent(),
NodeOrToken::Token(it) => it.parent(),
}