diff options
Diffstat (limited to 'crates/completion/src/completions/pattern.rs')
-rw-r--r-- | crates/completion/src/completions/pattern.rs | 261 |
1 files changed, 0 insertions, 261 deletions
diff --git a/crates/completion/src/completions/pattern.rs b/crates/completion/src/completions/pattern.rs deleted file mode 100644 index 595160ff5..000000000 --- a/crates/completion/src/completions/pattern.rs +++ /dev/null | |||
@@ -1,261 +0,0 @@ | |||
1 | //! Completes constats and paths in patterns. | ||
2 | |||
3 | use crate::{CompletionContext, Completions}; | ||
4 | |||
5 | /// Completes constants and paths in patterns. | ||
6 | pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) { | ||
7 | if !(ctx.is_pat_binding_or_const || ctx.is_irrefutable_pat_binding) { | ||
8 | return; | ||
9 | } | ||
10 | if ctx.record_pat_syntax.is_some() { | ||
11 | return; | ||
12 | } | ||
13 | |||
14 | // FIXME: ideally, we should look at the type we are matching against and | ||
15 | // suggest variants + auto-imports | ||
16 | ctx.scope.process_all_names(&mut |name, res| { | ||
17 | let add_resolution = match &res { | ||
18 | hir::ScopeDef::ModuleDef(def) => match def { | ||
19 | hir::ModuleDef::Adt(hir::Adt::Struct(strukt)) => { | ||
20 | acc.add_struct_pat(ctx, strukt.clone(), Some(name.clone())); | ||
21 | true | ||
22 | } | ||
23 | hir::ModuleDef::Variant(variant) if !ctx.is_irrefutable_pat_binding => { | ||
24 | acc.add_variant_pat(ctx, variant.clone(), Some(name.clone())); | ||
25 | true | ||
26 | } | ||
27 | hir::ModuleDef::Adt(hir::Adt::Enum(..)) | ||
28 | | hir::ModuleDef::Variant(..) | ||
29 | | hir::ModuleDef::Const(..) | ||
30 | | hir::ModuleDef::Module(..) => !ctx.is_irrefutable_pat_binding, | ||
31 | _ => false, | ||
32 | }, | ||
33 | hir::ScopeDef::MacroDef(_) => true, | ||
34 | _ => false, | ||
35 | }; | ||
36 | if add_resolution { | ||
37 | acc.add_resolution(ctx, name.to_string(), &res); | ||
38 | } | ||
39 | }); | ||
40 | } | ||
41 | |||
42 | #[cfg(test)] | ||
43 | mod tests { | ||
44 | use expect_test::{expect, Expect}; | ||
45 | |||
46 | use crate::{ | ||
47 | test_utils::{check_edit, completion_list}, | ||
48 | CompletionKind, | ||
49 | }; | ||
50 | |||
51 | fn check(ra_fixture: &str, expect: Expect) { | ||
52 | let actual = completion_list(ra_fixture, CompletionKind::Reference); | ||
53 | expect.assert_eq(&actual) | ||
54 | } | ||
55 | |||
56 | fn check_snippet(ra_fixture: &str, expect: Expect) { | ||
57 | let actual = completion_list(ra_fixture, CompletionKind::Snippet); | ||
58 | expect.assert_eq(&actual) | ||
59 | } | ||
60 | |||
61 | #[test] | ||
62 | fn completes_enum_variants_and_modules() { | ||
63 | check( | ||
64 | r#" | ||
65 | enum E { X } | ||
66 | use self::E::X; | ||
67 | const Z: E = E::X; | ||
68 | mod m {} | ||
69 | |||
70 | static FOO: E = E::X; | ||
71 | struct Bar { f: u32 } | ||
72 | |||
73 | fn foo() { | ||
74 | match E::X { $0 } | ||
75 | } | ||
76 | "#, | ||
77 | expect![[r#" | ||
78 | en E | ||
79 | ct Z | ||
80 | st Bar | ||
81 | ev X | ||
82 | md m | ||
83 | "#]], | ||
84 | ); | ||
85 | } | ||
86 | |||
87 | #[test] | ||
88 | fn completes_in_simple_macro_call() { | ||
89 | check( | ||
90 | r#" | ||
91 | macro_rules! m { ($e:expr) => { $e } } | ||
92 | enum E { X } | ||
93 | |||
94 | fn foo() { | ||
95 | m!(match E::X { $0 }) | ||
96 | } | ||
97 | "#, | ||
98 | expect![[r#" | ||
99 | en E | ||
100 | ma m!(…) macro_rules! m | ||
101 | "#]], | ||
102 | ); | ||
103 | } | ||
104 | |||
105 | #[test] | ||
106 | fn completes_in_irrefutable_let() { | ||
107 | check( | ||
108 | r#" | ||
109 | enum E { X } | ||
110 | use self::E::X; | ||
111 | const Z: E = E::X; | ||
112 | mod m {} | ||
113 | |||
114 | static FOO: E = E::X; | ||
115 | struct Bar { f: u32 } | ||
116 | |||
117 | fn foo() { | ||
118 | let $0 | ||
119 | } | ||
120 | "#, | ||
121 | expect![[r#" | ||
122 | st Bar | ||
123 | "#]], | ||
124 | ); | ||
125 | } | ||
126 | |||
127 | #[test] | ||
128 | fn completes_in_param() { | ||
129 | check( | ||
130 | r#" | ||
131 | enum E { X } | ||
132 | |||
133 | static FOO: E = E::X; | ||
134 | struct Bar { f: u32 } | ||
135 | |||
136 | fn foo($0) { | ||
137 | } | ||
138 | "#, | ||
139 | expect![[r#" | ||
140 | st Bar | ||
141 | "#]], | ||
142 | ); | ||
143 | } | ||
144 | |||
145 | #[test] | ||
146 | fn completes_pat_in_let() { | ||
147 | check_snippet( | ||
148 | r#" | ||
149 | struct Bar { f: u32 } | ||
150 | |||
151 | fn foo() { | ||
152 | let $0 | ||
153 | } | ||
154 | "#, | ||
155 | expect![[r#" | ||
156 | bn Bar Bar { f$1 }$0 | ||
157 | "#]], | ||
158 | ); | ||
159 | } | ||
160 | |||
161 | #[test] | ||
162 | fn completes_param_pattern() { | ||
163 | check_snippet( | ||
164 | r#" | ||
165 | struct Foo { bar: String, baz: String } | ||
166 | struct Bar(String, String); | ||
167 | struct Baz; | ||
168 | fn outer($0) {} | ||
169 | "#, | ||
170 | expect![[r#" | ||
171 | bn Foo Foo { bar$1, baz$2 }: Foo$0 | ||
172 | bn Bar Bar($1, $2): Bar$0 | ||
173 | "#]], | ||
174 | ) | ||
175 | } | ||
176 | |||
177 | #[test] | ||
178 | fn completes_let_pattern() { | ||
179 | check_snippet( | ||
180 | r#" | ||
181 | struct Foo { bar: String, baz: String } | ||
182 | struct Bar(String, String); | ||
183 | struct Baz; | ||
184 | fn outer() { | ||
185 | let $0 | ||
186 | } | ||
187 | "#, | ||
188 | expect![[r#" | ||
189 | bn Foo Foo { bar$1, baz$2 }$0 | ||
190 | bn Bar Bar($1, $2)$0 | ||
191 | "#]], | ||
192 | ) | ||
193 | } | ||
194 | |||
195 | #[test] | ||
196 | fn completes_refutable_pattern() { | ||
197 | check_snippet( | ||
198 | r#" | ||
199 | struct Foo { bar: i32, baz: i32 } | ||
200 | struct Bar(String, String); | ||
201 | struct Baz; | ||
202 | fn outer() { | ||
203 | match () { | ||
204 | $0 | ||
205 | } | ||
206 | } | ||
207 | "#, | ||
208 | expect![[r#" | ||
209 | bn Foo Foo { bar$1, baz$2 }$0 | ||
210 | bn Bar Bar($1, $2)$0 | ||
211 | "#]], | ||
212 | ) | ||
213 | } | ||
214 | |||
215 | #[test] | ||
216 | fn omits_private_fields_pat() { | ||
217 | check_snippet( | ||
218 | r#" | ||
219 | mod foo { | ||
220 | pub struct Foo { pub bar: i32, baz: i32 } | ||
221 | pub struct Bar(pub String, String); | ||
222 | pub struct Invisible(String, String); | ||
223 | } | ||
224 | use foo::*; | ||
225 | |||
226 | fn outer() { | ||
227 | match () { | ||
228 | $0 | ||
229 | } | ||
230 | } | ||
231 | "#, | ||
232 | expect![[r#" | ||
233 | bn Foo Foo { bar$1, .. }$0 | ||
234 | bn Bar Bar($1, ..)$0 | ||
235 | "#]], | ||
236 | ) | ||
237 | } | ||
238 | |||
239 | #[test] | ||
240 | fn only_shows_ident_completion() { | ||
241 | check_edit( | ||
242 | "Foo", | ||
243 | r#" | ||
244 | struct Foo(i32); | ||
245 | fn main() { | ||
246 | match Foo(92) { | ||
247 | $0(92) => (), | ||
248 | } | ||
249 | } | ||
250 | "#, | ||
251 | r#" | ||
252 | struct Foo(i32); | ||
253 | fn main() { | ||
254 | match Foo(92) { | ||
255 | Foo(92) => (), | ||
256 | } | ||
257 | } | ||
258 | "#, | ||
259 | ); | ||
260 | } | ||
261 | } | ||