From 4c29214bba65d23e18875bd060325c489be5a8e4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 7 Apr 2020 17:09:02 +0200 Subject: Move computation of missing fields into hir --- crates/ra_ide/src/completion/complete_record.rs | 59 ++++--------------------- 1 file changed, 9 insertions(+), 50 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/completion/complete_record.rs b/crates/ra_ide/src/completion/complete_record.rs index 2352ced5f..f46bcee5c 100644 --- a/crates/ra_ide/src/completion/complete_record.rs +++ b/crates/ra_ide/src/completion/complete_record.rs @@ -1,60 +1,19 @@ //! Complete fields in record literals and patterns. -use ra_syntax::{ast, ast::NameOwner, SmolStr}; - use crate::completion::{CompletionContext, Completions}; pub(super) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { - let (ty, variant, already_present_fields) = - match (ctx.record_lit_pat.as_ref(), ctx.record_lit_syntax.as_ref()) { - (None, None) => return None, - (Some(_), Some(_)) => unreachable!("A record cannot be both a literal and a pattern"), - (Some(record_pat), _) => ( - ctx.sema.type_of_pat(&record_pat.clone().into())?, - ctx.sema.resolve_record_pattern(record_pat)?, - pattern_ascribed_fields(record_pat), - ), - (_, Some(record_lit)) => ( - ctx.sema.type_of_expr(&record_lit.clone().into())?, - ctx.sema.resolve_record_literal(record_lit)?, - literal_ascribed_fields(record_lit), - ), - }; + let missing_fields = match (ctx.record_lit_pat.as_ref(), ctx.record_lit_syntax.as_ref()) { + (None, None) => return None, + (Some(_), Some(_)) => unreachable!("A record cannot be both a literal and a pattern"), + (Some(record_pat), _) => ctx.sema.record_pattern_missing_fields(record_pat), + (_, Some(record_lit)) => ctx.sema.record_literal_missing_fields(record_lit), + }; - for (field, field_ty) in ty.variant_fields(ctx.db, variant).into_iter().filter(|(field, _)| { - // FIXME: already_present_names better be `Vec` - !already_present_fields.contains(&SmolStr::from(field.name(ctx.db).to_string())) - }) { - acc.add_field(ctx, field, &field_ty); + for (field, ty) in missing_fields { + acc.add_field(ctx, field, &ty) } - Some(()) -} -fn literal_ascribed_fields(record_lit: &ast::RecordLit) -> Vec { - record_lit - .record_field_list() - .map(|field_list| field_list.fields()) - .map(|fields| { - fields - .into_iter() - .filter_map(|field| field.name_ref()) - .map(|name_ref| name_ref.text().clone()) - .collect() - }) - .unwrap_or_default() -} - -fn pattern_ascribed_fields(record_pat: &ast::RecordPat) -> Vec { - record_pat - .record_field_pat_list() - .map(|pat_list| { - pat_list - .record_field_pats() - .filter_map(|fild_pat| fild_pat.name()) - .chain(pat_list.bind_pats().filter_map(|bind_pat| bind_pat.name())) - .map(|name| name.text().clone()) - .collect() - }) - .unwrap_or_default() + Some(()) } #[cfg(test)] -- cgit v1.2.3