]> git.lizzy.rs Git - rust.git/commitdiff
Disable formatting macro-def if args do not fit on one line
authortopecongiro <seuchida@gmail.com>
Tue, 13 Feb 2018 19:00:26 +0000 (04:00 +0900)
committertopecongiro <seuchida@gmail.com>
Tue, 13 Feb 2018 19:00:26 +0000 (04:00 +0900)
rustfmt-core/src/macros.rs
rustfmt-core/tests/source/macro_rules.rs
rustfmt-core/tests/target/macro_rules.rs

index 535a82c81de471fabf450e65219b8fad0a32406c..a8a8c28c585b28f3b28fbce04a2b939a053f3c6c 100644 (file)
@@ -431,7 +431,8 @@ fn replace_names(input: &str) -> Option<(String, HashMap<String, String>)> {
 // and `(`/`)` have special meaning.
 //
 // We always try and format on one line.
-fn format_macro_args(toks: ThinTokenStream) -> Option<String> {
+// FIXME: Use multi-line when every thing does not fit on one line.
+fn format_macro_args(toks: ThinTokenStream, shape: Shape) -> Option<String> {
     let mut result = String::with_capacity(128);
     let mut insert_space = SpaceState::Never;
 
@@ -459,7 +460,7 @@ fn format_macro_args(toks: ThinTokenStream) -> Option<String> {
                 if let SpaceState::Always = insert_space {
                     result.push(' ');
                 }
-                let formatted = format_macro_args(d.tts)?;
+                let formatted = format_macro_args(d.tts, shape)?;
                 match d.delim {
                     DelimToken::Paren => {
                         result.push_str(&format!("({})", formatted));
@@ -482,7 +483,11 @@ fn format_macro_args(toks: ThinTokenStream) -> Option<String> {
         }
     }
 
-    Some(result)
+    if result.len() <= shape.width {
+        Some(result)
+    } else {
+        None
+    }
 }
 
 // We should insert a space if the next token is a:
@@ -617,7 +622,7 @@ fn macro_style(mac: &ast::Mac, context: &RewriteContext) -> MacroStyle {
 ///         a,
 ///         b,
 ///         c,
-//      ),
+///     ),
 /// }
 /// ```
 fn indent_macro_snippet(
@@ -757,7 +762,8 @@ fn rewrite(
             return None;
         }
 
-        let mut result = format_macro_args(self.args.clone())?;
+        // 5 = " => {"
+        let mut result = format_macro_args(self.args.clone(), shape.sub_width(5)?)?;
 
         if multi_branch_style {
             result += " =>";
@@ -847,7 +853,12 @@ fn format_macro_args_str(s: &str) -> String {
             &ParseSess::new(FilePathMapping::empty()),
             None,
         );
-        format_macro_args(input.into()).unwrap()
+        let shape = Shape {
+            width: 100,
+            indent: Indent::empty(),
+            offset: 0,
+        };
+        format_macro_args(input.into(), shape).unwrap()
     }
 
     #[test]
index 7d14f44971d5827f8d1f5f5ebcec2cfc9d23e802..ed5f7e16c04755e5c982a76cb0e6f00c785410d1 100644 (file)
@@ -49,3 +49,13 @@ macro_rules! m {
        mod macro_item    {  struct $item ; }
 }
 }
+
+// #2439
+macro_rules! m {
+    (
+        $line0_xxxxxxxxxxxxxxxxx: expr,
+        $line1_xxxxxxxxxxxxxxxxx: expr,
+        $line2_xxxxxxxxxxxxxxxxx: expr,
+        $line3_xxxxxxxxxxxxxxxxx: expr,
+    ) => {};
+}
index 647d442034fae98c2b6f76ef727d7fcb265d33c3..ef4a59e38cd4a40419de909477f1d31cbff45670 100644 (file)
@@ -41,3 +41,13 @@ mod macro_item {
         }
     }
 }
+
+// #2439
+macro_rules! m {
+    (
+        $line0_xxxxxxxxxxxxxxxxx: expr,
+        $line1_xxxxxxxxxxxxxxxxx: expr,
+        $line2_xxxxxxxxxxxxxxxxx: expr,
+        $line3_xxxxxxxxxxxxxxxxx: expr,
+    ) => {};
+}