about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNick Cameron <nrc@ncameron.org>2017-07-11 16:16:35 +1200
committerGitHub <noreply@github.com>2017-07-11 16:16:35 +1200
commit3552595a3b40abd1aa5a24074f4ab2caa2fa034f (patch)
treecc283baa14380e988a07b9a859997e834c9a93d6
parent4cfc2bfbd465b6d9510d82c35198ccb6a6a2d6d7 (diff)
parentbc90a12b8deaa14b996174881790a2fb5bc2a7e9 (diff)
Merge pull request #1780 from topecongiro/rfc/union
Format union
-rw-r--r--src/items.rs2
-rw-r--r--src/visitor.rs15
-rw-r--r--tests/source/unions.rs195
-rw-r--r--tests/target/unions.rs196
4 files changed, 405 insertions, 3 deletions
diff --git a/src/items.rs b/src/items.rs
index 96ffffa7b60..a5e35e144fb 100644
--- a/src/items.rs
+++ b/src/items.rs
@@ -1053,7 +1053,7 @@ fn format_unit_struct(item_name: &str, ident: ast::Ident, vis: &ast::Visibility)
     format!("{};", format_header(item_name, ident, vis))
 }
 
-fn format_struct_struct(
+pub fn format_struct_struct(
     context: &RewriteContext,
     item_name: &str,
     ident: ast::Ident,
diff --git a/src/visitor.rs b/src/visitor.rs
index 178f7c7691f..800f46374ec 100644
--- a/src/visitor.rs
+++ b/src/visitor.rs
@@ -426,8 +426,19 @@ impl<'a> FmtVisitor<'a> {
                 );
                 self.push_rewrite(item.span, rewrite);
             }
-            ast::ItemKind::Union(..) => {
-                // FIXME(#1157): format union definitions.
+            ast::ItemKind::Union(ref def, ref generics) => {
+                let rewrite = ::items::format_struct_struct(
+                    &self.get_context(),
+                    "union ",
+                    item.ident,
+                    &item.vis,
+                    def.fields(),
+                    Some(generics),
+                    item.span,
+                    self.block_indent,
+                    None,
+                );
+                self.push_rewrite(item.span, rewrite);
             }
             ast::ItemKind::GlobalAsm(..) => {
                 let snippet = Some(self.snippet(item.span));
diff --git a/tests/source/unions.rs b/tests/source/unions.rs
new file mode 100644
index 00000000000..107be79873c
--- /dev/null
+++ b/tests/source/unions.rs
@@ -0,0 +1,195 @@
+// rustfmt-normalize_comments: true
+// rustfmt-wrap_comments: true
+
+                                                                       /// A Doc comment
+#[AnAttribute]
+pub union Foo {
+    #[rustfmt_skip]
+    f :   SomeType, // Comment beside a field
+    f: SomeType, // Comment beside a field
+    // Comment on a field
+    #[AnAttribute]
+    g: SomeOtherType,
+      /// A doc comment on a field
+    h: AThirdType,
+    pub i: TypeForPublicField
+}
+
+// #1029
+pub union Foo {
+    #[doc(hidden)]
+    // This will NOT get deleted!
+    bar: String, // hi
+}
+
+// #1029
+union X {
+    // `x` is an important number.
+    #[allow(unused)] // TODO: use
+    x: u32,
+}
+
+// #410
+#[allow(missing_docs)]
+pub union Writebatch<K: Key> {
+    #[allow(dead_code)] //only used for holding the internal pointer
+    writebatch: RawWritebatch,
+    marker: PhantomData<K>,
+}
+
+// With a where clause and generics.
+pub union Foo<'a, Y: Baz>
+    where X: Whatever
+{
+    f: SomeType, // Comment beside a field
+}
+
+union Baz {
+
+    a: A,  // Comment A
+    b: B, // Comment B
+    c: C,   // Comment C
+
+}
+
+union Baz {
+    a: A,  // Comment A
+
+    b: B, // Comment B
+
+
+
+
+    c: C,   // Comment C
+}
+
+union Baz {
+
+    a: A,
+
+    b: B,
+    c: C,
+
+
+
+    
+    d: D
+
+}
+
+union Baz
+{
+    // Comment A
+    a: A,
+    
+    // Comment B
+b: B,
+    // Comment C
+      c: C,}
+
+pub union State<F: FnMut() -> time::Timespec> { now: F }
+
+pub union State<F: FnMut() -> ()> { now: F }
+
+pub union State<F: FnMut()> { now: F }
+
+union Palette { /// A map of indizes in the palette to a count of pixels in approximately that color
+                    foo: i32}
+
+// Splitting a single line comment into a block previously had a misalignment
+// when the field had attributes
+union FieldsWithAttributes {
+    // Pre Comment
+    #[rustfmt_skip] pub host:String, // Post comment BBBBBBBBBBBBBB BBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBB BBBBBBBBBBB
+    //Another pre comment
+    #[attr1]
+    #[attr2] pub id: usize // CCCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCC CCCCCCCCCCCC
+}
+
+union Deep {
+    deeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeep: node::Handle<IdRef<'id, Node<K, V>>,
+                                                     Type,
+                                                     NodeType>,
+}
+
+mod m {
+    union X<T> where T: Sized {
+        a: T,
+    }
+}
+
+union Issue677 {
+    pub ptr: *const libc::c_void,
+    pub trace: fn(  obj: 
+          *const libc::c_void, tracer   : *mut   JSTracer   ),
+}
+
+union Foo {}
+union Foo {
+    }
+union Foo {
+    // comment
+    }
+union Foo {
+    // trailing space ->    
+
+
+    }
+union Foo { /* comment */ }
+
+union LongUnion {
+    a: A,
+    the_quick_brown_fox_jumps_over_the_lazy_dog:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA,
+}
+
+union Deep {
+    deeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeep: node::Handle<IdRef<'id, Node<Key, Value>>,
+                                                                         Type,
+                                                                         NodeType>,
+}
+
+// #1364
+fn foo() {
+    convex_shape.set_point(0, &Vector2f { x: 400.0, y: 100.0 });
+    convex_shape.set_point(1, &Vector2f { x: 500.0, y: 70.0 });
+    convex_shape.set_point(2, &Vector2f { x: 450.0, y: 100.0 });
+    convex_shape.set_point(3, &Vector2f { x: 580.0, y: 150.0 });
+}
+
+// Vertical alignment
+union Foo {
+    aaaaa: u32, // a
+
+    b: u32, // b
+    cc: u32, // cc
+
+    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx: u32, // 1
+    yy: u32, // comment2
+    zzz: u32, // comment3
+
+    aaaaaa: u32, // comment4
+    bb: u32, // comment5
+    // separate
+    dd: u32, // comment7
+    c: u32, // comment6
+
+    aaaaaaa: u32, /* multi
+     * line
+     * comment
+     */
+    b: u32, // hi
+
+    do_not_push_this_comment1: u32, // comment1
+    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx: u32, // 2
+    please_do_not_push_this_comment3: u32, // comment3
+
+    do_not_push_this_comment1: u32, // comment1
+    // separate
+    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx: u32, // 2
+    please_do_not_push_this_comment3: u32, // comment3
+
+    do_not_push_this_comment1: u32, // comment1
+    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx: u32, // 2
+    // separate
+    please_do_not_push_this_comment3: u32, // comment3
+}
diff --git a/tests/target/unions.rs b/tests/target/unions.rs
new file mode 100644
index 00000000000..bd6328173b5
--- /dev/null
+++ b/tests/target/unions.rs
@@ -0,0 +1,196 @@
+// rustfmt-normalize_comments: true
+// rustfmt-wrap_comments: true
+
+/// A Doc comment
+#[AnAttribute]
+pub union Foo {
+    #[rustfmt_skip]
+    f :   SomeType, // Comment beside a field
+    f: SomeType, // Comment beside a field
+    // Comment on a field
+    #[AnAttribute]
+    g: SomeOtherType,
+    /// A doc comment on a field
+    h: AThirdType,
+    pub i: TypeForPublicField,
+}
+
+// #1029
+pub union Foo {
+    #[doc(hidden)]
+    // This will NOT get deleted!
+    bar: String, // hi
+}
+
+// #1029
+union X {
+    // `x` is an important number.
+    #[allow(unused)] // TODO: use
+    x: u32,
+}
+
+// #410
+#[allow(missing_docs)]
+pub union Writebatch<K: Key> {
+    #[allow(dead_code)] // only used for holding the internal pointer
+    writebatch: RawWritebatch,
+    marker: PhantomData<K>,
+}
+
+// With a where clause and generics.
+pub union Foo<'a, Y: Baz>
+where
+    X: Whatever,
+{
+    f: SomeType, // Comment beside a field
+}
+
+union Baz {
+    a: A, // Comment A
+    b: B, // Comment B
+    c: C, // Comment C
+}
+
+union Baz {
+    a: A, // Comment A
+
+    b: B, // Comment B
+
+    c: C, // Comment C
+}
+
+union Baz {
+    a: A,
+
+    b: B,
+    c: C,
+
+    d: D,
+}
+
+union Baz {
+    // Comment A
+    a: A,
+
+    // Comment B
+    b: B,
+    // Comment C
+    c: C,
+}
+
+pub union State<F: FnMut() -> time::Timespec> {
+    now: F,
+}
+
+pub union State<F: FnMut() -> ()> {
+    now: F,
+}
+
+pub union State<F: FnMut()> {
+    now: F,
+}
+
+union Palette {
+    /// A map of indizes in the palette to a count of pixels in approximately
+    /// that color
+    foo: i32,
+}
+
+// Splitting a single line comment into a block previously had a misalignment
+// when the field had attributes
+union FieldsWithAttributes {
+    // Pre Comment
+    #[rustfmt_skip] pub host:String, /* Post comment BBBBBBBBBBBBBB BBBBBBBBBBBBBBBB
+                                      * BBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBB BBBBBBBBBBB */
+    // Another pre comment
+    #[attr1]
+    #[attr2]
+    pub id: usize, /* CCCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCC
+                    * CCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCC CCCCCCCCCCCC */
+}
+
+union Deep {
+    deeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeep:
+        node::Handle<IdRef<'id, Node<K, V>>, Type, NodeType>,
+}
+
+mod m {
+    union X<T>
+    where
+        T: Sized,
+    {
+        a: T,
+    }
+}
+
+union Issue677 {
+    pub ptr: *const libc::c_void,
+    pub trace: fn(obj: *const libc::c_void, tracer: *mut JSTracer),
+}
+
+union Foo {}
+union Foo {}
+union Foo {
+    // comment
+}
+union Foo {
+    // trailing space ->
+}
+union Foo { /* comment */ }
+
+union LongUnion {
+    a: A,
+    the_quick_brown_fox_jumps_over_the_lazy_dog:
+        AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA,
+}
+
+union Deep {
+    deeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeep:
+        node::Handle<IdRef<'id, Node<Key, Value>>, Type, NodeType>,
+}
+
+// #1364
+fn foo() {
+    convex_shape.set_point(0, &Vector2f { x: 400.0, y: 100.0 });
+    convex_shape.set_point(1, &Vector2f { x: 500.0, y: 70.0 });
+    convex_shape.set_point(2, &Vector2f { x: 450.0, y: 100.0 });
+    convex_shape.set_point(3, &Vector2f { x: 580.0, y: 150.0 });
+}
+
+// Vertical alignment
+union Foo {
+    aaaaa: u32, // a
+
+    b: u32,  // b
+    cc: u32, // cc
+
+    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx: u32, // 1
+    yy: u32,  // comment2
+    zzz: u32, // comment3
+
+    aaaaaa: u32, // comment4
+    bb: u32,     // comment5
+    // separate
+    dd: u32, // comment7
+    c: u32,  // comment6
+
+    aaaaaaa: u32, /* multi
+                   * line
+                   * comment
+                   * */
+    b: u32, // hi
+
+    do_not_push_this_comment1: u32, // comment1
+    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx: u32, // 2
+    please_do_not_push_this_comment3: u32, // comment3
+
+    do_not_push_this_comment1: u32, // comment1
+    // separate
+    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx: u32, // 2
+    please_do_not_push_this_comment3: u32, // comment3
+
+    do_not_push_this_comment1: u32, // comment1
+    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx: u32, // 2
+    // separate
+    please_do_not_push_this_comment3: u32, // comment3
+}