fixes: ide-assists, generate_new indent loses

This commit is contained in:
A4-Tacks 2025-05-12 23:08:54 +08:00
parent 2bafe9d96c
commit 76dce6a9ab
No known key found for this signature in database
GPG Key ID: 86AC1F526BA06668

View File

@ -129,17 +129,23 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
// Get the mutable version of the impl to modify
let impl_def = if let Some(impl_def) = impl_def {
fn_.indent(impl_def.indent_level());
builder.make_mut(impl_def)
} else {
// Generate a new impl to add the method to
let impl_def = generate_impl(&ast::Adt::Struct(strukt.clone()));
let indent_level = strukt.indent_level();
fn_.indent(indent_level);
// Insert it after the adt
let strukt = builder.make_mut(strukt.clone());
ted::insert_all_raw(
ted::Position::after(strukt.syntax()),
vec![make::tokens::blank_line().into(), impl_def.syntax().clone().into()],
vec![
make::tokens::whitespace(&format!("\n\n{indent_level}")).into(),
impl_def.syntax().clone().into(),
],
);
impl_def
@ -425,6 +431,135 @@ impl Foo {
);
}
#[test]
fn non_zero_indent() {
check_assist(
generate_new,
r#"
mod foo {
struct $0Foo {}
}
"#,
r#"
mod foo {
struct Foo {}
impl Foo {
fn $0new() -> Self {
Self { }
}
}
}
"#,
);
check_assist(
generate_new,
r#"
mod foo {
mod bar {
struct $0Foo {}
}
}
"#,
r#"
mod foo {
mod bar {
struct Foo {}
impl Foo {
fn $0new() -> Self {
Self { }
}
}
}
}
"#,
);
check_assist(
generate_new,
r#"
mod foo {
struct $0Foo {}
impl Foo {
fn some() {}
}
}
"#,
r#"
mod foo {
struct Foo {}
impl Foo {
fn $0new() -> Self {
Self { }
}
fn some() {}
}
}
"#,
);
check_assist(
generate_new,
r#"
mod foo {
mod bar {
struct $0Foo {}
impl Foo {
fn some() {}
}
}
}
"#,
r#"
mod foo {
mod bar {
struct Foo {}
impl Foo {
fn $0new() -> Self {
Self { }
}
fn some() {}
}
}
}
"#,
);
check_assist(
generate_new,
r#"
mod foo {
mod bar {
struct $0Foo {}
impl Foo {
fn some() {}
}
}
}
"#,
r#"
mod foo {
mod bar {
struct Foo {}
impl Foo {
fn $0new() -> Self {
Self { }
}
fn some() {}
}
}
}
"#,
);
}
#[test]
fn check_visibility_of_new_fn_based_on_struct() {
check_assist(