Forbid method chain to get combined if it uses multi line.

If the method chain goes multi line before the last element, disallow combining
the method chain.
This commit is contained in:
topecongiro 2017-06-04 15:23:00 +09:00
parent fabbef2c3e
commit b49269ad39
4 changed files with 11 additions and 0 deletions

View File

@ -252,6 +252,9 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
String::new()
} else {
// Use new lines.
if context.force_one_line_chain {
return None;
}
format!("\n{}", nested_shape.indent.to_string(context.config))
};

View File

@ -1784,6 +1784,11 @@ fn try_overflow_last_arg(context: &RewriteContext,
// Replace the last item with its first line to see if it fits with
// first arguments.
let (orig_last, placeholder) = if overflow_last {
let mut context = context.clone();
match args[args.len() - 1].node {
ast::ExprKind::MethodCall(..) => context.force_one_line_chain = true,
_ => (),
}
last_arg_shape(&context, &item_vec, shape).map_or((None, None), |arg_shape| {
rewrite_last_arg_with_overflow(&context,
&args[args.len() - 1],

View File

@ -32,6 +32,8 @@ pub struct RewriteContext<'a> {
// When `format_if_else_cond_comment` is true, unindent the comment on top
// of the `else` or `else if`.
pub is_if_else_block: bool,
// When rewriting chain, veto going multi line except the last element
pub force_one_line_chain: bool,
}
impl<'a> RewriteContext<'a> {

View File

@ -629,6 +629,7 @@ pub fn get_context(&self) -> RewriteContext {
inside_macro: false,
use_block: false,
is_if_else_block: false,
force_one_line_chain: false,
}
}
}