fix(generate_method): correct method indentation inside generated impl

This commit is contained in:
feniljain 2022-09-15 17:22:44 +05:30
parent 2b61be2975
commit d0f2db3bf4

View file

@ -174,10 +174,11 @@ fn add_func_to_accumulator(
label: String, label: String,
) -> Option<()> { ) -> Option<()> {
acc.add(AssistId("generate_function", AssistKind::Generate), label, text_range, |builder| { acc.add(AssistId("generate_function", AssistKind::Generate), label, text_range, |builder| {
let function_template = function_builder.render(); let indent = IndentLevel::from_node(function_builder.target.syntax());
let function_template = function_builder.render(adt_name.is_some());
let mut func = function_template.to_string(ctx.config.snippet_cap); let mut func = function_template.to_string(ctx.config.snippet_cap);
if let Some(name) = adt_name { if let Some(name) = adt_name {
func = format!("\nimpl {} {{\n{}\n}}", name, func); func = format!("\n{}impl {} {{\n{}\n{}}}", indent, name, func, indent);
} }
builder.edit_file(file); builder.edit_file(file);
match ctx.config.snippet_cap { match ctx.config.snippet_cap {
@ -307,7 +308,7 @@ impl FunctionBuilder {
}) })
} }
fn render(self) -> FunctionTemplate { fn render(self, is_method: bool) -> FunctionTemplate {
let placeholder_expr = make::ext::expr_todo(); let placeholder_expr = make::ext::expr_todo();
let fn_body = make::block_expr(vec![], Some(placeholder_expr)); let fn_body = make::block_expr(vec![], Some(placeholder_expr));
let visibility = if self.needs_pub { Some(make::visibility_pub_crate()) } else { None }; let visibility = if self.needs_pub { Some(make::visibility_pub_crate()) } else { None };
@ -325,8 +326,14 @@ impl FunctionBuilder {
match self.target { match self.target {
GeneratedFunctionTarget::BehindItem(it) => { GeneratedFunctionTarget::BehindItem(it) => {
let indent = IndentLevel::from_node(&it); let mut indent = IndentLevel::from_node(&it);
leading_ws = format!("\n\n{}", indent); if is_method {
indent = indent + 1;
leading_ws = format!("{}", indent);
} else {
leading_ws = format!("\n\n{}", indent);
}
fn_def = fn_def.indent(indent); fn_def = fn_def.indent(indent);
trailing_ws = String::new(); trailing_ws = String::new();
} }
@ -1470,11 +1477,9 @@ fn foo() {S.bar$0();}
struct S; struct S;
fn foo() {S.bar();} fn foo() {S.bar();}
impl S { impl S {
fn bar(&self) ${0:-> _} {
todo!()
fn bar(&self) ${0:-> _} { }
todo!()
}
} }
", ",
) )
@ -1516,14 +1521,12 @@ fn foo() {s::S.bar$0();}
r" r"
mod s { mod s {
pub struct S; pub struct S;
impl S { impl S {
pub(crate) fn bar(&self) ${0:-> _} {
todo!()
pub(crate) fn bar(&self) ${0:-> _} { }
todo!()
} }
} }
}
fn foo() {s::S.bar();} fn foo() {s::S.bar();}
", ",
) )
@ -1550,11 +1553,9 @@ mod s {
} }
} }
impl S { impl S {
fn bar(&self) ${0:-> _} {
todo!()
fn bar(&self) ${0:-> _} { }
todo!()
}
} }
", ",
@ -1573,11 +1574,9 @@ fn foo() {$0S.bar();}
struct S; struct S;
fn foo() {S.bar();} fn foo() {S.bar();}
impl S { impl S {
fn bar(&self) ${0:-> _} {
todo!()
fn bar(&self) ${0:-> _} { }
todo!()
}
} }
", ",
) )
@ -1595,11 +1594,9 @@ fn foo() {S::bar$0();}
struct S; struct S;
fn foo() {S::bar();} fn foo() {S::bar();}
impl S { impl S {
fn bar() ${0:-> _} {
todo!()
fn bar() ${0:-> _} { }
todo!()
}
} }
", ",
) )
@ -1641,14 +1638,12 @@ fn foo() {s::S::bar$0();}
r" r"
mod s { mod s {
pub struct S; pub struct S;
impl S { impl S {
pub(crate) fn bar() ${0:-> _} {
todo!()
pub(crate) fn bar() ${0:-> _} { }
todo!()
} }
} }
}
fn foo() {s::S::bar();} fn foo() {s::S::bar();}
", ",
) )
@ -1666,11 +1661,9 @@ fn foo() {$0S::bar();}
struct S; struct S;
fn foo() {S::bar();} fn foo() {S::bar();}
impl S { impl S {
fn bar() ${0:-> _} {
todo!()
fn bar() ${0:-> _} { }
todo!()
}
} }
", ",
) )
@ -1845,11 +1838,9 @@ fn main() {
Foo::new(); Foo::new();
} }
impl Foo { impl Foo {
fn new() ${0:-> _} {
todo!()
fn new() ${0:-> _} { }
todo!()
}
} }
", ",
) )