From 5b18a4eef9e69260ce2f105b33553c929cb7d827 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 23 Aug 2019 15:55:21 +0300 Subject: rename struct -> record, pos -> tuple --- .../src/completion/complete_record_pattern.rs | 94 ++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 crates/ra_ide_api/src/completion/complete_record_pattern.rs (limited to 'crates/ra_ide_api/src/completion/complete_record_pattern.rs') diff --git a/crates/ra_ide_api/src/completion/complete_record_pattern.rs b/crates/ra_ide_api/src/completion/complete_record_pattern.rs new file mode 100644 index 000000000..8c8b47ea4 --- /dev/null +++ b/crates/ra_ide_api/src/completion/complete_record_pattern.rs @@ -0,0 +1,94 @@ +use hir::Substs; + +use crate::completion::{CompletionContext, Completions}; + +pub(super) fn complete_record_pattern(acc: &mut Completions, ctx: &CompletionContext) { + let (ty, variant) = match ctx.record_lit_pat.as_ref().and_then(|it| { + Some(( + ctx.analyzer.type_of_pat(ctx.db, &it.clone().into())?, + ctx.analyzer.resolve_record_pattern(it)?, + )) + }) { + Some(it) => it, + _ => return, + }; + let substs = &ty.substs().unwrap_or_else(Substs::empty); + + for field in variant.fields(ctx.db) { + acc.add_field(ctx, field, substs); + } +} + +#[cfg(test)] +mod tests { + use crate::completion::{do_completion, CompletionItem, CompletionKind}; + use insta::assert_debug_snapshot_matches; + + fn complete(code: &str) -> Vec { + do_completion(code, CompletionKind::Reference) + } + + #[test] + fn test_record_pattern_field() { + let completions = complete( + r" + struct S { foo: u32 } + + fn process(f: S) { + match f { + S { f<|>: 92 } => (), + } + } + ", + ); + assert_debug_snapshot_matches!(completions, @r###" + ⋮[ + ⋮ CompletionItem { + ⋮ label: "foo", + ⋮ source_range: [117; 118), + ⋮ delete: [117; 118), + ⋮ insert: "foo", + ⋮ kind: Field, + ⋮ detail: "u32", + ⋮ }, + ⋮] + "###); + } + + #[test] + fn test_record_pattern_enum_variant() { + let completions = complete( + r" + enum E { + S { foo: u32, bar: () } + } + + fn process(e: E) { + match e { + E::S { <|> } => (), + } + } + ", + ); + assert_debug_snapshot_matches!(completions, @r###" + ⋮[ + ⋮ CompletionItem { + ⋮ label: "bar", + ⋮ source_range: [161; 161), + ⋮ delete: [161; 161), + ⋮ insert: "bar", + ⋮ kind: Field, + ⋮ detail: "()", + ⋮ }, + ⋮ CompletionItem { + ⋮ label: "foo", + ⋮ source_range: [161; 161), + ⋮ delete: [161; 161), + ⋮ insert: "foo", + ⋮ kind: Field, + ⋮ detail: "u32", + ⋮ }, + ⋮] + "###); + } +} -- cgit v1.2.3