about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNick Cameron <nrc@ncameron.org>2016-05-19 08:38:49 +1200
committerMarcus Klaas de Vries <mail@marcusklaas.nl>2016-05-18 22:38:49 +0200
commit775de8a62b3c19521543cb6b9130979eb6747c75 (patch)
tree51044338e46d7e6f0951aceec2faea394eba35d8
parent9589cac62d54fa199524b6110df67f626ed3bdfa (diff)
Optionally put short struct variants on one line (#997)
Closes #418
-rw-r--r--src/config.rs2
-rw-r--r--src/issues.rs10
-rw-r--r--src/items.rs40
-rw-r--r--src/visitor.rs3
-rw-r--r--tests/target/enum-no_trailing_comma.rs18
-rw-r--r--tests/target/enum.rs24
-rw-r--r--tests/target/where-trailing-comma.rs4
7 files changed, 44 insertions, 57 deletions
diff --git a/src/config.rs b/src/config.rs
index 8e8f4bd2f3d..e4d0e4517c2 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -334,6 +334,8 @@ create_config! {
         "Maximum width of the args of a function call before falling back to vertical formatting";
     struct_lit_width: usize, 16,
         "Maximum width in the body of a struct lit before falling back to vertical formatting";
+    struct_variant_width: usize, 35,
+        "Maximum width in the body of a struct variant before falling back to vertical formatting";
     force_explicit_abi: bool, true, "Always print the abi for extern items";
     newline_style: NewlineStyle, NewlineStyle::Unix, "Unix or Windows line endings";
     fn_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for functions";
diff --git a/src/issues.rs b/src/issues.rs
index 40b09fdfca5..64797de2fda 100644
--- a/src/issues.rs
+++ b/src/issues.rs
@@ -32,14 +32,8 @@ impl ReportTactic {
 
 #[derive(Clone, Copy)]
 enum Seeking {
-    Issue {
-        todo_idx: usize,
-        fixme_idx: usize,
-    },
-    Number {
-        issue: Issue,
-        part: NumberPart,
-    },
+    Issue { todo_idx: usize, fixme_idx: usize },
+    Number { issue: Issue, part: NumberPart },
 }
 
 #[derive(Clone, Copy)]
diff --git a/src/items.rs b/src/items.rs
index b4b200e8b05..5c5e9923b32 100644
--- a/src/items.rs
+++ b/src/items.rs
@@ -14,7 +14,7 @@ use Indent;
 use utils::{CodeMapSpanUtils, format_mutability, format_visibility, contains_skip, end_typaram,
             wrap_str, last_line_width, semicolon_for_expr, format_unsafety, trim_newlines};
 use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic,
-            DefinitiveListTactic, definitive_tactic, format_item_list};
+            DefinitiveListTactic, ListTactic, definitive_tactic, format_item_list};
 use expr::{is_empty_block, is_simple_block_stmt, rewrite_assign_rhs};
 use comment::{FindUncommented, contains_comment};
 use visitor::FmtVisitor;
@@ -419,7 +419,8 @@ impl<'a> FmtVisitor<'a> {
                               &field.node.data,
                               None,
                               field.span,
-                              indent)
+                              indent,
+                              Some(self.config.struct_variant_width))
             }
             ast::VariantData::Unit(..) => {
                 let tag = if let Some(ref expr) = field.node.disr_expr {
@@ -588,7 +589,8 @@ pub fn format_struct(context: &RewriteContext,
                      struct_def: &ast::VariantData,
                      generics: Option<&ast::Generics>,
                      span: Span,
-                     offset: Indent)
+                     offset: Indent,
+                     one_line_width: Option<usize>)
                      -> Option<String> {
     match *struct_def {
         ast::VariantData::Unit(..) => format_unit_struct(item_name, ident, vis),
@@ -610,7 +612,8 @@ pub fn format_struct(context: &RewriteContext,
                                  fields,
                                  generics,
                                  span,
-                                 offset)
+                                 offset,
+                                 one_line_width)
         }
     }
 }
@@ -758,7 +761,8 @@ fn format_struct_struct(context: &RewriteContext,
                         fields: &[ast::StructField],
                         generics: Option<&ast::Generics>,
                         span: Span,
-                        offset: Indent)
+                        offset: Indent,
+                        one_line_width: Option<usize>)
                         -> Option<String> {
     let mut result = String::with_capacity(1024);
 
@@ -813,11 +817,18 @@ fn format_struct_struct(context: &RewriteContext,
                              |field| field.ty.span.hi,
                              |field| field.rewrite(context, item_budget, item_indent),
                              context.codemap.span_after(span, "{"),
-                             span.hi);
+                             span.hi)
+        .collect::<Vec<_>>();
     // 1 = ,
     let budget = context.config.max_width - offset.width() + context.config.tab_spaces - 1;
+
+    let tactic = match one_line_width {
+        Some(w) => definitive_tactic(&items, ListTactic::LimitedHorizontalVertical(w), budget),
+        None => DefinitiveListTactic::Vertical,
+    };
+
     let fmt = ListFormatting {
-        tactic: DefinitiveListTactic::Vertical,
+        tactic: tactic,
         separator: ",",
         trailing_separator: context.config.struct_trailing_comma,
         indent: item_indent,
@@ -825,11 +836,16 @@ fn format_struct_struct(context: &RewriteContext,
         ends_with_newline: true,
         config: context.config,
     };
-    Some(format!("{}\n{}{}\n{}}}",
-                 result,
-                 offset.block_indent(context.config).to_string(context.config),
-                 try_opt!(write_list(items, &fmt)),
-                 offset.to_string(context.config)))
+    let items_str = try_opt!(write_list(&items, &fmt));
+    if one_line_width.is_some() && !items_str.contains('\n') {
+        Some(format!("{} {} }}", result, items_str))
+    } else {
+        Some(format!("{}\n{}{}\n{}}}",
+                     result,
+                     offset.block_indent(context.config).to_string(context.config),
+                     items_str,
+                     offset.to_string(context.config)))
+    }
 }
 
 fn format_tuple_struct(context: &RewriteContext,
diff --git a/src/visitor.rs b/src/visitor.rs
index 48a1a778c20..fb508f64164 100644
--- a/src/visitor.rs
+++ b/src/visitor.rs
@@ -267,7 +267,8 @@ impl<'a> FmtVisitor<'a> {
                                            def,
                                            Some(generics),
                                            item.span,
-                                           indent)
+                                           indent,
+                                           None)
                         .map(|s| {
                             match *def {
                                 ast::VariantData::Tuple(..) => s + ";",
diff --git a/tests/target/enum-no_trailing_comma.rs b/tests/target/enum-no_trailing_comma.rs
index 4ad7d1f12d7..2e5a5ad23e3 100644
--- a/tests/target/enum-no_trailing_comma.rs
+++ b/tests/target/enum-no_trailing_comma.rs
@@ -21,21 +21,11 @@ enum TupY {
 }
 
 enum StructX {
-    A {
-        s: u16,
-    },
-    B {
-        u: u32,
-        i: i32,
-    }
+    A { s: u16 },
+    B { u: u32, i: i32 }
 }
 
 enum StructY {
-    A {
-        s: u16,
-    },
-    B {
-        u: u32,
-        i: i32,
-    }
+    A { s: u16 },
+    B { u: u32, i: i32 }
 }
diff --git a/tests/target/enum.rs b/tests/target/enum.rs
index ae1bc4bc276..aa51b90a79e 100644
--- a/tests/target/enum.rs
+++ b/tests/target/enum.rs
@@ -42,9 +42,7 @@ enum StructLikeVariants {
         #[Attr50]
         y: SomeType, // Aanother Comment
     },
-    SL {
-        a: A,
-    },
+    SL { a: A },
 }
 
 enum X {
@@ -64,10 +62,7 @@ pub enum EnumWithAttributes {
     SkippedItem(String,String,), // Post-comment
     #[another_attr]
     #[attr2]
-    ItemStruct {
-        x: usize,
-        y: usize,
-    }, /* Comment AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */
+    ItemStruct { x: usize, y: usize }, /* Comment AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */
     // And another
     ForcedPreflight, /* AAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
                       * AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */
@@ -81,24 +76,15 @@ pub enum SingleTuple {
 }
 
 pub enum SingleStruct {
-    Match {
-        name: String,
-        loc: usize,
-    }, // Post-comment
+    Match { name: String, loc: usize }, // Post-comment
 }
 
 pub enum GenericEnum<I, T>
     where I: Iterator<Item = T>
 {
     // Pre Comment
-    Left {
-        list: I,
-        root: T,
-    }, // Post-comment
-    Right {
-        list: I,
-        root: T,
-    }, // Post Comment
+    Left { list: I, root: T }, // Post-comment
+    Right { list: I, root: T }, // Post Comment
 }
 
 
diff --git a/tests/target/where-trailing-comma.rs b/tests/target/where-trailing-comma.rs
index c8682237ae6..4d4d19cdef8 100644
--- a/tests/target/where-trailing-comma.rs
+++ b/tests/target/where-trailing-comma.rs
@@ -33,9 +33,7 @@ enum E<S, T>
     where S: P,
           T: P,
 {
-    A {
-        a: T,
-    },
+    A { a: T },
 }
 
 type Double<T>