Improve make::struct_ field_list whitespace

Example
---
**Before this PR**:

```rust
struct Variant{
    field: u32
}
```

**After this PR**:

```rust
struct Variant {
    field: u32
}
```
This commit is contained in:
A4-Tacks 2025-09-07 21:49:55 +08:00
parent 1f4e5e82ff
commit 7e2dc40642
No known key found for this signature in database
GPG Key ID: DBD861323040663B
3 changed files with 39 additions and 36 deletions

View File

@ -478,7 +478,7 @@ macro_rules! foo {
};
}
struct TheVariant{ the_field: u8 }
struct TheVariant { the_field: u8 }
enum TheEnum {
TheVariant(TheVariant),
@ -502,7 +502,7 @@ enum Foo {
}
"#,
r#"
struct Bar{ node: Box<Foo> }
struct Bar { node: Box<Foo> }
enum Foo {
Bar(Bar),
@ -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),
@ -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) }"#,
);
@ -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) }"#,
);
@ -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>) }"#,
);
@ -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)]
@ -618,7 +618,7 @@ enum Enum {
}
}"#,
r#"
struct Variant{
struct Variant {
field: u32
}
@ -642,7 +642,7 @@ mod indenting {
}"#,
r#"
mod indenting {
struct Variant{
struct Variant {
field: u32
}
@ -668,7 +668,7 @@ enum A {
}
}"#,
r#"
struct One{
struct One {
// leading comment
/// doc comment
#[an_attr]
@ -700,7 +700,7 @@ enum A {
}
}"#,
r#"
struct One{
struct One {
// comment
/// doc
#[attr]
@ -747,7 +747,7 @@ enum A {
/* comment */
// other
/// comment
struct One{
struct One {
a: u32
}
@ -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) }"#,
);
@ -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) }"#,
);
@ -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) }"#,
);
@ -933,7 +933,7 @@ fn f() {
}
"#,
r#"
struct V{ i: i32, j: i32 }
struct V { i: i32, j: i32 }
enum E {
V(V)
@ -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)
@ -1057,7 +1057,7 @@ fn foo() {
}
"#,
r#"
struct One{ a: u32, b: u32 }
struct One { a: u32, b: u32 }
enum A { One(One) }
@ -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>),
@ -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>),
@ -1158,7 +1158,7 @@ enum X<'a, 'b> {
}
"#,
r#"
struct C{ c: () }
struct C { c: () }
enum X<'a, 'b> {
A { a: &'a () },
@ -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>),

View File

@ -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 }
"#,
);
@ -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 }
"#,
);
@ -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 }
"#,
);
@ -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> }
"#,
);

View File

@ -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_(