Skip to content

Commit

Permalink
Auto merge of #17982 - IvarWithoutBones:generate-impl-indent, r=Veykril
Browse files Browse the repository at this point in the history
fix: consider indentation in the "Generate impl" and "Generate trait impl" assists

This makes the generated impl's indentation match the ADT it targets, improving formatting when using nested modules inside of the same file or when defining types inside of a function. See the added tests for an example.

At first I tried to call some of the convenient helpers that delegate to `IndentLevel::increase_indent` on the generated impl, but as the comment on that function notes it does not indent the first token, making it inapplicable here. I hope the solution in this PR is acceptable, please let me know if I missed something :)
  • Loading branch information
bors committed Aug 30, 2024
2 parents 28142e4 + 29ecaa1 commit 16a29ce
Showing 1 changed file with 76 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
use syntax::{
ast::{self, make, AstNode, HasName},
ast::{self, edit_in_place::Indent, make, AstNode, HasName},
ted,
};

use crate::{utils, AssistContext, AssistId, AssistKind, Assists};

fn insert_impl(impl_: ast::Impl, nominal: &ast::Adt) {
let indent = nominal.indent_level();
ted::insert_all_raw(
ted::Position::after(nominal.syntax()),
vec![
// Add a blank line after the ADT, and indentation for the impl to match the ADT
make::tokens::whitespace(&format!("\n\n{indent}")).into(),
impl_.syntax().clone().into(),
],
);
}

// Assist: generate_impl
//
// Adds a new inherent impl for a type.
Expand Down Expand Up @@ -46,12 +58,7 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
}
}

// Add the impl after the adt
let nominal = edit.make_mut(nominal);
ted::insert_all_raw(
ted::Position::after(nominal.syntax()),
vec![make::tokens::blank_line().into(), impl_.syntax().clone().into()],
);
insert_impl(impl_, &edit.make_mut(nominal));
},
)
}
Expand Down Expand Up @@ -97,12 +104,7 @@ pub(crate) fn generate_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->
}
}

// Add the impl after the adt
let nominal = edit.make_mut(nominal);
ted::insert_all_raw(
ted::Position::after(nominal.syntax()),
vec![make::tokens::blank_line().into(), impl_.syntax().clone().into()],
);
insert_impl(impl_, &edit.make_mut(nominal));
},
)
}
Expand Down Expand Up @@ -418,4 +420,65 @@ mod tests {
"/// Has a lifetime parameter\nstruct Foo<'a, T: Foo<'a>> {}",
);
}

#[test]
fn add_impl_with_indent() {
check_assist(
generate_impl,
r#"
mod foo {
struct Bar$0 {}
}
"#,
r#"
mod foo {
struct Bar {}
impl Bar {$0}
}
"#,
);
}

#[test]
fn add_impl_with_multiple_indent() {
check_assist(
generate_impl,
r#"
mod foo {
fn bar() {
struct Baz$0 {}
}
}
"#,
r#"
mod foo {
fn bar() {
struct Baz {}
impl Baz {$0}
}
}
"#,
);
}

#[test]
fn add_trait_impl_with_indent() {
check_assist(
generate_trait_impl,
r#"
mod foo {
struct Bar$0 {}
}
"#,
r#"
mod foo {
struct Bar {}
impl ${0:_} for Bar {}
}
"#,
);
}
}

0 comments on commit 16a29ce

Please sign in to comment.