@@ -118,7 +118,6 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
118
118
false ,
119
119
)
120
120
. clone_for_update ( ) ;
121
- fn_. indent ( 1 . into ( ) ) ;
122
121
123
122
// Add a tabstop before the name
124
123
if let Some ( cap) = ctx. config . snippet_cap {
@@ -127,26 +126,45 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
127
126
}
128
127
}
129
128
129
+ let mut post_indent = None ;
130
+
130
131
// Get the mutable version of the impl to modify
131
132
let impl_def = if let Some ( impl_def) = impl_def {
133
+ fn_. indent ( impl_def. indent_level ( ) + 1 ) ;
132
134
builder. make_mut ( impl_def)
133
135
} else {
134
136
// Generate a new impl to add the method to
135
137
let impl_def = generate_impl ( & ast:: Adt :: Struct ( strukt. clone ( ) ) ) ;
138
+ let indent_level = strukt. indent_level ( ) ;
139
+ post_indent = Some ( indent_level) ;
140
+
141
+ fn_. indent ( 1 . into ( ) ) ;
136
142
137
143
// Insert it after the adt
138
144
let strukt = builder. make_mut ( strukt. clone ( ) ) ;
139
145
140
146
ted:: insert_all_raw (
141
147
ted:: Position :: after ( strukt. syntax ( ) ) ,
142
- vec ! [ make:: tokens:: blank_line( ) . into( ) , impl_def. syntax( ) . clone( ) . into( ) ] ,
148
+ if indent_level. is_zero ( ) {
149
+ vec ! [ make:: tokens:: blank_line( ) . into( ) , impl_def. syntax( ) . clone( ) . into( ) ]
150
+ } else {
151
+ vec ! [
152
+ make:: tokens:: blank_line( ) . into( ) ,
153
+ make:: tokens:: whitespace( & indent_level. to_string( ) ) . into( ) ,
154
+ impl_def. syntax( ) . clone( ) . into( ) ,
155
+ ]
156
+ } ,
143
157
) ;
144
158
145
159
impl_def
146
160
} ;
147
161
148
162
// Add the `new` method at the start of the impl
149
163
impl_def. get_or_create_assoc_item_list ( ) . add_item_at_start ( fn_. into ( ) ) ;
164
+
165
+ if let Some ( indent_level) = post_indent {
166
+ impl_def. indent ( indent_level) ;
167
+ }
150
168
} )
151
169
}
152
170
@@ -425,6 +443,135 @@ impl Foo {
425
443
) ;
426
444
}
427
445
446
+ #[ test]
447
+ fn non_zero_indent ( ) {
448
+ check_assist (
449
+ generate_new,
450
+ r#"
451
+ mod foo {
452
+ struct $0Foo {}
453
+ }
454
+ "# ,
455
+ r#"
456
+ mod foo {
457
+ struct Foo {}
458
+
459
+ impl Foo {
460
+ fn $0new() -> Self {
461
+ Self { }
462
+ }
463
+ }
464
+ }
465
+ "# ,
466
+ ) ;
467
+ check_assist (
468
+ generate_new,
469
+ r#"
470
+ mod foo {
471
+ mod bar {
472
+ struct $0Foo {}
473
+ }
474
+ }
475
+ "# ,
476
+ r#"
477
+ mod foo {
478
+ mod bar {
479
+ struct Foo {}
480
+
481
+ impl Foo {
482
+ fn $0new() -> Self {
483
+ Self { }
484
+ }
485
+ }
486
+ }
487
+ }
488
+ "# ,
489
+ ) ;
490
+ check_assist (
491
+ generate_new,
492
+ r#"
493
+ mod foo {
494
+ struct $0Foo {}
495
+
496
+ impl Foo {
497
+ fn some() {}
498
+ }
499
+ }
500
+ "# ,
501
+ r#"
502
+ mod foo {
503
+ struct Foo {}
504
+
505
+ impl Foo {
506
+ fn $0new() -> Self {
507
+ Self { }
508
+ }
509
+
510
+ fn some() {}
511
+ }
512
+ }
513
+ "# ,
514
+ ) ;
515
+ check_assist (
516
+ generate_new,
517
+ r#"
518
+ mod foo {
519
+ mod bar {
520
+ struct $0Foo {}
521
+
522
+ impl Foo {
523
+ fn some() {}
524
+ }
525
+ }
526
+ }
527
+ "# ,
528
+ r#"
529
+ mod foo {
530
+ mod bar {
531
+ struct Foo {}
532
+
533
+ impl Foo {
534
+ fn $0new() -> Self {
535
+ Self { }
536
+ }
537
+
538
+ fn some() {}
539
+ }
540
+ }
541
+ }
542
+ "# ,
543
+ ) ;
544
+ check_assist (
545
+ generate_new,
546
+ r#"
547
+ mod foo {
548
+ mod bar {
549
+ struct $0Foo {}
550
+
551
+ impl Foo {
552
+ fn some() {}
553
+ }
554
+ }
555
+ }
556
+ "# ,
557
+ r#"
558
+ mod foo {
559
+ mod bar {
560
+ struct Foo {}
561
+
562
+ impl Foo {
563
+ fn $0new() -> Self {
564
+ Self { }
565
+ }
566
+
567
+ fn some() {}
568
+ }
569
+ }
570
+ }
571
+ "# ,
572
+ ) ;
573
+ }
574
+
428
575
#[ test]
429
576
fn check_visibility_of_new_fn_based_on_struct ( ) {
430
577
check_assist (
0 commit comments