Add trailing comma when using Mixed indent style with newline

This commit is contained in:
topecongiro 2017-07-13 20:32:46 +09:00
parent 3bf66436e7
commit dcb953b901
9 changed files with 38 additions and 25 deletions

View file

@ -501,10 +501,12 @@ where
DefinitiveListTactic::Mixed
},
};
let mut ends_with_newline = tactic.ends_with_newline(context.config.array_layout());
if context.config.array_horizontal_layout_threshold() > 0 &&
items.len() > context.config.array_horizontal_layout_threshold()
{
tactic = DefinitiveListTactic::Mixed;
ends_with_newline = false;
if context.config.array_layout() == IndentStyle::Block {
nested_shape = try_opt!(
shape
@ -525,7 +527,7 @@ where
SeparatorTactic::Vertical
},
shape: nested_shape,
ends_with_newline: false,
ends_with_newline: ends_with_newline,
config: context.config,
};
let list_str = try_opt!(write_list(&items, &fmt));

View file

@ -2204,21 +2204,18 @@ fn rewrite_args(
.and_then(|item| item.post_comment.as_ref())
.map_or(false, |s| s.trim().starts_with("//"));
let (indent, trailing_comma, end_with_newline) = match context.config.fn_args_layout() {
IndentStyle::Block if fits_in_one_line => (
indent.block_indent(context.config),
SeparatorTactic::Never,
true,
),
let (indent, trailing_comma) = match context.config.fn_args_layout() {
IndentStyle::Block if fits_in_one_line => {
(indent.block_indent(context.config), SeparatorTactic::Never)
}
IndentStyle::Block => (
indent.block_indent(context.config),
context.config.trailing_comma(),
true,
),
IndentStyle::Visual if last_line_ends_with_comment => {
(arg_indent, context.config.trailing_comma(), true)
(arg_indent, context.config.trailing_comma())
}
IndentStyle::Visual => (arg_indent, SeparatorTactic::Never, false),
IndentStyle::Visual => (arg_indent, SeparatorTactic::Never),
};
let tactic = definitive_tactic(
@ -2242,7 +2239,7 @@ fn rewrite_args(
trailing_comma
},
shape: Shape::legacy(budget, indent),
ends_with_newline: end_with_newline,
ends_with_newline: tactic.ends_with_newline(context.config.fn_args_layout()),
config: context.config,
};
@ -2406,8 +2403,6 @@ where
let item_vec = items.collect::<Vec<_>>();
let tactic = definitive_tactic(&item_vec, ListTactic::HorizontalVertical, one_line_budget);
let ends_with_newline = context.config.generics_indent() == IndentStyle::Block &&
tactic == DefinitiveListTactic::Vertical;
let fmt = ListFormatting {
tactic: tactic,
separator: ",",
@ -2417,7 +2412,7 @@ where
context.config.trailing_comma()
},
shape: shape,
ends_with_newline: ends_with_newline,
ends_with_newline: tactic.ends_with_newline(context.config.generics_indent()),
config: context.config,
};
@ -2631,7 +2626,7 @@ fn rewrite_where_clause(
separator: ",",
trailing_separator: comma_tactic,
shape: Shape::legacy(budget, offset),
ends_with_newline: true,
ends_with_newline: tactic.ends_with_newline(context.config.where_pred_indent()),
config: context.config,
};
let preds_str = try_opt!(write_list(&item_vec, &fmt));

View file

@ -124,6 +124,15 @@ pub enum DefinitiveListTactic {
Mixed,
}
impl DefinitiveListTactic {
pub fn ends_with_newline(&self, indent_style: IndentStyle) -> bool {
match indent_style {
IndentStyle::Block => *self != DefinitiveListTactic::Horizontal,
IndentStyle::Visual => false,
}
}
}
pub fn definitive_tactic<I, T>(items: I, tactic: ListTactic, width: usize) -> DefinitiveListTactic
where
I: IntoIterator<Item = T> + Clone,
@ -169,7 +178,7 @@ where
// Now that we know how we will layout, we can decide for sure if there
// will be a trailing separator.
let trailing_separator = needs_trailing_separator(formatting.trailing_separator, tactic);
let mut trailing_separator = needs_trailing_separator(formatting.trailing_separator, tactic);
let mut result = String::new();
let cloned_items = items.clone();
let mut iter = items.into_iter().enumerate().peekable();
@ -182,7 +191,7 @@ where
let inner_item = try_opt!(item.item.as_ref());
let first = i == 0;
let last = iter.peek().is_none();
let separate = !last || trailing_separator;
let mut separate = !last || trailing_separator;
let item_sep_len = if separate { sep_len } else { 0 };
// Item string may be multi-line. Its length (used for block comment alignment)
@ -213,6 +222,13 @@ where
result.push('\n');
result.push_str(indent_str);
line_len = 0;
if tactic == DefinitiveListTactic::Mixed && formatting.ends_with_newline {
if last {
separate = true;
} else {
trailing_separator = true;
}
}
}
if line_len > 0 {

View file

@ -360,7 +360,7 @@ where
context.config.trailing_comma()
},
shape: list_shape,
ends_with_newline: false,
ends_with_newline: tactic.ends_with_newline(context.config.fn_call_style()),
config: context.config,
};

View file

@ -10,12 +10,12 @@ trait Lorem {
fn lorem(
ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: onsectetur,
adipiscing: Adipiscing, elit: Elit
adipiscing: Adipiscing, elit: Elit,
);
fn lorem(
ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: onsectetur,
adipiscing: Adipiscing, elit: Elit
adipiscing: Adipiscing, elit: Elit,
) {
// body
}

View file

@ -4,7 +4,7 @@
// Test compressed layout of args.
fn foo(
a: Aaaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbbbb, c: Ccccccccccccccccc, d: Ddddddddddddddddddddddddd,
e: Eeeeeeeeeeeeeeeeeee
e: Eeeeeeeeeeeeeeeeeee,
) {
foo();
}
@ -12,7 +12,7 @@ fn foo(
impl Foo {
fn foo(
self, a: Aaaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbbbb, c: Ccccccccccccccccc,
d: Ddddddddddddddddddddddddd, e: Eeeeeeeeeeeeeeeeeee
d: Ddddddddddddddddddddddddd, e: Eeeeeeeeeeeeeeeeeee,
) {
foo();
}

View file

@ -42,7 +42,7 @@ where
C,
D,
// pre comment
E, /* last comment */
E, // last comment
) -> &SomeType,
{
arg(a, b, c, d, e)

View file

@ -67,7 +67,7 @@ fn main() {
C,
D,
// pre comment
E, /* last comment */
E, // last comment
) -> &SomeType,
{
arg(a, b, c, d, e)

View file

@ -25,7 +25,7 @@ struct F {
y: String, // comment 3
z: Foo,
// comment
... /* comment 2 */
... // comment 2
),
}