aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion/src/completions
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-06-17 13:01:43 +0100
committerGitHub <[email protected]>2021-06-17 13:01:43 +0100
commitc82a9141abe6b6cbf5b55710dc8a315a3839081b (patch)
tree1d130833fa51a6fd46e3ff61f3f68155e546c535 /crates/ide_completion/src/completions
parent3b58d8f785917b4212cd917fced6c3006210e4d3 (diff)
parent9353f36516e5b4026ce3a181d578c3a63876a18f (diff)
Merge #9310
9310: internal: Refine and test UseTree completions r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
Diffstat (limited to 'crates/ide_completion/src/completions')
-rw-r--r--crates/ide_completion/src/completions/flyimport.rs2
-rw-r--r--crates/ide_completion/src/completions/keyword.rs64
-rw-r--r--crates/ide_completion/src/completions/qualified_path.rs159
-rw-r--r--crates/ide_completion/src/completions/unqualified_path.rs34
4 files changed, 21 insertions, 238 deletions
diff --git a/crates/ide_completion/src/completions/flyimport.rs b/crates/ide_completion/src/completions/flyimport.rs
index 4604feb5d..814c15653 100644
--- a/crates/ide_completion/src/completions/flyimport.rs
+++ b/crates/ide_completion/src/completions/flyimport.rs
@@ -109,7 +109,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
109 if !ctx.config.enable_imports_on_the_fly { 109 if !ctx.config.enable_imports_on_the_fly {
110 return None; 110 return None;
111 } 111 }
112 if ctx.use_item_syntax.is_some() 112 if ctx.in_use_tree()
113 || ctx.is_path_disallowed() 113 || ctx.is_path_disallowed()
114 || ctx.expects_item() 114 || ctx.expects_item()
115 || ctx.expects_assoc_item() 115 || ctx.expects_assoc_item()
diff --git a/crates/ide_completion/src/completions/keyword.rs b/crates/ide_completion/src/completions/keyword.rs
index 73bbc4345..9754122a0 100644
--- a/crates/ide_completion/src/completions/keyword.rs
+++ b/crates/ide_completion/src/completions/keyword.rs
@@ -18,17 +18,24 @@ pub(crate) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionC
18 item 18 item
19 }; 19 };
20 20
21 if ctx.use_item_syntax.is_some() { 21 if ctx.in_use_tree() {
22 let qual = ctx.path_qual(); 22 match &ctx.path_context {
23 if qual.is_none() { 23 Some(PathCompletionContext { qualifier: Some(qual), use_tree_parent, .. }) => {
24 kw_completion("crate::").add_to(acc); 24 if iter::successors(Some(qual.clone()), |p| p.qualifier())
25 } 25 .all(|p| p.segment().and_then(|s| s.super_token()).is_some())
26 kw_completion("self").add_to(acc); 26 {
27 if iter::successors(qual.cloned(), |p| p.qualifier()) 27 kw_completion("super::").add_to(acc);
28 .all(|p| p.segment().and_then(|s| s.super_token()).is_some()) 28 }
29 { 29 if *use_tree_parent {
30 kw_completion("super::").add_to(acc); 30 kw_completion("self").add_to(acc);
31 } 31 }
32 }
33 _ => {
34 kw_completion("crate::").add_to(acc);
35 kw_completion("self::").add_to(acc);
36 kw_completion("super::").add_to(acc);
37 }
38 };
32 } 39 }
33 40
34 // Suggest .await syntax for types that implement Future trait 41 // Suggest .await syntax for types that implement Future trait
@@ -200,41 +207,6 @@ mod tests {
200 } 207 }
201 208
202 #[test] 209 #[test]
203 fn test_keywords_in_use_stmt() {
204 check(
205 r"use $0",
206 expect![[r#"
207 kw crate::
208 kw self
209 kw super::
210 "#]],
211 );
212
213 // FIXME: `self` shouldn't be shown here and the check below
214 check(
215 r"use a::$0",
216 expect![[r#"
217 kw self
218 "#]],
219 );
220
221 check(
222 r"use super::$0",
223 expect![[r#"
224 kw self
225 kw super::
226 "#]],
227 );
228
229 check(
230 r"use a::{b, $0}",
231 expect![[r#"
232 kw self
233 "#]],
234 );
235 }
236
237 #[test]
238 fn test_keywords_in_function() { 210 fn test_keywords_in_function() {
239 check( 211 check(
240 r"fn quux() { $0 }", 212 r"fn quux() { $0 }",
diff --git a/crates/ide_completion/src/completions/qualified_path.rs b/crates/ide_completion/src/completions/qualified_path.rs
index 9432caa22..0597879ac 100644
--- a/crates/ide_completion/src/completions/qualified_path.rs
+++ b/crates/ide_completion/src/completions/qualified_path.rs
@@ -49,7 +49,7 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
49 hir::PathResolution::Def(hir::ModuleDef::Module(module)) => { 49 hir::PathResolution::Def(hir::ModuleDef::Module(module)) => {
50 let module_scope = module.scope(ctx.db, context_module); 50 let module_scope = module.scope(ctx.db, context_module);
51 for (name, def) in module_scope { 51 for (name, def) in module_scope {
52 if ctx.use_item_syntax.is_some() { 52 if ctx.in_use_tree() {
53 if let hir::ScopeDef::Unknown = def { 53 if let hir::ScopeDef::Unknown = def {
54 if let Some(name_ref) = ctx.name_ref_syntax.as_ref() { 54 if let Some(name_ref) = ctx.name_ref_syntax.as_ref() {
55 if name_ref.syntax().text() == name.to_string().as_str() { 55 if name_ref.syntax().text() == name.to_string().as_str() {
@@ -213,12 +213,6 @@ mod tests {
213 } 213 }
214 214
215 #[test] 215 #[test]
216 fn dont_complete_current_use() {
217 cov_mark::check!(dont_complete_current_use);
218 check(r#"use self::foo$0;"#, expect![[""]]);
219 }
220
221 #[test]
222 fn dont_complete_values_in_type_pos() { 216 fn dont_complete_values_in_type_pos() {
223 check( 217 check(
224 r#" 218 r#"
@@ -249,20 +243,6 @@ fn foo() {
249 } 243 }
250 244
251 #[test] 245 #[test]
252 fn dont_complete_current_use_in_braces_with_glob() {
253 check(
254 r#"
255mod foo { pub struct S; }
256use self::{foo::*, bar$0};
257"#,
258 expect![[r#"
259 st S
260 md foo
261 "#]],
262 );
263 }
264
265 #[test]
266 fn dont_complete_primitive_in_use() { 246 fn dont_complete_primitive_in_use() {
267 check_builtin(r#"use self::$0;"#, expect![[""]]); 247 check_builtin(r#"use self::$0;"#, expect![[""]]);
268 } 248 }
@@ -299,108 +279,6 @@ use self::{foo::*, bar$0};
299 } 279 }
300 280
301 #[test] 281 #[test]
302 fn completes_mod_with_same_name_as_function() {
303 check(
304 r#"
305use self::my::$0;
306
307mod my { pub struct Bar; }
308fn my() {}
309"#,
310 expect![[r#"
311 st Bar
312 "#]],
313 );
314 }
315
316 #[test]
317 fn filters_visibility() {
318 check(
319 r#"
320use self::my::$0;
321
322mod my {
323 struct Bar;
324 pub struct Foo;
325 pub use Bar as PublicBar;
326}
327"#,
328 expect![[r#"
329 st Foo
330 st PublicBar
331 "#]],
332 );
333 }
334
335 #[test]
336 fn completes_use_item_starting_with_self() {
337 check(
338 r#"
339use self::m::$0;
340
341mod m { pub struct Bar; }
342"#,
343 expect![[r#"
344 st Bar
345 "#]],
346 );
347 }
348
349 #[test]
350 fn completes_use_item_starting_with_crate() {
351 check(
352 r#"
353//- /lib.rs
354mod foo;
355struct Spam;
356//- /foo.rs
357use crate::Sp$0
358"#,
359 expect![[r#"
360 md foo
361 st Spam
362 "#]],
363 );
364 }
365
366 #[test]
367 fn completes_nested_use_tree() {
368 check(
369 r#"
370//- /lib.rs
371mod foo;
372struct Spam;
373//- /foo.rs
374use crate::{Sp$0};
375"#,
376 expect![[r#"
377 md foo
378 st Spam
379 "#]],
380 );
381 }
382
383 #[test]
384 fn completes_deeply_nested_use_tree() {
385 check(
386 r#"
387//- /lib.rs
388mod foo;
389pub mod bar {
390 pub mod baz {
391 pub struct Spam;
392 }
393}
394//- /foo.rs
395use crate::{bar::{baz::Sp$0}};
396"#,
397 expect![[r#"
398 st Spam
399 "#]],
400 );
401 }
402
403 #[test]
404 fn completes_enum_variant() { 282 fn completes_enum_variant() {
405 check( 283 check(
406 r#" 284 r#"
@@ -497,22 +375,6 @@ fn foo() { let _ = U::$0 }
497 } 375 }
498 376
499 #[test] 377 #[test]
500 fn completes_use_paths_across_crates() {
501 check(
502 r#"
503//- /main.rs crate:main deps:foo
504use foo::$0;
505
506//- /foo/lib.rs crate:foo
507pub mod bar { pub struct S; }
508"#,
509 expect![[r#"
510 md bar
511 "#]],
512 );
513 }
514
515 #[test]
516 fn completes_trait_associated_method_1() { 378 fn completes_trait_associated_method_1() {
517 check( 379 check(
518 r#" 380 r#"
@@ -714,25 +576,6 @@ impl MyStruct {
714 } 576 }
715 577
716 #[test] 578 #[test]
717 fn test_super_super_completion() {
718 check(
719 r#"
720mod a {
721 const A: usize = 0;
722 mod b {
723 const B: usize = 0;
724 mod c { use super::super::$0 }
725 }
726}
727"#,
728 expect![[r#"
729 md b
730 ct A
731 "#]],
732 );
733 }
734
735 #[test]
736 fn completes_reexported_items_under_correct_name() { 579 fn completes_reexported_items_under_correct_name() {
737 check( 580 check(
738 r#" 581 r#"
diff --git a/crates/ide_completion/src/completions/unqualified_path.rs b/crates/ide_completion/src/completions/unqualified_path.rs
index 2868d9b18..6f96eceb9 100644
--- a/crates/ide_completion/src/completions/unqualified_path.rs
+++ b/crates/ide_completion/src/completions/unqualified_path.rs
@@ -25,7 +25,7 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
25 return; 25 return;
26 } 26 }
27 27
28 if ctx.expects_use_tree() { 28 if ctx.in_use_tree() {
29 // only show modules in a fresh UseTree 29 // only show modules in a fresh UseTree
30 cov_mark::hit!(only_completes_modules_in_import); 30 cov_mark::hit!(only_completes_modules_in_import);
31 ctx.scope.process_all_names(&mut |name, res| { 31 ctx.scope.process_all_names(&mut |name, res| {
@@ -130,22 +130,6 @@ fn foo() {
130 } 130 }
131 131
132 #[test] 132 #[test]
133 fn only_completes_modules_in_import() {
134 cov_mark::check!(only_completes_modules_in_import);
135 check(
136 r#"
137use f$0
138
139struct Foo;
140mod foo {}
141"#,
142 expect![[r#"
143 md foo
144 "#]],
145 );
146 }
147
148 #[test]
149 fn bind_pat_and_path_ignore_at() { 133 fn bind_pat_and_path_ignore_at() {
150 check( 134 check(
151 r#" 135 r#"
@@ -359,22 +343,6 @@ fn _alpha() {}
359 } 343 }
360 344
361 #[test] 345 #[test]
362 fn completes_extern_prelude() {
363 check(
364 r#"
365//- /lib.rs crate:main deps:other_crate
366use $0;
367
368//- /other_crate/lib.rs crate:other_crate
369// nothing here
370"#,
371 expect![[r#"
372 md other_crate
373 "#]],
374 );
375 }
376
377 #[test]
378 fn completes_module_items_in_nested_modules() { 346 fn completes_module_items_in_nested_modules() {
379 check( 347 check(
380 r#" 348 r#"