Show Self pattern completions for Adts if inside impls

This commit is contained in:
Lukas Wirth 2021-02-09 19:47:21 +01:00
parent 2f171ca78d
commit e92180a1d8
3 changed files with 40 additions and 6 deletions

View file

@ -31,6 +31,14 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
_ => false, _ => false,
}, },
hir::ScopeDef::MacroDef(_) => true, 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, _ => false,
}; };
if add_resolution { 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
"#]],
)
}
} }

View file

@ -746,7 +746,7 @@ fn f() -> m::E { V$0 }
r#" r#"
enum Foo { Bar, Baz, Quux } enum Foo { Bar, Baz, Quux }
impl Foo { impl Foo {
fn foo() { let foo: Foo = Q$0 } fn foo() { match Foo::Bar { Q$0 } }
} }
"#, "#,
expect![[r#" expect![[r#"

View file

@ -276,6 +276,14 @@ impl<'a> CompletionContext<'a> {
}); });
} }
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( fn fill(
&mut self, &mut self,
original_file: &SyntaxNode, original_file: &SyntaxNode,
@ -345,6 +353,8 @@ impl<'a> CompletionContext<'a> {
self.is_irrefutable_pat_binding = true; self.is_irrefutable_pat_binding = true;
} }
} }
self.fill_impl_def();
} }
if is_node::<ast::Param>(name.syntax()) { if is_node::<ast::Param>(name.syntax()) {
self.is_param = true; self.is_param = true;
@ -372,11 +382,7 @@ impl<'a> CompletionContext<'a> {
self.sema.find_node_at_offset_with_macros(&original_file, offset); self.sema.find_node_at_offset_with_macros(&original_file, offset);
} }
self.impl_def = self self.fill_impl_def();
.sema
.ancestors_with_macros(self.token.parent())
.take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE)
.find_map(ast::Impl::cast);
let top_node = name_ref let top_node = name_ref
.syntax() .syntax()