Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide_completion/src/completions/record.rs')
| -rw-r--r-- | crates/ide_completion/src/completions/record.rs | 82 |
1 files changed, 54 insertions, 28 deletions
diff --git a/crates/ide_completion/src/completions/record.rs b/crates/ide_completion/src/completions/record.rs index 78d0623106..264b3784bf 100644 --- a/crates/ide_completion/src/completions/record.rs +++ b/crates/ide_completion/src/completions/record.rs @@ -14,32 +14,51 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> | ImmediateLocation::RecordExprUpdate(record_expr), ) => { let ty = ctx.sema.type_of_expr(&Expr::RecordExpr(record_expr.clone())); - let default_trait = ctx.famous_defs().core_default_Default(); - let impl_default_trait = default_trait.zip(ty).map_or(false, |(default_trait, ty)| { - ty.original.impls_trait(ctx.db, default_trait, &[]) - }); - - let missing_fields = ctx.sema.record_literal_missing_fields(record_expr); - if impl_default_trait && !missing_fields.is_empty() && ctx.path_qual().is_none() { - let completion_text = "..Default::default()"; - let mut item = - CompletionItem::new(SymbolKind::Field, ctx.source_range(), completion_text); - let completion_text = - completion_text.strip_prefix(ctx.token.text()).unwrap_or(completion_text); - item.insert_text(completion_text).set_relevance(CompletionRelevance { - exact_postfix_snippet_match: true, - ..Default::default() - }); - item.add_to(acc); - } - if ctx.previous_token_is(T![.]) { - let mut item = - CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), ".."); - item.insert_text("."); - item.add_to(acc); - return None; + + if let Some(hir::Adt::Union(un)) = ty.as_ref().and_then(|t| t.original.as_adt()) { + // ctx.sema.record_literal_missing_fields will always return + // an empty Vec on a union literal. This is normally + // reasonable, but here we'd like to present the full list + // of fields if the literal is empty. + let were_fields_specified = record_expr + .record_expr_field_list() + .and_then(|fl| fl.fields().next()) + .is_some(); + + match were_fields_specified { + false => un.fields(ctx.db).into_iter().map(|f| (f, f.ty(ctx.db))).collect(), + true => vec![], + } + } else { + let missing_fields = ctx.sema.record_literal_missing_fields(record_expr); + + let default_trait = ctx.famous_defs().core_default_Default(); + let impl_default_trait = + default_trait.zip(ty.as_ref()).map_or(false, |(default_trait, ty)| { + ty.original.impls_trait(ctx.db, default_trait, &[]) + }); + + if impl_default_trait && !missing_fields.is_empty() && ctx.path_qual().is_none() { + let completion_text = "..Default::default()"; + let mut item = + CompletionItem::new(SymbolKind::Field, ctx.source_range(), completion_text); + let completion_text = + completion_text.strip_prefix(ctx.token.text()).unwrap_or(completion_text); + item.insert_text(completion_text).set_relevance(CompletionRelevance { + exact_postfix_snippet_match: true, + ..Default::default() + }); + item.add_to(acc); + } + if ctx.previous_token_is(T![.]) { + let mut item = + CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), ".."); + item.insert_text("."); + item.add_to(acc); + return None; + } + missing_fields } - missing_fields } Some(ImmediateLocation::RecordPat(record_pat)) => { ctx.sema.record_pattern_missing_fields(record_pat) @@ -62,14 +81,21 @@ pub(crate) fn complete_record_literal( return None; } - if let hir::Adt::Struct(strukt) = ctx.expected_type.as_ref()?.as_adt()? { - if ctx.path_qual().is_none() { + match ctx.expected_type.as_ref()?.as_adt()? { + hir::Adt::Struct(strukt) if ctx.path_qual().is_none() => { let module = if let Some(module) = ctx.module { module } else { strukt.module(ctx.db) }; let path = module.find_use_path(ctx.db, hir::ModuleDef::from(strukt)); acc.add_struct_literal(ctx, strukt, path, None); } - } + hir::Adt::Union(un) if ctx.path_qual().is_none() => { + let module = if let Some(module) = ctx.module { module } else { un.module(ctx.db) }; + let path = module.find_use_path(ctx.db, hir::ModuleDef::from(un)); + + acc.add_union_literal(ctx, un, path, None); + } + _ => {} + }; Some(()) } |