aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs
blob: 708dc97771e8eddcc2f5645d9eff15063018c2a7 (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
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 {
        for (name, res) in ctx.analyzer.all_names(ctx.db) {
            if res.get_macros().is_some() {
                acc.add_resolution(ctx, name.to_string(), &res.only_macros());
            }
        }
    }
}

#[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!",
        kind: Macro,
        detail: "macro_rules! foo",
    },
]"##
        );
    }
}