1 //! Completes constants and paths in unqualified patterns.
3 use hir::db::DefDatabase;
6 context::{PatternContext, PatternRefutability},
7 CompletionContext, Completions,
10 /// Completes constants and paths in unqualified patterns.
11 pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
12 let refutable = match ctx.pattern_ctx {
13 Some(PatternContext { refutability, .. }) if ctx.path_context.is_none() => {
14 refutability == PatternRefutability::Refutable
18 let single_variant_enum = |enum_: hir::Enum| ctx.db.enum_data(enum_.into()).variants.len() == 1;
20 if let Some(hir::Adt::Enum(e)) =
21 ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt())
23 if refutable || single_variant_enum(e) {
24 super::enum_variants_with_paths(acc, ctx, e, |acc, ctx, variant, path| {
25 acc.add_qualified_variant_pat(ctx, variant, path.clone());
26 acc.add_qualified_enum_variant(ctx, variant, path);
31 // FIXME: ideally, we should look at the type we are matching against and
32 // suggest variants + auto-imports
33 ctx.process_all_names(&mut |name, res| {
34 let add_resolution = match res {
35 hir::ScopeDef::ModuleDef(def) => match def {
36 hir::ModuleDef::Adt(hir::Adt::Struct(strukt)) => {
37 acc.add_struct_pat(ctx, strukt, Some(name.clone()));
40 hir::ModuleDef::Variant(variant)
41 if refutable || single_variant_enum(variant.parent_enum(ctx.db)) =>
43 acc.add_variant_pat(ctx, variant, Some(name.clone()));
46 hir::ModuleDef::Adt(hir::Adt::Enum(e)) => refutable || single_variant_enum(e),
47 hir::ModuleDef::Const(..) | hir::ModuleDef::Module(..) => refutable,
50 hir::ScopeDef::MacroDef(mac) => mac.is_fn_like(),
51 hir::ScopeDef::ImplSelfType(impl_) => match impl_.self_ty(ctx.db).as_adt() {
52 Some(hir::Adt::Struct(strukt)) => {
53 acc.add_struct_pat(ctx, strukt, Some(name.clone()));
56 Some(hir::Adt::Enum(_)) => refutable,
62 acc.add_resolution(ctx, name, res);