aboutsummaryrefslogtreecommitdiff
path: root/crates/completion/src/completions/pattern.rs
blob: eee31098d13cb57d4f269c0a9209d8ce1b28d17b (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//! Completes constats and paths in patterns.

use crate::{CompletionContext, Completions};

/// Completes constants and paths in patterns.
pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
    if !(ctx.is_pat_binding_or_const || ctx.is_irrefutable_pat_binding) {
        return;
    }
    if ctx.record_pat_syntax.is_some() {
        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 add_resolution = match &res {
            hir::ScopeDef::ModuleDef(def) => match def {
                hir::ModuleDef::Adt(hir::Adt::Struct(strukt)) => {
                    acc.add_struct_pat(ctx, strukt.clone(), Some(name.clone()));
                    true
                }
                hir::ModuleDef::Variant(variant) if !ctx.is_irrefutable_pat_binding => {
                    acc.add_variant_pat(ctx, variant.clone(), Some(name.clone()));
                    true
                }
                hir::ModuleDef::Adt(hir::Adt::Enum(..))
                | hir::ModuleDef::Variant(..)
                | hir::ModuleDef::Const(..)
                | hir::ModuleDef::Module(..) => !ctx.is_irrefutable_pat_binding,
                _ => false,
            },
            hir::ScopeDef::MacroDef(_) => true,
            _ => false,
        };
        if add_resolution {
            acc.add_resolution(ctx, name.to_string(), &res);
        }
    });
}

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

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

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

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

    #[test]
    fn completes_enum_variants_and_modules() {
        check(
            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 { <|> }
}
"#,
            expect![[r#"
                en E
                ct Z
                st Bar
                ev X
                md m
            "#]],
        );
    }

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

fn foo() {
   m!(match E::X { <|> })
}
"#,
            expect![[r#"
                en E
                ma m!(…) macro_rules! m
            "#]],
        );
    }

    #[test]
    fn completes_in_irrefutable_let() {
        check(
            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() {
   let <|>
}
"#,
            expect![[r#"
                st Bar
            "#]],
        );
    }

    #[test]
    fn completes_in_param() {
        check(
            r#"
enum E { X }

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

fn foo(<|>) {
}
"#,
            expect![[r#"
                st Bar
            "#]],
        );
    }

    #[test]
    fn completes_pat_in_let() {
        check_snippet(
            r#"
struct Bar { f: u32 }

fn foo() {
   let <|>
}
"#,
            expect![[r#"
                bn Bar Bar { f$1 }$0
            "#]],
        );
    }

    #[test]
    fn completes_param_pattern() {
        check_snippet(
            r#"
struct Foo { bar: String, baz: String }
struct Bar(String, String);
struct Baz;
fn outer(<|>) {}
"#,
            expect![[r#"
                bn Foo Foo { bar$1, baz$2 }: Foo$0
                bn Bar Bar($1, $2): Bar$0
            "#]],
        )
    }

    #[test]
    fn completes_let_pattern() {
        check_snippet(
            r#"
struct Foo { bar: String, baz: String }
struct Bar(String, String);
struct Baz;
fn outer() {
    let <|>
}
"#,
            expect![[r#"
                bn Foo Foo { bar$1, baz$2 }$0
                bn Bar Bar($1, $2)$0
            "#]],
        )
    }

    #[test]
    fn completes_refutable_pattern() {
        check_snippet(
            r#"
struct Foo { bar: i32, baz: i32 }
struct Bar(String, String);
struct Baz;
fn outer() {
    match () {
        <|>
    }
}
"#,
            expect![[r#"
                bn Foo Foo { bar$1, baz$2 }$0
                bn Bar Bar($1, $2)$0
            "#]],
        )
    }

    #[test]
    fn omits_private_fields_pat() {
        check_snippet(
            r#"
mod foo {
    pub struct Foo { pub bar: i32, baz: i32 }
    pub struct Bar(pub String, String);
    pub struct Invisible(String, String);
}
use foo::*;

fn outer() {
    match () {
        <|>
    }
}
"#,
            expect![[r#"
                bn Foo Foo { bar$1, .. }$0
                bn Bar Bar($1, ..)$0
            "#]],
        )
    }

    #[test]
    fn only_shows_ident_completion() {
        check_edit(
            "Foo",
            r#"
struct Foo(i32);
fn main() {
    match Foo(92) {
        <|>(92) => (),
    }
}
"#,
            r#"
struct Foo(i32);
fn main() {
    match Foo(92) {
        Foo(92) => (),
    }
}
"#,
        );
    }
}