aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-06-16 20:45:02 +0100
committerLukas Wirth <[email protected]>2021-06-16 20:51:52 +0100
commitaa644b55859c6b5c6695a5d4fb35d1b6efbbebcc (patch)
tree47119538effd381ecd8e15d422103512f2b47406 /crates/ide_completion
parent11115ebad8d0cb367478a4f154abe08c0c25aa95 (diff)
Move test_utils into tests module
Diffstat (limited to 'crates/ide_completion')
-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.rs4
-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.rs6
-rw-r--r--crates/ide_completion/src/completions/postfix.rs4
-rw-r--r--crates/ide_completion/src/completions/qualified_path.rs6
-rw-r--r--crates/ide_completion/src/completions/record.rs8
-rw-r--r--crates/ide_completion/src/completions/snippet.rs4
-rw-r--r--crates/ide_completion/src/completions/trait_impl.rs4
-rw-r--r--crates/ide_completion/src/completions/unqualified_path.rs5
-rw-r--r--crates/ide_completion/src/context.rs2
-rw-r--r--crates/ide_completion/src/lib.rs2
-rw-r--r--crates/ide_completion/src/patterns.rs4
-rw-r--r--crates/ide_completion/src/render.rs2
-rw-r--r--crates/ide_completion/src/render/enum_variant.rs2
-rw-r--r--crates/ide_completion/src/render/function.rs2
-rw-r--r--crates/ide_completion/src/render/macro_.rs2
-rw-r--r--crates/ide_completion/src/test_utils.rs154
-rw-r--r--crates/ide_completion/src/tests.rs158
-rw-r--r--crates/ide_completion/src/tests/item_list.rs32
26 files changed, 194 insertions, 238 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 0a3df79d4..73bbc4345 100644
--- a/crates/ide_completion/src/completions/keyword.rs
+++ b/crates/ide_completion/src/completions/keyword.rs
@@ -190,12 +190,12 @@ mod tests {
190 use expect_test::{expect, Expect}; 190 use expect_test::{expect, Expect};
191 191
192 use crate::{ 192 use crate::{
193 test_utils::{check_edit, completion_list}, 193 tests::{check_edit, filtered_completion_list},
194 CompletionKind, 194 CompletionKind,
195 }; 195 };
196 196
197 fn check(ra_fixture: &str, expect: Expect) { 197 fn check(ra_fixture: &str, expect: Expect) {
198 let actual = completion_list(ra_fixture, CompletionKind::Keyword); 198 let actual = filtered_completion_list(ra_fixture, CompletionKind::Keyword);
199 expect.assert_eq(&actual) 199 expect.assert_eq(&actual)
200 } 200 }
201 201
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 e45b2a1ea..efe3c957a 100644
--- a/crates/ide_completion/src/completions/pattern.rs
+++ b/crates/ide_completion/src/completions/pattern.rs
@@ -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 165b9e6a5..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
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 d142265e0..81ddfa34f 100644
--- a/crates/ide_completion/src/completions/snippet.rs
+++ b/crates/ide_completion/src/completions/snippet.rs
@@ -87,10 +87,10 @@ fn ${1:feature}() {
87mod tests { 87mod tests {
88 use expect_test::{expect, Expect}; 88 use expect_test::{expect, Expect};
89 89
90 use crate::{test_utils::completion_list, CompletionKind}; 90 use crate::{tests::filtered_completion_list, CompletionKind};
91 91
92 fn check(ra_fixture: &str, expect: Expect) { 92 fn check(ra_fixture: &str, expect: Expect) {
93 let actual = completion_list(ra_fixture, CompletionKind::Snippet); 93 let actual = filtered_completion_list(ra_fixture, CompletionKind::Snippet);
94 expect.assert_eq(&actual) 94 expect.assert_eq(&actual)
95 } 95 }
96 96
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 3910de2c4..2868d9b18 100644
--- a/crates/ide_completion/src/completions/unqualified_path.rs
+++ b/crates/ide_completion/src/completions/unqualified_path.rs
@@ -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
diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs
index 441c080b1..121909857 100644
--- a/crates/ide_completion/src/context.rs
+++ b/crates/ide_completion/src/context.rs
@@ -695,7 +695,7 @@ mod tests {
695 use expect_test::{expect, Expect}; 695 use expect_test::{expect, Expect};
696 use hir::HirDisplay; 696 use hir::HirDisplay;
697 697
698 use crate::test_utils::{position, TEST_CONFIG}; 698 use crate::tests::{position, TEST_CONFIG};
699 699
700 use super::CompletionContext; 700 use super::CompletionContext;
701 701
diff --git a/crates/ide_completion/src/lib.rs b/crates/ide_completion/src/lib.rs
index be6442426..bf73818dc 100644
--- a/crates/ide_completion/src/lib.rs
+++ b/crates/ide_completion/src/lib.rs
@@ -9,8 +9,6 @@ mod render;
9 9
10#[cfg(test)] 10#[cfg(test)]
11mod tests; 11mod tests;
12#[cfg(test)]
13mod test_utils;
14 12
15use completions::flyimport::position_for_import; 13use completions::flyimport::position_for_import;
16use ide_db::{ 14use ide_db::{
diff --git a/crates/ide_completion/src/patterns.rs b/crates/ide_completion/src/patterns.rs
index 02cfe91e1..62e4334de 100644
--- a/crates/ide_completion/src/patterns.rs
+++ b/crates/ide_completion/src/patterns.rs
@@ -11,7 +11,7 @@ use syntax::{
11}; 11};
12 12
13#[cfg(test)] 13#[cfg(test)]
14use crate::test_utils::{check_pattern_is_applicable, check_pattern_is_not_applicable}; 14use crate::tests::{check_pattern_is_applicable, check_pattern_is_not_applicable};
15 15
16/// Immediate previous node to what we are completing. 16/// Immediate previous node to what we are completing.
17#[derive(Copy, Clone, Debug, PartialEq, Eq)] 17#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -324,7 +324,7 @@ fn previous_non_trivia_token(token: SyntaxToken) -> Option<SyntaxToken> {
324mod tests { 324mod tests {
325 use syntax::algo::find_node_at_offset; 325 use syntax::algo::find_node_at_offset;
326 326
327 use crate::test_utils::position; 327 use crate::tests::position;
328 328
329 use super::*; 329 use super::*;
330 330
diff --git a/crates/ide_completion/src/render.rs b/crates/ide_completion/src/render.rs
index 2bd2c44d0..4b55f7504 100644
--- a/crates/ide_completion/src/render.rs
+++ b/crates/ide_completion/src/render.rs
@@ -335,7 +335,7 @@ mod tests {
335 335
336 use crate::{ 336 use crate::{
337 item::CompletionRelevanceTypeMatch, 337 item::CompletionRelevanceTypeMatch,
338 test_utils::{check_edit, do_completion, get_all_items, TEST_CONFIG}, 338 tests::{check_edit, do_completion, get_all_items, TEST_CONFIG},
339 CompletionKind, CompletionRelevance, 339 CompletionKind, CompletionRelevance,
340 }; 340 };
341 341
diff --git a/crates/ide_completion/src/render/enum_variant.rs b/crates/ide_completion/src/render/enum_variant.rs
index 28f056e77..91dc178f3 100644
--- a/crates/ide_completion/src/render/enum_variant.rs
+++ b/crates/ide_completion/src/render/enum_variant.rs
@@ -121,7 +121,7 @@ impl<'a> EnumRender<'a> {
121 121
122#[cfg(test)] 122#[cfg(test)]
123mod tests { 123mod tests {
124 use crate::test_utils::check_edit; 124 use crate::tests::check_edit;
125 125
126 #[test] 126 #[test]
127 fn inserts_parens_for_tuple_enums() { 127 fn inserts_parens_for_tuple_enums() {
diff --git a/crates/ide_completion/src/render/function.rs b/crates/ide_completion/src/render/function.rs
index 1357b9f4a..19f2c86e9 100644
--- a/crates/ide_completion/src/render/function.rs
+++ b/crates/ide_completion/src/render/function.rs
@@ -191,7 +191,7 @@ impl<'a> FunctionRender<'a> {
191#[cfg(test)] 191#[cfg(test)]
192mod tests { 192mod tests {
193 use crate::{ 193 use crate::{
194 test_utils::{check_edit, check_edit_with_config, TEST_CONFIG}, 194 tests::{check_edit, check_edit_with_config, TEST_CONFIG},
195 CompletionConfig, 195 CompletionConfig,
196 }; 196 };
197 197
diff --git a/crates/ide_completion/src/render/macro_.rs b/crates/ide_completion/src/render/macro_.rs
index 3a7238bb8..d5a1f45d3 100644
--- a/crates/ide_completion/src/render/macro_.rs
+++ b/crates/ide_completion/src/render/macro_.rs
@@ -133,7 +133,7 @@ fn guess_macro_braces(macro_name: &str, docs: &str) -> (&'static str, &'static s
133 133
134#[cfg(test)] 134#[cfg(test)]
135mod tests { 135mod tests {
136 use crate::test_utils::check_edit; 136 use crate::tests::check_edit;
137 137
138 #[test] 138 #[test]
139 fn dont_insert_macro_call_parens_unncessary() { 139 fn dont_insert_macro_call_parens_unncessary() {
diff --git a/crates/ide_completion/src/test_utils.rs b/crates/ide_completion/src/test_utils.rs
deleted file mode 100644
index b0a4b2026..000000000
--- a/crates/ide_completion/src/test_utils.rs
+++ /dev/null
@@ -1,154 +0,0 @@
1//! Runs completion for testing purposes.
2
3use hir::{PrefixKind, Semantics};
4use ide_db::{
5 base_db::{fixture::ChangeFixture, FileLoader, FilePosition},
6 helpers::{
7 insert_use::{ImportGranularity, InsertUseConfig},
8 SnippetCap,
9 },
10 RootDatabase,
11};
12use itertools::Itertools;
13use stdx::{format_to, trim_indent};
14use syntax::{AstNode, NodeOrToken, SyntaxElement};
15use test_utils::assert_eq_text;
16
17use crate::{item::CompletionKind, CompletionConfig, CompletionItem};
18
19pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
20 enable_postfix_completions: true,
21 enable_imports_on_the_fly: true,
22 enable_self_on_the_fly: true,
23 add_call_parenthesis: true,
24 add_call_argument_snippets: true,
25 snippet_cap: SnippetCap::new(true),
26 insert_use: InsertUseConfig {
27 granularity: ImportGranularity::Crate,
28 prefix_kind: PrefixKind::Plain,
29 enforce_granularity: true,
30 group: true,
31 },
32};
33
34/// Creates analysis from a multi-file fixture, returns positions marked with $0.
35pub(crate) fn position(ra_fixture: &str) -> (RootDatabase, FilePosition) {
36 let change_fixture = ChangeFixture::parse(ra_fixture);
37 let mut database = RootDatabase::default();
38 database.apply_change(change_fixture.change);
39 let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
40 let offset = range_or_offset.expect_offset();
41 (database, FilePosition { file_id, offset })
42}
43
44pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> {
45 do_completion_with_config(TEST_CONFIG, code, kind)
46}
47
48pub(crate) fn do_completion_with_config(
49 config: CompletionConfig,
50 code: &str,
51 kind: CompletionKind,
52) -> Vec<CompletionItem> {
53 get_all_items(config, code)
54 .into_iter()
55 .filter(|c| c.completion_kind == kind)
56 .sorted_by(|l, r| l.label().cmp(r.label()))
57 .collect()
58}
59
60pub(crate) fn completion_list(code: &str, kind: CompletionKind) -> String {
61 completion_list_with_config(TEST_CONFIG, code, kind)
62}
63
64pub(crate) fn completion_list_with_config(
65 config: CompletionConfig,
66 code: &str,
67 kind: CompletionKind,
68) -> String {
69 let kind_completions: Vec<CompletionItem> =
70 get_all_items(config, code).into_iter().filter(|c| c.completion_kind == kind).collect();
71 let label_width = kind_completions
72 .iter()
73 .map(|it| monospace_width(it.label()))
74 .max()
75 .unwrap_or_default()
76 .min(16);
77 kind_completions
78 .into_iter()
79 .map(|it| {
80 let tag = it.kind().unwrap().tag();
81 let var_name = format!("{} {}", tag, it.label());
82 let mut buf = var_name;
83 if let Some(detail) = it.detail() {
84 let width = label_width.saturating_sub(monospace_width(it.label()));
85 format_to!(buf, "{:width$} {}", "", detail, width = width);
86 }
87 if it.deprecated() {
88 format_to!(buf, " DEPRECATED");
89 }
90 format_to!(buf, "\n");
91 buf
92 })
93 .collect()
94}
95
96fn monospace_width(s: &str) -> usize {
97 s.chars().count()
98}
99
100pub(crate) fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
101 check_edit_with_config(TEST_CONFIG, what, ra_fixture_before, ra_fixture_after)
102}
103
104pub(crate) fn check_edit_with_config(
105 config: CompletionConfig,
106 what: &str,
107 ra_fixture_before: &str,
108 ra_fixture_after: &str,
109) {
110 let ra_fixture_after = trim_indent(ra_fixture_after);
111 let (db, position) = position(ra_fixture_before);
112 let completions: Vec<CompletionItem> =
113 crate::completions(&db, &config, position).unwrap().into();
114 let (completion,) = completions
115 .iter()
116 .filter(|it| it.lookup() == what)
117 .collect_tuple()
118 .unwrap_or_else(|| panic!("can't find {:?} completion in {:#?}", what, completions));
119 let mut actual = db.file_text(position.file_id).to_string();
120
121 let mut combined_edit = completion.text_edit().to_owned();
122 if let Some(import_text_edit) =
123 completion.import_to_add().and_then(|edit| edit.to_text_edit(config.insert_use))
124 {
125 combined_edit.union(import_text_edit).expect(
126 "Failed to apply completion resolve changes: change ranges overlap, but should not",
127 )
128 }
129
130 combined_edit.apply(&mut actual);
131 assert_eq_text!(&ra_fixture_after, &actual)
132}
133
134pub(crate) fn check_pattern_is_applicable(code: &str, check: impl FnOnce(SyntaxElement) -> bool) {
135 let (db, pos) = position(code);
136
137 let sema = Semantics::new(&db);
138 let original_file = sema.parse(pos.file_id);
139 let token = original_file.syntax().token_at_offset(pos.offset).left_biased().unwrap();
140 assert!(check(NodeOrToken::Token(token)));
141}
142
143pub(crate) fn check_pattern_is_not_applicable(code: &str, check: fn(SyntaxElement) -> bool) {
144 let (db, pos) = position(code);
145 let sema = Semantics::new(&db);
146 let original_file = sema.parse(pos.file_id);
147 let token = original_file.syntax().token_at_offset(pos.offset).left_biased().unwrap();
148 assert!(!check(NodeOrToken::Token(token)));
149}
150
151pub(crate) fn get_all_items(config: CompletionConfig, code: &str) -> Vec<CompletionItem> {
152 let (db, position) = position(code);
153 crate::completions(&db, &config, position).unwrap().into()
154}
diff --git a/crates/ide_completion/src/tests.rs b/crates/ide_completion/src/tests.rs
index 2205603fa..1495924ea 100644
--- a/crates/ide_completion/src/tests.rs
+++ b/crates/ide_completion/src/tests.rs
@@ -1,11 +1,34 @@
1mod item_list; 1mod item_list;
2 2
3use expect_test::Expect; 3use hir::{PrefixKind, Semantics};
4use stdx::format_to; 4use ide_db::{
5 base_db::{fixture::ChangeFixture, FileLoader, FilePosition},
6 helpers::{
7 insert_use::{ImportGranularity, InsertUseConfig},
8 SnippetCap,
9 },
10 RootDatabase,
11};
12use itertools::Itertools;
13use stdx::{format_to, trim_indent};
14use syntax::{AstNode, NodeOrToken, SyntaxElement};
15use test_utils::assert_eq_text;
16
17use crate::{item::CompletionKind, CompletionConfig, CompletionItem};
5 18
6use crate::{ 19pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
7 test_utils::{self, get_all_items, TEST_CONFIG}, 20 enable_postfix_completions: true,
8 CompletionConfig, CompletionItem, 21 enable_imports_on_the_fly: true,
22 enable_self_on_the_fly: true,
23 add_call_parenthesis: true,
24 add_call_argument_snippets: true,
25 snippet_cap: SnippetCap::new(true),
26 insert_use: InsertUseConfig {
27 granularity: ImportGranularity::Crate,
28 prefix_kind: PrefixKind::Plain,
29 enforce_granularity: true,
30 group: true,
31 },
9}; 32};
10 33
11fn completion_list(code: &str) -> String { 34fn completion_list(code: &str) -> String {
@@ -13,18 +36,56 @@ fn completion_list(code: &str) -> String {
13} 36}
14 37
15fn completion_list_with_config(config: CompletionConfig, code: &str) -> String { 38fn completion_list_with_config(config: CompletionConfig, code: &str) -> String {
39 render_completion_list(get_all_items(config, code))
40}
41
42/// Creates analysis from a multi-file fixture, returns positions marked with $0.
43pub(crate) fn position(ra_fixture: &str) -> (RootDatabase, FilePosition) {
44 let change_fixture = ChangeFixture::parse(ra_fixture);
45 let mut database = RootDatabase::default();
46 database.apply_change(change_fixture.change);
47 let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
48 let offset = range_or_offset.expect_offset();
49 (database, FilePosition { file_id, offset })
50}
51
52pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> {
53 do_completion_with_config(TEST_CONFIG, code, kind)
54}
55
56pub(crate) fn do_completion_with_config(
57 config: CompletionConfig,
58 code: &str,
59 kind: CompletionKind,
60) -> Vec<CompletionItem> {
61 get_all_items(config, code)
62 .into_iter()
63 .filter(|c| c.completion_kind == kind)
64 .sorted_by(|l, r| l.label().cmp(r.label()))
65 .collect()
66}
67
68pub(crate) fn filtered_completion_list(code: &str, kind: CompletionKind) -> String {
69 filtered_completion_list_with_config(TEST_CONFIG, code, kind)
70}
71
72pub(crate) fn filtered_completion_list_with_config(
73 config: CompletionConfig,
74 code: &str,
75 kind: CompletionKind,
76) -> String {
77 let kind_completions: Vec<CompletionItem> =
78 get_all_items(config, code).into_iter().filter(|c| c.completion_kind == kind).collect();
79 render_completion_list(kind_completions)
80}
81
82fn render_completion_list(completions: Vec<CompletionItem>) -> String {
16 fn monospace_width(s: &str) -> usize { 83 fn monospace_width(s: &str) -> usize {
17 s.chars().count() 84 s.chars().count()
18 } 85 }
19 86 let label_width =
20 let kind_completions: Vec<CompletionItem> = get_all_items(config, code).into_iter().collect(); 87 completions.iter().map(|it| monospace_width(it.label())).max().unwrap_or_default().min(16);
21 let label_width = kind_completions 88 completions
22 .iter()
23 .map(|it| monospace_width(it.label()))
24 .max()
25 .unwrap_or_default()
26 .min(16);
27 kind_completions
28 .into_iter() 89 .into_iter()
29 .map(|it| { 90 .map(|it| {
30 let tag = it.kind().unwrap().tag(); 91 let tag = it.kind().unwrap().tag();
@@ -43,23 +104,64 @@ fn completion_list_with_config(config: CompletionConfig, code: &str) -> String {
43 .collect() 104 .collect()
44} 105}
45 106
46fn check(ra_fixture: &str, expect: Expect) { 107pub(crate) fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
47 let base = r#"#[rustc_builtin_macro] 108 check_edit_with_config(TEST_CONFIG, what, ra_fixture_before, ra_fixture_after)
48pub macro Clone {} 109}
49enum Enum { Variant } 110
50struct Struct {} 111pub(crate) fn check_edit_with_config(
51#[macro_export] 112 config: CompletionConfig,
52macro_rules! foo {} 113 what: &str,
53mod bar {} 114 ra_fixture_before: &str,
54const CONST: () = (); 115 ra_fixture_after: &str,
55trait Trait {} 116) {
56"#; 117 let ra_fixture_after = trim_indent(ra_fixture_after);
57 let actual = completion_list(&format!("{}{}", base, ra_fixture)); 118 let (db, position) = position(ra_fixture_before);
58 expect.assert_eq(&actual) 119 let completions: Vec<CompletionItem> =
120 crate::completions(&db, &config, position).unwrap().into();
121 let (completion,) = completions
122 .iter()
123 .filter(|it| it.lookup() == what)
124 .collect_tuple()
125 .unwrap_or_else(|| panic!("can't find {:?} completion in {:#?}", what, completions));
126 let mut actual = db.file_text(position.file_id).to_string();
127
128 let mut combined_edit = completion.text_edit().to_owned();
129 if let Some(import_text_edit) =
130 completion.import_to_add().and_then(|edit| edit.to_text_edit(config.insert_use))
131 {
132 combined_edit.union(import_text_edit).expect(
133 "Failed to apply completion resolve changes: change ranges overlap, but should not",
134 )
135 }
136
137 combined_edit.apply(&mut actual);
138 assert_eq_text!(&ra_fixture_after, &actual)
139}
140
141pub(crate) fn check_pattern_is_applicable(code: &str, check: impl FnOnce(SyntaxElement) -> bool) {
142 let (db, pos) = position(code);
143
144 let sema = Semantics::new(&db);
145 let original_file = sema.parse(pos.file_id);
146 let token = original_file.syntax().token_at_offset(pos.offset).left_biased().unwrap();
147 assert!(check(NodeOrToken::Token(token)));
148}
149
150pub(crate) fn check_pattern_is_not_applicable(code: &str, check: fn(SyntaxElement) -> bool) {
151 let (db, pos) = position(code);
152 let sema = Semantics::new(&db);
153 let original_file = sema.parse(pos.file_id);
154 let token = original_file.syntax().token_at_offset(pos.offset).left_biased().unwrap();
155 assert!(!check(NodeOrToken::Token(token)));
156}
157
158pub(crate) fn get_all_items(config: CompletionConfig, code: &str) -> Vec<CompletionItem> {
159 let (db, position) = position(code);
160 crate::completions(&db, &config, position).unwrap().into()
59} 161}
60 162
61fn check_no_completion(ra_fixture: &str) { 163fn check_no_completion(ra_fixture: &str) {
62 let (db, position) = test_utils::position(ra_fixture); 164 let (db, position) = position(ra_fixture);
63 165
64 assert!( 166 assert!(
65 crate::completions(&db, &TEST_CONFIG, position).is_none(), 167 crate::completions(&db, &TEST_CONFIG, position).is_none(),
diff --git a/crates/ide_completion/src/tests/item_list.rs b/crates/ide_completion/src/tests/item_list.rs
index 33b23b8b4..7c124ac37 100644
--- a/crates/ide_completion/src/tests/item_list.rs
+++ b/crates/ide_completion/src/tests/item_list.rs
@@ -1,14 +1,26 @@
1use expect_test::expect; 1use expect_test::{expect, Expect};
2 2
3use crate::tests::check; 3use crate::tests::completion_list;
4
5fn check(ra_fixture: &str, expect: Expect) {
6 let base = r#"#[rustc_builtin_macro]
7pub macro Clone {}
8enum Enum { Variant }
9struct Struct {}
10#[macro_export]
11macro_rules! foo {}
12mod bar {}
13const CONST: () = ();
14trait Trait {}
15"#;
16 let actual = completion_list(&format!("{}{}", base, ra_fixture));
17 expect.assert_eq(&actual)
18}
4 19
5#[test] 20#[test]
6fn in_mod_item_list() { 21fn in_mod_item_list() {
7 check( 22 check(
8 r#"mod tests { 23 r#"mod tests { $0 }"#,
9 $0
10}
11"#,
12 expect![[r##" 24 expect![[r##"
13 kw pub(crate) 25 kw pub(crate)
14 kw pub 26 kw pub
@@ -164,9 +176,7 @@ fn after_visibility_unsafe() {
164#[test] 176#[test]
165fn in_impl_assoc_item_list() { 177fn in_impl_assoc_item_list() {
166 check( 178 check(
167 r#"impl Struct { 179 r#"impl Struct { $0 }"#,
168 $0
169}"#,
170 expect![[r##" 180 expect![[r##"
171 kw pub(crate) 181 kw pub(crate)
172 kw pub 182 kw pub
@@ -184,9 +194,7 @@ fn in_impl_assoc_item_list() {
184#[test] 194#[test]
185fn in_impl_assoc_item_list_after_attr() { 195fn in_impl_assoc_item_list_after_attr() {
186 check( 196 check(
187 r#"impl Struct { 197 r#"impl Struct { #[attr] $0 }"#,
188 #[attr] $0
189}"#,
190 expect![[r#" 198 expect![[r#"
191 kw pub(crate) 199 kw pub(crate)
192 kw pub 200 kw pub