aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion/src/completions/macro_in_item_position.rs
blob: 781b96ff185a17aa35714a02bcdab8b32d0ab0d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//! Completes macro invocations used in item position.

use crate::{CompletionContext, Completions};

// Ideally this should be removed and moved into `(un)qualified_path` respectively
pub(crate) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) {
    // Show only macros in top level.
    if !ctx.expects_item() {
        return;
    }

    ctx.scope.process_all_names(&mut |name, res| {
        if let hir::ScopeDef::MacroDef(mac) = res {
            acc.add_macro(ctx, Some(name.clone()), mac);
        }
        // FIXME: This should be done in qualified_path/unqualified_path instead?
        if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res {
            acc.add_resolution(ctx, name, &res);
        }
    })
}

#[cfg(test)]
mod tests {
    use expect_test::{expect, Expect};

    use crate::{test_utils::completion_list, CompletionKind};

    fn check(ra_fixture: &str, expect: Expect) {
        let actual = completion_list(ra_fixture, CompletionKind::Reference);
        expect.assert_eq(&actual)
    }

    #[test]
    fn completes_macros_as_item() {
        check(
            r#"
macro_rules! foo { () => {} }
fn foo() {}

$0
"#,
            expect![[r#"
                ma foo!(…) macro_rules! foo
            "#]],
        )
    }
}