Use block indent for args if single line args exceeds max width

This commit is contained in:
topecongiro 2017-06-12 15:58:27 +09:00
parent c879d5ebd7
commit 9ad499786d
4 changed files with 31 additions and 4 deletions

View File

@ -1734,9 +1734,12 @@ fn rewrite_call_inner(context: &RewriteContext,
force_trailing_comma);
}
let args_shape = shape
.sub_width(last_line_width(&callee_str))
.ok_or(Ordering::Less)?;
Ok(format!("{}{}",
callee_str,
wrap_args_with_parens(context, &list_str, extendable, shape, nested_shape)))
wrap_args_with_parens(context, &list_str, extendable, args_shape, nested_shape)))
}
fn need_block_indent(s: &str, shape: Shape) -> bool {
@ -1906,14 +1909,23 @@ fn can_be_overflowed_expr(context: &RewriteContext, expr: &ast::Expr, args_len:
}
}
fn paren_overhead(context: &RewriteContext) -> usize {
if context.config.spaces_within_parens() {
4
} else {
2
}
}
fn wrap_args_with_parens(context: &RewriteContext,
args_str: &str,
is_extendable: bool,
shape: Shape,
nested_shape: Shape)
-> String {
if !context.use_block_indent() || (context.inside_macro && !args_str.contains('\n')) ||
is_extendable {
if !context.use_block_indent() ||
(context.inside_macro && !args_str.contains('\n') &&
args_str.len() + paren_overhead(context) <= shape.width) || is_extendable {
if context.config.spaces_within_parens() && args_str.len() > 0 {
format!("( {} )", args_str)
} else {

View File

@ -125,3 +125,9 @@ fn issue1581() {
},
);
}
fn issue1651() {
{
let type_list: Vec<_> = try_opt!(types.iter().map(|ty| ty.rewrite(context, shape)).collect());
}
}

View File

@ -3,7 +3,9 @@
// rustfmt should not add trailing comma when rewriting macro. See #1528.
fn a() {
panic!("this is a long string that goes past the maximum line length causing rustfmt to insert a comma here:");
panic!(
"this is a long string that goes past the maximum line length causing rustfmt to insert a comma here:"
);
foo(
oooptoptoptoptptooptoptoptoptptooptoptoptoptptoptoptoptoptpt(),
);

View File

@ -145,3 +145,10 @@ fn issue1581() {
}
});
}
fn issue1651() {
{
let type_list: Vec<_> =
try_opt!(types.iter().map(|ty| ty.rewrite(context, shape)).collect());
}
}