From b12fecb6317949a3d51dc1339c3fa0d0f0dae57d Mon Sep 17 00:00:00 2001 From: topecongiro Date: Tue, 19 Sep 2017 11:41:05 +0900 Subject: [PATCH] Use horizontal layout for a function with a single argument foo(long_arg) instead of foo( long_arg, ) --- src/expr.rs | 23 +++++++++++++++++------ src/lists.rs | 2 +- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index e06c928044a..dc6504de0b1 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -2215,12 +2215,23 @@ where _ 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, - ); + // Use horizontal layout for a function with a single argument as long as + // everything fits in a single line. + if args.len() == 1 + && args_max_width != 0 // Vertical layout is forced. + && !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, + ); + } } _ => (), } diff --git a/src/lists.rs b/src/lists.rs index f42e3ffe977..e499377e48a 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -713,7 +713,7 @@ where .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.post_comment.as_ref().map(|x| &(*x)[..])) + item.item.as_ref().map_or(0, |str| str.len())