Take into account parent indent when filling trait members
This commit is contained in:
parent
b3742873d9
commit
1df81f3d65
1 changed files with 40 additions and 8 deletions
|
@ -74,22 +74,25 @@ pub(crate) fn add_missing_impl_members(mut ctx: AssistCtx<impl HirDatabase>) ->
|
|||
}
|
||||
|
||||
ctx.add_action(AssistId("add_impl_missing_members"), "add missing impl members", |edit| {
|
||||
let indent = {
|
||||
let (parent_indent, indent) = {
|
||||
// FIXME: Find a way to get the indent already used in the file.
|
||||
// Now, we copy the indent of first item or indent with 4 spaces relative to impl block
|
||||
const DEFAULT_INDENT: &str = " ";
|
||||
let first_item = impl_item_list.impl_items().next();
|
||||
let first_item_indent = first_item.and_then(|i| leading_indent(i.syntax()));
|
||||
let impl_block_indent = || leading_indent(impl_node.syntax()).unwrap_or_default();
|
||||
let first_item_indent =
|
||||
first_item.and_then(|i| leading_indent(i.syntax())).map(ToOwned::to_owned);
|
||||
let impl_block_indent = leading_indent(impl_node.syntax()).unwrap_or_default();
|
||||
|
||||
first_item_indent
|
||||
.map(ToOwned::to_owned)
|
||||
.unwrap_or_else(|| impl_block_indent().to_owned() + DEFAULT_INDENT)
|
||||
(
|
||||
impl_block_indent.to_owned(),
|
||||
first_item_indent.unwrap_or_else(|| impl_block_indent.to_owned() + DEFAULT_INDENT),
|
||||
)
|
||||
};
|
||||
|
||||
let func_bodies = missing_fns.into_iter().map(build_func_body).join("\n");
|
||||
let func_bodies = String::from("\n") + &func_bodies;
|
||||
let func_bodies = reindent(&func_bodies, &indent) + "\n";
|
||||
let trailing_whitespace = format!("\n{}", parent_indent);
|
||||
let func_bodies = reindent(&func_bodies, &indent) + &trailing_whitespace;
|
||||
|
||||
let changed_range = {
|
||||
let last_whitespace = impl_item_list.syntax().children();
|
||||
|
@ -104,7 +107,9 @@ pub(crate) fn add_missing_impl_members(mut ctx: AssistCtx<impl HirDatabase>) ->
|
|||
let replaced_text_range = TextUnit::of_str(&func_bodies);
|
||||
|
||||
edit.replace(changed_range, func_bodies);
|
||||
edit.set_cursor(changed_range.start() + replaced_text_range - TextUnit::of_str("\n"));
|
||||
edit.set_cursor(
|
||||
changed_range.start() + replaced_text_range - TextUnit::of_str(&trailing_whitespace),
|
||||
);
|
||||
});
|
||||
|
||||
ctx.build()
|
||||
|
@ -241,6 +246,33 @@ trait Foo {
|
|||
struct S;
|
||||
impl Foo for S {
|
||||
fn valid(some: u32) -> bool { false }<|>
|
||||
}",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_indented_impl_block() {
|
||||
check_assist(
|
||||
add_missing_impl_members,
|
||||
"
|
||||
trait Foo {
|
||||
fn valid(some: u32) -> bool { false }
|
||||
}
|
||||
struct S;
|
||||
|
||||
mod my_mod {
|
||||
impl crate::Foo for S { <|> }
|
||||
}",
|
||||
"
|
||||
trait Foo {
|
||||
fn valid(some: u32) -> bool { false }
|
||||
}
|
||||
struct S;
|
||||
|
||||
mod my_mod {
|
||||
impl crate::Foo for S {
|
||||
fn valid(some: u32) -> bool { false }<|>
|
||||
}
|
||||
}",
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue