11use syntax:: {
22 AstNode , SyntaxKind ,
3- ast:: { self , HasAttrs , HasVisibility , edit:: IndentLevel , make } ,
4- syntax_editor:: { Element , Position , Removable } ,
3+ ast:: { self , HasAttrs , HasVisibility , edit:: IndentLevel , syntax_factory :: SyntaxFactory } ,
4+ syntax_editor:: { Element , Position , Removable , SyntaxEditor } ,
55} ;
66
77use crate :: {
@@ -22,6 +22,8 @@ use crate::{
2222// use std::fmt::Display;
2323// ```
2424pub ( crate ) fn unmerge_imports ( acc : & mut Assists , ctx : & AssistContext < ' _ > ) -> Option < ( ) > {
25+ let ( editor, _) = SyntaxEditor :: new ( ctx. source_file ( ) . syntax ( ) . clone ( ) ) ;
26+ let make = editor. make ( ) ;
2527 let tree = ctx. find_node_at_offset :: < ast:: UseTree > ( ) ?;
2628
2729 let tree_list = tree. syntax ( ) . parent ( ) . and_then ( ast:: UseTreeList :: cast) ?;
@@ -31,7 +33,7 @@ pub(crate) fn unmerge_imports(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
3133 }
3234
3335 let use_ = tree_list. syntax ( ) . ancestors ( ) . find_map ( ast:: Use :: cast) ?;
34- let path = resolve_full_path ( & tree) ?;
36+ let path = resolve_full_path ( & tree, make ) ?;
3537
3638 // If possible, explain what is going to be done.
3739 let label = match tree. path ( ) . and_then ( |path| path. first_segment ( ) ) {
@@ -41,7 +43,6 @@ pub(crate) fn unmerge_imports(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
4143
4244 let target = tree. syntax ( ) . text_range ( ) ;
4345 acc. add ( AssistId :: refactor_rewrite ( "unmerge_imports" ) , label, target, |builder| {
44- let editor = builder. make_editor ( use_. syntax ( ) ) ;
4546 let make = editor. make ( ) ;
4647 let new_use = make. use_ (
4748 use_. attrs ( ) ,
@@ -64,15 +65,15 @@ pub(crate) fn unmerge_imports(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
6465 } )
6566}
6667
67- fn resolve_full_path ( tree : & ast:: UseTree ) -> Option < ast:: Path > {
68+ fn resolve_full_path ( tree : & ast:: UseTree , make : & SyntaxFactory ) -> Option < ast:: Path > {
6869 let paths = tree
6970 . syntax ( )
7071 . ancestors ( )
7172 . take_while ( |n| n. kind ( ) != SyntaxKind :: USE )
7273 . filter_map ( ast:: UseTree :: cast)
7374 . filter_map ( |t| t. path ( ) ) ;
7475
75- let final_path = paths. reduce ( |prev, next| make:: path_concat ( next, prev) ) ?;
76+ let final_path = paths. reduce ( |prev, next| make. path_concat ( next, prev) ) ?;
7677 if final_path. segment ( ) . is_some_and ( |it| it. self_token ( ) . is_some ( ) ) {
7778 final_path. qualifier ( )
7879 } else {
0 commit comments