aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/completion
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api/src/completion')
-rw-r--r--crates/ra_ide_api/src/completion/complete_scope.rs468
1 files changed, 225 insertions, 243 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_scope.rs b/crates/ra_ide_api/src/completion/complete_scope.rs
index 07c2e5f94..079ecfacc 100644
--- a/crates/ra_ide_api/src/completion/complete_scope.rs
+++ b/crates/ra_ide_api/src/completion/complete_scope.rs
@@ -131,51 +131,49 @@ mod tests {
131 #[test] 131 #[test]
132 fn completes_bindings_from_let() { 132 fn completes_bindings_from_let() {
133 assert_debug_snapshot_matches!( 133 assert_debug_snapshot_matches!(
134 do_reference_completion( 134 do_reference_completion(
135 r" 135 r"
136 fn quux(x: i32) { 136 fn quux(x: i32) {
137 let y = 92; 137 let y = 92;
138 1 + <|>; 138 1 + <|>;
139 let z = (); 139 let z = ();
140 } 140 }
141 " 141 "
142 ), 142 ),
143 @r###" 143 @r###"[
144 ⋮[ 144 CompletionItem {
145 ⋮ CompletionItem { 145 label: "quux",
146 ⋮ label: "quux", 146 source_range: [91; 91),
147 ⋮ source_range: [91; 91), 147 delete: [91; 91),
148 ⋮ delete: [91; 91), 148 insert: "quux($0)",
149 ⋮ insert: "quux($0)", 149 kind: Function,
150 ⋮ kind: Function, 150 detail: "fn quux(x: i32)",
151 ⋮ detail: "fn quux(x: i32)", 151 },
152 ⋮ }, 152 CompletionItem {
153 ⋮ CompletionItem { 153 label: "x",
154 ⋮ label: "x", 154 source_range: [91; 91),
155 ⋮ source_range: [91; 91), 155 delete: [91; 91),
156 ⋮ delete: [91; 91), 156 insert: "x",
157 ⋮ insert: "x", 157 kind: Binding,
158 ⋮ kind: Binding, 158 detail: "i32",
159 ⋮ detail: "i32", 159 },
160 ⋮ }, 160 CompletionItem {
161 ⋮ CompletionItem { 161 label: "y",
162 ⋮ label: "y", 162 source_range: [91; 91),
163 ⋮ source_range: [91; 91), 163 delete: [91; 91),
164 ⋮ delete: [91; 91), 164 insert: "y",
165 ⋮ insert: "y", 165 kind: Binding,
166 ⋮ kind: Binding, 166 detail: "i32",
167 ⋮ detail: "i32", 167 },
168 ⋮ }, 168]"###
169 ⋮] 169 );
170 "###
171 );
172 } 170 }
173 171
174 #[test] 172 #[test]
175 fn completes_bindings_from_if_let() { 173 fn completes_bindings_from_if_let() {
176 assert_debug_snapshot_matches!( 174 assert_debug_snapshot_matches!(
177 do_reference_completion( 175 do_reference_completion(
178 r" 176 r"
179 fn quux() { 177 fn quux() {
180 if let Some(x) = foo() { 178 if let Some(x) = foo() {
181 let y = 92; 179 let y = 92;
@@ -186,188 +184,178 @@ mod tests {
186 } 184 }
187 } 185 }
188 " 186 "
189 ), 187 ),
190 @r###" 188 @r###"[
191 ⋮[ 189 CompletionItem {
192 ⋮ CompletionItem { 190 label: "a",
193 ⋮ label: "a", 191 source_range: [242; 242),
194 ⋮ source_range: [242; 242), 192 delete: [242; 242),
195 ⋮ delete: [242; 242), 193 insert: "a",
196 ⋮ insert: "a", 194 kind: Binding,
197 ⋮ kind: Binding, 195 },
198 ⋮ }, 196 CompletionItem {
199 ⋮ CompletionItem { 197 label: "b",
200 ⋮ label: "b", 198 source_range: [242; 242),
201 ⋮ source_range: [242; 242), 199 delete: [242; 242),
202 ⋮ delete: [242; 242), 200 insert: "b",
203 ⋮ insert: "b", 201 kind: Binding,
204 ⋮ kind: Binding, 202 detail: "i32",
205 ⋮ detail: "i32", 203 },
206 ⋮ }, 204 CompletionItem {
207 ⋮ CompletionItem { 205 label: "quux",
208 ⋮ label: "quux", 206 source_range: [242; 242),
209 ⋮ source_range: [242; 242), 207 delete: [242; 242),
210 ⋮ delete: [242; 242), 208 insert: "quux()$0",
211 ⋮ insert: "quux()$0", 209 kind: Function,
212 ⋮ kind: Function, 210 detail: "fn quux()",
213 ⋮ detail: "fn quux()", 211 },
214 ⋮ }, 212]"###
215 ⋮] 213 );
216 "###
217 );
218 } 214 }
219 215
220 #[test] 216 #[test]
221 fn completes_bindings_from_for() { 217 fn completes_bindings_from_for() {
222 assert_debug_snapshot_matches!( 218 assert_debug_snapshot_matches!(
223 do_reference_completion( 219 do_reference_completion(
224 r" 220 r"
225 fn quux() { 221 fn quux() {
226 for x in &[1, 2, 3] { 222 for x in &[1, 2, 3] {
227 <|> 223 <|>
228 } 224 }
229 } 225 }
230 " 226 "
231 ), 227 ),
232 @r###" 228 @r###"[
233 ⋮[ 229 CompletionItem {
234 ⋮ CompletionItem { 230 label: "quux",
235 ⋮ label: "quux", 231 source_range: [95; 95),
236 ⋮ source_range: [95; 95), 232 delete: [95; 95),
237 ⋮ delete: [95; 95), 233 insert: "quux()$0",
238 ⋮ insert: "quux()$0", 234 kind: Function,
239 ⋮ kind: Function, 235 detail: "fn quux()",
240 ⋮ detail: "fn quux()", 236 },
241 ⋮ }, 237 CompletionItem {
242 ⋮ CompletionItem { 238 label: "x",
243 ⋮ label: "x", 239 source_range: [95; 95),
244 ⋮ source_range: [95; 95), 240 delete: [95; 95),
245 ⋮ delete: [95; 95), 241 insert: "x",
246 ⋮ insert: "x", 242 kind: Binding,
247 ⋮ kind: Binding, 243 },
248 ⋮ }, 244]"###
249 ⋮] 245 );
250 "###
251 );
252 } 246 }
253 247
254 #[test] 248 #[test]
255 fn completes_generic_params() { 249 fn completes_generic_params() {
256 assert_debug_snapshot_matches!( 250 assert_debug_snapshot_matches!(
257 do_reference_completion( 251 do_reference_completion(
258 r" 252 r"
259 fn quux<T>() { 253 fn quux<T>() {
260 <|> 254 <|>
261 } 255 }
262 " 256 "
263 ), 257 ),
264 @r###" 258 @r###"[
265 ⋮[ 259 CompletionItem {
266 ⋮ CompletionItem { 260 label: "T",
267 ⋮ label: "T", 261 source_range: [52; 52),
268 ⋮ source_range: [52; 52), 262 delete: [52; 52),
269 ⋮ delete: [52; 52), 263 insert: "T",
270 ⋮ insert: "T", 264 kind: TypeParam,
271 ⋮ kind: TypeParam, 265 },
272 ⋮ }, 266 CompletionItem {
273 ⋮ CompletionItem { 267 label: "quux",
274 ⋮ label: "quux", 268 source_range: [52; 52),
275 ⋮ source_range: [52; 52), 269 delete: [52; 52),
276 ⋮ delete: [52; 52), 270 insert: "quux()$0",
277 ⋮ insert: "quux()$0", 271 kind: Function,
278 ⋮ kind: Function, 272 detail: "fn quux<T>()",
279 ⋮ detail: "fn quux<T>()", 273 },
280 ⋮ }, 274]"###
281 ⋮] 275 );
282 "###
283 );
284 } 276 }
285 277
286 #[test] 278 #[test]
287 fn completes_generic_params_in_struct() { 279 fn completes_generic_params_in_struct() {
288 assert_debug_snapshot_matches!( 280 assert_debug_snapshot_matches!(
289 do_reference_completion( 281 do_reference_completion(
290 r" 282 r"
291 struct X<T> { 283 struct X<T> {
292 x: <|> 284 x: <|>
293 } 285 }
294 " 286 "
295 ), 287 ),
296 @r###" 288 @r###"[
297 ⋮[ 289 CompletionItem {
298 ⋮ CompletionItem { 290 label: "T",
299 ⋮ label: "T", 291 source_range: [54; 54),
300 ⋮ source_range: [54; 54), 292 delete: [54; 54),
301 ⋮ delete: [54; 54), 293 insert: "T",
302 ⋮ insert: "T", 294 kind: TypeParam,
303 ⋮ kind: TypeParam, 295 },
304 ⋮ }, 296 CompletionItem {
305 ⋮ CompletionItem { 297 label: "X",
306 ⋮ label: "X", 298 source_range: [54; 54),
307 ⋮ source_range: [54; 54), 299 delete: [54; 54),
308 ⋮ delete: [54; 54), 300 insert: "X",
309 ⋮ insert: "X", 301 kind: Struct,
310 ⋮ kind: Struct, 302 },
311 ⋮ }, 303]"###
312 ⋮] 304 );
313 "###
314 );
315 } 305 }
316 306
317 #[test] 307 #[test]
318 fn completes_module_items() { 308 fn completes_module_items() {
319 assert_debug_snapshot_matches!( 309 assert_debug_snapshot_matches!(
320 do_reference_completion( 310 do_reference_completion(
321 r" 311 r"
322 struct Foo; 312 struct Foo;
323 enum Baz {} 313 enum Baz {}
324 fn quux() { 314 fn quux() {
325 <|> 315 <|>
326 } 316 }
327 " 317 "
328 ), 318 ),
329 @r###" 319 @r###"[
330 ⋮[ 320 CompletionItem {
331 ⋮ CompletionItem { 321 label: "Baz",
332 ⋮ label: "Baz", 322 source_range: [105; 105),
333 ⋮ source_range: [105; 105), 323 delete: [105; 105),
334 ⋮ delete: [105; 105), 324 insert: "Baz",
335 ⋮ insert: "Baz", 325 kind: Enum,
336 ⋮ kind: Enum, 326 },
337 ⋮ }, 327 CompletionItem {
338 ⋮ CompletionItem { 328 label: "Foo",
339 ⋮ label: "Foo", 329 source_range: [105; 105),
340 ⋮ source_range: [105; 105), 330 delete: [105; 105),
341 ⋮ delete: [105; 105), 331 insert: "Foo",
342 ⋮ insert: "Foo", 332 kind: Struct,
343 ⋮ kind: Struct, 333 },
344 ⋮ }, 334 CompletionItem {
345 ⋮ CompletionItem { 335 label: "quux",
346 ⋮ label: "quux", 336 source_range: [105; 105),
347 ⋮ source_range: [105; 105), 337 delete: [105; 105),
348 ⋮ delete: [105; 105), 338 insert: "quux()$0",
349 ⋮ insert: "quux()$0", 339 kind: Function,
350 ⋮ kind: Function, 340 detail: "fn quux()",
351 ⋮ detail: "fn quux()", 341 },
352 ⋮ }, 342]"###
353 ⋮]
354 "###
355 ); 343 );
356 } 344 }
357 345
358 #[test] 346 #[test]
359 fn completes_extern_prelude() { 347 fn completes_extern_prelude() {
360 assert_debug_snapshot_matches!( 348 assert_debug_snapshot_matches!(
361 do_reference_completion( 349 do_reference_completion(
362 r" 350 r"
363 //- /lib.rs 351 //- /lib.rs
364 use <|>; 352 use <|>;
365 353
366 //- /other_crate/lib.rs 354 //- /other_crate/lib.rs
367 // nothing here 355 // nothing here
368 " 356 "
369 ), 357 ),
370 @r#"[ 358 @r#"[
371 CompletionItem { 359 CompletionItem {
372 label: "other_crate", 360 label: "other_crate",
373 source_range: [4; 4), 361 source_range: [4; 4),
@@ -376,79 +364,75 @@ mod tests {
376 kind: Module, 364 kind: Module,
377 }, 365 },
378]"# 366]"#
379 ); 367 );
380 } 368 }
381 369
382 #[test] 370 #[test]
383 fn completes_module_items_in_nested_modules() { 371 fn completes_module_items_in_nested_modules() {
384 assert_debug_snapshot_matches!( 372 assert_debug_snapshot_matches!(
385 do_reference_completion( 373 do_reference_completion(
386 r" 374 r"
387 struct Foo; 375 struct Foo;
388 mod m { 376 mod m {
389 struct Bar; 377 struct Bar;
390 fn quux() { <|> } 378 fn quux() { <|> }
391 } 379 }
392 " 380 "
393 ), 381 ),
394 @r###" 382 @r###"[
395 ⋮[ 383 CompletionItem {
396 ⋮ CompletionItem { 384 label: "Bar",
397 ⋮ label: "Bar", 385 source_range: [117; 117),
398 ⋮ source_range: [117; 117), 386 delete: [117; 117),
399 ⋮ delete: [117; 117), 387 insert: "Bar",
400 ⋮ insert: "Bar", 388 kind: Struct,
401 ⋮ kind: Struct, 389 },
402 ⋮ }, 390 CompletionItem {
403 ⋮ CompletionItem { 391 label: "quux",
404 ⋮ label: "quux", 392 source_range: [117; 117),
405 ⋮ source_range: [117; 117), 393 delete: [117; 117),
406 ⋮ delete: [117; 117), 394 insert: "quux()$0",
407 ⋮ insert: "quux()$0", 395 kind: Function,
408 ⋮ kind: Function, 396 detail: "fn quux()",
409 ⋮ detail: "fn quux()", 397 },
410 ⋮ }, 398]"###
411 ⋮] 399 );
412 "###
413 );
414 } 400 }
415 401
416 #[test] 402 #[test]
417 fn completes_return_type() { 403 fn completes_return_type() {
418 assert_debug_snapshot_matches!( 404 assert_debug_snapshot_matches!(
419 do_reference_completion( 405 do_reference_completion(
420 r" 406 r"
421 struct Foo; 407 struct Foo;
422 fn x() -> <|> 408 fn x() -> <|>
423 " 409 "
424 ), 410 ),
425 @r###" 411 @r###"[
426 ⋮[ 412 CompletionItem {
427 ⋮ CompletionItem { 413 label: "Foo",
428 ⋮ label: "Foo", 414 source_range: [55; 55),
429 ⋮ source_range: [55; 55), 415 delete: [55; 55),
430 ⋮ delete: [55; 55), 416 insert: "Foo",
431 ⋮ insert: "Foo", 417 kind: Struct,
432 ⋮ kind: Struct, 418 },
433 ⋮ }, 419 CompletionItem {
434 ⋮ CompletionItem { 420 label: "x",
435 ⋮ label: "x", 421 source_range: [55; 55),
436 ⋮ source_range: [55; 55), 422 delete: [55; 55),
437 ⋮ delete: [55; 55), 423 insert: "x()$0",
438 ⋮ insert: "x()$0", 424 kind: Function,
439 ⋮ kind: Function, 425 detail: "fn x()",
440 ⋮ detail: "fn x()", 426 },
441 ⋮ }, 427]"###
442 ⋮] 428 );
443 "###
444 );
445 } 429 }
446 430
447 #[test] 431 #[test]
448 fn dont_show_both_completions_for_shadowing() { 432 fn dont_show_both_completions_for_shadowing() {
449 assert_debug_snapshot_matches!( 433 assert_debug_snapshot_matches!(
450 do_reference_completion( 434 do_reference_completion(
451 r" 435 r"
452 fn foo() { 436 fn foo() {
453 let bar = 92; 437 let bar = 92;
454 { 438 {
@@ -457,35 +441,33 @@ mod tests {
457 } 441 }
458 } 442 }
459 " 443 "
460 ), 444 ),
461 @r###" 445 @r###"[
462 ⋮[ 446 CompletionItem {
463 ⋮ CompletionItem { 447 label: "bar",
464 ⋮ label: "bar", 448 source_range: [146; 146),
465 ⋮ source_range: [146; 146), 449 delete: [146; 146),
466 ⋮ delete: [146; 146), 450 insert: "bar",
467 ⋮ insert: "bar", 451 kind: Binding,
468 ⋮ kind: Binding, 452 detail: "i32",
469 ⋮ detail: "i32", 453 },
470 ⋮ }, 454 CompletionItem {
471 ⋮ CompletionItem { 455 label: "foo",
472 ⋮ label: "foo", 456 source_range: [146; 146),
473 ⋮ source_range: [146; 146), 457 delete: [146; 146),
474 ⋮ delete: [146; 146), 458 insert: "foo()$0",
475 ⋮ insert: "foo()$0", 459 kind: Function,
476 ⋮ kind: Function, 460 detail: "fn foo()",
477 ⋮ detail: "fn foo()", 461 },
478 ⋮ }, 462]"###
479 ⋮] 463 );
480 "###
481 );
482 } 464 }
483 465
484 #[test] 466 #[test]
485 fn completes_self_in_methods() { 467 fn completes_self_in_methods() {
486 assert_debug_snapshot_matches!( 468 assert_debug_snapshot_matches!(
487 do_reference_completion(r"impl S { fn foo(&self) { <|> } }"), 469 do_reference_completion(r"impl S { fn foo(&self) { <|> } }"),
488 @r#"[ 470 @r#"[
489 CompletionItem { 471 CompletionItem {
490 label: "Self", 472 label: "Self",
491 source_range: [25; 25), 473 source_range: [25; 25),
@@ -502,14 +484,14 @@ mod tests {
502 detail: "&{unknown}", 484 detail: "&{unknown}",
503 }, 485 },
504]"# 486]"#
505 ); 487 );
506 } 488 }
507 489
508 #[test] 490 #[test]
509 fn completes_prelude() { 491 fn completes_prelude() {
510 assert_debug_snapshot_matches!( 492 assert_debug_snapshot_matches!(
511 do_reference_completion( 493 do_reference_completion(
512 " 494 "
513 //- /main.rs 495 //- /main.rs
514 fn foo() { let x: <|> } 496 fn foo() { let x: <|> }
515 497
@@ -521,8 +503,8 @@ mod tests {
521 struct Option; 503 struct Option;
522 } 504 }
523 " 505 "
524 ), 506 ),
525 @r#"[ 507 @r#"[
526 CompletionItem { 508 CompletionItem {
527 label: "Option", 509 label: "Option",
528 source_range: [18; 18), 510 source_range: [18; 18),
@@ -546,6 +528,6 @@ mod tests {
546 kind: Module, 528 kind: Module,
547 }, 529 },
548]"# 530]"#
549 ); 531 );
550 } 532 }
551} 533}