Skip to content

Commit f8c416e

Browse files
committed
Auto merge of rust-lang#12650 - lowr:fix/12591, r=lnicola
fix: improve whitespace insertion in pretty printer Fixes rust-lang#12591 The `=>` token in the macro_rules! should be parsed as one fat arrow, but it ["requires a lot of changes in r-a"](rust-analyzer/ungrammar@143cc52), so I left it for the larger refactoring in the future and put a FIXME note.
2 parents 7d0e5ae + 36d2b43 commit f8c416e

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

crates/ide-db/src/syntax_helpers/insert_whitespace_into_node.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ pub fn insert_ws_into(syn: SyntaxNode) -> SyntaxNode {
3333
let token = match event {
3434
WalkEvent::Enter(NodeOrToken::Token(token)) => token,
3535
WalkEvent::Leave(NodeOrToken::Node(node))
36-
if matches!(node.kind(), ATTR | MATCH_ARM | STRUCT | ENUM | UNION | FN | IMPL) =>
36+
if matches!(
37+
node.kind(),
38+
ATTR | MATCH_ARM | STRUCT | ENUM | UNION | FN | IMPL | MACRO_RULES
39+
) =>
3740
{
3841
if indent > 0 {
3942
mods.push((
@@ -66,9 +69,7 @@ pub fn insert_ws_into(syn: SyntaxNode) -> SyntaxNode {
6669
mods.push(do_ws(before, tok));
6770
}
6871

69-
if indent > 0 {
70-
mods.push(do_indent(after, tok, indent));
71-
}
72+
mods.push(do_indent(after, tok, indent));
7273
mods.push(do_nl(after, tok));
7374
}
7475
R_CURLY if is_last(|it| it != L_CURLY, true) => {
@@ -100,10 +101,19 @@ pub fn insert_ws_into(syn: SyntaxNode) -> SyntaxNode {
100101
}
101102
mods.push(do_nl(after, tok));
102103
}
104+
T![=] if is_next(|it| it == T![>], false) => {
105+
// FIXME: this branch is for `=>` in macro_rules!, which is currently parsed as
106+
// two separate symbols.
107+
mods.push(do_ws(before, tok));
108+
mods.push(do_ws(after, &tok.next_token().unwrap()));
109+
}
103110
T![->] | T![=] | T![=>] => {
104111
mods.push(do_ws(before, tok));
105112
mods.push(do_ws(after, tok));
106113
}
114+
T![!] if is_last(|it| it == MACRO_RULES_KW, false) && is_next(is_text, false) => {
115+
mods.push(do_ws(after, tok));
116+
}
107117
_ => (),
108118
}
109119

crates/ide/src/expand_macro.rs

+32
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,38 @@ fn main() {
360360
);
361361
}
362362

363+
#[test]
364+
fn macro_expand_inner_macro_rules() {
365+
check(
366+
r#"
367+
macro_rules! foo {
368+
($t:tt) => {{
369+
macro_rules! bar {
370+
() => {
371+
$t
372+
}
373+
}
374+
bar!()
375+
}};
376+
}
377+
378+
fn main() {
379+
foo$0!(42);
380+
}
381+
"#,
382+
expect![[r#"
383+
foo
384+
{
385+
macro_rules! bar {
386+
() => {
387+
42
388+
}
389+
}
390+
42
391+
}"#]],
392+
);
393+
}
394+
363395
#[test]
364396
fn macro_expand_inner_macro_fail_to_expand() {
365397
check(

0 commit comments

Comments
 (0)