aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/completion/complete_pattern.rs
blob: cb84bb93489d125a5540a635c9c940c8351d851e (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! FIXME: write short doc here

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

/// Completes constats and paths in patterns.
pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
    if !ctx.is_pat_binding {
        return;
    }
    // FIXME: ideally, we should look at the type we are matching against and
    // suggest variants + auto-imports
    ctx.scope().process_all_names(&mut |name, res| {
        let def = match &res {
            hir::ScopeDef::ModuleDef(def) => def,
            _ => return,
        };
        match def {
            hir::ModuleDef::Adt(hir::Adt::Enum(..))
            | hir::ModuleDef::EnumVariant(..)
            | hir::ModuleDef::Const(..)
            | hir::ModuleDef::Module(..) => (),
            _ => return,
        }
        acc.add_resolution(ctx, name.to_string(), &res)
    });
}

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

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

    #[test]
    fn completes_enum_variants_and_modules() {
        let completions = complete(
            r"
            enum E { X }
            use self::E::X;
            const Z: E = E::X;
            mod m {}

            static FOO: E = E::X;
            struct Bar { f: u32 }

            fn foo() {
               match E::X {
                   <|>
               }
            }
            ",
        );
        assert_debug_snapshot!(completions, @r###"
        [
            CompletionItem {
                label: "E",
                source_range: [246; 246),
                delete: [246; 246),
                insert: "E",
                kind: Enum,
            },
            CompletionItem {
                label: "X",
                source_range: [246; 246),
                delete: [246; 246),
                insert: "X",
                kind: EnumVariant,
            },
            CompletionItem {
                label: "Z",
                source_range: [246; 246),
                delete: [246; 246),
                insert: "Z",
                kind: Const,
            },
            CompletionItem {
                label: "m",
                source_range: [246; 246),
                delete: [246; 246),
                insert: "m",
                kind: Module,
            },
        ]
        "###);
    }

    #[test]
    fn completes_in_simple_macro_call() {
        let completions = complete(
            r"
            macro_rules! m { ($e:expr) => { $e } }
            enum E { X }

            fn foo() {
               m!(match E::X {
                   <|>
               })
            }
            ",
        );
        assert_debug_snapshot!(completions, @r###"
        [
            CompletionItem {
                label: "E",
                source_range: [151; 151),
                delete: [151; 151),
                insert: "E",
                kind: Enum,
            },
        ]
        "###);
    }
}