@@ -209,23 +209,12 @@ impl SyntaxNode {
209
209
SyntaxNode :: new ( data)
210
210
}
211
211
212
- /// Returns a green tree, equal to the green tree this node
213
- /// belongs two, except with this node substitute. The complexity
214
- /// of operation is proportional to the depth of the tree
215
212
pub fn replace_with ( & self , replacement : GreenNode ) -> GreenNode {
216
213
assert_eq ! ( self . kind( ) , replacement. kind( ) ) ;
217
214
match self . 0 . kind . as_child ( ) {
218
215
None => replacement,
219
216
Some ( ( parent, me, _offset) ) => {
220
- let mut replacement = Some ( replacement) ;
221
- let children = parent. green ( ) . children ( ) . enumerate ( ) . map ( |( i, child) | {
222
- if i as u32 == me {
223
- replacement. take ( ) . unwrap ( ) . into ( )
224
- } else {
225
- child. cloned ( )
226
- }
227
- } ) ;
228
- let new_parent = GreenNode :: new ( parent. kind ( ) , children) ;
217
+ let new_parent = parent. green ( ) . replace_child ( me as usize , replacement. into ( ) ) ;
229
218
parent. replace_with ( new_parent)
230
219
}
231
220
}
@@ -342,13 +331,11 @@ impl SyntaxNode {
342
331
Some ( SyntaxElement :: new ( element, parent. clone ( ) , index as u32 , offset) )
343
332
}
344
333
345
- /// Return the leftmost token in the subtree of this node
346
334
#[ inline]
347
335
pub fn first_token ( & self ) -> Option < SyntaxToken > {
348
336
self . first_child_or_token ( ) ?. first_token ( )
349
337
}
350
338
351
- /// Return the rightmost token in the subtree of this node
352
339
#[ inline]
353
340
pub fn last_token ( & self ) -> Option < SyntaxToken > {
354
341
self . last_child_or_token ( ) ?. last_token ( )
@@ -386,8 +373,6 @@ impl SyntaxNode {
386
373
} )
387
374
}
388
375
389
- /// Traverse the subtree rooted at the current node (including the current
390
- /// node) in preorder, excluding tokens.
391
376
#[ inline]
392
377
pub fn preorder ( & self ) -> impl Iterator < Item = WalkEvent < SyntaxNode > > {
393
378
let this = self . clone ( ) ;
@@ -411,8 +396,6 @@ impl SyntaxNode {
411
396
} )
412
397
}
413
398
414
- /// Traverse the subtree rooted at the current node (including the current
415
- /// node) in preorder, including tokens.
416
399
#[ inline]
417
400
pub fn preorder_with_tokens < ' a > ( & ' a self ) -> impl Iterator < Item = WalkEvent < SyntaxElement > > {
418
401
let start: SyntaxElement = self . clone ( ) . into ( ) ;
@@ -439,8 +422,6 @@ impl SyntaxNode {
439
422
} )
440
423
}
441
424
442
- /// Find a token in the subtree corresponding to this node, which covers the offset.
443
- /// Precondition: offset must be withing node's range.
444
425
pub fn token_at_offset ( & self , offset : TextSize ) -> TokenAtOffset < SyntaxToken > {
445
426
// TODO: this could be faster if we first drill-down to node, and only
446
427
// then switch to token search. We should also replace explicit
@@ -478,10 +459,6 @@ impl SyntaxNode {
478
459
}
479
460
}
480
461
481
- /// Return the deepest node or token in the current subtree that fully
482
- /// contains the range. If the range is empty and is contained in two leaf
483
- /// nodes, either one can be returned. Precondition: range must be contained
484
- /// withing the current node
485
462
pub fn covering_element ( & self , range : TextRange ) -> SyntaxElement {
486
463
let mut res: SyntaxElement = self . clone ( ) . into ( ) ;
487
464
loop {
@@ -493,28 +470,36 @@ impl SyntaxNode {
493
470
) ;
494
471
res = match & res {
495
472
NodeOrToken :: Token ( _) => return res,
496
- NodeOrToken :: Node ( node) => {
497
- match node
498
- . children_with_tokens ( )
499
- . find ( |child| child. text_range ( ) . contains_range ( range) )
500
- {
501
- Some ( child) => child,
502
- None => return res,
503
- }
504
- }
473
+ NodeOrToken :: Node ( node) => match node. child_or_token_at_range ( range) {
474
+ Some ( it) => it,
475
+ None => return res,
476
+ } ,
505
477
} ;
506
478
}
507
479
}
480
+
481
+ pub fn child_or_token_at_range ( & self , range : TextRange ) -> Option < SyntaxElement > {
482
+ let start_offset = self . text_range ( ) . start ( ) ;
483
+ let ( index, offset, child) = self . green ( ) . child_at_range ( range - start_offset) ?;
484
+ let index = index as u32 ;
485
+ let offset = offset + start_offset;
486
+ let res: SyntaxElement = match child {
487
+ NodeOrToken :: Node ( node) => {
488
+ let data =
489
+ NodeData :: new ( Kind :: Child { parent : self . clone ( ) , index, offset } , node. into ( ) ) ;
490
+ SyntaxNode :: new ( data) . into ( )
491
+ }
492
+ NodeOrToken :: Token ( _token) => SyntaxToken :: new ( self . clone ( ) , index, offset) . into ( ) ,
493
+ } ;
494
+ Some ( res)
495
+ }
508
496
}
509
497
510
498
impl SyntaxToken {
511
499
fn new ( parent : SyntaxNode , index : u32 , offset : TextSize ) -> SyntaxToken {
512
500
SyntaxToken { parent, index, offset }
513
501
}
514
502
515
- /// Returns a green tree, equal to the green tree this token
516
- /// belongs two, except with this token substitute. The complexity
517
- /// of operation is proportional to the depth of the tree
518
503
pub fn replace_with ( & self , replacement : GreenToken ) -> GreenNode {
519
504
assert_eq ! ( self . kind( ) , replacement. kind( ) ) ;
520
505
let mut replacement = Some ( replacement) ;
@@ -588,7 +573,6 @@ impl SyntaxToken {
588
573
} )
589
574
}
590
575
591
- /// Next token in the tree (i.e, not necessary a sibling)
592
576
pub fn next_token ( & self ) -> Option < SyntaxToken > {
593
577
match self . next_sibling_or_token ( ) {
594
578
Some ( element) => element. first_token ( ) ,
@@ -599,7 +583,7 @@ impl SyntaxToken {
599
583
. and_then ( |element| element. first_token ( ) ) ,
600
584
}
601
585
}
602
- /// Previous token in the tree (i.e, not necessary a sibling)
586
+
603
587
pub fn prev_token ( & self ) -> Option < SyntaxToken > {
604
588
match self . prev_sibling_or_token ( ) {
605
589
Some ( element) => element. last_token ( ) ,
0 commit comments