Skip to content

fix: improve whitespace insertion in pretty printer #12650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions crates/ide-db/src/syntax_helpers/insert_whitespace_into_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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((
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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));
}
_ => (),
}

Expand Down
32 changes: 32 additions & 0 deletions crates/ide/src/expand_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
fn macro_expand_inner_macro_fail_to_expand() {
check(
Expand Down