]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/completions/macro_in_item_position.rs
simplify
[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 pub(crate) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) {
6     // Show only macros in top level.
7     if !ctx.is_new_item {
8         return;
9     }
10
11     ctx.scope.process_all_names(&mut |name, res| {
12         if let hir::ScopeDef::MacroDef(mac) = res {
13             acc.add_macro(ctx, Some(name.to_string()), mac);
14         }
15     })
16 }
17
18 #[cfg(test)]
19 mod tests {
20     use expect_test::{expect, Expect};
21
22     use crate::{test_utils::completion_list, CompletionKind};
23
24     fn check(ra_fixture: &str, expect: Expect) {
25         let actual = completion_list(ra_fixture, CompletionKind::Reference);
26         expect.assert_eq(&actual)
27     }
28
29     #[test]
30     fn completes_macros_as_item() {
31         check(
32             r#"
33 macro_rules! foo { () => {} }
34 fn foo() {}
35
36 $0
37 "#,
38             expect![[r#"
39                 ma foo!(…) macro_rules! foo
40             "#]],
41         )
42     }
43 }