diff options
author | Seivan Heidari <[email protected]> | 2019-11-28 07:19:14 +0000 |
---|---|---|
committer | Seivan Heidari <[email protected]> | 2019-11-28 07:19:14 +0000 |
commit | 18a0937585b836ec5ed054b9ae48e0156ab6d9ef (patch) | |
tree | 9de2c0267ddcc00df717f90034d0843d751a851b /crates/ra_ide/src/completion/complete_record_literal.rs | |
parent | a7394b44c870f585eacfeb3036a33471aff49ff8 (diff) | |
parent | 484acc8a61d599662ed63a4cbda091d38a982551 (diff) |
Merge branch 'master' of https://github.com/rust-analyzer/rust-analyzer into feature/themes
Diffstat (limited to 'crates/ra_ide/src/completion/complete_record_literal.rs')
-rw-r--r-- | crates/ra_ide/src/completion/complete_record_literal.rs | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/crates/ra_ide/src/completion/complete_record_literal.rs b/crates/ra_ide/src/completion/complete_record_literal.rs new file mode 100644 index 000000000..577c394d2 --- /dev/null +++ b/crates/ra_ide/src/completion/complete_record_literal.rs | |||
@@ -0,0 +1,159 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use crate::completion::{CompletionContext, Completions}; | ||
4 | |||
5 | /// Complete fields in fields literals. | ||
6 | pub(super) fn complete_record_literal(acc: &mut Completions, ctx: &CompletionContext) { | ||
7 | let (ty, variant) = match ctx.record_lit_syntax.as_ref().and_then(|it| { | ||
8 | Some(( | ||
9 | ctx.analyzer.type_of(ctx.db, &it.clone().into())?, | ||
10 | ctx.analyzer.resolve_record_literal(it)?, | ||
11 | )) | ||
12 | }) { | ||
13 | Some(it) => it, | ||
14 | _ => return, | ||
15 | }; | ||
16 | |||
17 | for (field, field_ty) in ty.variant_fields(ctx.db, variant) { | ||
18 | acc.add_field(ctx, field, &field_ty); | ||
19 | } | ||
20 | } | ||
21 | |||
22 | #[cfg(test)] | ||
23 | mod tests { | ||
24 | use crate::completion::{do_completion, CompletionItem, CompletionKind}; | ||
25 | use insta::assert_debug_snapshot; | ||
26 | |||
27 | fn complete(code: &str) -> Vec<CompletionItem> { | ||
28 | do_completion(code, CompletionKind::Reference) | ||
29 | } | ||
30 | |||
31 | #[test] | ||
32 | fn test_record_literal_deprecated_field() { | ||
33 | let completions = complete( | ||
34 | r" | ||
35 | struct A { | ||
36 | #[deprecated] | ||
37 | the_field: u32, | ||
38 | } | ||
39 | fn foo() { | ||
40 | A { the<|> } | ||
41 | } | ||
42 | ", | ||
43 | ); | ||
44 | assert_debug_snapshot!(completions, @r###" | ||
45 | [ | ||
46 | CompletionItem { | ||
47 | label: "the_field", | ||
48 | source_range: [142; 145), | ||
49 | delete: [142; 145), | ||
50 | insert: "the_field", | ||
51 | kind: Field, | ||
52 | detail: "u32", | ||
53 | deprecated: true, | ||
54 | }, | ||
55 | ] | ||
56 | "###); | ||
57 | } | ||
58 | |||
59 | #[test] | ||
60 | fn test_record_literal_field() { | ||
61 | let completions = complete( | ||
62 | r" | ||
63 | struct A { the_field: u32 } | ||
64 | fn foo() { | ||
65 | A { the<|> } | ||
66 | } | ||
67 | ", | ||
68 | ); | ||
69 | assert_debug_snapshot!(completions, @r###" | ||
70 | [ | ||
71 | CompletionItem { | ||
72 | label: "the_field", | ||
73 | source_range: [83; 86), | ||
74 | delete: [83; 86), | ||
75 | insert: "the_field", | ||
76 | kind: Field, | ||
77 | detail: "u32", | ||
78 | }, | ||
79 | ] | ||
80 | "###); | ||
81 | } | ||
82 | |||
83 | #[test] | ||
84 | fn test_record_literal_enum_variant() { | ||
85 | let completions = complete( | ||
86 | r" | ||
87 | enum E { | ||
88 | A { a: u32 } | ||
89 | } | ||
90 | fn foo() { | ||
91 | let _ = E::A { <|> } | ||
92 | } | ||
93 | ", | ||
94 | ); | ||
95 | assert_debug_snapshot!(completions, @r###" | ||
96 | [ | ||
97 | CompletionItem { | ||
98 | label: "a", | ||
99 | source_range: [119; 119), | ||
100 | delete: [119; 119), | ||
101 | insert: "a", | ||
102 | kind: Field, | ||
103 | detail: "u32", | ||
104 | }, | ||
105 | ] | ||
106 | "###); | ||
107 | } | ||
108 | |||
109 | #[test] | ||
110 | fn test_record_literal_two_structs() { | ||
111 | let completions = complete( | ||
112 | r" | ||
113 | struct A { a: u32 } | ||
114 | struct B { b: u32 } | ||
115 | |||
116 | fn foo() { | ||
117 | let _: A = B { <|> } | ||
118 | } | ||
119 | ", | ||
120 | ); | ||
121 | assert_debug_snapshot!(completions, @r###" | ||
122 | [ | ||
123 | CompletionItem { | ||
124 | label: "b", | ||
125 | source_range: [119; 119), | ||
126 | delete: [119; 119), | ||
127 | insert: "b", | ||
128 | kind: Field, | ||
129 | detail: "u32", | ||
130 | }, | ||
131 | ] | ||
132 | "###); | ||
133 | } | ||
134 | |||
135 | #[test] | ||
136 | fn test_record_literal_generic_struct() { | ||
137 | let completions = complete( | ||
138 | r" | ||
139 | struct A<T> { a: T } | ||
140 | |||
141 | fn foo() { | ||
142 | let _: A<u32> = A { <|> } | ||
143 | } | ||
144 | ", | ||
145 | ); | ||
146 | assert_debug_snapshot!(completions, @r###" | ||
147 | [ | ||
148 | CompletionItem { | ||
149 | label: "a", | ||
150 | source_range: [93; 93), | ||
151 | delete: [93; 93), | ||
152 | insert: "a", | ||
153 | kind: Field, | ||
154 | detail: "u32", | ||
155 | }, | ||
156 | ] | ||
157 | "###); | ||
158 | } | ||
159 | } | ||