1 //! Complete fields in record literals and patterns.
2 use ide_db::SymbolKind;
3 use syntax::ast::{self, Expr};
6 context::{DotAccess, DotAccessKind, ExprCtx, PathCompletionCtx, PatternContext, Qualified},
7 CompletionContext, CompletionItem, CompletionItemKind, CompletionRelevance,
8 CompletionRelevancePostfixMatch, Completions,
11 pub(crate) fn complete_record_pattern_fields(
12 acc: &mut Completions,
13 ctx: &CompletionContext<'_>,
14 pattern_ctx: &PatternContext,
16 if let PatternContext { record_pat: Some(record_pat), .. } = pattern_ctx {
17 complete_fields(acc, ctx, ctx.sema.record_pattern_missing_fields(record_pat));
21 pub(crate) fn complete_record_expr_fields(
22 acc: &mut Completions,
23 ctx: &CompletionContext<'_>,
24 record_expr: &ast::RecordExpr,
27 let ty = ctx.sema.type_of_expr(&Expr::RecordExpr(record_expr.clone()));
29 let missing_fields = match ty.as_ref().and_then(|t| t.original.as_adt()) {
30 Some(hir::Adt::Union(un)) => {
31 // ctx.sema.record_literal_missing_fields will always return
32 // an empty Vec on a union literal. This is normally
33 // reasonable, but here we'd like to present the full list
34 // of fields if the literal is empty.
35 let were_fields_specified =
36 record_expr.record_expr_field_list().and_then(|fl| fl.fields().next()).is_some();
38 match were_fields_specified {
39 false => un.fields(ctx.db).into_iter().map(|f| (f, f.ty(ctx.db))).collect(),
44 let missing_fields = ctx.sema.record_literal_missing_fields(record_expr);
45 add_default_update(acc, ctx, ty, &missing_fields);
48 CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), "..");
49 item.insert_text(".");
56 complete_fields(acc, ctx, missing_fields);
59 // FIXME: This should probably be part of complete_path_expr
60 pub(crate) fn complete_record_expr_func_update(
61 acc: &mut Completions,
62 ctx: &CompletionContext<'_>,
63 path_ctx: &PathCompletionCtx,
66 if !matches!(path_ctx.qualified, Qualified::No) {
69 if let ExprCtx { is_func_update: Some(record_expr), .. } = expr_ctx {
70 let ty = ctx.sema.type_of_expr(&Expr::RecordExpr(record_expr.clone()));
72 match ty.as_ref().and_then(|t| t.original.as_adt()) {
73 Some(hir::Adt::Union(_)) => (),
75 let missing_fields = ctx.sema.record_literal_missing_fields(record_expr);
76 add_default_update(acc, ctx, ty, &missing_fields);
82 fn add_default_update(
83 acc: &mut Completions,
84 ctx: &CompletionContext<'_>,
85 ty: Option<hir::TypeInfo>,
86 missing_fields: &[(hir::Field, hir::Type)],
88 let default_trait = ctx.famous_defs().core_default_Default();
89 let impl_default_trait = default_trait
91 .map_or(false, |(default_trait, ty)| ty.original.impls_trait(ctx.db, default_trait, &[]));
92 if impl_default_trait && !missing_fields.is_empty() {
93 // FIXME: This should make use of scope_def like completions so we get all the other goodies
94 let completion_text = "..Default::default()";
95 let mut item = CompletionItem::new(SymbolKind::Field, ctx.source_range(), completion_text);
97 completion_text.strip_prefix(ctx.token.text()).unwrap_or(completion_text);
98 item.insert_text(completion_text).set_relevance(CompletionRelevance {
99 postfix_match: Some(CompletionRelevancePostfixMatch::Exact),
107 acc: &mut Completions,
108 ctx: &CompletionContext<'_>,
109 missing_fields: Vec<(hir::Field, hir::Type)>,
111 for (field, ty) in missing_fields {
117 kind: DotAccessKind::Field { receiver_is_ambiguous_float_literal: false },
128 use crate::tests::check_edit;
131 fn literal_struct_completion_edit() {
135 struct FooDesc { pub bar: bool }
137 fn create_foo(foo_desc: &FooDesc) -> () { () }
140 let foo = create_foo(&$0);
144 struct FooDesc { pub bar: bool }
146 fn create_foo(foo_desc: &FooDesc) -> () { () }
149 let foo = create_foo(&FooDesc { bar: ${1:()} }$0);
156 fn literal_struct_impl_self_completion() {
177 Self { bar: ${1:()} }$0
187 pub struct Foo(pub u64);
191 fn new() -> submod::Foo {
198 pub struct Foo(pub u64);
202 fn new() -> submod::Foo {
211 fn literal_struct_completion_from_sub_modules() {
213 "submod::Struct {…}",
221 fn f() -> submod::Struct {
232 fn f() -> submod::Struct {
233 submod::Struct { a: ${1:()} }$0
240 fn literal_struct_complexion_module() {
245 pub struct FooDesc { pub six: bool, pub neuf: Vec<String>, pub bar: bool }
246 pub fn create_foo(foo_desc: &FooDesc) -> () { () }
252 let foo = create_foo(&$0);
257 pub struct FooDesc { pub six: bool, pub neuf: Vec<String>, pub bar: bool }
258 pub fn create_foo(foo_desc: &FooDesc) -> () { () }
264 let foo = create_foo(&FooDesc { six: ${1:()}, neuf: ${2:()}, bar: ${3:()} }$0);
271 fn default_completion_edit() {
273 "..Default::default()",
275 //- minicore: default
276 struct Struct { foo: u32, bar: usize }
278 impl Default for Struct {
279 fn default() -> Self {}
290 struct Struct { foo: u32, bar: usize }
292 impl Default for Struct {
293 fn default() -> Self {}
305 "..Default::default()",
307 //- minicore: default
308 struct Struct { foo: u32, bar: usize }
310 impl Default for Struct {
311 fn default() -> Self {}
322 struct Struct { foo: u32, bar: usize }
324 impl Default for Struct {
325 fn default() -> Self {}
337 "..Default::default()",
339 //- minicore: default
340 struct Struct { foo: u32, bar: usize }
342 impl Default for Struct {
343 fn default() -> Self {}
354 struct Struct { foo: u32, bar: usize }
356 impl Default for Struct {
357 fn default() -> Self {}