Skip to content

Commit d2c42fc

Browse files
Merge pull request #20831 from A4-Tacks/record-expr-shorthand
Add shorthand field completion for record-expr
2 parents c585e67 + 9a9f011 commit d2c42fc

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

crates/ide-completion/src/completions/record.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,33 @@ fn baz() {
178178
)
179179
}
180180

181+
#[test]
182+
fn literal_struct_completion_shorthand() {
183+
check_edit(
184+
"FooDesc{}",
185+
r#"
186+
struct FooDesc { pub bar: bool, n: i32 }
187+
188+
fn create_foo(foo_desc: &FooDesc) -> () { () }
189+
190+
fn baz() {
191+
let bar = true;
192+
let foo = create_foo(&$0);
193+
}
194+
"#,
195+
r#"
196+
struct FooDesc { pub bar: bool, n: i32 }
197+
198+
fn create_foo(foo_desc: &FooDesc) -> () { () }
199+
200+
fn baz() {
201+
let bar = true;
202+
let foo = create_foo(&FooDesc { bar$1, n: ${2:()} }$0);
203+
}
204+
"#,
205+
)
206+
}
207+
181208
#[test]
182209
fn enum_variant_no_snippets() {
183210
let conf = CompletionConfig { snippet_cap: SnippetCap::new(false), ..TEST_CONFIG };

crates/ide-completion/src/render/variant.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,23 @@ pub(crate) fn render_record_lit(
2626
return RenderedLiteral { literal: path.to_owned(), detail: path.to_owned() };
2727
}
2828
let completions = fields.iter().enumerate().format_with(", ", |(idx, field), f| {
29+
let mut fmt_field = |fill, tab| {
30+
let field_name = field.name(ctx.db);
31+
32+
if let Some(local) = ctx.locals.get(&field_name)
33+
&& local
34+
.ty(ctx.db)
35+
.could_unify_with_deeply(ctx.db, &field.ty(ctx.db).to_type(ctx.db))
36+
{
37+
f(&format_args!("{}{tab}", field_name.display(ctx.db, ctx.edition)))
38+
} else {
39+
f(&format_args!("{}: {fill}", field_name.display(ctx.db, ctx.edition)))
40+
}
41+
};
2942
if snippet_cap.is_some() {
30-
f(&format_args!(
31-
"{}: ${{{}:()}}",
32-
field.name(ctx.db).display(ctx.db, ctx.edition),
33-
idx + 1
34-
))
43+
fmt_field(format_args!("${{{}:()}}", idx + 1), format_args!("${}", idx + 1))
3544
} else {
36-
f(&format_args!("{}: ()", field.name(ctx.db).display(ctx.db, ctx.edition)))
45+
fmt_field(format_args!("()"), format_args!(""))
3746
}
3847
});
3948

0 commit comments

Comments
 (0)