Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ macro_rules! foo {
};
}

struct TheVariant{ the_field: u8 }
struct TheVariant { the_field: u8 }

enum TheEnum {
TheVariant(TheVariant),
Expand All @@ -502,7 +502,7 @@ enum Foo {
}
"#,
r#"
struct Bar{ node: Box<Foo> }
struct Bar { node: Box<Foo> }

enum Foo {
Bar(Bar),
Expand All @@ -519,7 +519,7 @@ enum Foo {
}
"#,
r#"
struct Bar{ node: Box<Foo>, a: Arc<Box<Foo>> }
struct Bar { node: Box<Foo>, a: Arc<Box<Foo>> }

enum Foo {
Bar(Bar),
Expand Down Expand Up @@ -560,7 +560,7 @@ enum A { One(One) }"#,
check_assist(
extract_struct_from_enum_variant,
"enum A { $0One { foo: u32, bar: u32 } }",
r#"struct One{ foo: u32, bar: u32 }
r#"struct One { foo: u32, bar: u32 }

enum A { One(One) }"#,
);
Expand All @@ -571,7 +571,7 @@ enum A { One(One) }"#,
check_assist(
extract_struct_from_enum_variant,
"enum A { $0One { foo: u32 } }",
r#"struct One{ foo: u32 }
r#"struct One { foo: u32 }

enum A { One(One) }"#,
);
Expand All @@ -582,7 +582,7 @@ enum A { One(One) }"#,
check_assist(
extract_struct_from_enum_variant,
r"enum En<T> { Var { a: T$0 } }",
r#"struct Var<T>{ a: T }
r#"struct Var<T> { a: T }

enum En<T> { Var(Var<T>) }"#,
);
Expand All @@ -599,7 +599,7 @@ enum Enum { Variant{ field: u32$0 } }"#,
r#"
#[derive(Debug)]
#[derive(Clone)]
struct Variant{ field: u32 }
struct Variant { field: u32 }

#[derive(Debug)]
#[derive(Clone)]
Expand All @@ -618,7 +618,7 @@ enum Enum {
}
}"#,
r#"
struct Variant{
struct Variant {
field: u32
}

Expand All @@ -642,7 +642,7 @@ mod indenting {
}"#,
r#"
mod indenting {
struct Variant{
struct Variant {
field: u32
}

Expand All @@ -668,7 +668,7 @@ enum A {
}
}"#,
r#"
struct One{
struct One {
// leading comment
/// doc comment
#[an_attr]
Expand Down Expand Up @@ -700,7 +700,7 @@ enum A {
}
}"#,
r#"
struct One{
struct One {
// comment
/// doc
#[attr]
Expand Down Expand Up @@ -747,7 +747,7 @@ enum A {
/* comment */
// other
/// comment
struct One{
struct One {
a: u32
}

Expand Down Expand Up @@ -789,7 +789,7 @@ enum A {
extract_struct_from_enum_variant,
"enum A { $0One{ a: u32, pub(crate) b: u32, pub(super) c: u32, d: u32 } }",
r#"
struct One{ a: u32, pub(crate) b: u32, pub(super) c: u32, d: u32 }
struct One { a: u32, pub(crate) b: u32, pub(super) c: u32, d: u32 }

enum A { One(One) }"#,
);
Expand Down Expand Up @@ -850,7 +850,7 @@ pub enum A { One(One) }"#,
extract_struct_from_enum_variant,
"pub(in something) enum A { $0One{ a: u32, b: u32 } }",
r#"
pub(in something) struct One{ pub(in something) a: u32, pub(in something) b: u32 }
pub(in something) struct One { pub(in something) a: u32, pub(in something) b: u32 }

pub(in something) enum A { One(One) }"#,
);
Expand All @@ -862,7 +862,7 @@ pub(in something) enum A { One(One) }"#,
extract_struct_from_enum_variant,
"pub(crate) enum A { $0One{ a: u32, b: u32, c: u32 } }",
r#"
pub(crate) struct One{ pub(crate) a: u32, pub(crate) b: u32, pub(crate) c: u32 }
pub(crate) struct One { pub(crate) a: u32, pub(crate) b: u32, pub(crate) c: u32 }

pub(crate) enum A { One(One) }"#,
);
Expand Down Expand Up @@ -933,7 +933,7 @@ fn f() {
}
"#,
r#"
struct V{ i: i32, j: i32 }
struct V { i: i32, j: i32 }

enum E {
V(V)
Expand Down Expand Up @@ -1027,7 +1027,7 @@ fn f() {
"#,
r#"
//- /main.rs
struct V{ i: i32, j: i32 }
struct V { i: i32, j: i32 }

enum E {
V(V)
Expand Down Expand Up @@ -1057,7 +1057,7 @@ fn foo() {
}
"#,
r#"
struct One{ a: u32, b: u32 }
struct One { a: u32, b: u32 }

enum A { One(One) }

Expand Down Expand Up @@ -1114,7 +1114,7 @@ enum X<'a, 'b, 'x> {
}
"#,
r#"
struct A<'a, 'x>{ a: &'a &'x mut () }
struct A<'a, 'x> { a: &'a &'x mut () }

enum X<'a, 'b, 'x> {
A(A<'a, 'x>),
Expand All @@ -1136,7 +1136,7 @@ enum X<'b, T, V, const C: usize> {
}
"#,
r#"
struct A<'b, T, const C: usize>{ a: T, b: X<'b>, c: [u8; C] }
struct A<'b, T, const C: usize> { a: T, b: X<'b>, c: [u8; C] }

enum X<'b, T, V, const C: usize> {
A(A<'b, T, C>),
Expand All @@ -1158,7 +1158,7 @@ enum X<'a, 'b> {
}
"#,
r#"
struct C{ c: () }
struct C { c: () }

enum X<'a, 'b> {
A { a: &'a () },
Expand All @@ -1180,7 +1180,7 @@ enum En<T: TraitT, V: TraitV> {
}
"#,
r#"
struct A<T: TraitT>{ a: T }
struct A<T: TraitT> { a: T }

enum En<T: TraitT, V: TraitV> {
A(A<T>),
Expand Down
24 changes: 12 additions & 12 deletions crates/ide-diagnostics/src/handlers/json_is_not_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ mod tests {
}

#[derive(Serialize)]
struct Root1{ bar: f64, bay: i64, baz: (), r#box: bool, foo: String }
struct Root1 { bar: f64, bay: i64, baz: (), r#box: bool, foo: String }

"#,
);
Expand All @@ -252,9 +252,9 @@ mod tests {
}
"#,
r#"
struct Value1{ }
struct Bar1{ kind: String, value: Value1 }
struct Root1{ bar: Bar1, foo: String }
struct Value1 { }
struct Bar1 { kind: String, value: Value1 }
struct Root1 { bar: Bar1, foo: String }

"#,
);
Expand Down Expand Up @@ -284,12 +284,12 @@ mod tests {
}
"#,
r#"
struct Address1{ house: i64, street: String }
struct User1{ address: Address1, email: String }
struct AnotherUser1{ user: User1 }
struct Address2{ house: i64, street: String }
struct User2{ address: Address2, email: String }
struct Root1{ another_user: AnotherUser1, user: User2 }
struct Address1 { house: i64, street: String }
struct User1 { address: Address1, email: String }
struct AnotherUser1 { user: User1 }
struct Address2 { house: i64, street: String }
struct User2 { address: Address2, email: String }
struct Root1 { another_user: AnotherUser1, user: User2 }

"#,
);
Expand Down Expand Up @@ -326,9 +326,9 @@ mod tests {
use serde::Deserialize;

#[derive(Serialize, Deserialize)]
struct OfObject1{ x: i64, y: i64 }
struct OfObject1 { x: i64, y: i64 }
#[derive(Serialize, Deserialize)]
struct Root1{ empty: Vec<_>, nested: Vec<Vec<Vec<i64>>>, of_object: Vec<OfObject1>, of_string: Vec<String> }
struct Root1 { empty: Vec<_>, nested: Vec<Vec<Vec<i64>>>, of_object: Vec<OfObject1>, of_string: Vec<String> }

"#,
);
Expand Down
7 changes: 5 additions & 2 deletions crates/syntax/src/ast/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1244,14 +1244,17 @@ pub fn struct_(
generic_param_list: Option<ast::GenericParamList>,
field_list: ast::FieldList,
) -> ast::Struct {
let semicolon = if matches!(field_list, ast::FieldList::TupleFieldList(_)) { ";" } else { "" };
let (semicolon, ws) =
if matches!(field_list, ast::FieldList::TupleFieldList(_)) { (";", "") } else { ("", " ") };
let type_params = generic_param_list.map_or_else(String::new, |it| it.to_string());
let visibility = match visibility {
None => String::new(),
Some(it) => format!("{it} "),
};

ast_from_text(&format!("{visibility}struct {strukt_name}{type_params}{field_list}{semicolon}",))
ast_from_text(&format!(
"{visibility}struct {strukt_name}{type_params}{ws}{field_list}{semicolon}"
))
}

pub fn enum_(
Expand Down
Loading