]> git.lizzy.rs Git - rust.git/commitdiff
Properly follow the brace styles.
authorPavel Sountsov <siege@google.com>
Thu, 19 Nov 2015 06:34:14 +0000 (22:34 -0800)
committerSiegeLord <slabode@aim.com>
Thu, 19 Nov 2015 06:34:14 +0000 (22:34 -0800)
src/items.rs
tests/source/item-brace-style-always-next-line.rs [new file with mode: 0644]
tests/source/item-brace-style-next-line.rs [deleted file]
tests/source/item-brace-style-prefer-same-line.rs [new file with mode: 0644]
tests/source/item-brace-style-same-line-where.rs [new file with mode: 0644]
tests/source/item-brace-style-same-line.rs [deleted file]
tests/target/item-brace-style-always-next-line.rs [new file with mode: 0644]
tests/target/item-brace-style-next-line.rs [deleted file]
tests/target/item-brace-style-prefer-same-line.rs [new file with mode: 0644]
tests/target/item-brace-style-same-line-where.rs [new file with mode: 0644]
tests/target/item-brace-style-same-line.rs [deleted file]

index c4c55c841a69c32646207b2f84d4761fde45bb7b..5595b41607b5fcbfe79901f690b988e21a220fed 100644 (file)
@@ -634,18 +634,13 @@ pub fn visit_enum(&mut self,
         let header_str = self.format_header("enum ", ident, vis);
         self.buffer.push_str(&header_str);
 
-        let separator = if self.config.item_brace_style == BraceStyle::AlwaysNextLine &&
-                           !enum_def.variants.is_empty() {
-            format!("\n{}", self.block_indent.to_string(self.config))
-        } else {
-            " ".to_owned()
-        };
         let enum_snippet = self.snippet(span);
         let body_start = span.lo + BytePos(enum_snippet.find_uncommented("{").unwrap() as u32 + 1);
         let generics_str = self.format_generics(generics,
                                                 "{",
-                                                &separator,
                                                 "{",
+                                                self.config.item_brace_style,
+                                                enum_def.variants.is_empty(),
                                                 self.block_indent,
                                                 self.block_indent.block_indent(self.config),
                                                 mk_sp(span.lo, body_start))
@@ -820,24 +815,23 @@ fn format_struct_struct(&self,
 
         let body_lo = span_after(span, "{", self.codemap);
 
-        let separator = if self.config.item_brace_style == BraceStyle::AlwaysNextLine &&
-                           !fields.is_empty() {
-            format!("\n{}", self.block_indent.to_string(self.config))
-        } else {
-            " ".to_owned()
-        };
-
         let generics_str = match generics {
             Some(g) => {
                 try_opt!(self.format_generics(g,
                                               "{",
-                                              &separator,
                                               "{",
+                                              self.config.item_brace_style,
+                                              fields.is_empty(),
                                               offset,
                                               offset + header_str.len(),
                                               mk_sp(span.lo, body_lo)))
             }
-            None => format!("{}{{", separator),
+            None => if self.config.item_brace_style == BraceStyle::AlwaysNextLine &&
+                       !fields.is_empty() {
+                format!("\n{}{{", self.block_indent.to_string(self.config))
+            } else {
+                " {".to_owned()
+            },
         };
         result.push_str(&generics_str);
 
@@ -969,8 +963,9 @@ fn format_header(&self, item_name: &str, ident: ast::Ident, vis: ast::Visibility
     fn format_generics(&self,
                        generics: &ast::Generics,
                        opener: &str,
-                       separator: &str,
                        terminator: &str,
+                       brace_style: BraceStyle,
+                       force_same_line_brace: bool,
                        offset: Indent,
                        generics_offset: Indent,
                        span: Span)
@@ -985,11 +980,22 @@ fn format_generics(&self,
                                                                       terminator,
                                                                       Some(span.hi)));
             result.push_str(&where_clause_str);
-            result.push('\n');
-            result.push_str(&self.block_indent.to_string(self.config));
+            if !force_same_line_brace &&
+               (brace_style == BraceStyle::SameLineWhere ||
+                brace_style == BraceStyle::AlwaysNextLine) {
+                result.push('\n');
+                result.push_str(&self.block_indent.to_string(self.config));
+            } else {
+                result.push(' ');
+            }
             result.push_str(opener);
         } else {
-            result.push_str(separator);
+            if !force_same_line_brace && brace_style == BraceStyle::AlwaysNextLine {
+                result.push('\n');
+                result.push_str(&self.block_indent.to_string(self.config));
+            } else {
+                result.push(' ');
+            }
             result.push_str(opener);
         }
 
diff --git a/tests/source/item-brace-style-always-next-line.rs b/tests/source/item-brace-style-always-next-line.rs
new file mode 100644 (file)
index 0000000..96a6283
--- /dev/null
@@ -0,0 +1,29 @@
+// rustfmt-item_brace_style: AlwaysNextLine
+
+mod M {
+    enum A {
+        A,
+    }
+
+    struct B {
+        b: i32,
+    }
+
+    // For empty enums and structs, the brace remains on the same line.
+    enum C {}
+
+    struct D {}
+
+    enum A<T> where T: Copy {
+        A,
+    }
+
+    struct B<T> where T: Copy {
+        b: i32,
+    }
+
+    // For empty enums and structs, the brace remains on the same line.
+    enum C<T> where T: Copy {}
+
+    struct D<T> where T: Copy {}
+}
diff --git a/tests/source/item-brace-style-next-line.rs b/tests/source/item-brace-style-next-line.rs
deleted file mode 100644 (file)
index 96a6283..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-// rustfmt-item_brace_style: AlwaysNextLine
-
-mod M {
-    enum A {
-        A,
-    }
-
-    struct B {
-        b: i32,
-    }
-
-    // For empty enums and structs, the brace remains on the same line.
-    enum C {}
-
-    struct D {}
-
-    enum A<T> where T: Copy {
-        A,
-    }
-
-    struct B<T> where T: Copy {
-        b: i32,
-    }
-
-    // For empty enums and structs, the brace remains on the same line.
-    enum C<T> where T: Copy {}
-
-    struct D<T> where T: Copy {}
-}
diff --git a/tests/source/item-brace-style-prefer-same-line.rs b/tests/source/item-brace-style-prefer-same-line.rs
new file mode 100644 (file)
index 0000000..636a584
--- /dev/null
@@ -0,0 +1,29 @@
+// rustfmt-item_brace_style: PreferSameLine
+
+mod M {
+    enum A
+    {
+        A,
+    }
+
+    struct B
+    {
+        b: i32,
+    }
+
+    enum C {}
+
+    struct D {}
+
+    enum A<T> where T: Copy {
+        A,
+    }
+
+    struct B<T> where T: Copy {
+        b: i32,
+    }
+
+    enum C<T> where T: Copy {}
+
+    struct D<T> where T: Copy {}
+}
diff --git a/tests/source/item-brace-style-same-line-where.rs b/tests/source/item-brace-style-same-line-where.rs
new file mode 100644 (file)
index 0000000..7b2a95d
--- /dev/null
@@ -0,0 +1,31 @@
+// rustfmt-item_brace_style: SameLineWhere
+
+mod M {
+    enum A
+    {
+        A,
+    }
+
+    struct B
+    {
+        b: i32,
+    }
+
+    // For empty enums and structs, the brace remains on the same line.
+    enum C {}
+
+    struct D {}
+
+    enum A<T> where T: Copy {
+        A,
+    }
+
+    struct B<T> where T: Copy {
+        b: i32,
+    }
+
+    // For empty enums and structs, the brace remains on the same line.
+    enum C<T> where T: Copy {}
+
+    struct D<T> where T: Copy {}
+}
diff --git a/tests/source/item-brace-style-same-line.rs b/tests/source/item-brace-style-same-line.rs
deleted file mode 100644 (file)
index 636a584..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-// rustfmt-item_brace_style: PreferSameLine
-
-mod M {
-    enum A
-    {
-        A,
-    }
-
-    struct B
-    {
-        b: i32,
-    }
-
-    enum C {}
-
-    struct D {}
-
-    enum A<T> where T: Copy {
-        A,
-    }
-
-    struct B<T> where T: Copy {
-        b: i32,
-    }
-
-    enum C<T> where T: Copy {}
-
-    struct D<T> where T: Copy {}
-}
diff --git a/tests/target/item-brace-style-always-next-line.rs b/tests/target/item-brace-style-always-next-line.rs
new file mode 100644 (file)
index 0000000..48dcebc
--- /dev/null
@@ -0,0 +1,37 @@
+// rustfmt-item_brace_style: AlwaysNextLine
+
+mod M {
+    enum A
+    {
+        A,
+    }
+
+    struct B
+    {
+        b: i32,
+    }
+
+    // For empty enums and structs, the brace remains on the same line.
+    enum C {}
+
+    struct D {}
+
+    enum A<T>
+        where T: Copy
+    {
+        A,
+    }
+
+    struct B<T>
+        where T: Copy
+    {
+        b: i32,
+    }
+
+    // For empty enums and structs, the brace remains on the same line.
+    enum C<T>
+        where T: Copy {}
+
+    struct D<T>
+        where T: Copy {}
+}
diff --git a/tests/target/item-brace-style-next-line.rs b/tests/target/item-brace-style-next-line.rs
deleted file mode 100644 (file)
index a5c2410..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-// rustfmt-item_brace_style: AlwaysNextLine
-
-mod M {
-    enum A
-    {
-        A,
-    }
-
-    struct B
-    {
-        b: i32,
-    }
-
-    // For empty enums and structs, the brace remains on the same line.
-    enum C {}
-
-    struct D {}
-
-    enum A<T>
-        where T: Copy
-    {
-        A,
-    }
-
-    struct B<T>
-        where T: Copy
-    {
-        b: i32,
-    }
-
-    // For empty enums and structs, the brace remains on the same line.
-    enum C<T>
-        where T: Copy
-    {}
-
-    struct D<T>
-        where T: Copy
-    {}
-}
diff --git a/tests/target/item-brace-style-prefer-same-line.rs b/tests/target/item-brace-style-prefer-same-line.rs
new file mode 100644 (file)
index 0000000..b38bd2e
--- /dev/null
@@ -0,0 +1,31 @@
+// rustfmt-item_brace_style: PreferSameLine
+
+mod M {
+    enum A {
+        A,
+    }
+
+    struct B {
+        b: i32,
+    }
+
+    enum C {}
+
+    struct D {}
+
+    enum A<T>
+        where T: Copy {
+        A,
+    }
+
+    struct B<T>
+        where T: Copy {
+        b: i32,
+    }
+
+    enum C<T>
+        where T: Copy {}
+
+    struct D<T>
+        where T: Copy {}
+}
diff --git a/tests/target/item-brace-style-same-line-where.rs b/tests/target/item-brace-style-same-line-where.rs
new file mode 100644 (file)
index 0000000..cdcd813
--- /dev/null
@@ -0,0 +1,35 @@
+// rustfmt-item_brace_style: SameLineWhere
+
+mod M {
+    enum A {
+        A,
+    }
+
+    struct B {
+        b: i32,
+    }
+
+    // For empty enums and structs, the brace remains on the same line.
+    enum C {}
+
+    struct D {}
+
+    enum A<T>
+        where T: Copy
+    {
+        A,
+    }
+
+    struct B<T>
+        where T: Copy
+    {
+        b: i32,
+    }
+
+    // For empty enums and structs, the brace remains on the same line.
+    enum C<T>
+        where T: Copy {}
+
+    struct D<T>
+        where T: Copy {}
+}
diff --git a/tests/target/item-brace-style-same-line.rs b/tests/target/item-brace-style-same-line.rs
deleted file mode 100644 (file)
index b38bd2e..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-// rustfmt-item_brace_style: PreferSameLine
-
-mod M {
-    enum A {
-        A,
-    }
-
-    struct B {
-        b: i32,
-    }
-
-    enum C {}
-
-    struct D {}
-
-    enum A<T>
-        where T: Copy {
-        A,
-    }
-
-    struct B<T>
-        where T: Copy {
-        b: i32,
-    }
-
-    enum C<T>
-        where T: Copy {}
-
-    struct D<T>
-        where T: Copy {}
-}