From ebf8b1a96ed5cc4eecc91d82806d74c3e206ab9c Mon Sep 17 00:00:00 2001 From: andylizi Date: Tue, 24 May 2022 07:35:47 +0800 Subject: [PATCH 1/2] ide: insert whitespaces surrounding `_` in macro expansion --- .../ide-db/src/syntax_helpers/insert_whitespace_into_node.rs | 4 ++-- crates/ide/src/expand_macro.rs | 2 +- 2 files changed, 3 insertions(+), 3 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 16e609b1a7e..b5db8c1967b 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 @@ -57,7 +57,7 @@ pub fn insert_ws_into(syn: SyntaxNode) -> SyntaxNode { |f: fn(SyntaxKind) -> bool, default| -> bool { last.map(f).unwrap_or(default) }; match tok.kind() { - k if is_text(k) && is_next(|it| !it.is_punct(), true) => { + k if is_text(k) && is_next(|it| !it.is_punct() || it == UNDERSCORE, false) => { mods.push(do_ws(after, tok)); } L_CURLY if is_next(|it| it != R_CURLY, true) => { @@ -118,5 +118,5 @@ pub fn insert_ws_into(syn: SyntaxNode) -> SyntaxNode { } fn is_text(k: SyntaxKind) -> bool { - k.is_keyword() || k.is_literal() || k == IDENT + k.is_keyword() || k.is_literal() || k == IDENT || k == UNDERSCORE } diff --git a/crates/ide/src/expand_macro.rs b/crates/ide/src/expand_macro.rs index e4061016e60..290f797c5e8 100644 --- a/crates/ide/src/expand_macro.rs +++ b/crates/ide/src/expand_macro.rs @@ -385,7 +385,7 @@ fn main() { "#, expect![[r#" foo - 0 "#]], + 0"#]], ); } From e34ae760f0db5de18782d59b43165895dc6cc992 Mon Sep 17 00:00:00 2001 From: andylizi Date: Tue, 24 May 2022 12:45:58 +0800 Subject: [PATCH 2/2] add test for macro expand formatting --- crates/ide/src/expand_macro.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/ide/src/expand_macro.rs b/crates/ide/src/expand_macro.rs index 290f797c5e8..a578aba01f3 100644 --- a/crates/ide/src/expand_macro.rs +++ b/crates/ide/src/expand_macro.rs @@ -237,6 +237,24 @@ fn main() { ); } + #[test] + fn macro_expand_underscore() { + check( + r#" +macro_rules! bar { + ($i:tt) => { for _ in 0..$i {} } +} +fn main() { + ba$0r!(42); +} +"#, + expect![[r#" + bar + for _ in 0..42{} + "#]], + ); + } + #[test] fn macro_expand_recursive_expansion() { check(