diff options
| author | Nick Cameron <nrc@ncameron.org> | 2016-05-19 08:38:49 +1200 |
|---|---|---|
| committer | Marcus Klaas de Vries <mail@marcusklaas.nl> | 2016-05-18 22:38:49 +0200 |
| commit | 775de8a62b3c19521543cb6b9130979eb6747c75 (patch) | |
| tree | 51044338e46d7e6f0951aceec2faea394eba35d8 /src | |
| parent | 9589cac62d54fa199524b6110df67f626ed3bdfa (diff) | |
Optionally put short struct variants on one line (#997)
Closes #418
Diffstat (limited to 'src')
| -rw-r--r-- | src/config.rs | 2 | ||||
| -rw-r--r-- | src/issues.rs | 10 | ||||
| -rw-r--r-- | src/items.rs | 40 | ||||
| -rw-r--r-- | src/visitor.rs | 3 |
4 files changed, 34 insertions, 21 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 + ";", |
