2021-03-20 22:43:42 +01:00
|
|
|
//! Completes constants and paths in patterns.
|
2019-09-30 11:58:53 +03:00
|
|
|
|
2021-05-26 23:46:00 +02:00
|
|
|
use crate::{context::PatternRefutability, CompletionContext, Completions};
|
2019-02-24 23:49:47 +03:00
|
|
|
|
2020-12-20 18:19:23 +01:00
|
|
|
/// Completes constants and paths in patterns.
|
2020-10-25 10:59:15 +03:00
|
|
|
pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
|
2021-05-26 23:46:00 +02:00
|
|
|
let refutable = match ctx.is_pat_or_const {
|
|
|
|
Some(it) => it == PatternRefutability::Refutable,
|
|
|
|
None => return,
|
|
|
|
};
|
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())
|
|
|
|
{
|
|
|
|
super::complete_enum_variants(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
|
2020-07-11 01:26:24 +02:00
|
|
|
ctx.scope.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,
|
|
|
|
},
|
2020-11-25 23:25:10 +01:00
|
|
|
hir::ScopeDef::MacroDef(_) => true,
|
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 {
|
|
|
|
acc.add_resolution(ctx, name.to_string(), &res);
|
|
|
|
}
|
2019-09-12 23:35:53 +03:00
|
|
|
});
|
2019-02-24 23:49:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-08-21 13:19:31 +02:00
|
|
|
use expect_test::{expect, Expect};
|
2019-02-24 23:49:47 +03:00
|
|
|
|
2020-12-22 19:00:38 +01:00
|
|
|
use crate::{
|
|
|
|
test_utils::{check_edit, completion_list},
|
|
|
|
CompletionKind,
|
|
|
|
};
|
2020-07-04 15:10:55 +02:00
|
|
|
|
|
|
|
fn check(ra_fixture: &str, expect: Expect) {
|
|
|
|
let actual = completion_list(ra_fixture, CompletionKind::Reference);
|
|
|
|
expect.assert_eq(&actual)
|
2019-02-24 23:49:47 +03:00
|
|
|
}
|
|
|
|
|
2020-12-20 18:19:23 +01:00
|
|
|
fn check_snippet(ra_fixture: &str, expect: Expect) {
|
|
|
|
let actual = completion_list(ra_fixture, CompletionKind::Snippet);
|
|
|
|
expect.assert_eq(&actual)
|
|
|
|
}
|
|
|
|
|
2019-02-24 23:49:47 +03:00
|
|
|
#[test]
|
|
|
|
fn completes_enum_variants_and_modules() {
|
2020-07-04 15:10:55 +02:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
enum E { X }
|
|
|
|
use self::E::X;
|
|
|
|
const Z: E = E::X;
|
|
|
|
mod m {}
|
2019-02-24 23:49:47 +03:00
|
|
|
|
2020-07-04 15:10:55 +02:00
|
|
|
static FOO: E = E::X;
|
|
|
|
struct Bar { f: u32 }
|
2019-02-24 23:49:47 +03:00
|
|
|
|
2020-07-04 15:10:55 +02:00
|
|
|
fn foo() {
|
2021-03-15 17:23:08 +01:00
|
|
|
match E::X { a$0 }
|
2020-07-04 15:10:55 +02:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
en E
|
|
|
|
ct Z
|
2020-12-19 13:18:40 +02:00
|
|
|
st Bar
|
2020-12-22 19:00:38 +01:00
|
|
|
ev X
|
2020-07-04 15:10:55 +02:00
|
|
|
md m
|
|
|
|
"#]],
|
2019-02-24 23:49:47 +03:00
|
|
|
);
|
|
|
|
}
|
2020-03-07 15:47:10 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_in_simple_macro_call() {
|
2020-07-04 15:10:55 +02:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
macro_rules! m { ($e:expr) => { $e } }
|
|
|
|
enum E { X }
|
2020-03-07 15:47:10 +01:00
|
|
|
|
2020-07-04 15:10:55 +02:00
|
|
|
fn foo() {
|
2021-03-15 17:23:08 +01:00
|
|
|
m!(match E::X { a$0 })
|
2020-07-04 15:10:55 +02:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2021-03-15 17:23:08 +01:00
|
|
|
ev E::X ()
|
2020-07-04 15:10:55 +02:00
|
|
|
en E
|
|
|
|
ma m!(…) macro_rules! m
|
|
|
|
"#]],
|
2020-03-07 15:47:10 +01:00
|
|
|
);
|
|
|
|
}
|
2020-11-25 23:25:10 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_in_irrefutable_let() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
enum E { X }
|
|
|
|
use self::E::X;
|
|
|
|
const Z: E = E::X;
|
|
|
|
mod m {}
|
|
|
|
|
|
|
|
static FOO: E = E::X;
|
|
|
|
struct Bar { f: u32 }
|
|
|
|
|
|
|
|
fn foo() {
|
2021-03-15 17:23:08 +01:00
|
|
|
let a$0
|
2020-11-25 23:25:10 +01:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
st Bar
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
2020-12-20 18:19:23 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_in_param() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
enum E { X }
|
|
|
|
|
|
|
|
static FOO: E = E::X;
|
|
|
|
struct Bar { f: u32 }
|
|
|
|
|
2021-03-15 17:23:08 +01:00
|
|
|
fn foo(a$0) {
|
2020-12-20 18:19:23 +01:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
st Bar
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_pat_in_let() {
|
|
|
|
check_snippet(
|
|
|
|
r#"
|
|
|
|
struct Bar { f: u32 }
|
|
|
|
|
|
|
|
fn foo() {
|
2021-03-15 17:23:08 +01:00
|
|
|
let a$0
|
2020-12-20 18:19:23 +01:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2020-12-21 15:10:27 +01:00
|
|
|
bn Bar Bar { f$1 }$0
|
2020-12-20 18:19:23 +01:00
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_param_pattern() {
|
|
|
|
check_snippet(
|
|
|
|
r#"
|
|
|
|
struct Foo { bar: String, baz: String }
|
|
|
|
struct Bar(String, String);
|
|
|
|
struct Baz;
|
2021-03-15 17:23:08 +01:00
|
|
|
fn outer(a$0) {}
|
2020-12-20 18:19:23 +01:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2020-12-21 15:10:27 +01:00
|
|
|
bn Foo Foo { bar$1, baz$2 }: Foo$0
|
2020-12-20 18:19:23 +01:00
|
|
|
bn Bar Bar($1, $2): Bar$0
|
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_let_pattern() {
|
|
|
|
check_snippet(
|
|
|
|
r#"
|
|
|
|
struct Foo { bar: String, baz: String }
|
|
|
|
struct Bar(String, String);
|
|
|
|
struct Baz;
|
|
|
|
fn outer() {
|
2021-03-15 17:23:08 +01:00
|
|
|
let a$0
|
2020-12-20 18:19:23 +01:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2020-12-21 15:10:27 +01:00
|
|
|
bn Foo Foo { bar$1, baz$2 }$0
|
2020-12-20 18:19:23 +01:00
|
|
|
bn Bar Bar($1, $2)$0
|
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_refutable_pattern() {
|
|
|
|
check_snippet(
|
|
|
|
r#"
|
|
|
|
struct Foo { bar: i32, baz: i32 }
|
|
|
|
struct Bar(String, String);
|
|
|
|
struct Baz;
|
|
|
|
fn outer() {
|
|
|
|
match () {
|
2021-03-15 17:23:08 +01:00
|
|
|
a$0
|
2020-12-20 18:19:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2020-12-21 15:10:27 +01:00
|
|
|
bn Foo Foo { bar$1, baz$2 }$0
|
2020-12-20 18:19:23 +01:00
|
|
|
bn Bar Bar($1, $2)$0
|
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn omits_private_fields_pat() {
|
|
|
|
check_snippet(
|
|
|
|
r#"
|
|
|
|
mod foo {
|
|
|
|
pub struct Foo { pub bar: i32, baz: i32 }
|
|
|
|
pub struct Bar(pub String, String);
|
|
|
|
pub struct Invisible(String, String);
|
|
|
|
}
|
|
|
|
use foo::*;
|
|
|
|
|
|
|
|
fn outer() {
|
|
|
|
match () {
|
2021-03-15 17:23:08 +01:00
|
|
|
a$0
|
2020-12-20 18:19:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2020-12-21 15:10:27 +01:00
|
|
|
bn Foo Foo { bar$1, .. }$0
|
2020-12-20 18:19:23 +01:00
|
|
|
bn Bar Bar($1, ..)$0
|
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
2020-12-22 19:00:38 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn only_shows_ident_completion() {
|
|
|
|
check_edit(
|
|
|
|
"Foo",
|
|
|
|
r#"
|
|
|
|
struct Foo(i32);
|
|
|
|
fn main() {
|
|
|
|
match Foo(92) {
|
2021-03-15 17:23:08 +01:00
|
|
|
a$0(92) => (),
|
2020-12-22 19:00:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo(i32);
|
|
|
|
fn main() {
|
|
|
|
match Foo(92) {
|
|
|
|
Foo(92) => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
2021-02-09 19:47:21 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_self_pats() {
|
|
|
|
check_snippet(
|
|
|
|
r#"
|
|
|
|
struct Foo(i32);
|
|
|
|
impl Foo {
|
|
|
|
fn foo() {
|
|
|
|
match () {
|
2021-03-15 17:23:08 +01:00
|
|
|
a$0
|
2021-02-09 19:47:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
bn Self Self($1)$0
|
|
|
|
bn Foo Foo($1)$0
|
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
2021-02-09 21:32:05 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_qualified_variant() {
|
|
|
|
check_snippet(
|
|
|
|
r#"
|
|
|
|
enum Foo {
|
|
|
|
Bar { baz: i32 }
|
|
|
|
}
|
|
|
|
impl Foo {
|
|
|
|
fn foo() {
|
|
|
|
match {Foo::Bar { baz: 0 }} {
|
|
|
|
B$0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
bn Self::Bar Self::Bar { baz$1 }$0
|
|
|
|
bn Foo::Bar Foo::Bar { baz$1 }$0
|
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
2021-03-15 17:23:08 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_enum_variant_matcharm() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
enum Foo { Bar, Baz, Quux }
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let foo = Foo::Quux;
|
|
|
|
match foo { Qu$0 }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
ev Foo::Bar ()
|
|
|
|
ev Foo::Baz ()
|
|
|
|
ev Foo::Quux ()
|
|
|
|
en Foo
|
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_enum_variant_matcharm_ref() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
enum Foo { Bar, Baz, Quux }
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let foo = Foo::Quux;
|
|
|
|
match &foo { Qu$0 }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
ev Foo::Bar ()
|
|
|
|
ev Foo::Baz ()
|
|
|
|
ev Foo::Quux ()
|
|
|
|
en Foo
|
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_enum_variant_iflet() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
enum Foo { Bar, Baz, Quux }
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let foo = Foo::Quux;
|
|
|
|
if let Qu$0 = foo { }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
ev Foo::Bar ()
|
|
|
|
ev Foo::Baz ()
|
|
|
|
ev Foo::Quux ()
|
|
|
|
en Foo
|
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_enum_variant_impl() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
enum Foo { Bar, Baz, Quux }
|
|
|
|
impl Foo {
|
|
|
|
fn foo() { match Foo::Bar { Q$0 } }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
ev Self::Bar ()
|
|
|
|
ev Self::Baz ()
|
|
|
|
ev Self::Quux ()
|
|
|
|
ev Foo::Bar ()
|
|
|
|
ev Foo::Baz ()
|
|
|
|
ev Foo::Quux ()
|
|
|
|
sp Self
|
|
|
|
en Foo
|
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
2021-05-26 23:46:00 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_in_record_field_pat() {
|
|
|
|
check_snippet(
|
|
|
|
r#"
|
|
|
|
struct Foo { bar: Bar }
|
|
|
|
struct Bar(u32);
|
|
|
|
fn outer(Foo { bar: $0 }: Foo) {}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
bn Foo Foo { bar$1 }$0
|
|
|
|
bn Bar Bar($1)$0
|
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn skips_in_record_field_pat_name() {
|
|
|
|
check_snippet(
|
|
|
|
r#"
|
|
|
|
struct Foo { bar: Bar }
|
|
|
|
struct Bar(u32);
|
|
|
|
fn outer(Foo { bar$0 }: Foo) {}
|
|
|
|
"#,
|
|
|
|
expect![[r#""#]],
|
|
|
|
)
|
|
|
|
}
|
2019-02-24 23:49:47 +03:00
|
|
|
}
|