]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/completions/macro_in_item_position.rs
202e71215a460f6d0570adf3c81616f3923eb307
[rust.git] / crates / ide_completion / src / completions / macro_in_item_position.rs
1 //! Completes macro invocations used in item position.
2
3 use crate::{CompletionContext, Completions};
4
5 // Ideally this should be removed and moved into `(un)qualified_path` respectively
6 pub(crate) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) {
7     // Show only macros in top level.
8     if !ctx.is_new_item {
9         return;
10     }
11
12     ctx.scope.process_all_names(&mut |name, res| {
13         if let hir::ScopeDef::MacroDef(mac) = res {
14             acc.add_macro(ctx, Some(name.clone()), mac);
15         }
16         // FIXME: This should be done in qualified_path/unqualified_path instead?
17         if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res {
18             acc.add_resolution(ctx, name, &res);
19         }
20     })
21 }
22
23 #[cfg(test)]
24 mod tests {
25     use expect_test::{expect, Expect};
26
27     use crate::{test_utils::completion_list, CompletionKind};
28
29     fn check(ra_fixture: &str, expect: Expect) {
30         let actual = completion_list(ra_fixture, CompletionKind::Reference);
31         expect.assert_eq(&actual)
32     }
33
34     #[test]
35     fn completes_macros_as_item() {
36         check(
37             r#"
38 macro_rules! foo { () => {} }
39 fn foo() {}
40
41 $0
42 "#,
43             expect![[r#"
44                 ma foo!(…) macro_rules! foo
45             "#]],
46         )
47     }
48 }