diff --git a/crates/ide-completion/src/completions/record.rs b/crates/ide-completion/src/completions/record.rs index 2f5abd18934f..19600172e7bb 100644 --- a/crates/ide-completion/src/completions/record.rs +++ b/crates/ide-completion/src/completions/record.rs @@ -181,6 +181,33 @@ fn baz() { ) } + #[test] + fn literal_struct_completion_shorthand() { + check_edit( + "FooDesc{}", + r#" +struct FooDesc { pub bar: bool, n: i32 } + +fn create_foo(foo_desc: &FooDesc) -> () { () } + +fn baz() { + let bar = true; + let foo = create_foo(&$0); +} + "#, + r#" +struct FooDesc { pub bar: bool, n: i32 } + +fn create_foo(foo_desc: &FooDesc) -> () { () } + +fn baz() { + let bar = true; + let foo = create_foo(&FooDesc { bar$1, n: ${2:()} }$0); +} + "#, + ) + } + #[test] fn enum_variant_no_snippets() { let conf = CompletionConfig { snippet_cap: SnippetCap::new(false), ..TEST_CONFIG }; diff --git a/crates/ide-completion/src/render/variant.rs b/crates/ide-completion/src/render/variant.rs index 42324b4290a7..8b73b58d0689 100644 --- a/crates/ide-completion/src/render/variant.rs +++ b/crates/ide-completion/src/render/variant.rs @@ -26,14 +26,21 @@ pub(crate) fn render_record_lit( return RenderedLiteral { literal: path.to_owned(), detail: path.to_owned() }; } let completions = fields.iter().enumerate().format_with(", ", |(idx, field), f| { + let mut fmt_field = |fill, tab| { + let field_name = field.name(ctx.db); + + if let Some(local) = ctx.locals.get(&field_name) + && local.ty(ctx.db) == field.ty(ctx.db).to_type(ctx.db) + { + f(&format_args!("{}{tab}", field_name.display(ctx.db, ctx.edition))) + } else { + f(&format_args!("{}: {fill}", field_name.display(ctx.db, ctx.edition))) + } + }; if snippet_cap.is_some() { - f(&format_args!( - "{}: ${{{}:()}}", - field.name(ctx.db).display(ctx.db, ctx.edition), - idx + 1 - )) + fmt_field(format_args!("${{{}:()}}", idx + 1), format_args!("${}", idx + 1)) } else { - f(&format_args!("{}: ()", field.name(ctx.db).display(ctx.db, ctx.edition))) + fmt_field(format_args!("()"), format_args!("")) } });