aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/completion/complete_record_pattern.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-08-23 13:55:21 +0100
committerAleksey Kladov <[email protected]>2019-08-23 14:59:50 +0100
commit5b18a4eef9e69260ce2f105b33553c929cb7d827 (patch)
tree15f55b3eab48c3d0bbb1975fbd4db7cbb56d3e3e /crates/ra_ide_api/src/completion/complete_record_pattern.rs
parentc12dce0073c1766f7d2b10a69f8526a8093e70dc (diff)
rename struct -> record, pos -> tuple
Diffstat (limited to 'crates/ra_ide_api/src/completion/complete_record_pattern.rs')
-rw-r--r--crates/ra_ide_api/src/completion/complete_record_pattern.rs94
1 files changed, 94 insertions, 0 deletions
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 @@
1use hir::Substs;
2
3use crate::completion::{CompletionContext, Completions};
4
5pub(super) fn complete_record_pattern(acc: &mut Completions, ctx: &CompletionContext) {
6 let (ty, variant) = match ctx.record_lit_pat.as_ref().and_then(|it| {
7 Some((
8 ctx.analyzer.type_of_pat(ctx.db, &it.clone().into())?,
9 ctx.analyzer.resolve_record_pattern(it)?,
10 ))
11 }) {
12 Some(it) => it,
13 _ => return,
14 };
15 let substs = &ty.substs().unwrap_or_else(Substs::empty);
16
17 for field in variant.fields(ctx.db) {
18 acc.add_field(ctx, field, substs);
19 }
20}
21
22#[cfg(test)]
23mod tests {
24 use crate::completion::{do_completion, CompletionItem, CompletionKind};
25 use insta::assert_debug_snapshot_matches;
26
27 fn complete(code: &str) -> Vec<CompletionItem> {
28 do_completion(code, CompletionKind::Reference)
29 }
30
31 #[test]
32 fn test_record_pattern_field() {
33 let completions = complete(
34 r"
35 struct S { foo: u32 }
36
37 fn process(f: S) {
38 match f {
39 S { f<|>: 92 } => (),
40 }
41 }
42 ",
43 );
44 assert_debug_snapshot_matches!(completions, @r###"
45 ⋮[
46 ⋮ CompletionItem {
47 ⋮ label: "foo",
48 ⋮ source_range: [117; 118),
49 ⋮ delete: [117; 118),
50 ⋮ insert: "foo",
51 ⋮ kind: Field,
52 ⋮ detail: "u32",
53 ⋮ },
54 ⋮]
55 "###);
56 }
57
58 #[test]
59 fn test_record_pattern_enum_variant() {
60 let completions = complete(
61 r"
62 enum E {
63 S { foo: u32, bar: () }
64 }
65
66 fn process(e: E) {
67 match e {
68 E::S { <|> } => (),
69 }
70 }
71 ",
72 );
73 assert_debug_snapshot_matches!(completions, @r###"
74 ⋮[
75 ⋮ CompletionItem {
76 ⋮ label: "bar",
77 ⋮ source_range: [161; 161),
78 ⋮ delete: [161; 161),
79 ⋮ insert: "bar",
80 ⋮ kind: Field,
81 ⋮ detail: "()",
82 ⋮ },
83 ⋮ CompletionItem {
84 ⋮ label: "foo",
85 ⋮ source_range: [161; 161),
86 ⋮ delete: [161; 161),
87 ⋮ insert: "foo",
88 ⋮ kind: Field,
89 ⋮ detail: "u32",
90 ⋮ },
91 ⋮]
92 "###);
93 }
94}