@@ -6,7 +6,11 @@ use parser::{SyntaxKind, T};
6
6
7
7
use crate :: {
8
8
algo:: { self , neighbor} ,
9
- ast:: { self , edit:: IndentLevel , make, HasGenericArgs , HasGenericParams } ,
9
+ ast:: {
10
+ self , edit:: IndentLevel , make, syntax_factory:: SyntaxFactory , HasGenericArgs ,
11
+ HasGenericParams ,
12
+ } ,
13
+ syntax_editor:: SyntaxEditor ,
10
14
ted:: { self , Position } ,
11
15
AstNode , AstToken , Direction , SyntaxElement ,
12
16
SyntaxKind :: { ATTR , COMMENT , WHITESPACE } ,
@@ -385,6 +389,10 @@ pub trait Removable: AstNode {
385
389
fn remove ( & self ) ;
386
390
}
387
391
392
+ pub trait EditorRemovable : AstNode {
393
+ fn remove ( & self , editor : & mut SyntaxEditor ) ;
394
+ }
395
+
388
396
impl Removable for ast:: TypeBoundList {
389
397
fn remove ( & self ) {
390
398
match self . syntax ( ) . siblings_with_tokens ( Direction :: Prev ) . find ( |it| it. kind ( ) == T ! [ : ] ) {
@@ -439,16 +447,35 @@ impl Removable for ast::UseTree {
439
447
}
440
448
}
441
449
450
+ impl EditorRemovable for ast:: UseTree {
451
+ fn remove ( & self , editor : & mut SyntaxEditor ) {
452
+ for dir in [ Direction :: Next , Direction :: Prev ] {
453
+ if let Some ( next_use_tree) = neighbor ( self , dir) {
454
+ let separators = self
455
+ . syntax ( )
456
+ . siblings_with_tokens ( dir)
457
+ . skip ( 1 )
458
+ . take_while ( |it| it. as_node ( ) != Some ( next_use_tree. syntax ( ) ) ) ;
459
+ for sep in separators {
460
+ editor. delete ( sep) ;
461
+ }
462
+ break ;
463
+ }
464
+ }
465
+ editor. delete ( self . syntax ( ) ) ;
466
+ }
467
+ }
468
+
442
469
impl ast:: UseTree {
443
470
/// Deletes the usetree node represented by the input. Recursively removes parents, including use nodes that become empty.
444
471
pub fn remove_recursive ( self ) {
445
472
let parent = self . syntax ( ) . parent ( ) ;
446
473
447
- self . remove ( ) ;
474
+ Removable :: remove ( & self ) ;
448
475
449
476
if let Some ( u) = parent. clone ( ) . and_then ( ast:: Use :: cast) {
450
477
if u. use_tree ( ) . is_none ( ) {
451
- u . remove ( ) ;
478
+ Removable :: remove ( & u ) ;
452
479
}
453
480
} else if let Some ( u) = parent. and_then ( ast:: UseTreeList :: cast) {
454
481
if u. use_trees ( ) . next ( ) . is_none ( ) {
@@ -616,6 +643,45 @@ impl Removable for ast::Use {
616
643
}
617
644
}
618
645
646
+ impl EditorRemovable for ast:: Use {
647
+ fn remove ( & self , editor : & mut SyntaxEditor ) {
648
+ let make = SyntaxFactory :: new ( ) ;
649
+
650
+ let next_ws = self
651
+ . syntax ( )
652
+ . next_sibling_or_token ( )
653
+ . and_then ( |it| it. into_token ( ) )
654
+ . and_then ( ast:: Whitespace :: cast) ;
655
+ if let Some ( next_ws) = next_ws {
656
+ let ws_text = next_ws. syntax ( ) . text ( ) ;
657
+ if let Some ( rest) = ws_text. strip_prefix ( '\n' ) {
658
+ if rest. is_empty ( ) {
659
+ editor. delete ( next_ws. syntax ( ) ) ;
660
+ } else {
661
+ editor. replace ( next_ws. syntax ( ) , make. whitespace ( rest) ) ;
662
+ }
663
+ }
664
+ }
665
+ let prev_ws = self
666
+ . syntax ( )
667
+ . prev_sibling_or_token ( )
668
+ . and_then ( |it| it. into_token ( ) )
669
+ . and_then ( ast:: Whitespace :: cast) ;
670
+ if let Some ( prev_ws) = prev_ws {
671
+ let ws_text = prev_ws. syntax ( ) . text ( ) ;
672
+ let prev_newline = ws_text. rfind ( '\n' ) . map ( |x| x + 1 ) . unwrap_or ( 0 ) ;
673
+ let rest = & ws_text[ 0 ..prev_newline] ;
674
+ if rest. is_empty ( ) {
675
+ editor. delete ( prev_ws. syntax ( ) ) ;
676
+ } else {
677
+ editor. replace ( prev_ws. syntax ( ) , make. whitespace ( rest) ) ;
678
+ }
679
+ }
680
+
681
+ editor. delete ( self . syntax ( ) ) ;
682
+ }
683
+ }
684
+
619
685
impl ast:: Impl {
620
686
pub fn get_or_create_assoc_item_list ( & self ) -> ast:: AssocItemList {
621
687
if self . assoc_item_list ( ) . is_none ( ) {
0 commit comments