@@ -3,8 +3,7 @@ use std::collections::HashSet;
33use hir:: { self , HasCrate , HasSource , HasVisibility } ;
44use syntax:: {
55 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 _,
87 } ,
98 ted,
109} ;
@@ -141,7 +140,6 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
141140 is_unsafe,
142141 )
143142 . clone_for_update ( ) ;
144- f. reindent_to ( IndentLevel ( 1 ) ) ;
145143
146144 // Create or update an impl block, attach the function to it,
147145 // then insert into our code.
@@ -150,6 +148,9 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
150148 // Remember where in our source our `impl` block lives.
151149 let impl_def = edit. make_mut ( impl_def) ;
152150
151+ // Fixup function indentation.
152+ f. reindent_to ( impl_def. indent_level ( ) + 1 ) ;
153+
153154 // Attach the function to the impl block.
154155 let assoc_items = impl_def. get_or_create_assoc_item_list ( ) ;
155156 assoc_items. add_item ( f. clone ( ) . into ( ) ) ;
@@ -177,15 +178,22 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
177178 )
178179 . clone_for_update ( ) ;
179180
181+ // Fixup function indentation.
182+ f. reindent_to ( impl_def. indent_level ( ) + 1 ) ;
183+
180184 let assoc_items = impl_def. get_or_create_assoc_item_list ( ) ;
181185 assoc_items. add_item ( f. clone ( ) . into ( ) ) ;
182186
187+ // Fixup impl_def indentation
188+ let indent = strukt. indent_level ( ) ;
189+ impl_def. reindent_to ( indent) ;
190+
183191 // Insert the impl block.
184192 let strukt = edit. make_mut ( strukt. clone ( ) ) ;
185193 ted:: insert_all (
186194 ted:: Position :: after ( strukt. syntax ( ) ) ,
187195 vec ! [
188- make:: tokens:: blank_line ( ) . into( ) ,
196+ make:: tokens:: whitespace ( & format! ( " \n \n {indent}" ) ) . into( ) ,
189197 impl_def. syntax( ) . clone( ) . into( ) ,
190198 ] ,
191199 ) ;
@@ -242,6 +250,45 @@ impl Person {
242250 ) ;
243251 }
244252
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+
245292 #[ test]
246293 fn test_generate_delegate_update_impl_block ( ) {
247294 check_assist (
@@ -279,6 +326,47 @@ impl Person {
279326 ) ;
280327 }
281328
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+
282370 #[ test]
283371 fn test_generate_delegate_tuple_struct ( ) {
284372 check_assist (
0 commit comments