Avoid rewriting the last argument whenever possible

This commit is contained in:
topecongiro 2017-08-07 17:30:32 +09:00
parent be38606c77
commit bb8afcb091

View File

@ -2244,7 +2244,7 @@ fn try_overflow_last_arg<'a, T>(
// Replace the last item with its first line to see if it fits with
// first arguments.
let (orig_last, placeholder) = if overflow_last {
let placeholder = if overflow_last {
let mut context = context.clone();
if let Some(expr) = args[args.len() - 1].to_expr() {
match expr.node {
@ -2252,22 +2252,14 @@ fn try_overflow_last_arg<'a, T>(
_ => (),
}
}
last_arg_shape(&context, &item_vec, shape, args_max_width).map_or(
(None, None),
|arg_shape| {
rewrite_last_arg_with_overflow(
&context,
args,
&mut item_vec[args.len() - 1],
arg_shape,
)
},
)
last_arg_shape(&context, &item_vec, shape, args_max_width).and_then(|arg_shape| {
rewrite_last_arg_with_overflow(&context, args, &mut item_vec[args.len() - 1], arg_shape)
})
} else {
(None, None)
None
};
let tactic = definitive_tactic(
let mut tactic = definitive_tactic(
&*item_vec,
ListTactic::LimitedHorizontalVertical(args_max_width),
Separator::Comma,
@ -2280,10 +2272,17 @@ fn try_overflow_last_arg<'a, T>(
(true, DefinitiveListTactic::Horizontal, placeholder @ Some(..)) => {
item_vec[args.len() - 1].item = placeholder;
}
(true, _, _) => {
item_vec[args.len() - 1].item = orig_last;
_ if args.len() >= 1 => {
item_vec[args.len() - 1].item = args.last()
.and_then(|last_arg| last_arg.rewrite(context, shape));
tactic = definitive_tactic(
&*item_vec,
ListTactic::LimitedHorizontalVertical(args_max_width),
Separator::Comma,
one_line_width,
);
}
(false, _, _) => {}
_ => (),
}
tactic
@ -2358,7 +2357,7 @@ fn rewrite_last_arg_with_overflow<'a, T>(
args: &[&T],
last_item: &mut ListItem,
shape: Shape,
) -> (Option<String>, Option<String>)
) -> Option<String>
where
T: Rewrite + Spanned + ToExpr + 'a,
{
@ -2389,14 +2388,13 @@ fn rewrite_last_arg_with_overflow<'a, T>(
} else {
last_arg.rewrite(context, shape)
};
let orig_last = last_item.item.clone();
if let Some(rewrite) = rewrite {
let rewrite_first_line = Some(rewrite[..first_line_width(&rewrite)].to_owned());
last_item.item = rewrite_first_line;
(orig_last, Some(rewrite))
Some(rewrite)
} else {
(orig_last, None)
None
}
}