@@ -3,8 +3,7 @@ use std::collections::HashSet;
3
3
use hir:: { self , HasCrate , HasSource , HasVisibility } ;
4
4
use syntax:: {
5
5
ast:: {
6
- self , edit:: IndentLevel , edit_in_place:: Indent , make, AstNode , HasGenericParams , HasName ,
7
- HasVisibility as _,
6
+ self , edit_in_place:: Indent , make, AstNode , HasGenericParams , HasName , HasVisibility as _,
8
7
} ,
9
8
ted,
10
9
} ;
@@ -141,7 +140,6 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
141
140
is_unsafe,
142
141
)
143
142
. clone_for_update ( ) ;
144
- f. reindent_to ( IndentLevel ( 1 ) ) ;
145
143
146
144
// Create or update an impl block, attach the function to it,
147
145
// then insert into our code.
@@ -150,6 +148,9 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
150
148
// Remember where in our source our `impl` block lives.
151
149
let impl_def = edit. make_mut ( impl_def) ;
152
150
151
+ // Fixup function indentation.
152
+ f. reindent_to ( impl_def. indent_level ( ) + 1 ) ;
153
+
153
154
// Attach the function to the impl block.
154
155
let assoc_items = impl_def. get_or_create_assoc_item_list ( ) ;
155
156
assoc_items. add_item ( f. clone ( ) . into ( ) ) ;
@@ -177,15 +178,22 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
177
178
)
178
179
. clone_for_update ( ) ;
179
180
181
+ // Fixup function indentation.
182
+ f. reindent_to ( impl_def. indent_level ( ) + 1 ) ;
183
+
180
184
let assoc_items = impl_def. get_or_create_assoc_item_list ( ) ;
181
185
assoc_items. add_item ( f. clone ( ) . into ( ) ) ;
182
186
187
+ // Fixup impl_def indentation
188
+ let indent = strukt. indent_level ( ) ;
189
+ impl_def. reindent_to ( indent) ;
190
+
183
191
// Insert the impl block.
184
192
let strukt = edit. make_mut ( strukt. clone ( ) ) ;
185
193
ted:: insert_all (
186
194
ted:: Position :: after ( strukt. syntax ( ) ) ,
187
195
vec ! [
188
- make:: tokens:: blank_line ( ) . into( ) ,
196
+ make:: tokens:: whitespace ( & format! ( " \n \n {indent}" ) ) . into( ) ,
189
197
impl_def. syntax( ) . clone( ) . into( ) ,
190
198
] ,
191
199
) ;
@@ -242,6 +250,45 @@ impl Person {
242
250
) ;
243
251
}
244
252
253
+ #[ test]
254
+ fn test_generate_delegate_create_impl_block_match_indent ( ) {
255
+ check_assist (
256
+ generate_delegate_methods,
257
+ r#"
258
+ mod indent {
259
+ struct Age(u8);
260
+ impl Age {
261
+ fn age(&self) -> u8 {
262
+ self.0
263
+ }
264
+ }
265
+
266
+ struct Person {
267
+ ag$0e: Age,
268
+ }
269
+ }"# ,
270
+ r#"
271
+ mod indent {
272
+ struct Age(u8);
273
+ impl Age {
274
+ fn age(&self) -> u8 {
275
+ self.0
276
+ }
277
+ }
278
+
279
+ struct Person {
280
+ age: Age,
281
+ }
282
+
283
+ impl Person {
284
+ $0fn age(&self) -> u8 {
285
+ self.age.age()
286
+ }
287
+ }
288
+ }"# ,
289
+ ) ;
290
+ }
291
+
245
292
#[ test]
246
293
fn test_generate_delegate_update_impl_block ( ) {
247
294
check_assist (
@@ -279,6 +326,47 @@ impl Person {
279
326
) ;
280
327
}
281
328
329
+ #[ test]
330
+ fn test_generate_delegate_update_impl_block_match_indent ( ) {
331
+ check_assist (
332
+ generate_delegate_methods,
333
+ r#"
334
+ mod indent {
335
+ struct Age(u8);
336
+ impl Age {
337
+ fn age(&self) -> u8 {
338
+ self.0
339
+ }
340
+ }
341
+
342
+ struct Person {
343
+ ag$0e: Age,
344
+ }
345
+
346
+ impl Person {}
347
+ }"# ,
348
+ r#"
349
+ mod indent {
350
+ struct Age(u8);
351
+ impl Age {
352
+ fn age(&self) -> u8 {
353
+ self.0
354
+ }
355
+ }
356
+
357
+ struct Person {
358
+ age: Age,
359
+ }
360
+
361
+ impl Person {
362
+ $0fn age(&self) -> u8 {
363
+ self.age.age()
364
+ }
365
+ }
366
+ }"# ,
367
+ ) ;
368
+ }
369
+
282
370
#[ test]
283
371
fn test_generate_delegate_tuple_struct ( ) {
284
372
check_assist (
0 commit comments