fix: improve whitespace insertion in pretty printer

This commit is contained in:
Ryo Yoshida 2022-06-28 20:40:22 +09:00
parent 9eaf96c9ea
commit 36d2b43dfd
No known key found for this signature in database
GPG key ID: E25698A930586171
2 changed files with 46 additions and 4 deletions

View file

@ -33,7 +33,10 @@ pub fn insert_ws_into(syn: SyntaxNode) -> SyntaxNode {
let token = match event { let token = match event {
WalkEvent::Enter(NodeOrToken::Token(token)) => token, WalkEvent::Enter(NodeOrToken::Token(token)) => token,
WalkEvent::Leave(NodeOrToken::Node(node)) 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 { if indent > 0 {
mods.push(( mods.push((
@ -66,9 +69,7 @@ pub fn insert_ws_into(syn: SyntaxNode) -> SyntaxNode {
mods.push(do_ws(before, tok)); 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)); mods.push(do_nl(after, tok));
} }
R_CURLY if is_last(|it| it != L_CURLY, true) => { 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)); 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![=>] => { T![->] | T![=] | T![=>] => {
mods.push(do_ws(before, tok)); mods.push(do_ws(before, tok));
mods.push(do_ws(after, 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));
}
_ => (), _ => (),
} }

View file

@ -360,6 +360,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] #[test]
fn macro_expand_inner_macro_fail_to_expand() { fn macro_expand_inner_macro_fail_to_expand() {
check( check(