Skip to content

Commit d0f2db3

Browse files
committed
fix(generate_method): correct method indentation inside generated impl
1 parent 2b61be2 commit d0f2db3

File tree

1 file changed

+38
-47
lines changed

1 file changed

+38
-47
lines changed

crates/ide-assists/src/handlers/generate_function.rs

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,11 @@ fn add_func_to_accumulator(
174174
label: String,
175175
) -> Option<()> {
176176
acc.add(AssistId("generate_function", AssistKind::Generate), label, text_range, |builder| {
177-
let function_template = function_builder.render();
177+
let indent = IndentLevel::from_node(function_builder.target.syntax());
178+
let function_template = function_builder.render(adt_name.is_some());
178179
let mut func = function_template.to_string(ctx.config.snippet_cap);
179180
if let Some(name) = adt_name {
180-
func = format!("\nimpl {} {{\n{}\n}}", name, func);
181+
func = format!("\n{}impl {} {{\n{}\n{}}}", indent, name, func, indent);
181182
}
182183
builder.edit_file(file);
183184
match ctx.config.snippet_cap {
@@ -307,7 +308,7 @@ impl FunctionBuilder {
307308
})
308309
}
309310

310-
fn render(self) -> FunctionTemplate {
311+
fn render(self, is_method: bool) -> FunctionTemplate {
311312
let placeholder_expr = make::ext::expr_todo();
312313
let fn_body = make::block_expr(vec![], Some(placeholder_expr));
313314
let visibility = if self.needs_pub { Some(make::visibility_pub_crate()) } else { None };
@@ -325,8 +326,14 @@ impl FunctionBuilder {
325326

326327
match self.target {
327328
GeneratedFunctionTarget::BehindItem(it) => {
328-
let indent = IndentLevel::from_node(&it);
329-
leading_ws = format!("\n\n{}", indent);
329+
let mut indent = IndentLevel::from_node(&it);
330+
if is_method {
331+
indent = indent + 1;
332+
leading_ws = format!("{}", indent);
333+
} else {
334+
leading_ws = format!("\n\n{}", indent);
335+
}
336+
330337
fn_def = fn_def.indent(indent);
331338
trailing_ws = String::new();
332339
}
@@ -1470,11 +1477,9 @@ fn foo() {S.bar$0();}
14701477
struct S;
14711478
fn foo() {S.bar();}
14721479
impl S {
1473-
1474-
1475-
fn bar(&self) ${0:-> _} {
1476-
todo!()
1477-
}
1480+
fn bar(&self) ${0:-> _} {
1481+
todo!()
1482+
}
14781483
}
14791484
",
14801485
)
@@ -1516,14 +1521,12 @@ fn foo() {s::S.bar$0();}
15161521
r"
15171522
mod 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-
}
15271530
fn foo() {s::S.bar();}
15281531
",
15291532
)
@@ -1550,11 +1553,9 @@ mod s {
15501553
}
15511554
}
15521555
impl S {
1553-
1554-
1555-
fn bar(&self) ${0:-> _} {
1556-
todo!()
1557-
}
1556+
fn bar(&self) ${0:-> _} {
1557+
todo!()
1558+
}
15581559
}
15591560
15601561
",
@@ -1573,11 +1574,9 @@ fn foo() {$0S.bar();}
15731574
struct S;
15741575
fn foo() {S.bar();}
15751576
impl S {
1576-
1577-
1578-
fn bar(&self) ${0:-> _} {
1579-
todo!()
1580-
}
1577+
fn bar(&self) ${0:-> _} {
1578+
todo!()
1579+
}
15811580
}
15821581
",
15831582
)
@@ -1595,11 +1594,9 @@ fn foo() {S::bar$0();}
15951594
struct S;
15961595
fn foo() {S::bar();}
15971596
impl S {
1598-
1599-
1600-
fn bar() ${0:-> _} {
1601-
todo!()
1602-
}
1597+
fn bar() ${0:-> _} {
1598+
todo!()
1599+
}
16031600
}
16041601
",
16051602
)
@@ -1641,14 +1638,12 @@ fn foo() {s::S::bar$0();}
16411638
r"
16421639
mod 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-
}
16521647
fn foo() {s::S::bar();}
16531648
",
16541649
)
@@ -1666,11 +1661,9 @@ fn foo() {$0S::bar();}
16661661
struct S;
16671662
fn foo() {S::bar();}
16681663
impl S {
1669-
1670-
1671-
fn bar() ${0:-> _} {
1672-
todo!()
1673-
}
1664+
fn bar() ${0:-> _} {
1665+
todo!()
1666+
}
16741667
}
16751668
",
16761669
)
@@ -1845,11 +1838,9 @@ fn main() {
18451838
Foo::new();
18461839
}
18471840
impl Foo {
1848-
1849-
1850-
fn new() ${0:-> _} {
1851-
todo!()
1852-
}
1841+
fn new() ${0:-> _} {
1842+
todo!()
1843+
}
18531844
}
18541845
",
18551846
)

0 commit comments

Comments
 (0)