]> git.lizzy.rs Git - rust.git/commitdiff
Ignore empty lines inside arguments of macro with brace
authortopecongiro <seuchida@gmail.com>
Fri, 25 Aug 2017 13:35:22 +0000 (22:35 +0900)
committertopecongiro <seuchida@gmail.com>
Fri, 25 Aug 2017 13:35:22 +0000 (22:35 +0900)
src/macros.rs
tests/source/macros.rs
tests/target/macros.rs

index b1f740e6a87ee1d7af5effa85949a8377bb2de21..a841268ce07351365ae445cd6077145975437591 100644 (file)
@@ -309,31 +309,51 @@ fn macro_style(mac: &ast::Mac, context: &RewriteContext) -> MacroStyle {
 /// }
 /// ```
 fn indent_macro_snippet(macro_str: &str, indent: Indent) -> Option<String> {
-    let min_prefix_space_width =
-        try_opt!(macro_str.lines().skip(1).map(get_prefix_space_width).min());
-
     let mut lines = macro_str.lines();
-    let first_line = try_opt!(lines.next());
+    let first_line = try_opt!(lines.next().map(|s| s.trim_right()));
+    let mut trimmed_lines = Vec::with_capacity(16);
+
+    let min_prefix_space_width = try_opt!(
+        lines
+            .filter_map(|line| {
+                let prefix_space_width = if is_empty_line(line) {
+                    None
+                } else {
+                    get_prefix_space_width(line)
+                };
+                trimmed_lines.push((line.trim(), prefix_space_width));
+                prefix_space_width
+            })
+            .min()
+    );
 
     Some(
         String::from(first_line) + "\n" +
-            &lines
-                .map(|line| {
-                    let new_indent_width = indent.width() +
-                        get_prefix_space_width(line)
-                            .checked_sub(min_prefix_space_width)
-                            .unwrap_or(0);
-                    repeat_white_space(new_indent_width) + line.trim()
+            &trimmed_lines
+                .iter()
+                .map(|&(line, prefix_space_width)| match prefix_space_width {
+                    Some(original_indent_width) => {
+                        let new_indent_width = indent.width() +
+                            original_indent_width
+                                .checked_sub(min_prefix_space_width)
+                                .unwrap_or(0);
+                        repeat_white_space(new_indent_width) + line.trim()
+                    }
+                    None => String::new(),
                 })
                 .collect::<Vec<_>>()
                 .join("\n"),
     )
 }
 
-fn get_prefix_space_width(s: &str) -> usize {
-    s.chars().position(|c| c != ' ').unwrap_or(0)
+fn get_prefix_space_width(s: &str) -> Option<usize> {
+    s.chars().position(|c| c != ' ')
 }
 
 fn repeat_white_space(ws_count: usize) -> String {
     repeat(" ").take(ws_count).collect::<String>()
 }
+
+fn is_empty_line(s: &str) -> bool {
+    s.is_empty() || s.chars().all(char::is_whitespace)
+}
index d8e6efe9199c1bfd60fa6116abd2ae9cba19c36d..aa2861ecd173fe3ab7e0e006c8d035781ea75fb2 100644 (file)
@@ -134,6 +134,21 @@ fn issue_1885() {
     }).collect::<Vec<_>>();
 }
 
+fn issue_1917() {
+    mod x {
+        quickcheck! {
+            fn test(a: String, s: String, b: String) -> TestResult {
+                if a.find(&s).is_none() {
+
+                    TestResult::from_bool(true)
+                } else {
+                    TestResult::discard()
+                }
+            }
+        }
+    }
+}
+
 // Put the following tests with macro invocations whose arguments cannot be parsed as expressioins
 // at the end of the file for now.
 
index 21c0b983ff93cbb40a7d82407e23c5e20ad651e5..cbe15f9f7f7b4a576d7b7f63fcff73bb5fe96bf6 100644 (file)
@@ -178,6 +178,21 @@ fn issue_1885() {
         .collect::<Vec<_>>();
 }
 
+fn issue_1917() {
+    mod x {
+        quickcheck! {
+            fn test(a: String, s: String, b: String) -> TestResult {
+                if a.find(&s).is_none() {
+
+                    TestResult::from_bool(true)
+                } else {
+                    TestResult::discard()
+                }
+            }
+        }
+    }
+}
+
 // Put the following tests with macro invocations whose arguments cannot be parsed as expressioins
 // at the end of the file for now.