1 //! Completion tests for use trees.
2 use expect_test::{expect, Expect};
3
4 use crate::tests::completion_list;
5
check(ra_fixture: &str, expect: Expect)6 fn check(ra_fixture: &str, expect: Expect) {
7 let actual = completion_list(ra_fixture);
8 expect.assert_eq(&actual)
9 }
10
11 #[test]
use_tree_start()12 fn use_tree_start() {
13 cov_mark::check!(unqualified_path_selected_only);
14 check(
15 r#"
16 //- /lib.rs crate:main deps:other_crate
17 use f$0
18
19 struct Foo;
20 enum FooBar {
21 Foo,
22 Bar
23 }
24 mod foo {}
25 //- /other_crate/lib.rs crate:other_crate
26 // nothing here
27 "#,
28 expect![[r#"
29 en FooBar::
30 md foo
31 md other_crate
32 kw crate::
33 kw self::
34 "#]],
35 );
36 }
37
38 #[test]
use_tree_start_abs()39 fn use_tree_start_abs() {
40 cov_mark::check!(use_tree_crate_roots_only);
41 check(
42 r#"
43 //- /lib.rs crate:main deps:other_crate
44 use ::f$0
45
46 struct Foo;
47 mod foo {}
48 //- /other_crate/lib.rs crate:other_crate
49 // nothing here
50 "#,
51 expect![[r#"
52 md other_crate
53 "#]],
54 );
55 }
56
57 #[test]
dont_complete_current_use()58 fn dont_complete_current_use() {
59 cov_mark::check!(dont_complete_current_use);
60 check(r#"use self::foo$0;"#, expect![[r#""#]]);
61 check(
62 r#"
63 mod foo { pub struct S; }
64 use self::{foo::*, bar$0};
65 "#,
66 expect![[r#"
67 md foo
68 st S
69 "#]],
70 );
71 }
72
73 #[test]
nested_use_tree()74 fn nested_use_tree() {
75 check(
76 r#"
77 mod foo {
78 pub mod bar {
79 pub struct FooBar;
80 }
81 }
82 use foo::{bar::$0}
83 "#,
84 expect![[r#"
85 st FooBar
86 "#]],
87 );
88 check(
89 r#"
90 mod foo {
91 pub mod bar {
92 pub struct FooBar;
93 }
94 }
95 use foo::{$0}
96 "#,
97 expect![[r#"
98 md bar
99 kw self
100 "#]],
101 );
102 }
103
104 #[test]
deeply_nested_use_tree()105 fn deeply_nested_use_tree() {
106 check(
107 r#"
108 mod foo {
109 pub mod bar {
110 pub mod baz {
111 pub struct FooBarBaz;
112 }
113 }
114 }
115 use foo::{bar::{baz::$0}}
116 "#,
117 expect![[r#"
118 st FooBarBaz
119 "#]],
120 );
121 check(
122 r#"
123 mod foo {
124 pub mod bar {
125 pub mod baz {
126 pub struct FooBarBaz;
127 }
128 }
129 }
130 use foo::{bar::{$0}}
131 "#,
132 expect![[r#"
133 md baz
134 kw self
135 "#]],
136 );
137 }
138
139 #[test]
plain_qualified_use_tree()140 fn plain_qualified_use_tree() {
141 check(
142 r#"
143 use foo::$0
144
145 mod foo {
146 struct Private;
147 pub struct Foo;
148 macro_rules! foo_ { {} => {} }
149 pub use foo_ as foo;
150 }
151 struct Bar;
152 "#,
153 expect![[r#"
154 ma foo macro_rules! foo_
155 st Foo
156 "#]],
157 );
158 }
159
160 #[test]
enum_plain_qualified_use_tree()161 fn enum_plain_qualified_use_tree() {
162 cov_mark::check!(enum_plain_qualified_use_tree);
163 check(
164 r#"
165 use Foo::$0
166
167 enum Foo {
168 UnitVariant,
169 TupleVariant(),
170 RecordVariant {},
171 }
172 impl Foo {
173 const CONST: () = ()
174 fn func() {}
175 }
176 "#,
177 expect![[r#"
178 ev RecordVariant RecordVariant
179 ev TupleVariant TupleVariant
180 ev UnitVariant UnitVariant
181 "#]],
182 );
183 }
184
185 #[test]
self_qualified_use_tree()186 fn self_qualified_use_tree() {
187 check(
188 r#"
189 use self::$0
190
191 mod foo {}
192 struct Bar;
193 "#,
194 expect![[r#"
195 md foo
196 st Bar
197 "#]],
198 );
199 }
200
201 #[test]
super_qualified_use_tree()202 fn super_qualified_use_tree() {
203 check(
204 r#"
205 mod bar {
206 use super::$0
207 }
208
209 mod foo {}
210 struct Bar;
211 "#,
212 expect![[r#"
213 md bar
214 md foo
215 st Bar
216 "#]],
217 );
218 }
219
220 #[test]
super_super_qualified_use_tree()221 fn super_super_qualified_use_tree() {
222 check(
223 r#"
224 mod a {
225 const A: usize = 0;
226 mod b {
227 const B: usize = 0;
228 mod c { use super::super::$0 }
229 }
230 }
231 "#,
232 expect![[r#"
233 ct A
234 md b
235 kw super::
236 "#]],
237 );
238 }
239
240 #[test]
crate_qualified_use_tree()241 fn crate_qualified_use_tree() {
242 check(
243 r#"
244 use crate::$0
245
246 mod foo {}
247 struct Bar;
248 "#,
249 expect![[r#"
250 md foo
251 st Bar
252 "#]],
253 );
254 }
255
256 #[test]
extern_crate_qualified_use_tree()257 fn extern_crate_qualified_use_tree() {
258 check(
259 r#"
260 //- /lib.rs crate:main deps:other_crate
261 use other_crate::$0
262 //- /other_crate/lib.rs crate:other_crate
263 pub struct Foo;
264 pub mod foo {}
265 "#,
266 expect![[r#"
267 md foo
268 st Foo
269 "#]],
270 );
271 }
272
273 #[test]
pub_use_tree()274 fn pub_use_tree() {
275 check(
276 r#"
277 pub struct X;
278 pub mod bar {}
279 pub use $0;
280 "#,
281 expect![[r#"
282 md bar
283 kw crate::
284 kw self::
285 "#]],
286 );
287 }
288
289 #[test]
pub_suggest_use_tree_super_acc_to_depth_in_tree()290 fn pub_suggest_use_tree_super_acc_to_depth_in_tree() {
291 // https://github.com/rust-lang/rust-analyzer/issues/12439
292 // Check discussion in https://github.com/rust-lang/rust-analyzer/pull/12447
293
294 check(
295 r#"
296 mod foo {
297 mod bar {
298 pub use super::$0;
299 }
300 }
301 "#,
302 expect![[r#"
303 md bar
304 kw super::
305 "#]],
306 );
307
308 // Not suggest super when at crate root
309 check(
310 r#"
311 mod foo {
312 mod bar {
313 pub use super::super::$0;
314 }
315 }
316 "#,
317 expect![[r#"
318 md foo
319 "#]],
320 );
321
322 check(
323 r#"
324 mod foo {
325 use $0;
326 }
327 "#,
328 expect![[r#"
329 kw crate::
330 kw self::
331 kw super::
332 "#]],
333 );
334
335 // Not suggest super after another kw in path ( here it is foo1 )
336 check(
337 r#"
338 mod foo {
339 mod bar {
340 use super::super::foo1::$0;
341 }
342 }
343
344 mod foo1 {
345 pub mod bar1 {}
346 }
347 "#,
348 expect![[r#"
349 md bar1
350 "#]],
351 );
352 }
353
354 #[test]
use_tree_braces_at_start()355 fn use_tree_braces_at_start() {
356 check(
357 r#"
358 struct X;
359 mod bar {}
360 use {$0};
361 "#,
362 expect![[r#"
363 md bar
364 kw crate::
365 kw self::
366 "#]],
367 );
368 }
369
370 #[test]
impl_prefix_does_not_add_fn_snippet()371 fn impl_prefix_does_not_add_fn_snippet() {
372 // regression test for 7222
373 check(
374 r#"
375 mod foo {
376 pub fn bar(x: u32) {}
377 }
378 use self::foo::impl$0
379 "#,
380 expect![[r#"
381 fn bar fn(u32)
382 "#]],
383 );
384 }
385
386 #[test]
use_tree_no_unstable_items_on_stable()387 fn use_tree_no_unstable_items_on_stable() {
388 check(
389 r#"
390 //- /lib.rs crate:main deps:std
391 use std::$0
392 //- /std.rs crate:std
393 #[unstable]
394 pub mod simd {}
395 #[unstable]
396 pub struct S;
397 #[unstable]
398 pub fn foo() {}
399 #[unstable]
400 #[macro_export]
401 marco_rules! m { () => {} }
402 "#,
403 expect![""],
404 );
405 }
406
407 #[test]
use_tree_unstable_items_on_nightly()408 fn use_tree_unstable_items_on_nightly() {
409 check(
410 r#"
411 //- toolchain:nightly
412 //- /lib.rs crate:main deps:std
413 use std::$0
414 //- /std.rs crate:std
415 #[unstable]
416 pub mod simd {}
417 #[unstable]
418 pub struct S;
419 #[unstable]
420 pub fn foo() {}
421 #[unstable]
422 #[macro_export]
423 marco_rules! m { () => {} }
424 "#,
425 expect![[r#"
426 fn foo fn()
427 md simd
428 st S
429 "#]],
430 );
431 }
432