1 //! Complete fields in record literals and patterns.
2 use ide_db::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 = ctx.famous_defs().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()? {
63 if let Some(module) = ctx.scope.module() { module } else { strukt.module(ctx.db) };
65 let path = module.find_use_path(ctx.db, hir::ModuleDef::from(strukt));
67 acc.add_struct_literal(ctx, strukt, path, None);
75 use crate::tests::check_edit;
78 fn literal_struct_completion_edit() {
82 struct FooDesc { pub bar: bool }
84 fn create_foo(foo_desc: &FooDesc) -> () { () }
87 let foo = create_foo(&$0);
91 struct FooDesc { pub bar: bool }
93 fn create_foo(foo_desc: &FooDesc) -> () { () }
96 let foo = create_foo(&FooDesc { bar: ${1:()} }$0);
103 fn literal_struct_completion_from_sub_modules() {
113 fn f() -> submod::Struct {
124 fn f() -> submod::Struct {
125 submod::Struct { a: ${1:()} }$0
132 fn literal_struct_complexion_module() {
137 pub struct FooDesc { pub six: bool, pub neuf: Vec<String>, pub bar: bool }
138 pub fn create_foo(foo_desc: &FooDesc) -> () { () }
144 let foo = create_foo(&$0);
149 pub struct FooDesc { pub six: bool, pub neuf: Vec<String>, pub bar: bool }
150 pub fn create_foo(foo_desc: &FooDesc) -> () { () }
156 let foo = create_foo(&FooDesc { six: ${1:()}, neuf: ${2:()}, bar: ${3:()} }$0);
163 fn default_completion_edit() {
165 "..Default::default()",
167 //- minicore: default
168 struct Struct { foo: u32, bar: usize }
170 impl Default for Struct {
171 fn default() -> Self {}
182 struct Struct { foo: u32, bar: usize }
184 impl Default for Struct {
185 fn default() -> Self {}
197 "..Default::default()",
199 //- minicore: default
200 struct Struct { foo: u32, bar: usize }
202 impl Default for Struct {
203 fn default() -> Self {}
214 struct Struct { foo: u32, bar: usize }
216 impl Default for Struct {
217 fn default() -> Self {}
229 "..Default::default()",
231 //- minicore: default
232 struct Struct { foo: u32, bar: usize }
234 impl Default for Struct {
235 fn default() -> Self {}
246 struct Struct { foo: u32, bar: usize }
248 impl Default for Struct {
249 fn default() -> Self {}