From d8f6013404a88d845cda6793162e7ac24b0ccbf2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 7 Apr 2020 18:23:18 +0200 Subject: Fix names of test modules --- crates/ra_ide/src/completion/complete_record.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_ide/src/completion') diff --git a/crates/ra_ide/src/completion/complete_record.rs b/crates/ra_ide/src/completion/complete_record.rs index 79f5c8c8f..b180e2388 100644 --- a/crates/ra_ide/src/completion/complete_record.rs +++ b/crates/ra_ide/src/completion/complete_record.rs @@ -59,7 +59,7 @@ fn pattern_ascribed_fields(record_pat: &ast::RecordPat) -> Vec { #[cfg(test)] mod tests { - mod record_lit_tests { + mod record_pat_tests { use insta::assert_debug_snapshot; use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; @@ -205,7 +205,7 @@ mod tests { } } - mod record_pat_tests { + mod record_lit_tests { use insta::assert_debug_snapshot; use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; -- cgit v1.2.3 From 7819d99d6bc617ee8653e9dc2fa4d82072d6c594 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 7 Apr 2020 18:25:47 +0200 Subject: Add functional update test --- crates/ra_ide/src/completion/complete_record.rs | 33 +++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'crates/ra_ide/src/completion') diff --git a/crates/ra_ide/src/completion/complete_record.rs b/crates/ra_ide/src/completion/complete_record.rs index b180e2388..2352ced5f 100644 --- a/crates/ra_ide/src/completion/complete_record.rs +++ b/crates/ra_ide/src/completion/complete_record.rs @@ -410,5 +410,38 @@ mod tests { ] "###); } + + #[test] + fn completes_functional_update() { + let completions = complete( + r" + struct S { + foo1: u32, + foo2: u32, + } + + fn main() { + let foo1 = 1; + let s = S { + foo1, + <|> + .. loop {} + } + } + ", + ); + assert_debug_snapshot!(completions, @r###" + [ + CompletionItem { + label: "foo2", + source_range: [221; 221), + delete: [221; 221), + insert: "foo2", + kind: Field, + detail: "u32", + }, + ] + "###); + } } } -- cgit v1.2.3 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/completion') 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