aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs
blob: d808b23571c29c1cb8944dab9d571d0f8e7b13ba (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! FIXME: write short doc here

use crate::completion::{CompletionContext, Completions};

pub(super) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) {
    // Show only macros in top level.
    if ctx.is_new_item {
        ctx.analyzer.process_all_names(ctx.db, &mut |name, res| {
            if let hir::ScopeDef::MacroDef(mac) = res {
                acc.add_macro(ctx, Some(name.to_string()), mac);
            }
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::completion::{do_completion, CompletionItem, CompletionKind};
    use insta::assert_debug_snapshot;

    fn do_reference_completion(code: &str) -> Vec<CompletionItem> {
        do_completion(code, CompletionKind::Reference)
    }

    #[test]
    fn completes_macros_as_item() {
        assert_debug_snapshot!(
            do_reference_completion(
                "
                //- /main.rs
                macro_rules! foo {
                    () => {}
                }

                fn foo() {}

                <|>
                "
            ),
            @r##"[
    CompletionItem {
        label: "foo!",
        source_range: [46; 46),
        delete: [46; 46),
        insert: "foo!($0)",
        kind: Macro,
        detail: "macro_rules! foo",
    },
]"##
        );
    }

    #[test]
    fn completes_vec_macros_with_square_brackets() {
        assert_debug_snapshot!(
            do_reference_completion(
                "
                //- /main.rs
                macro_rules! vec {
                    () => {}
                }

                fn foo() {}

                <|>
                "
            ),
            @r##"[
    CompletionItem {
        label: "vec!",
        source_range: [46; 46),
        delete: [46; 46),
        insert: "vec![$0]",
        kind: Macro,
        detail: "macro_rules! vec",
    },
]"##
        );
    }
}