Skip to content

Commit 9ff7ab6

Browse files
committed
Carry over attributes in extract_struct_from_enum_variant
1 parent f3dc432 commit 9ff7ab6

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ use itertools::Itertools;
1515
use rustc_hash::FxHashSet;
1616
use syntax::{
1717
algo::find_node_at_offset,
18-
ast::{self, make, AstNode, GenericParamsOwner, NameOwner, TypeBoundsOwner, VisibilityOwner},
19-
ted, SyntaxNode, T,
18+
ast::{
19+
self, make, AstNode, AttrsOwner, GenericParamsOwner, NameOwner, TypeBoundsOwner,
20+
VisibilityOwner,
21+
},
22+
ted::{self, Position},
23+
SyntaxNode, T,
2024
};
2125

2226
use crate::{AssistContext, AssistId, AssistKind, Assists};
@@ -186,8 +190,16 @@ fn create_struct_def(
186190
};
187191

188192
// FIXME: This uses all the generic params of the enum, but the variant might not use all of them.
189-
make::struct_(enum_.visibility(), variant_name, enum_.generic_param_list(), field_list)
190-
.clone_for_update()
193+
let strukt =
194+
make::struct_(enum_.visibility(), variant_name, enum_.generic_param_list(), field_list)
195+
.clone_for_update();
196+
197+
// copy attributes
198+
ted::insert_all(
199+
Position::first_child_of(strukt.syntax()),
200+
enum_.attrs().map(|it| it.syntax().clone_for_update().into()).collect(),
201+
);
202+
strukt
191203
}
192204

193205
fn update_variant(variant: &ast::Variant, generic: Option<ast::GenericParamList>) -> Option<()> {
@@ -336,7 +348,7 @@ enum A { One(One) }"#,
336348
}
337349

338350
#[test]
339-
fn test_extract_struct_keeps_generics() {
351+
fn test_extract_struct_carries_over_generics() {
340352
check_assist(
341353
extract_struct_from_enum_variant,
342354
r"enum En<T> { Var { a: T$0 } }",
@@ -346,6 +358,21 @@ enum En<T> { Var(Var<T>) }"#,
346358
);
347359
}
348360

361+
#[test]
362+
fn test_extract_struct_carries_over_attributes() {
363+
check_assist(
364+
extract_struct_from_enum_variant,
365+
r#"#[derive(Debug)]
366+
#[derive(Clone)]
367+
enum Enum { Variant{ field: u32$0 } }"#,
368+
r#"#[derive(Debug)]#[derive(Clone)] struct Variant{ pub field: u32 }
369+
370+
#[derive(Debug)]
371+
#[derive(Clone)]
372+
enum Enum { Variant(Variant) }"#,
373+
);
374+
}
375+
349376
#[test]
350377
fn test_extract_struct_keep_comments_and_attrs_one_field_named() {
351378
check_assist(

0 commit comments

Comments
 (0)