2021-12-06 15:51:33 +01:00
|
|
|
//! Completes constants and paths in unqualified patterns.
|
2019-09-30 11:58:53 +03:00
|
|
|
|
2021-08-14 19:06:35 +02:00
|
|
|
use crate::{
|
|
|
|
context::{PatternContext, PatternRefutability},
|
|
|
|
CompletionContext, Completions,
|
|
|
|
};
|
2019-02-24 23:49:47 +03:00
|
|
|
|
2021-12-06 15:51:33 +01:00
|
|
|
/// Completes constants and paths in unqualified patterns.
|
2020-10-25 10:59:15 +03:00
|
|
|
pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
|
2021-08-14 19:06:35 +02:00
|
|
|
let refutable = match ctx.pattern_ctx {
|
2021-12-06 15:51:33 +01:00
|
|
|
Some(PatternContext { refutability, .. }) if ctx.path_context.is_none() => {
|
|
|
|
refutability == PatternRefutability::Refutable
|
|
|
|
}
|
|
|
|
_ => return,
|
2021-05-26 23:46:00 +02:00
|
|
|
};
|
2020-04-11 23:33:17 +02:00
|
|
|
|
2021-05-26 22:39:47 +02:00
|
|
|
if refutable {
|
2021-05-05 22:55:12 +02:00
|
|
|
if let Some(hir::Adt::Enum(e)) =
|
|
|
|
ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt())
|
|
|
|
{
|
2021-06-16 17:37:23 +02:00
|
|
|
super::enum_variants_with_paths(acc, ctx, e, |acc, ctx, variant, path| {
|
2021-03-15 17:23:08 +01:00
|
|
|
acc.add_qualified_variant_pat(ctx, variant, path.clone());
|
|
|
|
acc.add_qualified_enum_variant(ctx, variant, path);
|
|
|
|
});
|
|
|
|
}
|
2021-02-09 21:32:05 +01:00
|
|
|
}
|
|
|
|
|
2019-03-23 10:53:48 +03:00
|
|
|
// FIXME: ideally, we should look at the type we are matching against and
|
2019-02-24 23:49:47 +03:00
|
|
|
// suggest variants + auto-imports
|
2021-07-28 15:59:02 +02:00
|
|
|
ctx.process_all_names(&mut |name, res| {
|
2020-11-25 23:25:10 +01:00
|
|
|
let add_resolution = match &res {
|
2020-12-20 18:19:23 +01:00
|
|
|
hir::ScopeDef::ModuleDef(def) => match def {
|
|
|
|
hir::ModuleDef::Adt(hir::Adt::Struct(strukt)) => {
|
2021-03-17 01:56:31 +01:00
|
|
|
acc.add_struct_pat(ctx, *strukt, Some(name.clone()));
|
2020-12-20 18:19:23 +01:00
|
|
|
true
|
|
|
|
}
|
2021-05-26 22:39:47 +02:00
|
|
|
hir::ModuleDef::Variant(variant) if refutable => {
|
2021-03-17 01:56:31 +01:00
|
|
|
acc.add_variant_pat(ctx, *variant, Some(name.clone()));
|
2020-12-20 18:19:23 +01:00
|
|
|
true
|
2020-11-25 23:25:10 +01:00
|
|
|
}
|
2020-12-20 18:19:23 +01:00
|
|
|
hir::ModuleDef::Adt(hir::Adt::Enum(..))
|
|
|
|
| hir::ModuleDef::Variant(..)
|
|
|
|
| hir::ModuleDef::Const(..)
|
2021-05-26 22:39:47 +02:00
|
|
|
| hir::ModuleDef::Module(..) => refutable,
|
2020-12-20 18:19:23 +01:00
|
|
|
_ => false,
|
|
|
|
},
|
2021-06-08 17:31:47 +02:00
|
|
|
hir::ScopeDef::MacroDef(mac) => mac.is_fn_like(),
|
2021-03-29 17:46:33 +02:00
|
|
|
hir::ScopeDef::ImplSelfType(impl_) => match impl_.self_ty(ctx.db).as_adt() {
|
2021-02-09 19:47:21 +01:00
|
|
|
Some(hir::Adt::Struct(strukt)) => {
|
|
|
|
acc.add_struct_pat(ctx, strukt, Some(name.clone()));
|
|
|
|
true
|
|
|
|
}
|
2021-05-26 22:39:47 +02:00
|
|
|
Some(hir::Adt::Enum(_)) => refutable,
|
2021-02-09 19:47:21 +01:00
|
|
|
_ => true,
|
|
|
|
},
|
2020-11-25 23:25:10 +01:00
|
|
|
_ => false,
|
2019-02-24 23:49:47 +03:00
|
|
|
};
|
2020-11-25 23:25:10 +01:00
|
|
|
if add_resolution {
|
2021-05-31 14:13:09 +02:00
|
|
|
acc.add_resolution(ctx, name, &res);
|
2020-11-25 23:25:10 +01:00
|
|
|
}
|
2019-09-12 23:35:53 +03:00
|
|
|
});
|
2019-02-24 23:49:47 +03:00
|
|
|
}
|