1
1
use std:: collections:: HashSet ;
2
2
3
3
use 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
+ } ;
5
11
6
12
use 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} ,
8
14
AssistContext , AssistId , AssistKind , Assists , GroupLabel ,
9
15
} ;
10
- use syntax:: ast:: edit:: AstNodeEdit ;
11
16
12
17
// Assist: generate_delegate_methods
13
18
//
@@ -96,7 +101,7 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
96
101
AssistId ( "generate_delegate_methods" , AssistKind :: Generate ) ,
97
102
format ! ( "Generate delegate for `{field_name}.{name}()`" , ) ,
98
103
target,
99
- |builder | {
104
+ |edit | {
100
105
// Create the function
101
106
let method_source = match method. source ( ctx. db ( ) ) {
102
107
Some ( source) => source. value ,
@@ -135,32 +140,25 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
135
140
is_const,
136
141
is_unsafe,
137
142
)
138
- . indent ( ast:: edit:: IndentLevel ( 1 ) )
139
143
. clone_for_update ( ) ;
140
-
141
- let cursor = Cursor :: Before ( f. syntax ( ) ) ;
144
+ f. reindent_to ( IndentLevel ( 1 ) ) ;
142
145
143
146
// Create or update an impl block, attach the function to it,
144
147
// then insert into our code.
145
148
match impl_def {
146
149
Some ( impl_def) => {
147
150
// 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) ;
150
152
151
- // Attach the function to the impl block
153
+ // Attach the function to the impl block.
152
154
let assoc_items = impl_def. get_or_create_assoc_item_list ( ) ;
153
155
assoc_items. add_item ( f. clone ( ) . into ( ) ) ;
154
156
155
157
// 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) ;
164
162
}
165
163
}
166
164
None => {
@@ -178,22 +176,22 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
178
176
None ,
179
177
)
180
178
. clone_for_update ( ) ;
179
+
181
180
let assoc_items = impl_def. get_or_create_assoc_item_list ( ) ;
182
181
assoc_items. add_item ( f. clone ( ) . into ( ) ) ;
183
182
184
183
// 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)
197
195
}
198
196
}
199
197
}
0 commit comments