From e92180a1d8c964d386fc5ffb80bfb05abdb6c153 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 9 Feb 2021 19:47:21 +0100 Subject: Show Self pattern completions for Adts if inside impls --- crates/completion/src/completions/pattern.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'crates/completion/src/completions/pattern.rs') diff --git a/crates/completion/src/completions/pattern.rs b/crates/completion/src/completions/pattern.rs index 595160ff5..43a5160cb 100644 --- a/crates/completion/src/completions/pattern.rs +++ b/crates/completion/src/completions/pattern.rs @@ -31,6 +31,14 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) { _ => false, }, hir::ScopeDef::MacroDef(_) => true, + hir::ScopeDef::ImplSelfType(impl_) => match impl_.target_ty(ctx.db).as_adt() { + Some(hir::Adt::Struct(strukt)) => { + acc.add_struct_pat(ctx, strukt, Some(name.clone())); + true + } + Some(hir::Adt::Enum(_)) => !ctx.is_irrefutable_pat_binding, + _ => true, + }, _ => false, }; if add_resolution { @@ -258,4 +266,24 @@ fn main() { "#, ); } + + #[test] + fn completes_self_pats() { + check_snippet( + r#" +struct Foo(i32); +impl Foo { + fn foo() { + match () { + $0 + } + } +} + "#, + expect![[r#" + bn Self Self($1)$0 + bn Foo Foo($1)$0 + "#]], + ) + } } -- cgit v1.2.3