11use std:: collections:: HashSet ;
22
33use hir:: { self , HasCrate , HasSource , HasVisibility } ;
4- use syntax:: ast:: { self , make, AstNode , HasGenericParams , HasName , HasVisibility as _} ;
4+ use syntax:: {
5+ ast:: {
6+ self , edit:: IndentLevel , edit_in_place:: Indent , make, AstNode , HasGenericParams , HasName ,
7+ HasVisibility as _,
8+ } ,
9+ ted,
10+ } ;
511
612use crate :: {
7- utils:: { convert_param_list_to_arg_list, find_struct_impl, render_snippet , Cursor } ,
13+ utils:: { convert_param_list_to_arg_list, find_struct_impl} ,
814 AssistContext , AssistId , AssistKind , Assists , GroupLabel ,
915} ;
10- use syntax:: ast:: edit:: AstNodeEdit ;
1116
1217// Assist: generate_delegate_methods
1318//
@@ -96,7 +101,7 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
96101 AssistId ( "generate_delegate_methods" , AssistKind :: Generate ) ,
97102 format ! ( "Generate delegate for `{field_name}.{name}()`" , ) ,
98103 target,
99- |builder | {
104+ |edit | {
100105 // Create the function
101106 let method_source = match method. source ( ctx. db ( ) ) {
102107 Some ( source) => source. value ,
@@ -135,32 +140,25 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
135140 is_const,
136141 is_unsafe,
137142 )
138- . indent ( ast:: edit:: IndentLevel ( 1 ) )
139143 . clone_for_update ( ) ;
140-
141- let cursor = Cursor :: Before ( f. syntax ( ) ) ;
144+ f. reindent_to ( IndentLevel ( 1 ) ) ;
142145
143146 // Create or update an impl block, attach the function to it,
144147 // then insert into our code.
145148 match impl_def {
146149 Some ( impl_def) => {
147150 // Remember where in our source our `impl` block lives.
148- let impl_def = impl_def. clone_for_update ( ) ;
149- let old_range = impl_def. syntax ( ) . text_range ( ) ;
151+ let impl_def = edit. make_mut ( impl_def) ;
150152
151- // Attach the function to the impl block
153+ // Attach the function to the impl block.
152154 let assoc_items = impl_def. get_or_create_assoc_item_list ( ) ;
153155 assoc_items. add_item ( f. clone ( ) . into ( ) ) ;
154156
155157 // Update the impl block.
156- match ctx. config . snippet_cap {
157- Some ( cap) => {
158- let snippet = render_snippet ( cap, impl_def. syntax ( ) , cursor) ;
159- builder. replace_snippet ( cap, old_range, snippet) ;
160- }
161- None => {
162- builder. replace ( old_range, impl_def. syntax ( ) . to_string ( ) ) ;
163- }
158+ ted:: replace ( impl_def. syntax ( ) , impl_def. syntax ( ) ) ;
159+
160+ if let Some ( cap) = ctx. config . snippet_cap {
161+ edit. add_tabstop_before ( cap, f) ;
164162 }
165163 }
166164 None => {
@@ -178,22 +176,22 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
178176 None ,
179177 )
180178 . clone_for_update ( ) ;
179+
181180 let assoc_items = impl_def. get_or_create_assoc_item_list ( ) ;
182181 assoc_items. add_item ( f. clone ( ) . into ( ) ) ;
183182
184183 // Insert the impl block.
185- match ctx. config . snippet_cap {
186- Some ( cap) => {
187- let offset = strukt. syntax ( ) . text_range ( ) . end ( ) ;
188- let snippet = render_snippet ( cap, impl_def. syntax ( ) , cursor) ;
189- let snippet = format ! ( "\n \n {snippet}" ) ;
190- builder. insert_snippet ( cap, offset, snippet) ;
191- }
192- None => {
193- let offset = strukt. syntax ( ) . text_range ( ) . end ( ) ;
194- let snippet = format ! ( "\n \n {}" , impl_def. syntax( ) ) ;
195- builder. insert ( offset, snippet) ;
196- }
184+ let strukt = edit. make_mut ( strukt. clone ( ) ) ;
185+ ted:: insert_all (
186+ ted:: Position :: after ( strukt. syntax ( ) ) ,
187+ vec ! [
188+ make:: tokens:: blank_line( ) . into( ) ,
189+ impl_def. syntax( ) . clone( ) . into( ) ,
190+ ] ,
191+ ) ;
192+
193+ if let Some ( cap) = ctx. config . snippet_cap {
194+ edit. add_tabstop_before ( cap, f)
197195 }
198196 }
199197 }
0 commit comments