aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion/src/completions
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-06-16 20:53:43 +0100
committerGitHub <[email protected]>2021-06-16 20:53:43 +0100
commitd6b8af44829521a9f925c4d87599efa3fef38edc (patch)
tree47119538effd381ecd8e15d422103512f2b47406 /crates/ide_completion/src/completions
parentf38770cd2606148bfe764351849ea7ebea45132c (diff)
parentaa644b55859c6b5c6695a5d4fb35d1b6efbbebcc (diff)
Merge #9301
9301: internal: Start refactoring ide_completion tests r=Veykril a=Veykril Our current completion test infra resovles around usually just checking a specific `CompletionKind` which is suboptimal. We only see what we want to see in tests with this causing us to miss a lot of incorrect completions we are doing. Instead we should test for different cursor locations for all kinds(sans the magic kind maybe? not sure yet). This way we will also see potential duplicate completions that merely different in their kind. Also since most completion submodules complete things in tests of other modules due to the tests overlapping it makes more sense to group these tests differently which implies moving them to a new module. Exceptions for this might be stuff like attribute completion as these cannot currently interfere. I only wrote a few tests to check for completions in `ItemList` position so far and I already found a few incorrect/irrelevant completions as these haven't been tested properly due to them being hidden by the `CompletionKind` filtering. I think `CompletionKind` doesn't really seem to be beneficial to me as to I can't think of a occasion where we would want to only check a specific completion kind. Co-authored-by: Lukas Wirth <[email protected]>
Diffstat (limited to 'crates/ide_completion/src/completions')
-rw-r--r--crates/ide_completion/src/completions/attribute.rs4
-rw-r--r--crates/ide_completion/src/completions/attribute/derive.rs4
-rw-r--r--crates/ide_completion/src/completions/attribute/lint.rs2
-rw-r--r--crates/ide_completion/src/completions/dot.rs4
-rw-r--r--crates/ide_completion/src/completions/flyimport.rs4
-rw-r--r--crates/ide_completion/src/completions/fn_param.rs4
-rw-r--r--crates/ide_completion/src/completions/keyword.rs109
-rw-r--r--crates/ide_completion/src/completions/lifetime.rs5
-rw-r--r--crates/ide_completion/src/completions/mod_.rs4
-rw-r--r--crates/ide_completion/src/completions/pattern.rs8
-rw-r--r--crates/ide_completion/src/completions/postfix.rs4
-rw-r--r--crates/ide_completion/src/completions/qualified_path.rs24
-rw-r--r--crates/ide_completion/src/completions/record.rs8
-rw-r--r--crates/ide_completion/src/completions/snippet.rs28
-rw-r--r--crates/ide_completion/src/completions/trait_impl.rs4
-rw-r--r--crates/ide_completion/src/completions/unqualified_path.rs55
16 files changed, 60 insertions, 211 deletions
diff --git a/crates/ide_completion/src/completions/attribute.rs b/crates/ide_completion/src/completions/attribute.rs
index 6df569c2a..3866c5917 100644
--- a/crates/ide_completion/src/completions/attribute.rs
+++ b/crates/ide_completion/src/completions/attribute.rs
@@ -322,7 +322,7 @@ mod tests {
322 322
323 use expect_test::{expect, Expect}; 323 use expect_test::{expect, Expect};
324 324
325 use crate::{test_utils::completion_list, CompletionKind}; 325 use crate::{tests::filtered_completion_list, CompletionKind};
326 326
327 #[test] 327 #[test]
328 fn attributes_are_sorted() { 328 fn attributes_are_sorted() {
@@ -341,7 +341,7 @@ mod tests {
341 } 341 }
342 342
343 fn check(ra_fixture: &str, expect: Expect) { 343 fn check(ra_fixture: &str, expect: Expect) {
344 let actual = completion_list(ra_fixture, CompletionKind::Attribute); 344 let actual = filtered_completion_list(ra_fixture, CompletionKind::Attribute);
345 expect.assert_eq(&actual); 345 expect.assert_eq(&actual);
346 } 346 }
347 347
diff --git a/crates/ide_completion/src/completions/attribute/derive.rs b/crates/ide_completion/src/completions/attribute/derive.rs
index 20bbbba46..5201095e8 100644
--- a/crates/ide_completion/src/completions/attribute/derive.rs
+++ b/crates/ide_completion/src/completions/attribute/derive.rs
@@ -82,7 +82,7 @@ const DEFAULT_DERIVE_COMPLETIONS: &[DeriveDependencies] = &[
82mod tests { 82mod tests {
83 use expect_test::{expect, Expect}; 83 use expect_test::{expect, Expect};
84 84
85 use crate::{test_utils::completion_list, CompletionKind}; 85 use crate::{tests::filtered_completion_list, CompletionKind};
86 86
87 fn check(ra_fixture: &str, expect: Expect) { 87 fn check(ra_fixture: &str, expect: Expect) {
88 let builtin_derives = r#" 88 let builtin_derives = r#"
@@ -106,7 +106,7 @@ pub macro PartialOrd {}
106pub macro Ord {} 106pub macro Ord {}
107 107
108"#; 108"#;
109 let actual = completion_list( 109 let actual = filtered_completion_list(
110 &format!("{} {}", builtin_derives, ra_fixture), 110 &format!("{} {}", builtin_derives, ra_fixture),
111 CompletionKind::Attribute, 111 CompletionKind::Attribute,
112 ); 112 );
diff --git a/crates/ide_completion/src/completions/attribute/lint.rs b/crates/ide_completion/src/completions/attribute/lint.rs
index ca99e9759..4812b075c 100644
--- a/crates/ide_completion/src/completions/attribute/lint.rs
+++ b/crates/ide_completion/src/completions/attribute/lint.rs
@@ -34,7 +34,7 @@ pub(super) fn complete_lint(
34#[cfg(test)] 34#[cfg(test)]
35mod tests { 35mod tests {
36 36
37 use crate::test_utils::check_edit; 37 use crate::tests::check_edit;
38 38
39 #[test] 39 #[test]
40 fn check_empty() { 40 fn check_empty() {
diff --git a/crates/ide_completion/src/completions/dot.rs b/crates/ide_completion/src/completions/dot.rs
index 9552875c1..7f75d4298 100644
--- a/crates/ide_completion/src/completions/dot.rs
+++ b/crates/ide_completion/src/completions/dot.rs
@@ -101,10 +101,10 @@ fn complete_methods(
101mod tests { 101mod tests {
102 use expect_test::{expect, Expect}; 102 use expect_test::{expect, Expect};
103 103
104 use crate::{test_utils::completion_list, CompletionKind}; 104 use crate::{tests::filtered_completion_list, CompletionKind};
105 105
106 fn check(ra_fixture: &str, expect: Expect) { 106 fn check(ra_fixture: &str, expect: Expect) {
107 let actual = completion_list(ra_fixture, CompletionKind::Reference); 107 let actual = filtered_completion_list(ra_fixture, CompletionKind::Reference);
108 expect.assert_eq(&actual); 108 expect.assert_eq(&actual);
109 } 109 }
110 110
diff --git a/crates/ide_completion/src/completions/flyimport.rs b/crates/ide_completion/src/completions/flyimport.rs
index 30b8d44bd..4604feb5d 100644
--- a/crates/ide_completion/src/completions/flyimport.rs
+++ b/crates/ide_completion/src/completions/flyimport.rs
@@ -227,11 +227,11 @@ mod tests {
227 227
228 use crate::{ 228 use crate::{
229 item::CompletionKind, 229 item::CompletionKind,
230 test_utils::{check_edit, check_edit_with_config, completion_list, TEST_CONFIG}, 230 tests::{check_edit, check_edit_with_config, filtered_completion_list, TEST_CONFIG},
231 }; 231 };
232 232
233 fn check(ra_fixture: &str, expect: Expect) { 233 fn check(ra_fixture: &str, expect: Expect) {
234 let actual = completion_list(ra_fixture, CompletionKind::Magic); 234 let actual = filtered_completion_list(ra_fixture, CompletionKind::Magic);
235 expect.assert_eq(&actual); 235 expect.assert_eq(&actual);
236 } 236 }
237 237
diff --git a/crates/ide_completion/src/completions/fn_param.rs b/crates/ide_completion/src/completions/fn_param.rs
index cb90e8a3e..c9f0e2473 100644
--- a/crates/ide_completion/src/completions/fn_param.rs
+++ b/crates/ide_completion/src/completions/fn_param.rs
@@ -64,10 +64,10 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
64mod tests { 64mod tests {
65 use expect_test::{expect, Expect}; 65 use expect_test::{expect, Expect};
66 66
67 use crate::{test_utils::completion_list, CompletionKind}; 67 use crate::{tests::filtered_completion_list, CompletionKind};
68 68
69 fn check(ra_fixture: &str, expect: Expect) { 69 fn check(ra_fixture: &str, expect: Expect) {
70 let actual = completion_list(ra_fixture, CompletionKind::Magic); 70 let actual = filtered_completion_list(ra_fixture, CompletionKind::Magic);
71 expect.assert_eq(&actual); 71 expect.assert_eq(&actual);
72 } 72 }
73 73
diff --git a/crates/ide_completion/src/completions/keyword.rs b/crates/ide_completion/src/completions/keyword.rs
index 0fccbeccf..73bbc4345 100644
--- a/crates/ide_completion/src/completions/keyword.rs
+++ b/crates/ide_completion/src/completions/keyword.rs
@@ -75,7 +75,9 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
75 return; 75 return;
76 } 76 }
77 77
78 if expects_item || ctx.expects_non_trait_assoc_item() || ctx.expect_record_field() { 78 if !ctx.has_visibility_prev_sibling()
79 && (expects_item || ctx.expects_non_trait_assoc_item() || ctx.expect_record_field())
80 {
79 add_keyword("pub(crate)", "pub(crate) "); 81 add_keyword("pub(crate)", "pub(crate) ");
80 add_keyword("pub", "pub "); 82 add_keyword("pub", "pub ");
81 } 83 }
@@ -88,11 +90,13 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
88 } 90 }
89 91
90 if expects_item || has_block_expr_parent { 92 if expects_item || has_block_expr_parent {
93 if !ctx.has_visibility_prev_sibling() {
94 add_keyword("impl", "impl $1 {\n $0\n}");
95 add_keyword("extern", "extern $0");
96 }
91 add_keyword("use", "use $0"); 97 add_keyword("use", "use $0");
92 add_keyword("impl", "impl $1 {\n $0\n}");
93 add_keyword("trait", "trait $1 {\n $0\n}"); 98 add_keyword("trait", "trait $1 {\n $0\n}");
94 add_keyword("static", "static $0"); 99 add_keyword("static", "static $0");
95 add_keyword("extern", "extern $0");
96 add_keyword("mod", "mod $0"); 100 add_keyword("mod", "mod $0");
97 } 101 }
98 102
@@ -186,12 +190,12 @@ mod tests {
186 use expect_test::{expect, Expect}; 190 use expect_test::{expect, Expect};
187 191
188 use crate::{ 192 use crate::{
189 test_utils::{check_edit, completion_list}, 193 tests::{check_edit, filtered_completion_list},
190 CompletionKind, 194 CompletionKind,
191 }; 195 };
192 196
193 fn check(ra_fixture: &str, expect: Expect) { 197 fn check(ra_fixture: &str, expect: Expect) {
194 let actual = completion_list(ra_fixture, CompletionKind::Keyword); 198 let actual = filtered_completion_list(ra_fixture, CompletionKind::Keyword);
195 expect.assert_eq(&actual) 199 expect.assert_eq(&actual)
196 } 200 }
197 201
@@ -231,30 +235,6 @@ mod tests {
231 } 235 }
232 236
233 #[test] 237 #[test]
234 fn test_keywords_at_source_file_level() {
235 check(
236 r"m$0",
237 expect![[r#"
238 kw pub(crate)
239 kw pub
240 kw unsafe
241 kw fn
242 kw const
243 kw type
244 kw use
245 kw impl
246 kw trait
247 kw static
248 kw extern
249 kw mod
250 kw enum
251 kw struct
252 kw union
253 "#]],
254 );
255 }
256
257 #[test]
258 fn test_keywords_in_function() { 238 fn test_keywords_in_function() {
259 check( 239 check(
260 r"fn quux() { $0 }", 240 r"fn quux() { $0 }",
@@ -263,11 +243,11 @@ mod tests {
263 kw fn 243 kw fn
264 kw const 244 kw const
265 kw type 245 kw type
266 kw use
267 kw impl 246 kw impl
247 kw extern
248 kw use
268 kw trait 249 kw trait
269 kw static 250 kw static
270 kw extern
271 kw mod 251 kw mod
272 kw match 252 kw match
273 kw while 253 kw while
@@ -291,11 +271,11 @@ mod tests {
291 kw fn 271 kw fn
292 kw const 272 kw const
293 kw type 273 kw type
294 kw use
295 kw impl 274 kw impl
275 kw extern
276 kw use
296 kw trait 277 kw trait
297 kw static 278 kw static
298 kw extern
299 kw mod 279 kw mod
300 kw match 280 kw match
301 kw while 281 kw while
@@ -319,11 +299,11 @@ mod tests {
319 kw fn 299 kw fn
320 kw const 300 kw const
321 kw type 301 kw type
322 kw use
323 kw impl 302 kw impl
303 kw extern
304 kw use
324 kw trait 305 kw trait
325 kw static 306 kw static
326 kw extern
327 kw mod 307 kw mod
328 kw match 308 kw match
329 kw while 309 kw while
@@ -370,49 +350,6 @@ fn quux() -> i32 {
370 } 350 }
371 351
372 #[test] 352 #[test]
373 fn test_keywords_in_trait_def() {
374 check(
375 r"trait My { $0 }",
376 expect![[r#"
377 kw unsafe
378 kw fn
379 kw const
380 kw type
381 "#]],
382 );
383 }
384
385 #[test]
386 fn test_keywords_in_impl_def() {
387 check(
388 r"impl My { $0 }",
389 expect![[r#"
390 kw pub(crate)
391 kw pub
392 kw unsafe
393 kw fn
394 kw const
395 kw type
396 "#]],
397 );
398 }
399
400 #[test]
401 fn test_keywords_in_impl_def_with_attr() {
402 check(
403 r"impl My { #[foo] $0 }",
404 expect![[r#"
405 kw pub(crate)
406 kw pub
407 kw unsafe
408 kw fn
409 kw const
410 kw type
411 "#]],
412 );
413 }
414
415 #[test]
416 fn test_keywords_in_loop() { 353 fn test_keywords_in_loop() {
417 check( 354 check(
418 r"fn my() { loop { $0 } }", 355 r"fn my() { loop { $0 } }",
@@ -421,11 +358,11 @@ fn quux() -> i32 {
421 kw fn 358 kw fn
422 kw const 359 kw const
423 kw type 360 kw type
424 kw use
425 kw impl 361 kw impl
362 kw extern
363 kw use
426 kw trait 364 kw trait
427 kw static 365 kw static
428 kw extern
429 kw mod 366 kw mod
430 kw match 367 kw match
431 kw while 368 kw while
@@ -443,18 +380,6 @@ fn quux() -> i32 {
443 } 380 }
444 381
445 #[test] 382 #[test]
446 fn test_keywords_after_unsafe_in_item_list() {
447 check(
448 r"unsafe $0",
449 expect![[r#"
450 kw fn
451 kw trait
452 kw impl
453 "#]],
454 );
455 }
456
457 #[test]
458 fn test_keywords_after_unsafe_in_block_expr() { 383 fn test_keywords_after_unsafe_in_block_expr() {
459 check( 384 check(
460 r"fn my_fn() { unsafe $0 }", 385 r"fn my_fn() { unsafe $0 }",
diff --git a/crates/ide_completion/src/completions/lifetime.rs b/crates/ide_completion/src/completions/lifetime.rs
index 8ccccb646..36f595164 100644
--- a/crates/ide_completion/src/completions/lifetime.rs
+++ b/crates/ide_completion/src/completions/lifetime.rs
@@ -50,7 +50,7 @@ mod tests {
50 use expect_test::{expect, Expect}; 50 use expect_test::{expect, Expect};
51 51
52 use crate::{ 52 use crate::{
53 test_utils::{check_edit, completion_list_with_config, TEST_CONFIG}, 53 tests::{check_edit, filtered_completion_list_with_config, TEST_CONFIG},
54 CompletionConfig, CompletionKind, 54 CompletionConfig, CompletionKind,
55 }; 55 };
56 56
@@ -59,7 +59,8 @@ mod tests {
59 } 59 }
60 60
61 fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect) { 61 fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect) {
62 let actual = completion_list_with_config(config, ra_fixture, CompletionKind::Reference); 62 let actual =
63 filtered_completion_list_with_config(config, ra_fixture, CompletionKind::Reference);
63 expect.assert_eq(&actual) 64 expect.assert_eq(&actual)
64 } 65 }
65 66
diff --git a/crates/ide_completion/src/completions/mod_.rs b/crates/ide_completion/src/completions/mod_.rs
index 6a5746fb9..5def0d06a 100644
--- a/crates/ide_completion/src/completions/mod_.rs
+++ b/crates/ide_completion/src/completions/mod_.rs
@@ -141,11 +141,11 @@ fn module_chain_to_containing_module_file(
141 141
142#[cfg(test)] 142#[cfg(test)]
143mod tests { 143mod tests {
144 use crate::{test_utils::completion_list, CompletionKind}; 144 use crate::{tests::filtered_completion_list, CompletionKind};
145 use expect_test::{expect, Expect}; 145 use expect_test::{expect, Expect};
146 146
147 fn check(ra_fixture: &str, expect: Expect) { 147 fn check(ra_fixture: &str, expect: Expect) {
148 let actual = completion_list(ra_fixture, CompletionKind::Magic); 148 let actual = filtered_completion_list(ra_fixture, CompletionKind::Magic);
149 expect.assert_eq(&actual); 149 expect.assert_eq(&actual);
150 } 150 }
151 151
diff --git a/crates/ide_completion/src/completions/pattern.rs b/crates/ide_completion/src/completions/pattern.rs
index 1daa8595a..efe3c957a 100644
--- a/crates/ide_completion/src/completions/pattern.rs
+++ b/crates/ide_completion/src/completions/pattern.rs
@@ -13,7 +13,7 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
13 if let Some(hir::Adt::Enum(e)) = 13 if let Some(hir::Adt::Enum(e)) =
14 ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt()) 14 ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt())
15 { 15 {
16 super::complete_enum_variants(acc, ctx, e, |acc, ctx, variant, path| { 16 super::enum_variants_with_paths(acc, ctx, e, |acc, ctx, variant, path| {
17 acc.add_qualified_variant_pat(ctx, variant, path.clone()); 17 acc.add_qualified_variant_pat(ctx, variant, path.clone());
18 acc.add_qualified_enum_variant(ctx, variant, path); 18 acc.add_qualified_enum_variant(ctx, variant, path);
19 }); 19 });
@@ -61,17 +61,17 @@ mod tests {
61 use expect_test::{expect, Expect}; 61 use expect_test::{expect, Expect};
62 62
63 use crate::{ 63 use crate::{
64 test_utils::{check_edit, completion_list}, 64 tests::{check_edit, filtered_completion_list},
65 CompletionKind, 65 CompletionKind,
66 }; 66 };
67 67
68 fn check(ra_fixture: &str, expect: Expect) { 68 fn check(ra_fixture: &str, expect: Expect) {
69 let actual = completion_list(ra_fixture, CompletionKind::Reference); 69 let actual = filtered_completion_list(ra_fixture, CompletionKind::Reference);
70 expect.assert_eq(&actual) 70 expect.assert_eq(&actual)
71 } 71 }
72 72
73 fn check_snippet(ra_fixture: &str, expect: Expect) { 73 fn check_snippet(ra_fixture: &str, expect: Expect) {
74 let actual = completion_list(ra_fixture, CompletionKind::Snippet); 74 let actual = filtered_completion_list(ra_fixture, CompletionKind::Snippet);
75 expect.assert_eq(&actual) 75 expect.assert_eq(&actual)
76 } 76 }
77 77
diff --git a/crates/ide_completion/src/completions/postfix.rs b/crates/ide_completion/src/completions/postfix.rs
index 9f98b21be..c3c7e4589 100644
--- a/crates/ide_completion/src/completions/postfix.rs
+++ b/crates/ide_completion/src/completions/postfix.rs
@@ -307,12 +307,12 @@ mod tests {
307 use expect_test::{expect, Expect}; 307 use expect_test::{expect, Expect};
308 308
309 use crate::{ 309 use crate::{
310 test_utils::{check_edit, completion_list}, 310 tests::{check_edit, filtered_completion_list},
311 CompletionKind, 311 CompletionKind,
312 }; 312 };
313 313
314 fn check(ra_fixture: &str, expect: Expect) { 314 fn check(ra_fixture: &str, expect: Expect) {
315 let actual = completion_list(ra_fixture, CompletionKind::Postfix); 315 let actual = filtered_completion_list(ra_fixture, CompletionKind::Postfix);
316 expect.assert_eq(&actual) 316 expect.assert_eq(&actual)
317 } 317 }
318 318
diff --git a/crates/ide_completion/src/completions/qualified_path.rs b/crates/ide_completion/src/completions/qualified_path.rs
index 1643eeed4..9432caa22 100644
--- a/crates/ide_completion/src/completions/qualified_path.rs
+++ b/crates/ide_completion/src/completions/qualified_path.rs
@@ -198,17 +198,17 @@ mod tests {
198 use expect_test::{expect, Expect}; 198 use expect_test::{expect, Expect};
199 199
200 use crate::{ 200 use crate::{
201 test_utils::{check_edit, completion_list}, 201 tests::{check_edit, filtered_completion_list},
202 CompletionKind, 202 CompletionKind,
203 }; 203 };
204 204
205 fn check(ra_fixture: &str, expect: Expect) { 205 fn check(ra_fixture: &str, expect: Expect) {
206 let actual = completion_list(ra_fixture, CompletionKind::Reference); 206 let actual = filtered_completion_list(ra_fixture, CompletionKind::Reference);
207 expect.assert_eq(&actual); 207 expect.assert_eq(&actual);
208 } 208 }
209 209
210 fn check_builtin(ra_fixture: &str, expect: Expect) { 210 fn check_builtin(ra_fixture: &str, expect: Expect) {
211 let actual = completion_list(ra_fixture, CompletionKind::BuiltinType); 211 let actual = filtered_completion_list(ra_fixture, CompletionKind::BuiltinType);
212 expect.assert_eq(&actual); 212 expect.assert_eq(&actual);
213 } 213 }
214 214
@@ -714,24 +714,6 @@ impl MyStruct {
714 } 714 }
715 715
716 #[test] 716 #[test]
717 fn completes_in_item_list() {
718 check(
719 r#"
720struct MyStruct {}
721#[macro_export]
722macro_rules! foo {}
723mod bar {}
724
725crate::$0
726"#,
727 expect![[r#"
728 md bar
729 ma foo!(…) #[macro_export] macro_rules! foo
730 "#]],
731 )
732 }
733
734 #[test]
735 fn test_super_super_completion() { 717 fn test_super_super_completion() {
736 check( 718 check(
737 r#" 719 r#"
diff --git a/crates/ide_completion/src/completions/record.rs b/crates/ide_completion/src/completions/record.rs
index 0ac47cdbe..47523f72f 100644
--- a/crates/ide_completion/src/completions/record.rs
+++ b/crates/ide_completion/src/completions/record.rs
@@ -51,17 +51,17 @@ mod tests {
51 use ide_db::helpers::FamousDefs; 51 use ide_db::helpers::FamousDefs;
52 52
53 use crate::{ 53 use crate::{
54 test_utils::{self, completion_list}, 54 tests::{self, filtered_completion_list},
55 CompletionKind, 55 CompletionKind,
56 }; 56 };
57 57
58 fn check(ra_fixture: &str, expect: Expect) { 58 fn check(ra_fixture: &str, expect: Expect) {
59 let actual = completion_list(ra_fixture, CompletionKind::Reference); 59 let actual = filtered_completion_list(ra_fixture, CompletionKind::Reference);
60 expect.assert_eq(&actual); 60 expect.assert_eq(&actual);
61 } 61 }
62 62
63 fn check_snippet(ra_fixture: &str, expect: Expect) { 63 fn check_snippet(ra_fixture: &str, expect: Expect) {
64 let actual = completion_list( 64 let actual = filtered_completion_list(
65 &format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE), 65 &format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE),
66 CompletionKind::Snippet, 66 CompletionKind::Snippet,
67 ); 67 );
@@ -69,7 +69,7 @@ mod tests {
69 } 69 }
70 70
71 fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) { 71 fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
72 test_utils::check_edit( 72 tests::check_edit(
73 what, 73 what,
74 &format!( 74 &format!(
75 "//- /main.rs crate:main deps:core{}\n{}", 75 "//- /main.rs crate:main deps:core{}\n{}",
diff --git a/crates/ide_completion/src/completions/snippet.rs b/crates/ide_completion/src/completions/snippet.rs
index b9862de67..81ddfa34f 100644
--- a/crates/ide_completion/src/completions/snippet.rs
+++ b/crates/ide_completion/src/completions/snippet.rs
@@ -1,6 +1,7 @@
1//! This file provides snippet completions, like `pd` => `eprintln!(...)`. 1//! This file provides snippet completions, like `pd` => `eprintln!(...)`.
2 2
3use ide_db::helpers::SnippetCap; 3use ide_db::helpers::SnippetCap;
4use syntax::T;
4 5
5use crate::{ 6use crate::{
6 context::PathCompletionContext, item::Builder, CompletionContext, CompletionItem, 7 context::PathCompletionContext, item::Builder, CompletionContext, CompletionItem,
@@ -35,9 +36,13 @@ pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionConte
35} 36}
36 37
37pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) { 38pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
38 if !ctx.expects_item() { 39 if !ctx.expects_item() || ctx.previous_token_is(T![unsafe]) || ctx.path_qual().is_some() {
39 return; 40 return;
40 } 41 }
42 if ctx.has_visibility_prev_sibling() {
43 return; // technically we could do some of these snippet completions if we were to put the
44 // attributes before the vis node.
45 }
41 let cap = match ctx.config.snippet_cap { 46 let cap = match ctx.config.snippet_cap {
42 Some(it) => it, 47 Some(it) => it,
43 None => return, 48 None => return,
@@ -82,10 +87,10 @@ fn ${1:feature}() {
82mod tests { 87mod tests {
83 use expect_test::{expect, Expect}; 88 use expect_test::{expect, Expect};
84 89
85 use crate::{test_utils::completion_list, CompletionKind}; 90 use crate::{tests::filtered_completion_list, CompletionKind};
86 91
87 fn check(ra_fixture: &str, expect: Expect) { 92 fn check(ra_fixture: &str, expect: Expect) {
88 let actual = completion_list(ra_fixture, CompletionKind::Snippet); 93 let actual = filtered_completion_list(ra_fixture, CompletionKind::Snippet);
89 expect.assert_eq(&actual) 94 expect.assert_eq(&actual)
90 } 95 }
91 96
@@ -105,21 +110,4 @@ mod tests {
105 check(r#"fn foo(x: i32) { ::foo$0 }"#, expect![[""]]); 110 check(r#"fn foo(x: i32) { ::foo$0 }"#, expect![[""]]);
106 check(r#"fn foo(x: i32) { ::$0 }"#, expect![[""]]); 111 check(r#"fn foo(x: i32) { ::$0 }"#, expect![[""]]);
107 } 112 }
108
109 #[test]
110 fn completes_snippets_in_items() {
111 check(
112 r#"
113#[cfg(test)]
114mod tests {
115 $0
116}
117"#,
118 expect![[r#"
119 sn tmod (Test module)
120 sn tfn (Test function)
121 sn macro_rules
122 "#]],
123 )
124 }
125} 113}
diff --git a/crates/ide_completion/src/completions/trait_impl.rs b/crates/ide_completion/src/completions/trait_impl.rs
index a60e5f43c..dc1d198cc 100644
--- a/crates/ide_completion/src/completions/trait_impl.rs
+++ b/crates/ide_completion/src/completions/trait_impl.rs
@@ -246,12 +246,12 @@ mod tests {
246 use expect_test::{expect, Expect}; 246 use expect_test::{expect, Expect};
247 247
248 use crate::{ 248 use crate::{
249 test_utils::{check_edit, completion_list}, 249 tests::{check_edit, filtered_completion_list},
250 CompletionKind, 250 CompletionKind,
251 }; 251 };
252 252
253 fn check(ra_fixture: &str, expect: Expect) { 253 fn check(ra_fixture: &str, expect: Expect) {
254 let actual = completion_list(ra_fixture, CompletionKind::Magic); 254 let actual = filtered_completion_list(ra_fixture, CompletionKind::Magic);
255 expect.assert_eq(&actual) 255 expect.assert_eq(&actual)
256 } 256 }
257 257
diff --git a/crates/ide_completion/src/completions/unqualified_path.rs b/crates/ide_completion/src/completions/unqualified_path.rs
index b5af1c810..2868d9b18 100644
--- a/crates/ide_completion/src/completions/unqualified_path.rs
+++ b/crates/ide_completion/src/completions/unqualified_path.rs
@@ -40,7 +40,7 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
40 if let Some(hir::Adt::Enum(e)) = 40 if let Some(hir::Adt::Enum(e)) =
41 ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt()) 41 ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt())
42 { 42 {
43 super::complete_enum_variants(acc, ctx, e, |acc, ctx, variant, path| { 43 super::enum_variants_with_paths(acc, ctx, e, |acc, ctx, variant, path| {
44 acc.add_qualified_enum_variant(ctx, variant, path) 44 acc.add_qualified_enum_variant(ctx, variant, path)
45 }); 45 });
46 } 46 }
@@ -93,7 +93,7 @@ mod tests {
93 use expect_test::{expect, Expect}; 93 use expect_test::{expect, Expect};
94 94
95 use crate::{ 95 use crate::{
96 test_utils::{check_edit, completion_list_with_config, TEST_CONFIG}, 96 tests::{check_edit, filtered_completion_list_with_config, TEST_CONFIG},
97 CompletionConfig, CompletionKind, 97 CompletionConfig, CompletionKind,
98 }; 98 };
99 99
@@ -102,7 +102,8 @@ mod tests {
102 } 102 }
103 103
104 fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect) { 104 fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect) {
105 let actual = completion_list_with_config(config, ra_fixture, CompletionKind::Reference); 105 let actual =
106 filtered_completion_list_with_config(config, ra_fixture, CompletionKind::Reference);
106 expect.assert_eq(&actual) 107 expect.assert_eq(&actual)
107 } 108 }
108 109
@@ -500,18 +501,6 @@ fn f() {$0}
500 check( 501 check(
501 r#" 502 r#"
502#[rustc_builtin_macro] 503#[rustc_builtin_macro]
503pub macro Clone {}
504
505struct S;
506impl S {
507 $0
508}
509"#,
510 expect![[r#""#]],
511 );
512 check(
513 r#"
514#[rustc_builtin_macro]
515pub macro bench {} 504pub macro bench {}
516 505
517fn f() {$0} 506fn f() {$0}
@@ -773,42 +762,6 @@ impl My$0
773 } 762 }
774 763
775 #[test] 764 #[test]
776 fn completes_in_assoc_item_list() {
777 check(
778 r#"
779macro_rules! foo {}
780mod bar {}
781
782struct MyStruct {}
783impl MyStruct {
784 $0
785}
786"#,
787 expect![[r#"
788 md bar
789 ma foo!(…) macro_rules! foo
790 "#]],
791 )
792 }
793
794 #[test]
795 fn completes_in_item_list() {
796 check(
797 r#"
798struct MyStruct {}
799macro_rules! foo {}
800mod bar {}
801
802$0
803"#,
804 expect![[r#"
805 md bar
806 ma foo!(…) macro_rules! foo
807 "#]],
808 )
809 }
810
811 #[test]
812 fn completes_types_and_const_in_arg_list() { 765 fn completes_types_and_const_in_arg_list() {
813 check( 766 check(
814 r#" 767 r#"