@@ -6,7 +6,11 @@ use parser::{SyntaxKind, T};
66
77use crate :: {
88 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 ,
1014 ted:: { self , Position } ,
1115 AstNode , AstToken , Direction , SyntaxElement ,
1216 SyntaxKind :: { ATTR , COMMENT , WHITESPACE } ,
@@ -385,6 +389,10 @@ pub trait Removable: AstNode {
385389 fn remove ( & self ) ;
386390}
387391
392+ pub trait EditorRemovable : AstNode {
393+ fn remove ( & self , editor : & mut SyntaxEditor ) ;
394+ }
395+
388396impl Removable for ast:: TypeBoundList {
389397 fn remove ( & self ) {
390398 match self . syntax ( ) . siblings_with_tokens ( Direction :: Prev ) . find ( |it| it. kind ( ) == T ! [ : ] ) {
@@ -439,16 +447,35 @@ impl Removable for ast::UseTree {
439447 }
440448}
441449
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+
442469impl ast:: UseTree {
443470 /// Deletes the usetree node represented by the input. Recursively removes parents, including use nodes that become empty.
444471 pub fn remove_recursive ( self ) {
445472 let parent = self . syntax ( ) . parent ( ) ;
446473
447- self . remove ( ) ;
474+ Removable :: remove ( & self ) ;
448475
449476 if let Some ( u) = parent. clone ( ) . and_then ( ast:: Use :: cast) {
450477 if u. use_tree ( ) . is_none ( ) {
451- u . remove ( ) ;
478+ Removable :: remove ( & u ) ;
452479 }
453480 } else if let Some ( u) = parent. and_then ( ast:: UseTreeList :: cast) {
454481 if u. use_trees ( ) . next ( ) . is_none ( ) {
@@ -616,6 +643,45 @@ impl Removable for ast::Use {
616643 }
617644}
618645
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+
619685impl ast:: Impl {
620686 pub fn get_or_create_assoc_item_list ( & self ) -> ast:: AssocItemList {
621687 if self . assoc_item_list ( ) . is_none ( ) {
0 commit comments