Use horizontal layout for a function with a single argument

foo(long_arg)

instead of

foo(
    long_arg,
)
This commit is contained in:
topecongiro 2017-09-19 11:41:05 +09:00
parent b751030640
commit b12fecb631
2 changed files with 18 additions and 7 deletions

View File

@ -2215,12 +2215,23 @@ where
_ if args.len() >= 1 => { _ if args.len() >= 1 => {
item_vec[args.len() - 1].item = args.last() item_vec[args.len() - 1].item = args.last()
.and_then(|last_arg| last_arg.rewrite(context, shape)); .and_then(|last_arg| last_arg.rewrite(context, shape));
tactic = definitive_tactic( // Use horizontal layout for a function with a single argument as long as
&*item_vec, // everything fits in a single line.
ListTactic::LimitedHorizontalVertical(args_max_width), if args.len() == 1
Separator::Comma, && args_max_width != 0 // Vertical layout is forced.
one_line_width, && !item_vec[0].has_comment()
); && !item_vec[0].inner_as_ref().contains('\n')
&& ::lists::total_item_width(&item_vec[0]) <= one_line_width
{
tactic = DefinitiveListTactic::Horizontal;
} else {
tactic = definitive_tactic(
&*item_vec,
ListTactic::LimitedHorizontalVertical(args_max_width),
Separator::Comma,
one_line_width,
);
}
} }
_ => (), _ => (),
} }

View File

@ -713,7 +713,7 @@ where
.fold((0, 0), |acc, l| (acc.0 + 1, acc.1 + l)) .fold((0, 0), |acc, l| (acc.0 + 1, acc.1 + l))
} }
fn total_item_width(item: &ListItem) -> usize { pub fn total_item_width(item: &ListItem) -> usize {
comment_len(item.pre_comment.as_ref().map(|x| &(*x)[..])) comment_len(item.pre_comment.as_ref().map(|x| &(*x)[..]))
+ comment_len(item.post_comment.as_ref().map(|x| &(*x)[..])) + comment_len(item.post_comment.as_ref().map(|x| &(*x)[..]))
+ item.item.as_ref().map_or(0, |str| str.len()) + item.item.as_ref().map_or(0, |str| str.len())