aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/completion/complete_record_literal.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/completion/complete_record_literal.rs')
-rw-r--r--crates/ra_ide/src/completion/complete_record_literal.rs37
1 files changed, 10 insertions, 27 deletions
diff --git a/crates/ra_ide/src/completion/complete_record_literal.rs b/crates/ra_ide/src/completion/complete_record_literal.rs
index e4e764f58..8b67d3ba2 100644
--- a/crates/ra_ide/src/completion/complete_record_literal.rs
+++ b/crates/ra_ide/src/completion/complete_record_literal.rs
@@ -1,36 +1,19 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use super::get_missing_fields;
3use crate::completion::{CompletionContext, Completions}; 4use crate::completion::{CompletionContext, Completions};
4use ra_syntax::SmolStr; 5use either::Either;
5 6
6/// Complete fields in fields literals. 7/// Complete fields in fields literals.
7pub(super) fn complete_record_literal(acc: &mut Completions, ctx: &CompletionContext) { 8pub(super) fn complete_record_literal(
8 let (ty, variant) = match ctx.record_lit_syntax.as_ref().and_then(|it| { 9 acc: &mut Completions,
9 Some((ctx.sema.type_of_expr(&it.clone().into())?, ctx.sema.resolve_record_literal(it)?)) 10 ctx: &CompletionContext,
10 }) { 11) -> Option<()> {
11 Some(it) => it, 12 let record_lit = ctx.record_lit_syntax.as_ref()?;
12 _ => return, 13 for (field, field_ty) in get_missing_fields(ctx, Either::Left(record_lit))? {
13 }; 14 acc.add_field(ctx, field, &field_ty);
14
15 let already_present_names: Vec<SmolStr> = ctx
16 .record_lit_syntax
17 .as_ref()
18 .and_then(|record_literal| record_literal.record_field_list())
19 .map(|field_list| field_list.fields())
20 .map(|fields| {
21 fields
22 .into_iter()
23 .filter_map(|field| field.name_ref())
24 .map(|name_ref| name_ref.text().clone())
25 .collect()
26 })
27 .unwrap_or_default();
28
29 for (field, field_ty) in ty.variant_fields(ctx.db, variant) {
30 if !already_present_names.contains(&SmolStr::from(field.name(ctx.db).to_string())) {
31 acc.add_field(ctx, field, &field_ty);
32 }
33 } 15 }
16 Some(())
34} 17}
35 18
36#[cfg(test)] 19#[cfg(test)]