Skip to content

Commit 76dce6a

Browse files
committed
fixes: ide-assists, generate_new indent loses
1 parent 2bafe9d commit 76dce6a

File tree

1 file changed

+136
-1
lines changed

1 file changed

+136
-1
lines changed

crates/ide-assists/src/handlers/generate_new.rs

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,23 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
129129

130130
// Get the mutable version of the impl to modify
131131
let impl_def = if let Some(impl_def) = impl_def {
132+
fn_.indent(impl_def.indent_level());
132133
builder.make_mut(impl_def)
133134
} else {
134135
// Generate a new impl to add the method to
135136
let impl_def = generate_impl(&ast::Adt::Struct(strukt.clone()));
137+
let indent_level = strukt.indent_level();
138+
fn_.indent(indent_level);
136139

137140
// Insert it after the adt
138141
let strukt = builder.make_mut(strukt.clone());
139142

140143
ted::insert_all_raw(
141144
ted::Position::after(strukt.syntax()),
142-
vec![make::tokens::blank_line().into(), impl_def.syntax().clone().into()],
145+
vec![
146+
make::tokens::whitespace(&format!("\n\n{indent_level}")).into(),
147+
impl_def.syntax().clone().into(),
148+
],
143149
);
144150

145151
impl_def
@@ -425,6 +431,135 @@ impl Foo {
425431
);
426432
}
427433

434+
#[test]
435+
fn non_zero_indent() {
436+
check_assist(
437+
generate_new,
438+
r#"
439+
mod foo {
440+
struct $0Foo {}
441+
}
442+
"#,
443+
r#"
444+
mod foo {
445+
struct Foo {}
446+
447+
impl Foo {
448+
fn $0new() -> Self {
449+
Self { }
450+
}
451+
}
452+
}
453+
"#,
454+
);
455+
check_assist(
456+
generate_new,
457+
r#"
458+
mod foo {
459+
mod bar {
460+
struct $0Foo {}
461+
}
462+
}
463+
"#,
464+
r#"
465+
mod foo {
466+
mod bar {
467+
struct Foo {}
468+
469+
impl Foo {
470+
fn $0new() -> Self {
471+
Self { }
472+
}
473+
}
474+
}
475+
}
476+
"#,
477+
);
478+
check_assist(
479+
generate_new,
480+
r#"
481+
mod foo {
482+
struct $0Foo {}
483+
484+
impl Foo {
485+
fn some() {}
486+
}
487+
}
488+
"#,
489+
r#"
490+
mod foo {
491+
struct Foo {}
492+
493+
impl Foo {
494+
fn $0new() -> Self {
495+
Self { }
496+
}
497+
498+
fn some() {}
499+
}
500+
}
501+
"#,
502+
);
503+
check_assist(
504+
generate_new,
505+
r#"
506+
mod foo {
507+
mod bar {
508+
struct $0Foo {}
509+
510+
impl Foo {
511+
fn some() {}
512+
}
513+
}
514+
}
515+
"#,
516+
r#"
517+
mod foo {
518+
mod bar {
519+
struct Foo {}
520+
521+
impl Foo {
522+
fn $0new() -> Self {
523+
Self { }
524+
}
525+
526+
fn some() {}
527+
}
528+
}
529+
}
530+
"#,
531+
);
532+
check_assist(
533+
generate_new,
534+
r#"
535+
mod foo {
536+
mod bar {
537+
struct $0Foo {}
538+
539+
impl Foo {
540+
fn some() {}
541+
}
542+
}
543+
}
544+
"#,
545+
r#"
546+
mod foo {
547+
mod bar {
548+
struct Foo {}
549+
550+
impl Foo {
551+
fn $0new() -> Self {
552+
Self { }
553+
}
554+
555+
fn some() {}
556+
}
557+
}
558+
}
559+
"#,
560+
);
561+
}
562+
428563
#[test]
429564
fn check_visibility_of_new_fn_based_on_struct() {
430565
check_assist(

0 commit comments

Comments
 (0)