From 92b54d6490fc638a136c07b42921ba7ff67743fa Mon Sep 17 00:00:00 2001 From: topecongiro Date: Wed, 24 May 2017 00:06:23 +0900 Subject: [PATCH] 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. --- src/chains.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/chains.rs b/src/chains.rs index 7ab81aa7197..5ea43708af3 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -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 }