aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/completion/complete_struct_pattern.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-07-21 13:06:00 +0100
committerbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-07-21 13:06:00 +0100
commit1137fc47bbbc8d648db3bc669e41fd059a09dd1d (patch)
tree8d0e74c6c5a734fc64edf2504aabfd11c09354c4 /crates/ra_ide_api/src/completion/complete_struct_pattern.rs
parent7bde8012cb28c44de7ffc779003781d385323808 (diff)
parent5fe19d2fbd2daa05b2cd3b1ebb6fa926e9d86c36 (diff)
Merge #1572
1572: Provide completion in struct patterns r=matklad a=viorina Co-authored-by: Ekaterina Babshukova <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/src/completion/complete_struct_pattern.rs')
-rw-r--r--crates/ra_ide_api/src/completion/complete_struct_pattern.rs94
1 files changed, 94 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_struct_pattern.rs b/crates/ra_ide_api/src/completion/complete_struct_pattern.rs
new file mode 100644
index 000000000..d0dde5930
--- /dev/null
+++ b/crates/ra_ide_api/src/completion/complete_struct_pattern.rs
@@ -0,0 +1,94 @@
1use hir::Substs;
2
3use crate::completion::{CompletionContext, Completions};
4
5pub(super) fn complete_struct_pattern(acc: &mut Completions, ctx: &CompletionContext) {
6 let (ty, variant) = match ctx.struct_lit_pat.as_ref().and_then(|it| {
7 Some((
8 ctx.analyzer.type_of_pat(ctx.db, &it.clone().into())?,
9 ctx.analyzer.resolve_struct_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_struct_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_struct_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}