Unnamed repository; edit this file 'description' to name the repository.
Auto merge of #12370 - andylizi:macro-expand-underscore, r=lnicola
ide: insert whitespaces surrounding `_` in macro expansion #### Before ```rust for_in 0..10 { foo(); } ``` #### After ```rust for _ in 0..10 { foo(); } ```
bors 2022-05-24
parent cc8140a · parent e34ae76 · commit d5965aa
-rw-r--r--crates/ide-db/src/syntax_helpers/insert_whitespace_into_node.rs4
-rw-r--r--crates/ide/src/expand_macro.rs20
2 files changed, 21 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 16e609b1a7..b5db8c1967 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 e4061016e6..a578aba01f 100644
--- a/crates/ide/src/expand_macro.rs
+++ b/crates/ide/src/expand_macro.rs
@@ -238,6 +238,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(
r#"
@@ -385,7 +403,7 @@ fn main() {
"#,
expect![[r#"
foo
- 0 "#]],
+ 0"#]],
);
}