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,
7 CompletionRelevance, Completions,
10 pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
11 let missing_fields = match &ctx.completion_location {
13 ImmediateLocation::RecordExpr(record_expr)
14 | ImmediateLocation::RecordExprUpdate(record_expr),
16 let ty = ctx.sema.type_of_expr(&Expr::RecordExpr(record_expr.clone()));
17 let default_trait = ctx.famous_defs().core_default_Default();
18 let impl_default_trait = default_trait.zip(ty).map_or(false, |(default_trait, ty)| {
19 ty.original.impls_trait(ctx.db, default_trait, &[])
22 let missing_fields = ctx.sema.record_literal_missing_fields(record_expr);
23 if impl_default_trait && !missing_fields.is_empty() && ctx.path_qual().is_none() {
24 let completion_text = "..Default::default()";
26 CompletionItem::new(SymbolKind::Field, ctx.source_range(), completion_text);
28 completion_text.strip_prefix(ctx.token.text()).unwrap_or(completion_text);
29 item.insert_text(completion_text).set_relevance(CompletionRelevance {
30 exact_postfix_snippet_match: true,
35 if ctx.previous_token_is(T![.]) {
37 CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), "..");
38 item.insert_text(".");
44 Some(ImmediateLocation::RecordPat(record_pat)) => {
45 ctx.sema.record_pattern_missing_fields(record_pat)
50 for (field, ty) in missing_fields {
51 acc.add_field(ctx, None, field, &ty);
57 pub(crate) fn complete_record_literal(
58 acc: &mut Completions,
59 ctx: &CompletionContext,
61 if !ctx.expects_expression() {
65 if let hir::Adt::Struct(strukt) = ctx.expected_type.as_ref()?.as_adt()? {
66 let module = if let Some(module) = ctx.module { module } else { strukt.module(ctx.db) };
68 let path = module.find_use_path(ctx.db, hir::ModuleDef::from(strukt));
70 acc.add_struct_literal(ctx, strukt, path, None);
78 use crate::tests::check_edit;
81 fn literal_struct_completion_edit() {
85 struct FooDesc { pub bar: bool }
87 fn create_foo(foo_desc: &FooDesc) -> () { () }
90 let foo = create_foo(&$0);
94 struct FooDesc { pub bar: bool }
96 fn create_foo(foo_desc: &FooDesc) -> () { () }
99 let foo = create_foo(&FooDesc { bar: ${1:()} }$0);
106 fn literal_struct_completion_from_sub_modules() {
116 fn f() -> submod::Struct {
127 fn f() -> submod::Struct {
128 submod::Struct { a: ${1:()} }$0
135 fn literal_struct_complexion_module() {
140 pub struct FooDesc { pub six: bool, pub neuf: Vec<String>, pub bar: bool }
141 pub fn create_foo(foo_desc: &FooDesc) -> () { () }
147 let foo = create_foo(&$0);
152 pub struct FooDesc { pub six: bool, pub neuf: Vec<String>, pub bar: bool }
153 pub fn create_foo(foo_desc: &FooDesc) -> () { () }
159 let foo = create_foo(&FooDesc { six: ${1:()}, neuf: ${2:()}, bar: ${3:()} }$0);
166 fn default_completion_edit() {
168 "..Default::default()",
170 //- minicore: default
171 struct Struct { foo: u32, bar: usize }
173 impl Default for Struct {
174 fn default() -> Self {}
185 struct Struct { foo: u32, bar: usize }
187 impl Default for Struct {
188 fn default() -> Self {}
200 "..Default::default()",
202 //- minicore: default
203 struct Struct { foo: u32, bar: usize }
205 impl Default for Struct {
206 fn default() -> Self {}
217 struct Struct { foo: u32, bar: usize }
219 impl Default for Struct {
220 fn default() -> Self {}
232 "..Default::default()",
234 //- minicore: default
235 struct Struct { foo: u32, bar: usize }
237 impl Default for Struct {
238 fn default() -> Self {}
249 struct Struct { foo: u32, bar: usize }
251 impl Default for Struct {
252 fn default() -> Self {}