Skip to content

Commit f922b80

Browse files
committed
- clean up match in ide_completion::completions::record::complete_record_literal
- use original instead of adjusted type in ide_completion::completions::record::complete_record - don't even bother checking if we can complete union literals to Default or to struct update syntax
1 parent 6519b0a commit f922b80

File tree

1 file changed

+50
-55
lines changed
  • crates/ide_completion/src/completions

1 file changed

+50
-55
lines changed

crates/ide_completion/src/completions/record.rs

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,50 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
1515
) => {
1616
let ty = ctx.sema.type_of_expr(&Expr::RecordExpr(record_expr.clone()));
1717

18-
let default_trait = ctx.famous_defs().core_default_Default();
19-
let impl_default_trait =
20-
default_trait.zip(ty.as_ref()).map_or(false, |(default_trait, ty)| {
21-
ty.original.impls_trait(ctx.db, default_trait, &[])
22-
});
23-
24-
let missing_fields = match ty.and_then(|t| t.adjusted().as_adt()) {
25-
Some(hir::Adt::Union(un)) => {
26-
// ctx.sema.record_literal_missing_fields will always return
27-
// an empty Vec on a union literal. This is normally
28-
// reasonable, but here we'd like to present the full list
29-
// of fields if the literal is empty.
30-
let were_fields_specified = record_expr
31-
.record_expr_field_list()
32-
.and_then(|fl| fl.fields().next())
33-
.is_some();
34-
35-
match were_fields_specified {
36-
false => un.fields(ctx.db).into_iter().map(|f| (f, f.ty(ctx.db))).collect(),
37-
true => vec![],
38-
}
18+
if let Some(hir::Adt::Union(un)) = ty.as_ref().and_then(|t| t.original.as_adt()) {
19+
// ctx.sema.record_literal_missing_fields will always return
20+
// an empty Vec on a union literal. This is normally
21+
// reasonable, but here we'd like to present the full list
22+
// of fields if the literal is empty.
23+
let were_fields_specified = record_expr
24+
.record_expr_field_list()
25+
.and_then(|fl| fl.fields().next())
26+
.is_some();
27+
28+
match were_fields_specified {
29+
false => un.fields(ctx.db).into_iter().map(|f| (f, f.ty(ctx.db))).collect(),
30+
true => vec![],
3931
}
40-
_ => ctx.sema.record_literal_missing_fields(record_expr),
41-
};
42-
if impl_default_trait && !missing_fields.is_empty() && ctx.path_qual().is_none() {
43-
let completion_text = "..Default::default()";
44-
let mut item =
45-
CompletionItem::new(SymbolKind::Field, ctx.source_range(), completion_text);
46-
let completion_text =
47-
completion_text.strip_prefix(ctx.token.text()).unwrap_or(completion_text);
48-
item.insert_text(completion_text).set_relevance(CompletionRelevance {
49-
exact_postfix_snippet_match: true,
50-
..Default::default()
51-
});
52-
item.add_to(acc);
53-
}
54-
if ctx.previous_token_is(T![.]) {
55-
let mut item =
56-
CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), "..");
57-
item.insert_text(".");
58-
item.add_to(acc);
59-
return None;
32+
} else {
33+
let missing_fields = ctx.sema.record_literal_missing_fields(record_expr);
34+
35+
let default_trait = ctx.famous_defs().core_default_Default();
36+
let impl_default_trait =
37+
default_trait.zip(ty.as_ref()).map_or(false, |(default_trait, ty)| {
38+
ty.original.impls_trait(ctx.db, default_trait, &[])
39+
});
40+
41+
if impl_default_trait && !missing_fields.is_empty() && ctx.path_qual().is_none() {
42+
let completion_text = "..Default::default()";
43+
let mut item =
44+
CompletionItem::new(SymbolKind::Field, ctx.source_range(), completion_text);
45+
let completion_text =
46+
completion_text.strip_prefix(ctx.token.text()).unwrap_or(completion_text);
47+
item.insert_text(completion_text).set_relevance(CompletionRelevance {
48+
exact_postfix_snippet_match: true,
49+
..Default::default()
50+
});
51+
item.add_to(acc);
52+
}
53+
if ctx.previous_token_is(T![.]) {
54+
let mut item =
55+
CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), "..");
56+
item.insert_text(".");
57+
item.add_to(acc);
58+
return None;
59+
}
60+
missing_fields
6061
}
61-
missing_fields
6262
}
6363
Some(ImmediateLocation::RecordPat(record_pat)) => {
6464
ctx.sema.record_pattern_missing_fields(record_pat)
@@ -82,22 +82,17 @@ pub(crate) fn complete_record_literal(
8282
}
8383

8484
match ctx.expected_type.as_ref()?.as_adt()? {
85-
hir::Adt::Struct(strukt) => {
86-
if ctx.path_qual().is_none() {
87-
let module =
88-
if let Some(module) = ctx.module { module } else { strukt.module(ctx.db) };
89-
let path = module.find_use_path(ctx.db, hir::ModuleDef::from(strukt));
85+
hir::Adt::Struct(strukt) if ctx.path_qual().is_none() => {
86+
let module = if let Some(module) = ctx.module { module } else { strukt.module(ctx.db) };
87+
let path = module.find_use_path(ctx.db, hir::ModuleDef::from(strukt));
9088

91-
acc.add_struct_literal(ctx, strukt, path, None);
92-
}
89+
acc.add_struct_literal(ctx, strukt, path, None);
9390
}
94-
hir::Adt::Union(un) => {
95-
if ctx.path_qual().is_none() {
96-
let module = if let Some(module) = ctx.module { module } else { un.module(ctx.db) };
97-
let path = module.find_use_path(ctx.db, hir::ModuleDef::from(un));
91+
hir::Adt::Union(un) if ctx.path_qual().is_none() => {
92+
let module = if let Some(module) = ctx.module { module } else { un.module(ctx.db) };
93+
let path = module.find_use_path(ctx.db, hir::ModuleDef::from(un));
9894

99-
acc.add_union_literal(ctx, un, path, None);
100-
}
95+
acc.add_union_literal(ctx, un, path, None);
10196
}
10297
_ => {}
10398
};

0 commit comments

Comments
 (0)