]> git.lizzy.rs Git - rust.git/commitdiff
Factor out a mess
authortopecongiro <seuchida@gmail.com>
Fri, 1 Dec 2017 04:44:40 +0000 (13:44 +0900)
committerSeiichi Uchida <seuchida@gmail.com>
Sun, 3 Dec 2017 02:34:18 +0000 (11:34 +0900)
src/expr.rs

index 0215975f9a35d4d26e86d7cf377a66650fc41d97..2c878fe16b58d0a57f379c7e51bbaf4f04a834d3 100644 (file)
@@ -2042,42 +2042,27 @@ fn try_overflow_last_arg<'a, T>(
                 tactic = DefinitiveListTactic::Horizontal;
             } else {
                 tactic = default_tactic();
-                let is_simple_enough =
-                    tactic == DefinitiveListTactic::Vertical && is_every_args_simple(args);
-                if is_simple_enough
-                    && FORMAT_LIKE_WHITELIST
-                        .iter()
-                        .find(|s| **s == callee_str)
-                        .is_some()
-                {
-                    let args_tactic = definitive_tactic(
-                        &item_vec[1..],
-                        ListTactic::HorizontalVertical,
-                        Separator::Comma,
-                        nested_shape.width,
-                    );
-                    tactic = if args_tactic == DefinitiveListTactic::Horizontal {
-                        DefinitiveListTactic::FormatCall
-                    } else {
-                        default_tactic()
-                    };
-                } else if is_simple_enough && item_vec.len() >= 2
-                    && WRITE_LIKE_WHITELIST
-                        .iter()
-                        .find(|s| **s == callee_str)
-                        .is_some()
-                {
+
+                // For special-case macros, we may want to use different tactics.
+                let maybe_args_offset = maybe_get_args_offset(callee_str, args);
+
+                if tactic == DefinitiveListTactic::Vertical && maybe_args_offset.is_some() {
+                    let args_offset = maybe_args_offset.unwrap();
                     let args_tactic = definitive_tactic(
-                        &item_vec[2..],
+                        &item_vec[args_offset..],
                         ListTactic::HorizontalVertical,
                         Separator::Comma,
                         nested_shape.width,
                     );
-                    tactic = if args_tactic == DefinitiveListTactic::Horizontal {
-                        DefinitiveListTactic::WriteCall
-                    } else {
-                        default_tactic()
-                    };
+
+                    // Every argument is simple and fits on a single line.
+                    if args_tactic == DefinitiveListTactic::Horizontal {
+                        tactic = if args_offset == 1 {
+                            DefinitiveListTactic::FormatCall
+                        } else {
+                            DefinitiveListTactic::WriteCall
+                        };
+                    }
                 }
             }
         }
@@ -2111,6 +2096,29 @@ fn is_every_args_simple<T: ToExpr>(lists: &[&T]) -> bool {
         .all(|arg| arg.to_expr().map_or(false, is_simple_arg))
 }
 
+/// In case special-case style is required, returns an offset from which we start horizontal layout.
+fn maybe_get_args_offset<T: ToExpr>(callee_str: &str, args: &[&T]) -> Option<usize> {
+    if FORMAT_LIKE_WHITELIST
+        .iter()
+        .find(|s| **s == callee_str)
+        .is_some()
+        && args.len() >= 1
+        && is_every_args_simple(args)
+    {
+        Some(1)
+    } else if WRITE_LIKE_WHITELIST
+        .iter()
+        .find(|s| **s == callee_str)
+        .is_some()
+        && args.len() >= 2
+        && is_every_args_simple(args)
+    {
+        Some(2)
+    } else {
+        None
+    }
+}
+
 /// Returns a shape for the last argument which is going to be overflowed.
 fn last_arg_shape<T>(
     lists: &[&T],