1
- use hir:: { HasSource , HirDisplay , Module , Semantics , TypeInfo } ;
1
+ use hir:: { Adt , HasSource , HirDisplay , Module , Semantics , TypeInfo } ;
2
2
use ide_db:: {
3
3
base_db:: FileId ,
4
4
defs:: { Definition , NameRefClass } ,
@@ -145,7 +145,8 @@ fn gen_method(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
145
145
return None ;
146
146
}
147
147
let ( impl_, file) = get_adt_source ( ctx, & adt, fn_name. text ( ) . as_str ( ) ) ?;
148
- let ( target, insert_offset) = get_method_target ( ctx, & target_module, & impl_) ?;
148
+ let ( target, insert_offset) = get_method_target ( ctx, & impl_, & adt) ?;
149
+
149
150
let function_builder =
150
151
FunctionBuilder :: from_method_call ( ctx, & call, & fn_name, target_module, target) ?;
151
152
let text_range = call. syntax ( ) . text_range ( ) ;
@@ -174,10 +175,11 @@ fn add_func_to_accumulator(
174
175
label : String ,
175
176
) -> Option < ( ) > {
176
177
acc. add ( AssistId ( "generate_function" , AssistKind :: Generate ) , label, text_range, |builder| {
177
- let function_template = function_builder. render ( ) ;
178
+ let indent = IndentLevel :: from_node ( function_builder. target . syntax ( ) ) ;
179
+ let function_template = function_builder. render ( adt_name. is_some ( ) ) ;
178
180
let mut func = function_template. to_string ( ctx. config . snippet_cap ) ;
179
181
if let Some ( name) = adt_name {
180
- func = format ! ( "\n impl {} {{\n {}\n }} " , name, func) ;
182
+ func = format ! ( "\n {}impl {} {{\n {}\n {}}} " , indent , name, func, indent ) ;
181
183
}
182
184
builder. edit_file ( file) ;
183
185
match ctx. config . snippet_cap {
@@ -307,7 +309,7 @@ impl FunctionBuilder {
307
309
} )
308
310
}
309
311
310
- fn render ( self ) -> FunctionTemplate {
312
+ fn render ( self , is_method : bool ) -> FunctionTemplate {
311
313
let placeholder_expr = make:: ext:: expr_todo ( ) ;
312
314
let fn_body = make:: block_expr ( vec ! [ ] , Some ( placeholder_expr) ) ;
313
315
let visibility = if self . needs_pub { Some ( make:: visibility_pub_crate ( ) ) } else { None } ;
@@ -325,8 +327,14 @@ impl FunctionBuilder {
325
327
326
328
match self . target {
327
329
GeneratedFunctionTarget :: BehindItem ( it) => {
328
- let indent = IndentLevel :: from_node ( & it) ;
329
- leading_ws = format ! ( "\n \n {}" , indent) ;
330
+ let mut indent = IndentLevel :: from_node ( & it) ;
331
+ if is_method {
332
+ indent = indent + 1 ;
333
+ leading_ws = format ! ( "{}" , indent) ;
334
+ } else {
335
+ leading_ws = format ! ( "\n \n {}" , indent) ;
336
+ }
337
+
330
338
fn_def = fn_def. indent ( indent) ;
331
339
trailing_ws = String :: new ( ) ;
332
340
}
@@ -411,14 +419,13 @@ fn get_fn_target(
411
419
412
420
fn get_method_target (
413
421
ctx : & AssistContext < ' _ > ,
414
- target_module : & Module ,
415
422
impl_ : & Option < ast:: Impl > ,
423
+ adt : & Adt ,
416
424
) -> Option < ( GeneratedFunctionTarget , TextSize ) > {
417
425
let target = match impl_ {
418
426
Some ( impl_) => next_space_for_fn_in_impl ( impl_) ?,
419
427
None => {
420
- next_space_for_fn_in_module ( ctx. sema . db , & target_module. definition_source ( ctx. sema . db ) ) ?
421
- . 1
428
+ GeneratedFunctionTarget :: BehindItem ( adt. source ( ctx. sema . db ) ?. syntax ( ) . value . clone ( ) )
422
429
}
423
430
} ;
424
431
Some ( ( target. clone ( ) , get_insert_offset ( & target) ) )
@@ -437,7 +444,7 @@ fn assoc_fn_target_info(
437
444
return None ;
438
445
}
439
446
let ( impl_, file) = get_adt_source ( ctx, & adt, fn_name) ?;
440
- let ( target, insert_offset) = get_method_target ( ctx, & module , & impl_ ) ?;
447
+ let ( target, insert_offset) = get_method_target ( ctx, & impl_ , & adt ) ?;
441
448
let adt_name = if impl_. is_none ( ) { Some ( adt. name ( ctx. sema . db ) ) } else { None } ;
442
449
Some ( TargetInfo :: new ( target_module, adt_name, target, file, insert_offset) )
443
450
}
@@ -1468,14 +1475,12 @@ fn foo() {S.bar$0();}
1468
1475
" ,
1469
1476
r"
1470
1477
struct S;
1471
- fn foo() {S.bar();}
1472
1478
impl S {
1473
-
1474
-
1475
- fn bar(&self) ${0:-> _} {
1476
- todo!()
1477
- }
1479
+ fn bar(&self) ${0:-> _} {
1480
+ todo!()
1481
+ }
1478
1482
}
1483
+ fn foo() {S.bar();}
1479
1484
" ,
1480
1485
)
1481
1486
}
@@ -1516,14 +1521,12 @@ fn foo() {s::S.bar$0();}
1516
1521
r"
1517
1522
mod s {
1518
1523
pub struct S;
1519
- impl S {
1520
-
1521
-
1522
- pub(crate) fn bar(&self) ${0:-> _} {
1523
- todo!()
1524
+ impl S {
1525
+ pub(crate) fn bar(&self) ${0:-> _} {
1526
+ todo!()
1527
+ }
1524
1528
}
1525
1529
}
1526
- }
1527
1530
fn foo() {s::S.bar();}
1528
1531
" ,
1529
1532
)
@@ -1544,18 +1547,16 @@ mod s {
1544
1547
" ,
1545
1548
r"
1546
1549
struct S;
1550
+ impl S {
1551
+ fn bar(&self) ${0:-> _} {
1552
+ todo!()
1553
+ }
1554
+ }
1547
1555
mod s {
1548
1556
fn foo() {
1549
1557
super::S.bar();
1550
1558
}
1551
1559
}
1552
- impl S {
1553
-
1554
-
1555
- fn bar(&self) ${0:-> _} {
1556
- todo!()
1557
- }
1558
- }
1559
1560
1560
1561
" ,
1561
1562
)
@@ -1571,14 +1572,12 @@ fn foo() {$0S.bar();}
1571
1572
" ,
1572
1573
r"
1573
1574
struct S;
1574
- fn foo() {S.bar();}
1575
1575
impl S {
1576
-
1577
-
1578
- fn bar(&self) ${0:-> _} {
1579
- todo!()
1580
- }
1576
+ fn bar(&self) ${0:-> _} {
1577
+ todo!()
1578
+ }
1581
1579
}
1580
+ fn foo() {S.bar();}
1582
1581
" ,
1583
1582
)
1584
1583
}
@@ -1593,14 +1592,12 @@ fn foo() {S::bar$0();}
1593
1592
" ,
1594
1593
r"
1595
1594
struct S;
1596
- fn foo() {S::bar();}
1597
1595
impl S {
1598
-
1599
-
1600
- fn bar() ${0:-> _} {
1601
- todo!()
1602
- }
1596
+ fn bar() ${0:-> _} {
1597
+ todo!()
1598
+ }
1603
1599
}
1600
+ fn foo() {S::bar();}
1604
1601
" ,
1605
1602
)
1606
1603
}
@@ -1641,14 +1638,12 @@ fn foo() {s::S::bar$0();}
1641
1638
r"
1642
1639
mod s {
1643
1640
pub struct S;
1644
- impl S {
1645
-
1646
-
1647
- pub(crate) fn bar() ${0:-> _} {
1648
- todo!()
1641
+ impl S {
1642
+ pub(crate) fn bar() ${0:-> _} {
1643
+ todo!()
1644
+ }
1649
1645
}
1650
1646
}
1651
- }
1652
1647
fn foo() {s::S::bar();}
1653
1648
" ,
1654
1649
)
@@ -1664,14 +1659,12 @@ fn foo() {$0S::bar();}
1664
1659
" ,
1665
1660
r"
1666
1661
struct S;
1667
- fn foo() {S::bar();}
1668
1662
impl S {
1669
-
1670
-
1671
- fn bar() ${0:-> _} {
1672
- todo!()
1673
- }
1663
+ fn bar() ${0:-> _} {
1664
+ todo!()
1665
+ }
1674
1666
}
1667
+ fn foo() {S::bar();}
1675
1668
" ,
1676
1669
)
1677
1670
}
@@ -1841,15 +1834,13 @@ fn main() {
1841
1834
" ,
1842
1835
r"
1843
1836
enum Foo {}
1844
- fn main() {
1845
- Foo::new();
1846
- }
1847
1837
impl Foo {
1848
-
1849
-
1850
- fn new() ${0:-> _} {
1851
- todo!()
1838
+ fn new() ${0:-> _} {
1839
+ todo!()
1840
+ }
1852
1841
}
1842
+ fn main() {
1843
+ Foo::new();
1853
1844
}
1854
1845
" ,
1855
1846
)
0 commit comments