1 //! Completes constats and paths in patterns.
3 use crate::{CompletionContext, Completions};
5 /// Completes constants and paths in patterns.
6 pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
7 if !(ctx.is_pat_binding_or_const || ctx.is_irrefutable_pat_binding) {
10 if ctx.record_pat_syntax.is_some() {
14 if !ctx.is_irrefutable_pat_binding {
15 if let Some(ty) = ctx.expected_type.as_ref() {
16 super::complete_enum_variants(acc, ctx, ty, |acc, ctx, variant, path| {
17 acc.add_qualified_variant_pat(ctx, variant, path.clone());
18 acc.add_qualified_enum_variant(ctx, variant, path);
23 // FIXME: ideally, we should look at the type we are matching against and
24 // suggest variants + auto-imports
25 ctx.scope.process_all_names(&mut |name, res| {
26 let add_resolution = match &res {
27 hir::ScopeDef::ModuleDef(def) => match def {
28 hir::ModuleDef::Adt(hir::Adt::Struct(strukt)) => {
29 acc.add_struct_pat(ctx, strukt.clone(), Some(name.clone()));
32 hir::ModuleDef::Variant(variant) if !ctx.is_irrefutable_pat_binding => {
33 acc.add_variant_pat(ctx, variant.clone(), Some(name.clone()));
36 hir::ModuleDef::Adt(hir::Adt::Enum(..))
37 | hir::ModuleDef::Variant(..)
38 | hir::ModuleDef::Const(..)
39 | hir::ModuleDef::Module(..) => !ctx.is_irrefutable_pat_binding,
42 hir::ScopeDef::MacroDef(_) => true,
43 hir::ScopeDef::ImplSelfType(impl_) => match impl_.target_ty(ctx.db).as_adt() {
44 Some(hir::Adt::Struct(strukt)) => {
45 acc.add_struct_pat(ctx, strukt, Some(name.clone()));
48 Some(hir::Adt::Enum(_)) => !ctx.is_irrefutable_pat_binding,
54 acc.add_resolution(ctx, name.to_string(), &res);
61 use expect_test::{expect, Expect};
64 test_utils::{check_edit, completion_list},
68 fn check(ra_fixture: &str, expect: Expect) {
69 let actual = completion_list(ra_fixture, CompletionKind::Reference);
70 expect.assert_eq(&actual)
73 fn check_snippet(ra_fixture: &str, expect: Expect) {
74 let actual = completion_list(ra_fixture, CompletionKind::Snippet);
75 expect.assert_eq(&actual)
79 fn completes_enum_variants_and_modules() {
105 fn completes_in_simple_macro_call() {
108 macro_rules! m { ($e:expr) => { $e } }
112 m!(match E::X { a$0 })
118 ma m!(…) macro_rules! m
124 fn completes_in_irrefutable_let() {
132 static FOO: E = E::X;
133 struct Bar { f: u32 }
146 fn completes_in_param() {
151 static FOO: E = E::X;
152 struct Bar { f: u32 }
164 fn completes_pat_in_let() {
167 struct Bar { f: u32 }
180 fn completes_param_pattern() {
183 struct Foo { bar: String, baz: String }
184 struct Bar(String, String);
189 bn Foo Foo { bar$1, baz$2 }: Foo$0
190 bn Bar Bar($1, $2): Bar$0
196 fn completes_let_pattern() {
199 struct Foo { bar: String, baz: String }
200 struct Bar(String, String);
207 bn Foo Foo { bar$1, baz$2 }$0
214 fn completes_refutable_pattern() {
217 struct Foo { bar: i32, baz: i32 }
218 struct Bar(String, String);
227 bn Foo Foo { bar$1, baz$2 }$0
234 fn omits_private_fields_pat() {
238 pub struct Foo { pub bar: i32, baz: i32 }
239 pub struct Bar(pub String, String);
240 pub struct Invisible(String, String);
251 bn Foo Foo { bar$1, .. }$0
258 fn only_shows_ident_completion() {
281 fn completes_self_pats() {
301 fn completes_qualified_variant() {
309 match {Foo::Bar { baz: 0 }} {
316 bn Self::Bar Self::Bar { baz$1 }$0
317 bn Foo::Bar Foo::Bar { baz$1 }$0
323 fn completes_enum_variant_matcharm() {
326 enum Foo { Bar, Baz, Quux }
343 fn completes_enum_variant_matcharm_ref() {
346 enum Foo { Bar, Baz, Quux }
363 fn completes_enum_variant_iflet() {
366 enum Foo { Bar, Baz, Quux }
370 if let Qu$0 = foo { }
383 fn completes_enum_variant_impl() {
386 enum Foo { Bar, Baz, Quux }
388 fn foo() { match Foo::Bar { Q$0 } }