1 //! Complete fields in record literals and patterns.
2 use ide_db::{helpers::FamousDefs, SymbolKind};
3 use syntax::{ast::Expr, T};
6 patterns::ImmediateLocation, CompletionContext, CompletionItem, CompletionItemKind, Completions,
9 pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
10 let missing_fields = match &ctx.completion_location {
12 ImmediateLocation::RecordExpr(record_expr)
13 | ImmediateLocation::RecordExprUpdate(record_expr),
15 let ty = ctx.sema.type_of_expr(&Expr::RecordExpr(record_expr.clone()));
16 let default_trait = FamousDefs(&ctx.sema, ctx.krate).core_default_Default();
17 let impl_default_trait = default_trait.zip(ty).map_or(false, |(default_trait, ty)| {
18 ty.original.impls_trait(ctx.db, default_trait, &[])
21 let missing_fields = ctx.sema.record_literal_missing_fields(record_expr);
22 if impl_default_trait && !missing_fields.is_empty() && ctx.path_qual().is_none() {
23 let completion_text = "..Default::default()";
25 CompletionItem::new(SymbolKind::Field, ctx.source_range(), completion_text);
27 completion_text.strip_prefix(ctx.token.text()).unwrap_or(completion_text);
28 item.insert_text(completion_text);
31 if ctx.previous_token_is(T![.]) {
33 CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), "..");
34 item.insert_text(".");
40 Some(ImmediateLocation::RecordPat(record_pat)) => {
41 ctx.sema.record_pattern_missing_fields(record_pat)
46 for (field, ty) in missing_fields {
47 acc.add_field(ctx, None, field, &ty);
53 pub(crate) fn complete_record_literal(
54 acc: &mut Completions,
55 ctx: &CompletionContext,
57 if !ctx.expects_expression() {
61 if let hir::Adt::Struct(strukt) = ctx.expected_type.as_ref()?.as_adt()? {
62 acc.add_struct_literal(ctx, strukt, None);
70 use crate::tests::check_edit;
73 fn literal_struct_completion_edit() {
77 struct FooDesc { pub bar: bool }
79 fn create_foo(foo_desc: &FooDesc) -> () { () }
82 let foo = create_foo(&$0);
86 struct FooDesc { pub bar: bool }
88 fn create_foo(foo_desc: &FooDesc) -> () { () }
91 let foo = create_foo(&FooDesc { bar: ${1:()} }$0);
98 fn literal_struct_complexion_module() {
103 pub struct FooDesc { pub six: bool, pub neuf: Vec<String>, pub bar: bool }
104 pub fn create_foo(foo_desc: &FooDesc) -> () { () }
110 let foo = create_foo(&$0);
115 pub struct FooDesc { pub six: bool, pub neuf: Vec<String>, pub bar: bool }
116 pub fn create_foo(foo_desc: &FooDesc) -> () { () }
122 let foo = create_foo(&FooDesc { six: ${1:()}, neuf: ${2:()}, bar: ${3:()} }$0);
129 fn default_completion_edit() {
131 "..Default::default()",
133 //- minicore: default
134 struct Struct { foo: u32, bar: usize }
136 impl Default for Struct {
137 fn default() -> Self {}
148 struct Struct { foo: u32, bar: usize }
150 impl Default for Struct {
151 fn default() -> Self {}
163 "..Default::default()",
165 //- minicore: default
166 struct Struct { foo: u32, bar: usize }
168 impl Default for Struct {
169 fn default() -> Self {}
180 struct Struct { foo: u32, bar: usize }
182 impl Default for Struct {
183 fn default() -> Self {}
195 "..Default::default()",
197 //- minicore: default
198 struct Struct { foo: u32, bar: usize }
200 impl Default for Struct {
201 fn default() -> Self {}
212 struct Struct { foo: u32, bar: usize }
214 impl Default for Struct {
215 fn default() -> Self {}