From e92180a1d8c964d386fc5ffb80bfb05abdb6c153 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 9 Feb 2021 19:47:21 +0100 Subject: [PATCH] Show Self pattern completions for Adts if inside impls --- crates/completion/src/completions/pattern.rs | 28 +++++++++++++++++++ .../src/completions/unqualified_path.rs | 2 +- crates/completion/src/context.rs | 16 +++++++---- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/crates/completion/src/completions/pattern.rs b/crates/completion/src/completions/pattern.rs index 595160ff57a..43a5160cba3 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 + "#]], + ) + } } diff --git a/crates/completion/src/completions/unqualified_path.rs b/crates/completion/src/completions/unqualified_path.rs index fb67756bbfc..5112ecc2d9c 100644 --- a/crates/completion/src/completions/unqualified_path.rs +++ b/crates/completion/src/completions/unqualified_path.rs @@ -746,7 +746,7 @@ fn completes_enum_variant_impl() { r#" enum Foo { Bar, Baz, Quux } impl Foo { - fn foo() { let foo: Foo = Q$0 } + fn foo() { match Foo::Bar { Q$0 } } } "#, expect![[r#" diff --git a/crates/completion/src/context.rs b/crates/completion/src/context.rs index b1e8eba8564..3db3578555f 100644 --- a/crates/completion/src/context.rs +++ b/crates/completion/src/context.rs @@ -276,6 +276,14 @@ fn fill_keyword_patterns(&mut self, file_with_fake_ident: &SyntaxNode, offset: T }); } + fn fill_impl_def(&mut self) { + self.impl_def = self + .sema + .ancestors_with_macros(self.token.parent()) + .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE) + .find_map(ast::Impl::cast); + } + fn fill( &mut self, original_file: &SyntaxNode, @@ -345,6 +353,8 @@ fn fill( self.is_irrefutable_pat_binding = true; } } + + self.fill_impl_def(); } if is_node::(name.syntax()) { self.is_param = true; @@ -372,11 +382,7 @@ fn classify_name_ref( self.sema.find_node_at_offset_with_macros(&original_file, offset); } - self.impl_def = self - .sema - .ancestors_with_macros(self.token.parent()) - .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE) - .find_map(ast::Impl::cast); + self.fill_impl_def(); let top_node = name_ref .syntax()