@@ -13,7 +13,7 @@ use crate::{
1313 assist_context:: { AssistContext , Assists } ,
1414 utils:: {
1515 DefaultMethods , IgnoreAssocItems , add_trait_assoc_items_to_impl_with_factory,
16- filter_assoc_items, gen_trait_fn_body, generate_trait_impl,
16+ filter_assoc_items, gen_trait_fn_body, generate_trait_impl, generate_trait_impl_with_item ,
1717 } ,
1818} ;
1919
@@ -127,7 +127,7 @@ fn add_assist(
127127 let label = format ! ( "Convert to manual `impl {replace_trait_path} for {annotated_name}`" ) ;
128128
129129 acc. add ( AssistId :: refactor ( "replace_derive_with_manual_impl" ) , label, target, |builder| {
130- let make = SyntaxFactory :: without_mappings ( ) ;
130+ let make = SyntaxFactory :: with_mappings ( ) ;
131131 let insert_after = Position :: after ( adt. syntax ( ) ) ;
132132 let impl_is_unsafe = trait_. map ( |s| s. is_unsafe ( ctx. db ( ) ) ) . unwrap_or ( false ) ;
133133 let impl_def = impl_def_from_trait (
@@ -141,7 +141,7 @@ fn add_assist(
141141 ) ;
142142
143143 let mut editor = builder. make_editor ( attr. syntax ( ) ) ;
144- update_attribute ( & mut editor, old_derives, old_tree, old_trait_path, attr) ;
144+ update_attribute ( & make , & mut editor, old_derives, old_tree, old_trait_path, attr) ;
145145
146146 let trait_path = make. ty_path ( replace_trait_path. clone ( ) ) . into ( ) ;
147147
@@ -177,6 +177,7 @@ fn add_assist(
177177 insert_after,
178178 vec ! [ make. whitespace( "\n \n " ) . into( ) , impl_def. syntax( ) . clone( ) . into( ) ] ,
179179 ) ;
180+ editor. add_mappings ( make. finish_with_mappings ( ) ) ;
180181 builder. add_file_edits ( ctx. vfs_file_id ( ) , editor) ;
181182 } )
182183}
@@ -207,8 +208,8 @@ fn impl_def_from_trait(
207208 return None ;
208209 }
209210 let make = SyntaxFactory :: without_mappings ( ) ;
210- let trait_ty = make. ty_path ( trait_path. clone ( ) ) . into ( ) ;
211- let impl_def = generate_trait_impl ( & make, impl_is_unsafe, adt, trait_ty) ;
211+ let trait_ty: ast :: Type = make. ty_path ( trait_path. clone ( ) ) . into ( ) ;
212+ let impl_def = generate_trait_impl ( & make, impl_is_unsafe, adt, trait_ty. clone ( ) ) ;
212213
213214 let assoc_items = add_trait_assoc_items_to_impl_with_factory (
214215 & make,
@@ -223,7 +224,7 @@ fn impl_def_from_trait(
223224 assoc_items. split_first ( ) . map ( |( first, other) | ( first. clone_subtree ( ) , other) )
224225 {
225226 let first_item = if let ast:: AssocItem :: Fn ( ref func) = first
226- && let Some ( body) = gen_trait_fn_body ( func, trait_path, adt, None )
227+ && let Some ( body) = gen_trait_fn_body ( & make , func, trait_path, adt, None )
227228 && let Some ( func_body) = func. body ( )
228229 {
229230 let mut editor = SyntaxEditor :: new ( first. syntax ( ) . clone ( ) ) ;
@@ -239,21 +240,17 @@ fn impl_def_from_trait(
239240 make. assoc_item_list_empty ( )
240241 } ;
241242
242- let impl_def = impl_def. clone_subtree ( ) ;
243- let mut editor = SyntaxEditor :: new ( impl_def. syntax ( ) . clone ( ) ) ;
244- editor. replace ( impl_def. assoc_item_list ( ) ?. syntax ( ) , assoc_item_list. syntax ( ) ) ;
245- let impl_def = ast:: Impl :: cast ( editor. finish ( ) . new_root ( ) . clone ( ) ) ?;
246- Some ( impl_def)
243+ Some ( generate_trait_impl_with_item ( & make, impl_is_unsafe, adt, trait_ty, assoc_item_list) )
247244}
248245
249246fn update_attribute (
247+ make : & SyntaxFactory ,
250248 editor : & mut SyntaxEditor ,
251249 old_derives : & [ ast:: Path ] ,
252250 old_tree : & ast:: TokenTree ,
253251 old_trait_path : & ast:: Path ,
254252 attr : & ast:: Attr ,
255253) {
256- let make = SyntaxFactory :: without_mappings ( ) ;
257254 let new_derives = old_derives
258255 . iter ( )
259256 . filter ( |t| t. to_string ( ) != old_trait_path. to_string ( ) )
0 commit comments