Skip to content
Open
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions crates/ide-completion/src/completions/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
19 changes: 13 additions & 6 deletions crates/ide-completion/src/render/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(""))
}
});

Expand Down