diff options
Diffstat (limited to 'crates/ide_completion/src/completions/pattern.rs')
-rw-r--r-- | crates/ide_completion/src/completions/pattern.rs | 116 |
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; | |||
85 | struct Bar { f: u32 } | 88 | struct Bar { f: u32 } |
86 | 89 | ||
87 | fn foo() { | 90 | fn 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 } } | |||
106 | enum E { X } | 109 | enum E { X } |
107 | 110 | ||
108 | fn foo() { | 111 | fn 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; | |||
129 | struct Bar { f: u32 } | 133 | struct Bar { f: u32 } |
130 | 134 | ||
131 | fn foo() { | 135 | fn 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 } | |||
147 | static FOO: E = E::X; | 151 | static FOO: E = E::X; |
148 | struct Bar { f: u32 } | 152 | struct Bar { f: u32 } |
149 | 153 | ||
150 | fn foo($0) { | 154 | fn foo(a$0) { |
151 | } | 155 | } |
152 | "#, | 156 | "#, |
153 | expect![[r#" | 157 | expect![[r#" |
@@ -163,7 +167,7 @@ fn foo($0) { | |||
163 | struct Bar { f: u32 } | 167 | struct Bar { f: u32 } |
164 | 168 | ||
165 | fn foo() { | 169 | fn foo() { |
166 | let $0 | 170 | let a$0 |
167 | } | 171 | } |
168 | "#, | 172 | "#, |
169 | expect![[r#" | 173 | expect![[r#" |
@@ -179,7 +183,7 @@ fn foo() { | |||
179 | struct Foo { bar: String, baz: String } | 183 | struct Foo { bar: String, baz: String } |
180 | struct Bar(String, String); | 184 | struct Bar(String, String); |
181 | struct Baz; | 185 | struct Baz; |
182 | fn outer($0) {} | 186 | fn 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 } | |||
196 | struct Bar(String, String); | 200 | struct Bar(String, String); |
197 | struct Baz; | 201 | struct Baz; |
198 | fn outer() { | 202 | fn 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); | |||
215 | struct Baz; | 219 | struct Baz; |
216 | fn outer() { | 220 | fn outer() { |
217 | match () { | 221 | match () { |
218 | $0 | 222 | a$0 |
219 | } | 223 | } |
220 | } | 224 | } |
221 | "#, | 225 | "#, |
@@ -239,7 +243,7 @@ use foo::*; | |||
239 | 243 | ||
240 | fn outer() { | 244 | fn outer() { |
241 | match () { | 245 | match () { |
242 | $0 | 246 | a$0 |
243 | } | 247 | } |
244 | } | 248 | } |
245 | "#, | 249 | "#, |
@@ -258,7 +262,7 @@ fn outer() { | |||
258 | struct Foo(i32); | 262 | struct Foo(i32); |
259 | fn main() { | 263 | fn 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); | |||
281 | impl Foo { | 285 | impl 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#" | ||
326 | enum Foo { Bar, Baz, Quux } | ||
327 | |||
328 | fn 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#" | ||
346 | enum Foo { Bar, Baz, Quux } | ||
347 | |||
348 | fn 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#" | ||
366 | enum Foo { Bar, Baz, Quux } | ||
367 | |||
368 | fn 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#" | ||
386 | enum Foo { Bar, Baz, Quux } | ||
387 | impl 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 | } |