diff options
Diffstat (limited to 'crates/ide_assists/src/handlers')
-rw-r--r-- | crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs | 37 |
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; | |||
15 | use rustc_hash::FxHashSet; | 15 | use rustc_hash::FxHashSet; |
16 | use syntax::{ | 16 | use 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 | ||
22 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 26 | use 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 | ||
193 | fn update_variant(variant: &ast::Variant, generic: Option<ast::GenericParamList>) -> Option<()> { | 205 | fn 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)] | ||
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 | |||
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, |