diff --git a/src/config.rs b/src/config.rs index 8e8f4bd2f3d6..e4d0e4517c26 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 40b09fdfca5b..64797de2fdaa 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 b4b200e8b051..5c5e9923b322 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) -> Option { 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) -> Option { 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::>(); // 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 48a1a778c20a..fb508f641641 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 4ad7d1f12d71..2e5a5ad23e3a 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 ae1bc4bc2761..aa51b90a79ed 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 where I: Iterator { // 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 c8682237ae6a..4d4d19cdef81 100644 --- a/tests/target/where-trailing-comma.rs +++ b/tests/target/where-trailing-comma.rs @@ -33,9 +33,7 @@ enum E where S: P, T: P, { - A { - a: T, - }, + A { a: T }, } type Double