about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-07-01 08:32:04 +0000
committerbors <bors@rust-lang.org>2022-07-01 08:32:04 +0000
commitf8c416e1b9b591d9c72b613f4dcbe05432f57ee8 (patch)
tree484d49e033cb1ab608965f812a79745e9bbcd2bc
parent7d0e5aea902b7f8d8fba6d64d9209c1f45a268b4 (diff)
parent36d2b43dfd6b7eb6d5549d04405c8d3323da29b5 (diff)
downloadrust-f8c416e1b9b591d9c72b613f4dcbe05432f57ee8.tar.gz
rust-f8c416e1b9b591d9c72b613f4dcbe05432f57ee8.zip
Auto merge of #12650 - lowr:fix/12591, r=lnicola
fix: improve whitespace insertion in pretty printer

Fixes #12591

The `=>` token in the macro_rules! should be parsed as one fat arrow, but it ["requires a lot of changes in r-a"](https://github.com/rust-analyzer/ungrammar/commit/143cc528b11290bf2463de1fb5a97a15f9b2ed44), so I left it for the larger refactoring in the future and put a FIXME note.
-rw-r--r--crates/ide-db/src/syntax_helpers/insert_whitespace_into_node.rs18
-rw-r--r--crates/ide/src/expand_macro.rs32
2 files changed, 46 insertions, 4 deletions
diff --git a/crates/ide-db/src/syntax_helpers/insert_whitespace_into_node.rs b/crates/ide-db/src/syntax_helpers/insert_whitespace_into_node.rs
index a06f18543fc..f54ae6c9202 100644
--- a/crates/ide-db/src/syntax_helpers/insert_whitespace_into_node.rs
+++ b/crates/ide-db/src/syntax_helpers/insert_whitespace_into_node.rs
@@ -33,7 +33,10 @@ pub fn insert_ws_into(syn: SyntaxNode) -> SyntaxNode {
         let token = match event {
             WalkEvent::Enter(NodeOrToken::Token(token)) => token,
             WalkEvent::Leave(NodeOrToken::Node(node))
-                if matches!(node.kind(), ATTR | MATCH_ARM | STRUCT | ENUM | UNION | FN | IMPL) =>
+                if matches!(
+                    node.kind(),
+                    ATTR | MATCH_ARM | STRUCT | ENUM | UNION | FN | IMPL | MACRO_RULES
+                ) =>
             {
                 if indent > 0 {
                     mods.push((
@@ -66,9 +69,7 @@ pub fn insert_ws_into(syn: SyntaxNode) -> SyntaxNode {
                     mods.push(do_ws(before, tok));
                 }
 
-                if indent > 0 {
-                    mods.push(do_indent(after, tok, indent));
-                }
+                mods.push(do_indent(after, tok, indent));
                 mods.push(do_nl(after, tok));
             }
             R_CURLY if is_last(|it| it != L_CURLY, true) => {
@@ -100,10 +101,19 @@ pub fn insert_ws_into(syn: SyntaxNode) -> SyntaxNode {
                 }
                 mods.push(do_nl(after, tok));
             }
+            T![=] if is_next(|it| it == T![>], false) => {
+                // FIXME: this branch is for `=>` in macro_rules!, which is currently parsed as
+                // two separate symbols.
+                mods.push(do_ws(before, tok));
+                mods.push(do_ws(after, &tok.next_token().unwrap()));
+            }
             T![->] | T![=] | T![=>] => {
                 mods.push(do_ws(before, tok));
                 mods.push(do_ws(after, tok));
             }
+            T![!] if is_last(|it| it == MACRO_RULES_KW, false) && is_next(is_text, false) => {
+                mods.push(do_ws(after, tok));
+            }
             _ => (),
         }
 
diff --git a/crates/ide/src/expand_macro.rs b/crates/ide/src/expand_macro.rs
index d36670e983f..fd0a29f64f6 100644
--- a/crates/ide/src/expand_macro.rs
+++ b/crates/ide/src/expand_macro.rs
@@ -361,6 +361,38 @@ fn main() {
     }
 
     #[test]
+    fn macro_expand_inner_macro_rules() {
+        check(
+            r#"
+macro_rules! foo {
+    ($t:tt) => {{
+        macro_rules! bar {
+            () => {
+                $t
+            }
+        }
+        bar!()
+    }};
+}
+
+fn main() {
+    foo$0!(42);
+}
+            "#,
+            expect![[r#"
+                foo
+                {
+                  macro_rules! bar {
+                    () => {
+                      42
+                    }
+                  }
+                  42
+                }"#]],
+        );
+    }
+
+    #[test]
     fn macro_expand_inner_macro_fail_to_expand() {
         check(
             r#"