Unnamed repository; edit this file 'description' to name the repository.
Auto merge of #12123 - rainy-me:feat/impl-self-completion, r=Veykril
feat: provide Self in record literal completion close #12106
bors 2022-05-02
parent 9e10d4b · parent f3e3abf · commit f83dccf
-rw-r--r--crates/ide-completion/src/completions/record.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/crates/ide-completion/src/completions/record.rs b/crates/ide-completion/src/completions/record.rs
index 6057c0e004..05fbe8513e 100644
--- a/crates/ide-completion/src/completions/record.rs
+++ b/crates/ide-completion/src/completions/record.rs
@@ -89,6 +89,12 @@ pub(crate) fn complete_record_literal(
.filter(|it| it.len() > 1);
acc.add_struct_literal(ctx, strukt, path, None);
+
+ let impl_ = ctx.impl_def.as_ref()?;
+ let impl_adt = ctx.sema.to_def(impl_)?.self_ty(ctx.db).as_adt()?;
+ if hir::Adt::Struct(strukt) == impl_adt {
+ acc.add_struct_literal(ctx, strukt, None, Some(hir::known::SELF_TYPE));
+ }
}
hir::Adt::Union(un) if ctx.path_qual().is_none() => {
let path = ctx
@@ -134,6 +140,61 @@ fn baz() {
}
#[test]
+ fn literal_struct_impl_self_completion() {
+ check_edit(
+ "Self {…}",
+ r#"
+struct Foo {
+ bar: u64,
+}
+
+impl Foo {
+ fn new() -> Foo {
+ Self$0
+ }
+}
+ "#,
+ r#"
+struct Foo {
+ bar: u64,
+}
+
+impl Foo {
+ fn new() -> Foo {
+ Self { bar: ${1:()} }$0
+ }
+}
+ "#,
+ );
+
+ check_edit(
+ "Self(…)",
+ r#"
+mod submod {
+ pub struct Foo(pub u64);
+}
+
+impl submod::Foo {
+ fn new() -> submod::Foo {
+ Self$0
+ }
+}
+ "#,
+ r#"
+mod submod {
+ pub struct Foo(pub u64);
+}
+
+impl submod::Foo {
+ fn new() -> submod::Foo {
+ Self(${1:()})$0
+ }
+}
+ "#,
+ )
+ }
+
+ #[test]
fn literal_struct_completion_from_sub_modules() {
check_edit(
"submod::Struct {…}",