Skip to content

Commit 145cc29

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

File tree

1 file changed

+147
-4
lines changed

1 file changed

+147
-4
lines changed

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

Lines changed: 147 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,25 +128,39 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
128128
}
129129

130130
// Get the mutable version of the impl to modify
131-
let impl_def = if let Some(impl_def) = impl_def {
132-
builder.make_mut(impl_def)
131+
let (post_indent, impl_def) = if let Some(impl_def) = impl_def {
132+
fn_.indent(impl_def.indent_level());
133+
(None, 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();
136138

137139
// Insert it after the adt
138140
let strukt = builder.make_mut(strukt.clone());
139141

140142
ted::insert_all_raw(
141143
ted::Position::after(strukt.syntax()),
142-
vec![make::tokens::blank_line().into(), impl_def.syntax().clone().into()],
144+
if indent_level.is_zero() {
145+
vec![make::tokens::blank_line().into(), impl_def.syntax().clone().into()]
146+
} else {
147+
vec![
148+
make::tokens::blank_line().into(),
149+
make::tokens::whitespace(&indent_level.to_string()).into(),
150+
impl_def.syntax().clone().into(),
151+
]
152+
},
143153
);
144154

145-
impl_def
155+
(Some(indent_level), impl_def)
146156
};
147157

148158
// Add the `new` method at the start of the impl
149159
impl_def.get_or_create_assoc_item_list().add_item_at_start(fn_.into());
160+
161+
if let Some(indent_level) = post_indent {
162+
impl_def.indent(indent_level);
163+
}
150164
})
151165
}
152166

@@ -425,6 +439,135 @@ impl Foo {
425439
);
426440
}
427441

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

0 commit comments

Comments
 (0)