diff options
author | Kirill Bulatov <[email protected]> | 2019-07-21 22:10:29 +0100 |
---|---|---|
committer | Kirill Bulatov <[email protected]> | 2019-07-21 22:10:29 +0100 |
commit | 31aef808d96b779dbc8ce41e27857965e79bd96f (patch) | |
tree | 254d69a1ec3abe6d70b2dd9737ef699f33f88f62 /crates/ra_ide_api/src/completion/complete_struct_pattern.rs | |
parent | ba76017d2eb1b7606106c15478ac658dc32b6dbd (diff) | |
parent | d690249bc81bc265cb3d1836c2922325f4fdb8af (diff) |
Merge branch 'master' into add-type-lenses
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.rs | 94 |
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 @@ | |||
1 | use hir::Substs; | ||
2 | |||
3 | use crate::completion::{CompletionContext, Completions}; | ||
4 | |||
5 | pub(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)] | ||
23 | mod 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 | } | ||