Skip to content

Commit a0db478

Browse files
committed
generate_derive no longer breaks indentation
1 parent 5ee39a6 commit a0db478

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use syntax::{
2-
ast::{self, AstNode, HasAttrs},
2+
ast::{self, edit::IndentLevel, AstNode, HasAttrs},
33
SyntaxKind::{COMMENT, WHITESPACE},
44
TextSize,
55
};
@@ -42,7 +42,12 @@ pub(crate) fn generate_derive(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
4242
.next();
4343
match derive_attr {
4444
None => {
45-
builder.insert_snippet(cap, node_start, "#[derive($0)]\n");
45+
let indent_level = IndentLevel::from_node(nominal.syntax());
46+
builder.insert_snippet(
47+
cap,
48+
node_start,
49+
format!("#[derive($0)]\n{indent_level}"),
50+
);
4651
}
4752
Some(tt) => {
4853
// Just move the cursor.
@@ -84,6 +89,20 @@ mod tests {
8489
"struct Foo { $0 a: i32, }",
8590
"#[derive($0)]\nstruct Foo { a: i32, }",
8691
);
92+
check_assist(
93+
generate_derive,
94+
"
95+
mod m {
96+
struct Foo { a: i32,$0 }
97+
}
98+
",
99+
"
100+
mod m {
101+
#[derive($0)]
102+
struct Foo { a: i32, }
103+
}
104+
",
105+
);
87106
}
88107

89108
#[test]
@@ -111,6 +130,24 @@ struct Foo { a: i32$0, }
111130
struct Foo { a: i32, }
112131
",
113132
);
133+
check_assist(
134+
generate_derive,
135+
"
136+
mod m {
137+
/// `Foo` is a pretty important struct.
138+
/// It does stuff.
139+
struct Foo { a: i32,$0 }
140+
}
141+
",
142+
"
143+
mod m {
144+
/// `Foo` is a pretty important struct.
145+
/// It does stuff.
146+
#[derive($0)]
147+
struct Foo { a: i32, }
148+
}
149+
",
150+
);
114151
}
115152

116153
#[test]

0 commit comments

Comments
 (0)