Repository: rust-lang/rust-analyzer
Pull request #12098 (2022-04-27 12:41:18)

fix: show `macro_rules` snippet in blocks
fixes https://github.com/rust-lang/rust-analyzer/issues/12092


Patch:
diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs
index 8c2219860b..2c21bbc165 100644
--- a/crates/ide/src/runnables.rs
+++ b/crates/ide/src/runnables.rs
@@ -435,8 +435,7 @@ fn module_def_doctest(db: &RootDatabase, def: Definition) -> Option<Runnable> {
                             ty_args.format_with(", ", |ty, cb| cb(&ty.display(db)))
                         );
                     }
-                    format_to!(path, "::{}", def_name);
-                    return Some(path);
+                    return Some(format!(r#""{}::{}""#, path, def_name));
                 }
             }
         }
@@ -978,7 +977,7 @@ impl Data {
                         },
                         kind: DocTest {
                             test_id: Path(
-                                "Data::foo",
+                                "\"Data::foo\"",
                             ),
                         },
                         cfg: None,
@@ -1372,7 +1371,7 @@ impl Foo {
                         },
                         kind: DocTest {
                             test_id: Path(
-                                "foo::Foo::foo",
+                                "\"foo::Foo::foo\"",
                             ),
                         },
                         cfg: None,
@@ -2078,7 +2077,7 @@ impl<T, U> Foo<T, U> {
                         },
                         kind: DocTest {
                             test_id: Path(
-                                "Foo<T, U>::t",
+                                "\"Foo<T, U>::t\"",
                             ),
                         },
                         cfg: None,
diff --git a/crates/ide_completion/src/completions/snippet.rs b/crates/ide_completion/src/completions/snippet.rs
index 6a0e09dd64..c00de6ff50 100644
--- a/crates/ide_completion/src/completions/snippet.rs
+++ b/crates/ide_completion/src/completions/snippet.rs
@@ -43,7 +43,7 @@ pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionConte
 }
 
 pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
-    if !ctx.expects_item()
+    if !(ctx.expects_item() || ctx.has_block_expr_parent())
         || ctx.previous_token_is(T![unsafe])
         || ctx.path_qual().is_some()
         || ctx.has_impl_or_trait_prev_sibling()
@@ -63,11 +63,13 @@ pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionConte
         add_custom_completions(acc, ctx, cap, SnippetScope::Item);
     }
 
-    let mut item = snippet(
-        ctx,
-        cap,
-        "tmod (Test module)",
-        "\
+    // Test-related snippets shouldn't be shown in blocks.
+    if !ctx.has_block_expr_parent() {
+        let mut item = snippet(
+            ctx,
+            cap,
+            "tmod (Test module)",
+            "\
 #[cfg(test)]
 mod tests {
     use super::*;
@@ -77,22 +79,23 @@ mod tests {
         $0
     }
 }",
-    );
-    item.lookup_by("tmod");
-    item.add_to(acc);
-
-    let mut item = snippet(
-        ctx,
-        cap,
-        "tfn (Test function)",
-        "\
+        );
+        item.lookup_by("tmod");
+        item.add_to(acc);
+
+        let mut item = snippet(
+            ctx,
+            cap,
+            "tfn (Test function)",
+            "\
 #[test]
 fn ${1:feature}() {
     $0
 }",
-    );
-    item.lookup_by("tfn");
-    item.add_to(acc);
+        );
+        item.lookup_by("tfn");
+        item.add_to(acc);
+    }
 
     let item = snippet(
         ctx,


Test patch:
diff --git a/crates/ide_completion/src/tests/expression.rs b/crates/ide_completion/src/tests/expression.rs
index 650c68134d..5984149046 100644
--- a/crates/ide_completion/src/tests/expression.rs
+++ b/crates/ide_completion/src/tests/expression.rs
@@ -153,6 +153,7 @@ impl Unit {
             kw return
             sn pd
             sn ppd
+            sn macro_rules
             kw self
             kw super
             kw crate
@@ -246,10 +247,11 @@ fn complete_in_block() {
             kw return
             sn pd
             sn ppd
+            sn macro_rules
             kw self
             kw super
             kw crate
-            fn foo()     fn()
+            fn foo()       fn()
             bt u32
         "#]],
     )
@@ -293,10 +295,11 @@ fn complete_after_if_expr() {
             kw return
             sn pd
             sn ppd
+            sn macro_rules
             kw self
             kw super
             kw crate
-            fn foo()     fn()
+            fn foo()       fn()
             bt u32
         "#]],
     )
@@ -366,10 +369,11 @@ fn completes_in_loop_ctx() {
             kw return
             sn pd
             sn ppd
+            sn macro_rules
             kw self
             kw super
             kw crate
-            fn my()      fn()
+            fn my()        fn()
             bt u32
         "#]],
     );