Skip to content

Commit

Permalink
fix: consider indentation in the "Generate impl" and "Generate trait …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
IvarWithoutBones committed Aug 29, 2024
1 parent 28142e4 commit 29ecaa1
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 29ecaa1

Please sign in to comment.