Skip to content

Commit 6669ea8

Browse files
committed
Leave attrs on the variant, not the extracted struct
1 parent 82ff740 commit 6669ea8

File tree

1 file changed

+30
-52
lines changed

1 file changed

+30
-52
lines changed

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

Lines changed: 30 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,21 @@ pub(crate) fn extract_struct_from_enum_variant(
101101
});
102102
}
103103

104-
let indent = enum_ast.indent_level();
105104
let generic_params = enum_ast
106105
.generic_param_list()
107106
.and_then(|known_generics| extract_generic_params(&known_generics, &field_list));
108107
let generics = generic_params.as_ref().map(|generics| generics.clone_for_update());
109-
let def =
110-
create_struct_def(variant_name.clone(), &variant, &field_list, generics, &enum_ast);
108+
let def = create_struct_def(variant_name.clone(), &field_list, generics, &enum_ast);
109+
110+
let enum_ast = variant.parent_enum();
111+
let indent = enum_ast.indent_level();
111112
def.reindent_to(indent);
112113

113-
let start_offset = &variant.parent_enum().syntax().clone();
114-
ted::insert_all_raw(
115-
ted::Position::before(start_offset),
114+
ted::insert_all(
115+
ted::Position::before(enum_ast.syntax()),
116116
vec![
117117
def.syntax().clone().into(),
118-
make::tokens::whitespace(&format!("\n\n{}", indent)).into(),
118+
make::tokens::whitespace(&format!("\n\n{indent}")).into(),
119119
],
120120
);
121121

@@ -227,8 +227,7 @@ fn tag_generics_in_variant(ty: &ast::Type, generics: &mut [(ast::GenericParam, b
227227
}
228228

229229
fn create_struct_def(
230-
variant_name: ast::Name,
231-
variant: &ast::Variant,
230+
name: ast::Name,
232231
field_list: &Either<ast::RecordFieldList, ast::TupleFieldList>,
233232
generics: Option<ast::GenericParamList>,
234233
enum_: &ast::Enum,
@@ -269,37 +268,9 @@ fn create_struct_def(
269268
field_list.into()
270269
}
271270
};
272-
273271
field_list.reindent_to(IndentLevel::single());
274272

275-
let strukt = make::struct_(enum_vis, variant_name, generics, field_list).clone_for_update();
276-
277-
// FIXME: Consider making this an actual function somewhere (like in `AttrsOwnerEdit`) after some deliberation
278-
let attrs_and_docs = |node: &SyntaxNode| {
279-
let mut select_next_ws = false;
280-
node.children_with_tokens().filter(move |child| {
281-
let accept = match child.kind() {
282-
ATTR | COMMENT => {
283-
select_next_ws = true;
284-
return true;
285-
}
286-
WHITESPACE if select_next_ws => true,
287-
_ => false,
288-
};
289-
select_next_ws = false;
290-
291-
accept
292-
})
293-
};
294-
295-
// copy attributes & comments from variant
296-
let variant_attrs = attrs_and_docs(variant.syntax())
297-
.map(|tok| match tok.kind() {
298-
WHITESPACE => make::tokens::single_newline().into(),
299-
_ => tok,
300-
})
301-
.collect();
302-
ted::insert_all(ted::Position::first_child_of(strukt.syntax()), variant_attrs);
273+
let strukt = make::struct_(enum_vis, name, generics, field_list).clone_for_update();
303274

304275
// copy attributes from enum
305276
ted::insert_all(
@@ -346,13 +317,20 @@ fn update_variant(variant: &ast::Variant, generics: Option<ast::GenericParamList
346317
})
347318
.unwrap_or_else(|| make::ty(&name.text()));
348319

320+
// change from a record to a tuple field list
349321
let tuple_field = make::tuple_field(None, ty);
350-
let replacement = make::variant(
351-
name,
352-
Some(ast::FieldList::TupleFieldList(make::tuple_field_list(iter::once(tuple_field)))),
353-
)
354-
.clone_for_update();
355-
ted::replace(variant.syntax(), replacement.syntax());
322+
let field_list = make::tuple_field_list(iter::once(tuple_field)).clone_for_update();
323+
ted::replace(variant.field_list()?.syntax(), field_list.syntax());
324+
325+
// remove any ws after the name
326+
if let Some(ws) = name
327+
.syntax()
328+
.siblings_with_tokens(syntax::Direction::Next)
329+
.find_map(|tok| tok.into_token().filter(|tok| tok.kind() == WHITESPACE))
330+
{
331+
ted::remove(SyntaxElement::Token(ws));
332+
}
333+
356334
Some(())
357335
}
358336

@@ -628,15 +606,15 @@ enum A {
628606
}
629607
}"#,
630608
r#"
631-
/* comment */
632-
// other
633-
/// comment
634-
#[attr]
635609
struct One{
636610
a: u32
637611
}
638612
639613
enum A {
614+
/* comment */
615+
// other
616+
/// comment
617+
#[attr]
640618
One(One)
641619
}"#,
642620
);
@@ -655,13 +633,13 @@ enum A {
655633
$0One(u32, u32)
656634
}"#,
657635
r#"
658-
/* comment */
659-
// other
660-
/// comment
661-
#[attr]
662636
struct One(u32, u32);
663637
664638
enum A {
639+
/* comment */
640+
// other
641+
/// comment
642+
#[attr]
665643
One(One)
666644
}"#,
667645
);

0 commit comments

Comments
 (0)