Skip to content

Commit b12957b

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

File tree

1 file changed

+149
-2
lines changed

1 file changed

+149
-2
lines changed

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

Lines changed: 149 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
118118
false,
119119
)
120120
.clone_for_update();
121-
fn_.indent(1.into());
122121

123122
// Add a tabstop before the name
124123
if let Some(cap) = ctx.config.snippet_cap {
@@ -127,26 +126,45 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
127126
}
128127
}
129128

129+
let mut post_indent = None;
130+
130131
// Get the mutable version of the impl to modify
131132
let impl_def = if let Some(impl_def) = impl_def {
133+
fn_.indent(impl_def.indent_level() + 1);
132134
builder.make_mut(impl_def)
133135
} else {
134136
// Generate a new impl to add the method to
135137
let impl_def = generate_impl(&ast::Adt::Struct(strukt.clone()));
138+
let indent_level = strukt.indent_level();
139+
post_indent = Some(indent_level);
140+
141+
fn_.indent(1.into());
136142

137143
// Insert it after the adt
138144
let strukt = builder.make_mut(strukt.clone());
139145

140146
ted::insert_all_raw(
141147
ted::Position::after(strukt.syntax()),
142-
vec![make::tokens::blank_line().into(), impl_def.syntax().clone().into()],
148+
if indent_level.is_zero() {
149+
vec![make::tokens::blank_line().into(), impl_def.syntax().clone().into()]
150+
} else {
151+
vec![
152+
make::tokens::blank_line().into(),
153+
make::tokens::whitespace(&indent_level.to_string()).into(),
154+
impl_def.syntax().clone().into(),
155+
]
156+
},
143157
);
144158

145159
impl_def
146160
};
147161

148162
// Add the `new` method at the start of the impl
149163
impl_def.get_or_create_assoc_item_list().add_item_at_start(fn_.into());
164+
165+
if let Some(indent_level) = post_indent {
166+
impl_def.indent(indent_level);
167+
}
150168
})
151169
}
152170

@@ -425,6 +443,135 @@ impl Foo {
425443
);
426444
}
427445

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

0 commit comments

Comments
 (0)