@@ -7,7 +7,10 @@ use std::cmp::Ordering;
77use hir:: Semantics ;
88use syntax:: {
99 algo,
10- ast:: { self , make, AstNode , HasAttrs , HasModuleItem , HasVisibility , PathSegmentKind } ,
10+ ast:: {
11+ self , edit_in_place:: Removable , make, AstNode , HasAttrs , HasModuleItem , HasVisibility ,
12+ PathSegmentKind ,
13+ } ,
1114 ted, Direction , NodeOrToken , SyntaxKind , SyntaxNode ,
1215} ;
1316
@@ -192,20 +195,24 @@ pub fn insert_use(scope: &ImportScope, path: ast::Path, cfg: &InsertUseConfig) {
192195 insert_use_ ( scope, & path, cfg. group , use_item) ;
193196}
194197
195- pub fn remove_path_if_in_use_stmt ( path : & ast:: Path ) {
198+ pub fn ast_to_remove_for_path_in_use_stmt ( path : & ast:: Path ) -> Option < Box < dyn Removable > > {
196199 // FIXME: improve this
197200 if path. parent_path ( ) . is_some ( ) {
198- return ;
201+ return None ;
199202 }
200- if let Some ( use_tree) = path. syntax ( ) . parent ( ) . and_then ( ast:: UseTree :: cast) {
201- if use_tree. use_tree_list ( ) . is_some ( ) || use_tree. star_token ( ) . is_some ( ) {
202- return ;
203- }
204- if let Some ( use_) = use_tree. syntax ( ) . parent ( ) . and_then ( ast:: Use :: cast) {
205- use_. remove ( ) ;
206- return ;
207- }
208- use_tree. remove ( ) ;
203+ let use_tree = path. syntax ( ) . parent ( ) . and_then ( ast:: UseTree :: cast) ?;
204+ if use_tree. use_tree_list ( ) . is_some ( ) || use_tree. star_token ( ) . is_some ( ) {
205+ return None ;
206+ }
207+ if let Some ( use_) = use_tree. syntax ( ) . parent ( ) . and_then ( ast:: Use :: cast) {
208+ return Some ( Box :: new ( use_) ) ;
209+ }
210+ Some ( Box :: new ( use_tree) )
211+ }
212+
213+ pub fn remove_path_if_in_use_stmt ( path : & ast:: Path ) {
214+ if let Some ( node) = ast_to_remove_for_path_in_use_stmt ( path) {
215+ node. remove ( ) ;
209216 }
210217}
211218
0 commit comments