This commit is contained in:
A4-Tacks 2025-10-14 14:41:03 +03:00 committed by GitHub
commit 36b9f33cb8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 40 additions and 6 deletions

View file

@ -178,6 +178,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 };

View file

@ -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!(""))
}
});