]> git.lizzy.rs Git - rust.git/commitdiff
Direct format vec! instead of using rewrite_pair
authortopecongiro <seuchida@gmail.com>
Wed, 14 Jun 2017 11:33:54 +0000 (20:33 +0900)
committertopecongiro <seuchida@gmail.com>
Wed, 14 Jun 2017 11:33:54 +0000 (20:33 +0900)
src/macros.rs

index 562e776f8d3bf93f743e79e803a7dfb3bae8d293..cfc14b53adb13e7acfa049df7bbb25ad4d52b44c 100644 (file)
@@ -30,7 +30,7 @@
 use Shape;
 use codemap::SpanUtils;
 use rewrite::{Rewrite, RewriteContext};
-use expr::{rewrite_call, rewrite_array, rewrite_pair};
+use expr::{rewrite_call, rewrite_array};
 use comment::{FindUncommented, contains_comment};
 use utils::mk_sp;
 
@@ -192,15 +192,28 @@ pub fn rewrite_macro(
                 } else {
                     ("[", "]")
                 };
-                rewrite_pair(
-                    &*expr_vec[0],
-                    &*expr_vec[1],
-                    lbr,
-                    "; ",
-                    rbr,
-                    context,
-                    mac_shape,
-                ).map(|s| format!("{}{}", macro_name, s))
+                // 6 = `vec!` + `; `
+                let total_overhead = lbr.len() + rbr.len() + 6;
+                let lhs = try_opt!(expr_vec[0].rewrite(context, mac_shape));
+                let rhs = try_opt!(expr_vec[1].rewrite(context, mac_shape));
+                if !lhs.contains('\n') && !rhs.contains('\n') &&
+                    lhs.len() + rhs.len() + total_overhead <= shape.width
+                {
+                    Some(format!("{}{}{}; {}{}", macro_name, lbr, lhs, rhs, rbr))
+                } else {
+                    let nested_indent = shape.indent.block_indent(context.config);
+                    Some(format!(
+                        "{}{}\n{}{};\n{}{}\n{}{}",
+                        macro_name,
+                        lbr,
+                        nested_indent.to_string(context.config),
+                        lhs,
+                        nested_indent.to_string(context.config),
+                        rhs,
+                        shape.indent.to_string(context.config),
+                        rbr
+                    ))
+                }
             } else {
                 // Format macro invocation as array literal.
                 let rewrite = try_opt!(rewrite_array(