Add heuristic when rewriting chain with a single child

rustfmt splits chain into multiline when the length of chain exceeds
`chain_one_line_max`. However, currenly this rule only applies when the chain
has more than one child. This can lead to unexpected long chain if the parent
is long. This commit adds heuristic that if the length of parent is longer
than the half of `chain_one_line_max` we use multiline even if there is
only a single child.
This commit is contained in:
topecongiro 2017-05-24 00:06:23 +09:00
parent ec33121aaf
commit 92b54d6490

View file

@ -155,10 +155,10 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
first_child_shape,
other_child_shape);
let child_shape_iter =
Some(first_child_shape)
.into_iter()
.chain(::std::iter::repeat(other_child_shape).take(subexpr_list.len() - 1));
let child_shape_iter = Some(first_child_shape)
.into_iter()
.chain(::std::iter::repeat(other_child_shape)
.take(subexpr_list.len() - 1));
let iter = subexpr_list.iter().rev().zip(child_shape_iter);
let mut rewrites =
try_opt!(iter.map(|(e, shape)| rewrite_chain_subexpr(e, total_span, context, shape))
@ -175,7 +175,7 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
if rewrites.len() > 1 {
true
} else if rewrites.len() == 1 {
one_line_len > shape.width
parent_rewrite.len() > context.config.chain_one_line_max() / 2
} else {
false
}