Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #20831 from A4-Tacks/record-expr-shorthand
Add shorthand field completion for record-expr
Chayim Refael Friedman 6 months ago
parent c585e67 · parent 9a9f011 · commit d2c42fc
-rw-r--r--crates/ide-completion/src/completions/record.rs27
-rw-r--r--crates/ide-completion/src/render/variant.rs21
2 files changed, 42 insertions, 6 deletions
diff --git a/crates/ide-completion/src/completions/record.rs b/crates/ide-completion/src/completions/record.rs
index 28b324d61a..bfa567009c 100644
--- a/crates/ide-completion/src/completions/record.rs
+++ b/crates/ide-completion/src/completions/record.rs
@@ -179,6 +179,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 };
// tuple variant
diff --git a/crates/ide-completion/src/render/variant.rs b/crates/ide-completion/src/render/variant.rs
index 42324b4290..37d0fa18c4 100644
--- a/crates/ide-completion/src/render/variant.rs
+++ b/crates/ide-completion/src/render/variant.rs
@@ -26,14 +26,23 @@ 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)
+ .could_unify_with_deeply(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!(""))
}
});