11use syntax:: {
2- ast:: { self , AstNode , HasGenericParams , HasName , edit:: AstNodeEdit , make} ,
2+ ast:: {
3+ self , AstNode , HasGenericParams , HasName , edit:: AstNodeEdit , syntax_factory:: SyntaxFactory ,
4+ } ,
35 syntax_editor:: { Position , SyntaxEditor } ,
46} ;
57
68use crate :: {
79 AssistContext , AssistId , Assists ,
8- utils:: { self , DefaultMethods , IgnoreAssocItems } ,
10+ utils:: {
11+ self , DefaultMethods , IgnoreAssocItems , generate_impl_with_factory,
12+ generate_trait_impl_intransitive,
13+ } ,
914} ;
1015
1116fn insert_impl (
1217 editor : & mut SyntaxEditor ,
18+ make : & SyntaxFactory ,
1319 impl_ : & ast:: Impl ,
1420 nominal : & impl AstNodeEdit ,
1521) -> ast:: Impl {
@@ -20,7 +26,7 @@ fn insert_impl(
2026 Position :: after ( nominal. syntax ( ) ) ,
2127 vec ! [
2228 // Add a blank line after the ADT, and indentation for the impl to match the ADT
23- make:: tokens :: whitespace( & format!( "\n \n {indent}" ) ) . into( ) ,
29+ make. whitespace( & format!( "\n \n {indent}" ) ) . into( ) ,
2430 impl_. syntax( ) . clone( ) . into( ) ,
2531 ] ,
2632 ) ;
@@ -59,12 +65,13 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
5965 format ! ( "Generate impl for `{name}`" ) ,
6066 target,
6167 |edit| {
68+ let make = SyntaxFactory :: with_mappings ( ) ;
6269 // Generate the impl
63- let impl_ = utils :: generate_impl ( & nominal) ;
70+ let impl_ = generate_impl_with_factory ( & make , & nominal) ;
6471
6572 let mut editor = edit. make_editor ( nominal. syntax ( ) ) ;
6673
67- let impl_ = insert_impl ( & mut editor, & impl_, & nominal) ;
74+ let impl_ = insert_impl ( & mut editor, & make , & impl_, & nominal) ;
6875 // Add a tabstop after the left curly brace
6976 if let Some ( cap) = ctx. config . snippet_cap
7077 && let Some ( l_curly) = impl_. assoc_item_list ( ) . and_then ( |it| it. l_curly_token ( ) )
@@ -73,6 +80,7 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
7380 editor. add_annotation ( l_curly, tabstop) ;
7481 }
7582
83+ editor. add_mappings ( make. finish_with_mappings ( ) ) ;
7684 edit. add_file_edits ( ctx. vfs_file_id ( ) , editor) ;
7785 } ,
7886 )
@@ -109,12 +117,13 @@ pub(crate) fn generate_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->
109117 format ! ( "Generate trait impl for `{name}`" ) ,
110118 target,
111119 |edit| {
120+ let make = SyntaxFactory :: with_mappings ( ) ;
112121 // Generate the impl
113- let impl_ = utils :: generate_trait_impl_intransitive ( & nominal, make:: ty_placeholder ( ) ) ;
122+ let impl_ = generate_trait_impl_intransitive ( & make , & nominal, make. ty_placeholder ( ) ) ;
114123
115124 let mut editor = edit. make_editor ( nominal. syntax ( ) ) ;
116125
117- let impl_ = insert_impl ( & mut editor, & impl_, & nominal) ;
126+ let impl_ = insert_impl ( & mut editor, & make , & impl_, & nominal) ;
118127 // Make the trait type a placeholder snippet
119128 if let Some ( cap) = ctx. config . snippet_cap {
120129 if let Some ( trait_) = impl_. trait_ ( ) {
@@ -128,6 +137,7 @@ pub(crate) fn generate_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->
128137 }
129138 }
130139
140+ editor. add_mappings ( make. finish_with_mappings ( ) ) ;
131141 edit. add_file_edits ( ctx. vfs_file_id ( ) , editor) ;
132142 } ,
133143 )
@@ -166,9 +176,10 @@ pub(crate) fn generate_impl_trait(acc: &mut Assists, ctx: &AssistContext<'_>) ->
166176 format ! ( "Generate `{name}` impl for type" ) ,
167177 target,
168178 |edit| {
179+ let make = SyntaxFactory :: with_mappings ( ) ;
169180 let mut editor = edit. make_editor ( trait_. syntax ( ) ) ;
170181
171- let holder_arg = ast:: GenericArg :: TypeArg ( make:: type_arg ( make:: ty_placeholder ( ) ) ) ;
182+ let holder_arg = ast:: GenericArg :: TypeArg ( make. type_arg ( make. ty_placeholder ( ) ) ) ;
172183 let missing_items = utils:: filter_assoc_items (
173184 & ctx. sema ,
174185 & hir_trait. items ( ctx. db ( ) ) ,
@@ -177,25 +188,24 @@ pub(crate) fn generate_impl_trait(acc: &mut Assists, ctx: &AssistContext<'_>) ->
177188 ) ;
178189
179190 let trait_gen_args = trait_. generic_param_list ( ) . map ( |list| {
180- make:: generic_arg_list ( list. generic_params ( ) . map ( |_| holder_arg. clone ( ) ) )
191+ make. generic_arg_list ( list. generic_params ( ) . map ( |_| holder_arg. clone ( ) ) , false )
181192 } ) ;
182193
183194 let make_impl_ = |body| {
184- make:: impl_trait (
195+ make. impl_trait (
185196 None ,
186197 trait_. unsafe_token ( ) . is_some ( ) ,
187198 None ,
188199 trait_gen_args. clone ( ) ,
189200 None ,
190201 None ,
191202 false ,
192- make:: ty ( & name. text ( ) ) ,
193- make:: ty_placeholder ( ) ,
203+ make. ty ( & name. text ( ) ) ,
204+ make. ty_placeholder ( ) ,
194205 None ,
195206 None ,
196207 body,
197208 )
198- . clone_for_update ( )
199209 } ;
200210
201211 let impl_ = if missing_items. is_empty ( ) {
@@ -210,11 +220,12 @@ pub(crate) fn generate_impl_trait(acc: &mut Assists, ctx: &AssistContext<'_>) ->
210220 & impl_,
211221 & target_scope,
212222 ) ;
213- let assoc_item_list = make:: assoc_item_list ( Some ( assoc_items) ) ;
223+ let assoc_item_list = make. assoc_item_list ( assoc_items) ;
214224 make_impl_ ( Some ( assoc_item_list) )
215225 } ;
216226
217- let impl_ = insert_impl ( & mut editor, & impl_, & trait_) ;
227+ let impl_ = insert_impl ( & mut editor, & make, & impl_, & trait_) ;
228+ editor. add_mappings ( make. finish_with_mappings ( ) ) ;
218229
219230 if let Some ( cap) = ctx. config . snippet_cap {
220231 if let Some ( generics) = impl_. trait_ ( ) . and_then ( |it| it. generic_arg_list ( ) ) {
0 commit comments