about summary refs log tree commit diff
path: root/src/expr.rs
diff options
context:
space:
mode:
authortopecongiro <seuchida@gmail.com>2017-05-25 06:10:00 +0900
committertopecongiro <seuchida@gmail.com>2017-05-25 06:10:00 +0900
commitc8ad521c9f4d45935cc24c87772f00f6df360152 (patch)
treedb78732af29e003e120d88ed526ef711749fb12f /src/expr.rs
parent93c556c6ba197ce136aa50a2d252e77a1421cba5 (diff)
Decide whether args fit in a single line inside rewrite_call_args
Diffstat (limited to 'src/expr.rs')
-rw-r--r--src/expr.rs29
1 files changed, 16 insertions, 13 deletions
diff --git a/src/expr.rs b/src/expr.rs
index 29edb0a3e3f..bb9e55e4f16 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -1673,15 +1673,9 @@ fn rewrite_call_inner<R>(context: &RewriteContext,
                                                    one_line_width,
                                                    force_trailing_comma)
         .ok_or(Ordering::Less)?;
-    let arg_one_line_budget = min(one_line_width, context.config.fn_call_width());
     Ok(format!("{}{}",
                callee_str,
-               wrap_args_with_parens(context,
-                                     &list_str,
-                                     extendable,
-                                     arg_one_line_budget,
-                                     shape,
-                                     nested_shape)))
+               wrap_args_with_parens(context, &list_str, extendable, shape, nested_shape)))
 }
 
 fn rewrite_call_args(context: &RewriteContext,
@@ -1771,12 +1765,16 @@ fn rewrite_call_args(context: &RewriteContext,
         config: context.config,
     };
 
-    let args_in_single_line =
+    let almost_no_newline =
         item_vec
             .iter()
             .rev()
             .skip(1)
             .all(|item| item.item.as_ref().map_or(false, |s| !s.contains('\n')));
+    let extendable = almost_no_newline &&
+                     item_vec.iter().fold(0, |acc, item| {
+        acc + item.item.as_ref().map_or(0, |s| 2 + first_line_width(s))
+    }) <= min(one_line_width, context.config.fn_call_width()) + 2;
 
     match write_list(&item_vec, &fmt) {
         // If arguments do not fit in a single line and do not contain newline,
@@ -1791,7 +1789,15 @@ fn rewrite_call_args(context: &RewriteContext,
             fmt.tactic = DefinitiveListTactic::Vertical;
             write_list(&item_vec, &fmt).map(|rw| (false, rw))
         }
-        rewrite @ _ => rewrite.map(|rw| (args_in_single_line && is_extendable(args), rw)),
+        rewrite @ _ => {
+            rewrite.map(|rw| {
+                            (extendable &&
+                             rw.chars()
+                                 .last()
+                                 .map_or(true, |c| force_trailing_comma || c != ','),
+                             rw)
+                        })
+        }
     }
 }
 
@@ -1841,14 +1847,11 @@ fn is_extendable(args: &[ptr::P<ast::Expr>]) -> bool {
 fn wrap_args_with_parens(context: &RewriteContext,
                          args_str: &str,
                          is_extendable: bool,
-                         one_line_budget: usize,
                          shape: Shape,
                          nested_shape: Shape)
                          -> String {
     if context.config.fn_call_style() == IndentStyle::Visual ||
-       (context.inside_macro && !args_str.contains('\n')) ||
-       ((is_extendable || !args_str.contains('\n')) &&
-        first_line_width(&args_str) <= one_line_budget) {
+       (context.inside_macro && !args_str.contains('\n')) || is_extendable {
         if context.config.spaces_within_parens() && args_str.len() > 0 {
             format!("( {} )", args_str)
         } else {