Skip to content

feat: provide Self in record literal completion #12123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions crates/ide-completion/src/completions/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -133,6 +139,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(
Expand Down