1- use hir:: { HasSource , HirDisplay , Module , Semantics , TypeInfo } ;
1+ use hir:: { Adt , HasSource , HirDisplay , Module , Semantics , TypeInfo } ;
22use ide_db:: {
33 base_db:: FileId ,
44 defs:: { Definition , NameRefClass } ,
@@ -145,7 +145,8 @@ fn gen_method(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
145145 return None ;
146146 }
147147 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+
149150 let function_builder =
150151 FunctionBuilder :: from_method_call ( ctx, & call, & fn_name, target_module, target) ?;
151152 let text_range = call. syntax ( ) . text_range ( ) ;
@@ -174,10 +175,11 @@ fn add_func_to_accumulator(
174175 label : String ,
175176) -> Option < ( ) > {
176177 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 ( ) ) ;
178180 let mut func = function_template. to_string ( ctx. config . snippet_cap ) ;
179181 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 ) ;
181183 }
182184 builder. edit_file ( file) ;
183185 match ctx. config . snippet_cap {
@@ -307,7 +309,7 @@ impl FunctionBuilder {
307309 } )
308310 }
309311
310- fn render ( self ) -> FunctionTemplate {
312+ fn render ( self , is_method : bool ) -> FunctionTemplate {
311313 let placeholder_expr = make:: ext:: expr_todo ( ) ;
312314 let fn_body = make:: block_expr ( vec ! [ ] , Some ( placeholder_expr) ) ;
313315 let visibility = if self . needs_pub { Some ( make:: visibility_pub_crate ( ) ) } else { None } ;
@@ -325,8 +327,14 @@ impl FunctionBuilder {
325327
326328 match self . target {
327329 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+
330338 fn_def = fn_def. indent ( indent) ;
331339 trailing_ws = String :: new ( ) ;
332340 }
@@ -411,14 +419,13 @@ fn get_fn_target(
411419
412420fn get_method_target (
413421 ctx : & AssistContext < ' _ > ,
414- target_module : & Module ,
415422 impl_ : & Option < ast:: Impl > ,
423+ adt : & Adt ,
416424) -> Option < ( GeneratedFunctionTarget , TextSize ) > {
417425 let target = match impl_ {
418426 Some ( impl_) => next_space_for_fn_in_impl ( impl_) ?,
419427 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 ( ) )
422429 }
423430 } ;
424431 Some ( ( target. clone ( ) , get_insert_offset ( & target) ) )
@@ -437,7 +444,7 @@ fn assoc_fn_target_info(
437444 return None ;
438445 }
439446 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 ) ?;
441448 let adt_name = if impl_. is_none ( ) { Some ( adt. name ( ctx. sema . db ) ) } else { None } ;
442449 Some ( TargetInfo :: new ( target_module, adt_name, target, file, insert_offset) )
443450}
@@ -1468,14 +1475,12 @@ fn foo() {S.bar$0();}
14681475" ,
14691476 r"
14701477struct S;
1471- fn foo() {S.bar();}
14721478impl S {
1473-
1474-
1475- fn bar(&self) ${0:-> _} {
1476- todo!()
1477- }
1479+ fn bar(&self) ${0:-> _} {
1480+ todo!()
1481+ }
14781482}
1483+ fn foo() {S.bar();}
14791484" ,
14801485 )
14811486 }
@@ -1516,14 +1521,12 @@ fn foo() {s::S.bar$0();}
15161521 r"
15171522mod s {
15181523 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+ }
15241528 }
15251529}
1526- }
15271530fn foo() {s::S.bar();}
15281531" ,
15291532 )
@@ -1544,18 +1547,16 @@ mod s {
15441547" ,
15451548 r"
15461549struct S;
1550+ impl S {
1551+ fn bar(&self) ${0:-> _} {
1552+ todo!()
1553+ }
1554+ }
15471555mod s {
15481556 fn foo() {
15491557 super::S.bar();
15501558 }
15511559}
1552- impl S {
1553-
1554-
1555- fn bar(&self) ${0:-> _} {
1556- todo!()
1557- }
1558- }
15591560
15601561" ,
15611562 )
@@ -1571,14 +1572,12 @@ fn foo() {$0S.bar();}
15711572" ,
15721573 r"
15731574struct S;
1574- fn foo() {S.bar();}
15751575impl S {
1576-
1577-
1578- fn bar(&self) ${0:-> _} {
1579- todo!()
1580- }
1576+ fn bar(&self) ${0:-> _} {
1577+ todo!()
1578+ }
15811579}
1580+ fn foo() {S.bar();}
15821581" ,
15831582 )
15841583 }
@@ -1593,14 +1592,12 @@ fn foo() {S::bar$0();}
15931592" ,
15941593 r"
15951594struct S;
1596- fn foo() {S::bar();}
15971595impl S {
1598-
1599-
1600- fn bar() ${0:-> _} {
1601- todo!()
1602- }
1596+ fn bar() ${0:-> _} {
1597+ todo!()
1598+ }
16031599}
1600+ fn foo() {S::bar();}
16041601" ,
16051602 )
16061603 }
@@ -1641,14 +1638,12 @@ fn foo() {s::S::bar$0();}
16411638 r"
16421639mod s {
16431640 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+ }
16491645 }
16501646}
1651- }
16521647fn foo() {s::S::bar();}
16531648" ,
16541649 )
@@ -1664,14 +1659,12 @@ fn foo() {$0S::bar();}
16641659" ,
16651660 r"
16661661struct S;
1667- fn foo() {S::bar();}
16681662impl S {
1669-
1670-
1671- fn bar() ${0:-> _} {
1672- todo!()
1673- }
1663+ fn bar() ${0:-> _} {
1664+ todo!()
1665+ }
16741666}
1667+ fn foo() {S::bar();}
16751668" ,
16761669 )
16771670 }
@@ -1841,15 +1834,13 @@ fn main() {
18411834" ,
18421835 r"
18431836enum Foo {}
1844- fn main() {
1845- Foo::new();
1846- }
18471837impl Foo {
1848-
1849-
1850- fn new() ${0:-> _} {
1851- todo!()
1838+ fn new() ${0:-> _} {
1839+ todo!()
1840+ }
18521841}
1842+ fn main() {
1843+ Foo::new();
18531844}
18541845" ,
18551846 )
0 commit comments