aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion/src/completions/pattern.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_completion/src/completions/pattern.rs')
-rw-r--r--crates/ide_completion/src/completions/pattern.rs116
1 files changed, 101 insertions, 15 deletions
diff --git a/crates/ide_completion/src/completions/pattern.rs b/crates/ide_completion/src/completions/pattern.rs
index 9282c3827..46cef58f0 100644
--- a/crates/ide_completion/src/completions/pattern.rs
+++ b/crates/ide_completion/src/completions/pattern.rs
@@ -11,10 +11,13 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
11 return; 11 return;
12 } 12 }
13 13
14 if let Some(ty) = &ctx.expected_type { 14 if !ctx.is_irrefutable_pat_binding {
15 super::complete_enum_variants(acc, ctx, ty, |acc, ctx, variant, path| { 15 if let Some(ty) = ctx.expected_type.as_ref() {
16 acc.add_qualified_variant_pat(ctx, variant, path) 16 super::complete_enum_variants(acc, ctx, ty, |acc, ctx, variant, path| {
17 }); 17 acc.add_qualified_variant_pat(ctx, variant, path.clone());
18 acc.add_qualified_enum_variant(ctx, variant, path);
19 });
20 }
18 } 21 }
19 22
20 // FIXME: ideally, we should look at the type we are matching against and 23 // FIXME: ideally, we should look at the type we are matching against and
@@ -85,7 +88,7 @@ static FOO: E = E::X;
85struct Bar { f: u32 } 88struct Bar { f: u32 }
86 89
87fn foo() { 90fn foo() {
88 match E::X { $0 } 91 match E::X { a$0 }
89} 92}
90"#, 93"#,
91 expect![[r#" 94 expect![[r#"
@@ -106,10 +109,11 @@ macro_rules! m { ($e:expr) => { $e } }
106enum E { X } 109enum E { X }
107 110
108fn foo() { 111fn foo() {
109 m!(match E::X { $0 }) 112 m!(match E::X { a$0 })
110} 113}
111"#, 114"#,
112 expect![[r#" 115 expect![[r#"
116 ev E::X ()
113 en E 117 en E
114 ma m!(…) macro_rules! m 118 ma m!(…) macro_rules! m
115 "#]], 119 "#]],
@@ -129,7 +133,7 @@ static FOO: E = E::X;
129struct Bar { f: u32 } 133struct Bar { f: u32 }
130 134
131fn foo() { 135fn foo() {
132 let $0 136 let a$0
133} 137}
134"#, 138"#,
135 expect![[r#" 139 expect![[r#"
@@ -147,7 +151,7 @@ enum E { X }
147static FOO: E = E::X; 151static FOO: E = E::X;
148struct Bar { f: u32 } 152struct Bar { f: u32 }
149 153
150fn foo($0) { 154fn foo(a$0) {
151} 155}
152"#, 156"#,
153 expect![[r#" 157 expect![[r#"
@@ -163,7 +167,7 @@ fn foo($0) {
163struct Bar { f: u32 } 167struct Bar { f: u32 }
164 168
165fn foo() { 169fn foo() {
166 let $0 170 let a$0
167} 171}
168"#, 172"#,
169 expect![[r#" 173 expect![[r#"
@@ -179,7 +183,7 @@ fn foo() {
179struct Foo { bar: String, baz: String } 183struct Foo { bar: String, baz: String }
180struct Bar(String, String); 184struct Bar(String, String);
181struct Baz; 185struct Baz;
182fn outer($0) {} 186fn outer(a$0) {}
183"#, 187"#,
184 expect![[r#" 188 expect![[r#"
185 bn Foo Foo { bar$1, baz$2 }: Foo$0 189 bn Foo Foo { bar$1, baz$2 }: Foo$0
@@ -196,7 +200,7 @@ struct Foo { bar: String, baz: String }
196struct Bar(String, String); 200struct Bar(String, String);
197struct Baz; 201struct Baz;
198fn outer() { 202fn outer() {
199 let $0 203 let a$0
200} 204}
201"#, 205"#,
202 expect![[r#" 206 expect![[r#"
@@ -215,7 +219,7 @@ struct Bar(String, String);
215struct Baz; 219struct Baz;
216fn outer() { 220fn outer() {
217 match () { 221 match () {
218 $0 222 a$0
219 } 223 }
220} 224}
221"#, 225"#,
@@ -239,7 +243,7 @@ use foo::*;
239 243
240fn outer() { 244fn outer() {
241 match () { 245 match () {
242 $0 246 a$0
243 } 247 }
244} 248}
245"#, 249"#,
@@ -258,7 +262,7 @@ fn outer() {
258struct Foo(i32); 262struct Foo(i32);
259fn main() { 263fn main() {
260 match Foo(92) { 264 match Foo(92) {
261 $0(92) => (), 265 a$0(92) => (),
262 } 266 }
263} 267}
264"#, 268"#,
@@ -281,7 +285,7 @@ struct Foo(i32);
281impl Foo { 285impl Foo {
282 fn foo() { 286 fn foo() {
283 match () { 287 match () {
284 $0 288 a$0
285 } 289 }
286 } 290 }
287} 291}
@@ -314,4 +318,86 @@ impl Foo {
314 "#]], 318 "#]],
315 ) 319 )
316 } 320 }
321
322 #[test]
323 fn completes_enum_variant_matcharm() {
324 check(
325 r#"
326enum Foo { Bar, Baz, Quux }
327
328fn main() {
329 let foo = Foo::Quux;
330 match foo { Qu$0 }
331}
332"#,
333 expect![[r#"
334 ev Foo::Bar ()
335 ev Foo::Baz ()
336 ev Foo::Quux ()
337 en Foo
338 "#]],
339 )
340 }
341
342 #[test]
343 fn completes_enum_variant_matcharm_ref() {
344 check(
345 r#"
346enum Foo { Bar, Baz, Quux }
347
348fn main() {
349 let foo = Foo::Quux;
350 match &foo { Qu$0 }
351}
352"#,
353 expect![[r#"
354 ev Foo::Bar ()
355 ev Foo::Baz ()
356 ev Foo::Quux ()
357 en Foo
358 "#]],
359 )
360 }
361
362 #[test]
363 fn completes_enum_variant_iflet() {
364 check(
365 r#"
366enum Foo { Bar, Baz, Quux }
367
368fn main() {
369 let foo = Foo::Quux;
370 if let Qu$0 = foo { }
371}
372"#,
373 expect![[r#"
374 ev Foo::Bar ()
375 ev Foo::Baz ()
376 ev Foo::Quux ()
377 en Foo
378 "#]],
379 )
380 }
381
382 #[test]
383 fn completes_enum_variant_impl() {
384 check(
385 r#"
386enum Foo { Bar, Baz, Quux }
387impl Foo {
388 fn foo() { match Foo::Bar { Q$0 } }
389}
390"#,
391 expect![[r#"
392 ev Self::Bar ()
393 ev Self::Baz ()
394 ev Self::Quux ()
395 ev Foo::Bar ()
396 ev Foo::Baz ()
397 ev Foo::Quux ()
398 sp Self
399 en Foo
400 "#]],
401 )
402 }
317} 403}