1 //! Complete fields in record literals and patterns.
2 use ide_db::{helpers::FamousDefs, SymbolKind};
3 use syntax::{ast::Expr, T};
6 item::CompletionKind, patterns::ImmediateLocation, CompletionContext, CompletionItem,
7 CompletionItemKind, 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 = FamousDefs(&ctx.sema, ctx.krate).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()";
25 let mut item = CompletionItem::new(
26 CompletionKind::Snippet,
31 completion_text.strip_prefix(ctx.token.text()).unwrap_or(completion_text);
32 item.insert_text(completion_text).kind(SymbolKind::Field);
35 if ctx.previous_token_is(T![.]) {
37 CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), "..");
38 item.insert_text(".").kind(CompletionItemKind::Snippet);
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 acc.add_struct_literal(ctx, strukt, None);
74 use crate::tests::check_edit;
77 fn literal_struct_completion_edit() {
81 struct FooDesc { pub bar: bool }
83 fn create_foo(foo_desc: &FooDesc) -> () { () }
86 let foo = create_foo(&$0);
90 struct FooDesc { pub bar: bool }
92 fn create_foo(foo_desc: &FooDesc) -> () { () }
95 let foo = create_foo(&FooDesc { bar: ${1:()} }$0);
102 fn literal_struct_complexion_module() {
107 pub struct FooDesc { pub six: bool, pub neuf: Vec<String>, pub bar: bool }
108 pub fn create_foo(foo_desc: &FooDesc) -> () { () }
114 let foo = create_foo(&$0);
119 pub struct FooDesc { pub six: bool, pub neuf: Vec<String>, pub bar: bool }
120 pub fn create_foo(foo_desc: &FooDesc) -> () { () }
126 let foo = create_foo(&FooDesc { six: ${1:()}, neuf: ${2:()}, bar: ${3:()} }$0);
133 fn default_completion_edit() {
135 "..Default::default()",
137 //- minicore: default
138 struct Struct { foo: u32, bar: usize }
140 impl Default for Struct {
141 fn default() -> Self {}
152 struct Struct { foo: u32, bar: usize }
154 impl Default for Struct {
155 fn default() -> Self {}
167 "..Default::default()",
169 //- minicore: default
170 struct Struct { foo: u32, bar: usize }
172 impl Default for Struct {
173 fn default() -> Self {}
184 struct Struct { foo: u32, bar: usize }
186 impl Default for Struct {
187 fn default() -> Self {}
199 "..Default::default()",
201 //- minicore: default
202 struct Struct { foo: u32, bar: usize }
204 impl Default for Struct {
205 fn default() -> Self {}
216 struct Struct { foo: u32, bar: usize }
218 impl Default for Struct {
219 fn default() -> Self {}