aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-06-02 16:54:57 +0100
committerLukas Wirth <[email protected]>2021-06-02 16:55:08 +0100
commit9ff7ab680c338e62679ad75eee0e1a357ce07fa8 (patch)
tree7f204234ba4775ab8c0f07a8480a4cc48e9a2709 /crates
parentf3dc4321c8febf5cf3a4204e2cb444fd453350e4 (diff)
Carry over attributes in extract_struct_from_enum_variant
Diffstat (limited to 'crates')
-rw-r--r--crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs37
1 files changed, 32 insertions, 5 deletions
diff --git a/crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs
index 730fc28bf..6ce417086 100644
--- a/crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs
+++ b/crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs
@@ -15,8 +15,12 @@ use itertools::Itertools;
15use rustc_hash::FxHashSet; 15use rustc_hash::FxHashSet;
16use syntax::{ 16use syntax::{
17 algo::find_node_at_offset, 17 algo::find_node_at_offset,
18 ast::{self, make, AstNode, GenericParamsOwner, NameOwner, TypeBoundsOwner, VisibilityOwner}, 18 ast::{
19 ted, SyntaxNode, T, 19 self, make, AstNode, AttrsOwner, GenericParamsOwner, NameOwner, TypeBoundsOwner,
20 VisibilityOwner,
21 },
22 ted::{self, Position},
23 SyntaxNode, T,
20}; 24};
21 25
22use crate::{AssistContext, AssistId, AssistKind, Assists}; 26use crate::{AssistContext, AssistId, AssistKind, Assists};
@@ -186,8 +190,16 @@ fn create_struct_def(
186 }; 190 };
187 191
188 // FIXME: This uses all the generic params of the enum, but the variant might not use all of them. 192 // 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) 193 let strukt =
190 .clone_for_update() 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
191} 203}
192 204
193fn update_variant(variant: &ast::Variant, generic: Option<ast::GenericParamList>) -> Option<()> { 205fn update_variant(variant: &ast::Variant, generic: Option<ast::GenericParamList>) -> Option<()> {
@@ -336,7 +348,7 @@ enum A { One(One) }"#,
336 } 348 }
337 349
338 #[test] 350 #[test]
339 fn test_extract_struct_keeps_generics() { 351 fn test_extract_struct_carries_over_generics() {
340 check_assist( 352 check_assist(
341 extract_struct_from_enum_variant, 353 extract_struct_from_enum_variant,
342 r"enum En<T> { Var { a: T$0 } }", 354 r"enum En<T> { Var { a: T$0 } }",
@@ -347,6 +359,21 @@ enum En<T> { Var(Var<T>) }"#,
347 } 359 }
348 360
349 #[test] 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)]
367enum Enum { Variant{ field: u32$0 } }"#,
368 r#"#[derive(Debug)]#[derive(Clone)] struct Variant{ pub field: u32 }
369
370#[derive(Debug)]
371#[derive(Clone)]
372enum Enum { Variant(Variant) }"#,
373 );
374 }
375
376 #[test]
350 fn test_extract_struct_keep_comments_and_attrs_one_field_named() { 377 fn test_extract_struct_keep_comments_and_attrs_one_field_named() {
351 check_assist( 378 check_assist(
352 extract_struct_from_enum_variant, 379 extract_struct_from_enum_variant,