Coalesce trailing comma options

This commit is contained in:
Nick Cameron 2017-02-24 10:31:23 +13:00
parent 7ad352239a
commit 6a58d91239
10 changed files with 14 additions and 213 deletions

View File

@ -104,6 +104,7 @@ configuration_option_enum! { TypeDensity:
Wide,
}
impl Density {
pub fn to_list_tactic(self) -> ListTactic {
match self {
@ -348,6 +349,8 @@ create_config! {
control_brace_style: ControlBraceStyle, ControlBraceStyle::AlwaysSameLine,
"Brace style for control flow constructs";
impl_empty_single_line: bool, true, "Put empty-body implementations on a single line";
trailing_comma: SeparatorTactic, SeparatorTactic::Vertical,
"How to handle trailing commas for lists";
fn_empty_single_line: bool, true, "Put empty-body functions on a single line";
fn_single_line: bool, false, "Put single-expression functions on a single line";
fn_return_indent: ReturnIndent, ReturnIndent::WithArgs,
@ -366,16 +369,10 @@ create_config! {
where_layout: ListTactic, ListTactic::Vertical, "Element layout inside a where clause";
where_pred_indent: BlockIndentStyle, BlockIndentStyle::Visual,
"Indentation style of a where predicate";
where_trailing_comma: bool, false, "Put a trailing comma on where clauses";
generics_indent: BlockIndentStyle, BlockIndentStyle::Visual, "Indentation of generics";
struct_trailing_comma: SeparatorTactic, SeparatorTactic::Vertical,
"If there is a trailing comma on structs";
struct_lit_trailing_comma: SeparatorTactic, SeparatorTactic::Vertical,
"If there is a trailing comma on literal structs";
struct_lit_style: StructLitStyle, StructLitStyle::Block, "Style of struct definition";
struct_lit_multiline_style: MultilineStyle, MultilineStyle::PreferSingle,
"Multiline style on literal structs";
enum_trailing_comma: bool, true, "Put a trailing comma on enum declarations";
report_todo: ReportTactic, ReportTactic::Never,
"Report all, none or unnumbered occurrences of TODO in source file comments";
report_fixme: ReportTactic, ReportTactic::Never,
@ -396,7 +393,6 @@ create_config! {
wrap_match_arms: bool, true, "Wrap multiline match arms in blocks";
match_block_trailing_comma: bool, false,
"Put a trailing comma after a block based match arm (non-block arms are not affected)";
match_wildcard_trailing_comma: bool, true, "Put a trailing comma after a wildcard arm";
closure_block_indent_threshold: isize, 5, "How many lines a closure must have before it is \
block indented. -1 means never use block indent.";
space_before_type_annotation: bool, false,

View File

@ -1150,12 +1150,6 @@ fn arm_end_pos(arm: &ast::Arm) -> BytePos {
}
fn arm_comma(config: &Config, arm: &ast::Arm, body: &ast::Expr) -> &'static str {
if !config.match_wildcard_trailing_comma {
if arm.pats.len() == 1 && arm.pats[0].node == ast::PatKind::Wild && arm.guard.is_none() {
return "";
}
}
if config.match_block_trailing_comma {
","
} else if let ast::ExprKind::Block(ref block) = body.node {
@ -1759,7 +1753,7 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
trailing_separator: if base.is_some() {
SeparatorTactic::Never
} else {
context.config.struct_lit_trailing_comma
context.config.trailing_comma
},
shape: nested_shape,
ends_with_newline: ends_with_newline,

View File

@ -434,7 +434,7 @@ impl<'a> FmtVisitor<'a> {
let fmt = ListFormatting {
tactic: DefinitiveListTactic::Vertical,
separator: ",",
trailing_separator: SeparatorTactic::from_bool(self.config.enum_trailing_comma),
trailing_separator: self.config.trailing_comma,
shape: Shape::legacy(budget, self.block_indent),
ends_with_newline: true,
config: self.config,
@ -526,7 +526,6 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -
offset.block_only()),
context.config.where_density,
"{",
true,
None));
if try_opt!(is_impl_single_line(context, &items, &result, &where_clause_str, &item)) {
@ -795,7 +794,6 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
offset.block_only()),
where_density,
"{",
has_body,
None));
// If the where clause cannot fit on the same line,
// put the where clause on a new line
@ -952,7 +950,7 @@ fn format_struct_struct(context: &RewriteContext,
let fmt = ListFormatting {
tactic: tactic,
separator: ",",
trailing_separator: context.config.struct_trailing_comma,
trailing_separator: context.config.trailing_comma,
shape: Shape::legacy(budget, item_indent),
ends_with_newline: true,
config: context.config,
@ -1010,7 +1008,6 @@ fn format_tuple_struct(context: &RewriteContext,
Shape::legacy(where_budget, offset.block_only()),
Density::Compressed,
";",
false,
None))
}
None => "".to_owned(),
@ -1103,7 +1100,6 @@ pub fn rewrite_type_alias(context: &RewriteContext,
Shape::legacy(where_budget, indent),
context.config.where_density,
"=",
false,
Some(span.hi)));
result.push_str(&where_clause_str);
result.push_str(" = ");
@ -1645,7 +1641,6 @@ fn rewrite_fn_base(context: &RewriteContext,
Shape::legacy(where_budget, indent),
where_density,
"{",
has_body,
Some(span.hi)));
if last_line_width(&result) + where_clause_str.len() > context.config.max_width &&
@ -1929,7 +1924,6 @@ fn rewrite_where_clause(context: &RewriteContext,
shape: Shape,
density: Density,
terminator: &str,
allow_trailing_comma: bool,
span_end: Option<BytePos>)
-> Option<String> {
if where_clause.predicates.is_empty() {
@ -1969,12 +1963,17 @@ fn rewrite_where_clause(context: &RewriteContext,
// FIXME: we don't need to collect here if the where_layout isn't
// HorizontalVertical.
let tactic = definitive_tactic(&item_vec, context.config.where_layout, budget);
let use_trailing_comma = allow_trailing_comma && context.config.where_trailing_comma;
let mut comma_tactic = context.config.trailing_comma;
// Kind of a hack because we don't usually have trailing commas in where clauses.
if comma_tactic == SeparatorTactic::Vertical {
comma_tactic = SeparatorTactic::Never;
}
let fmt = ListFormatting {
tactic: tactic,
separator: ",",
trailing_separator: SeparatorTactic::from_bool(use_trailing_comma),
trailing_separator: comma_tactic,
shape: Shape::legacy(budget, offset),
ends_with_newline: true,
config: context.config,
@ -2034,7 +2033,6 @@ fn format_generics(context: &RewriteContext,
offset.block_only()),
Density::Tall,
terminator,
true,
Some(span.hi)));
result.push_str(&where_clause_str);
if !force_same_line_brace &&

View File

@ -12,12 +12,9 @@ where_density = "Tall"
where_indent = "Tabbed"
where_layout = "Vertical"
where_pred_indent = "Visual"
where_trailing_comma = false
generics_indent = "Visual"
struct_trailing_comma = "Vertical"
struct_lit_trailing_comma = "Vertical"
trailing_comma = "Vertical"
struct_lit_style = "Block"
enum_trailing_comma = true
report_todo = "Always"
report_fixme = "Never"
reorder_imports = false

View File

@ -1,41 +0,0 @@
// rustfmt-enum_trailing_comma: false
enum X {
A,
B,
}
enum Y {
A,
B
}
enum TupX {
A(u32),
B(i32, u16),
}
enum TupY {
A(u32),
B(i32, u16)
}
enum StructX {
A {
s: u16,
},
B {
u: u32,
i: i32,
},
}
enum StructY {
A {
s: u16,
},
B {
u: u32,
i: i32,
}
}

View File

@ -1,10 +0,0 @@
// rustfmt-match_wildcard_trailing_comma: false
fn match_wild(x: i32) -> i32 {
match x {
1 => 1,
2 => 2,
3 => 3,
_ => 0,
}
}

View File

@ -1,36 +0,0 @@
// rustfmt-where_trailing_comma: true
fn f<S, T>(x: T, y: S) -> T where T: P, S: Q
{
x
}
impl Trait for T where T: P
{
fn f(x: T) -> T where T: Q + R
{
x
}
}
struct Pair<S, T> where T: P, S: P + Q {
a: T,
b: S
}
struct TupPair<S, T> (S, T) where T: P, S: P + Q;
enum E<S, T> where S: P, T: P {
A {a: T},
}
type Double<T> where T: P, T: Q = Pair<T, T>;
extern "C" {
fn f<S, T>(x: T, y: S) -> T where T: P, S: Q;
}
trait Q<S, T> where T: P, S: R
{
fn f<U, V>(self, x: T, y: S, z: U) -> Self where U: P, V: P;
}

View File

@ -1,31 +0,0 @@
// rustfmt-enum_trailing_comma: false
enum X {
A,
B
}
enum Y {
A,
B
}
enum TupX {
A(u32),
B(i32, u16)
}
enum TupY {
A(u32),
B(i32, u16)
}
enum StructX {
A { s: u16 },
B { u: u32, i: i32 }
}
enum StructY {
A { s: u16 },
B { u: u32, i: i32 }
}

View File

@ -1,10 +0,0 @@
// rustfmt-match_wildcard_trailing_comma: false
fn match_wild(x: i32) -> i32 {
match x {
1 => 1,
2 => 2,
3 => 3,
_ => 0
}
}

View File

@ -1,56 +0,0 @@
// rustfmt-where_trailing_comma: true
fn f<S, T>(x: T, y: S) -> T
where T: P,
S: Q,
{
x
}
impl Trait for T
where T: P,
{
fn f(x: T) -> T
where T: Q + R,
{
x
}
}
struct Pair<S, T>
where T: P,
S: P + Q,
{
a: T,
b: S,
}
struct TupPair<S, T>(S, T)
where T: P,
S: P + Q;
enum E<S, T>
where S: P,
T: P,
{
A { a: T },
}
type Double<T>
where T: P,
T: Q = Pair<T, T>;
extern "C" {
fn f<S, T>(x: T, y: S) -> T
where T: P,
S: Q;
}
trait Q<S, T>
where T: P,
S: R,
{
fn f<U, V>(self, x: T, y: S, z: U) -> Self
where U: P,
V: P;
}