Optionally put short struct variants on one line (#997)

Closes #418
This commit is contained in:
Nick Cameron 2016-05-19 08:38:49 +12:00 committed by Marcus Klaas de Vries
parent 9589cac62d
commit 775de8a62b
7 changed files with 44 additions and 57 deletions

View File

@ -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";

View File

@ -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)]

View File

@ -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,

View File

@ -267,7 +267,8 @@ impl<'a> FmtVisitor<'a> {
def,
Some(generics),
item.span,
indent)
indent,
None)
.map(|s| {
match *def {
ast::VariantData::Tuple(..) => s + ";",

View File

@ -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 }
}

View File

@ -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
}

View File

@ -33,9 +33,7 @@ enum E<S, T>
where S: P,
T: P,
{
A {
a: T,
},
A { a: T },
}
type Double<T>