aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/references.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-06-24 10:29:43 +0100
committerAleksey Kladov <[email protected]>2020-06-24 10:29:43 +0100
commitc6795fb83a850dde6ac0b08decf108c0c3aa452a (patch)
tree6f5ae66dc754b6e20069bdcbccf70fa42b236b57 /crates/ra_ide/src/references.rs
parente9cb818c2683e06153f013c3b8d03f7f2719eb02 (diff)
More consistent usage of fixtures
Diffstat (limited to 'crates/ra_ide/src/references.rs')
-rw-r--r--crates/ra_ide/src/references.rs488
1 files changed, 249 insertions, 239 deletions
diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs
index 4a96d6505..50929bb72 100644
--- a/crates/ra_ide/src/references.rs
+++ b/crates/ra_ide/src/references.rs
@@ -197,233 +197,238 @@ mod tests {
197 197
198 #[test] 198 #[test]
199 fn test_struct_literal_after_space() { 199 fn test_struct_literal_after_space() {
200 let code = r#" 200 let refs = get_all_refs(
201 struct Foo <|>{ 201 r#"
202 a: i32, 202struct Foo <|>{
203 } 203 a: i32,
204 impl Foo { 204}
205 fn f() -> i32 { 42 } 205impl Foo {
206 } 206 fn f() -> i32 { 42 }
207 fn main() { 207}
208 let f: Foo; 208fn main() {
209 f = Foo {a: Foo::f()}; 209 let f: Foo;
210 }"#; 210 f = Foo {a: Foo::f()};
211 211}
212 let refs = get_all_refs(code); 212"#,
213 );
213 check_result( 214 check_result(
214 refs, 215 refs,
215 "Foo STRUCT_DEF FileId(1) 5..39 12..15 Other", 216 "Foo STRUCT_DEF FileId(1) 0..26 7..10 Other",
216 &["FileId(1) 138..141 StructLiteral"], 217 &["FileId(1) 101..104 StructLiteral"],
217 ); 218 );
218 } 219 }
219 220
220 #[test] 221 #[test]
221 fn test_struct_literal_befor_space() { 222 fn test_struct_literal_before_space() {
222 let code = r#" 223 let refs = get_all_refs(
223 struct Foo<|> {} 224 r#"
224 fn main() { 225struct Foo<|> {}
225 let f: Foo; 226 fn main() {
226 f = Foo {}; 227 let f: Foo;
227 }"#; 228 f = Foo {};
228 229}
229 let refs = get_all_refs(code); 230"#,
231 );
230 check_result( 232 check_result(
231 refs, 233 refs,
232 "Foo STRUCT_DEF FileId(1) 5..18 12..15 Other", 234 "Foo STRUCT_DEF FileId(1) 0..13 7..10 Other",
233 &["FileId(1) 54..57 Other", "FileId(1) 71..74 StructLiteral"], 235 &["FileId(1) 41..44 Other", "FileId(1) 54..57 StructLiteral"],
234 ); 236 );
235 } 237 }
236 238
237 #[test] 239 #[test]
238 fn test_struct_literal_with_generic_type() { 240 fn test_struct_literal_with_generic_type() {
239 let code = r#" 241 let refs = get_all_refs(
240 struct Foo<T> <|>{} 242 r#"
241 fn main() { 243struct Foo<T> <|>{}
242 let f: Foo::<i32>; 244 fn main() {
243 f = Foo {}; 245 let f: Foo::<i32>;
244 }"#; 246 f = Foo {};
245 247}
246 let refs = get_all_refs(code); 248"#,
249 );
247 check_result( 250 check_result(
248 refs, 251 refs,
249 "Foo STRUCT_DEF FileId(1) 5..21 12..15 Other", 252 "Foo STRUCT_DEF FileId(1) 0..16 7..10 Other",
250 &["FileId(1) 81..84 StructLiteral"], 253 &["FileId(1) 64..67 StructLiteral"],
251 ); 254 );
252 } 255 }
253 256
254 #[test] 257 #[test]
255 fn test_struct_literal_for_tuple() { 258 fn test_struct_literal_for_tuple() {
256 let code = r#" 259 let refs = get_all_refs(
257 struct Foo<|>(i32); 260 r#"
258 261struct Foo<|>(i32);
259 fn main() {
260 let f: Foo;
261 f = Foo(1);
262 }"#;
263 262
264 let refs = get_all_refs(code); 263fn main() {
264 let f: Foo;
265 f = Foo(1);
266}
267"#,
268 );
265 check_result( 269 check_result(
266 refs, 270 refs,
267 "Foo STRUCT_DEF FileId(1) 5..21 12..15 Other", 271 "Foo STRUCT_DEF FileId(1) 0..16 7..10 Other",
268 &["FileId(1) 71..74 StructLiteral"], 272 &["FileId(1) 54..57 StructLiteral"],
269 ); 273 );
270 } 274 }
271 275
272 #[test] 276 #[test]
273 fn test_find_all_refs_for_local() { 277 fn test_find_all_refs_for_local() {
274 let code = r#" 278 let refs = get_all_refs(
275 fn main() { 279 r#"
276 let mut i = 1; 280fn main() {
277 let j = 1; 281 let mut i = 1;
278 i = i<|> + j; 282 let j = 1;
283 i = i<|> + j;
279 284
280 { 285 {
281 i = 0; 286 i = 0;
282 } 287 }
283
284 i = 5;
285 }"#;
286 288
287 let refs = get_all_refs(code); 289 i = 5;
290}"#,
291 );
288 check_result( 292 check_result(
289 refs, 293 refs,
290 "i BIND_PAT FileId(1) 33..34 Other Write", 294 "i BIND_PAT FileId(1) 24..25 Other Write",
291 &[ 295 &[
292 "FileId(1) 67..68 Other Write", 296 "FileId(1) 50..51 Other Write",
293 "FileId(1) 71..72 Other Read", 297 "FileId(1) 54..55 Other Read",
294 "FileId(1) 101..102 Other Write", 298 "FileId(1) 76..77 Other Write",
295 "FileId(1) 127..128 Other Write", 299 "FileId(1) 94..95 Other Write",
296 ], 300 ],
297 ); 301 );
298 } 302 }
299 303
300 #[test] 304 #[test]
301 fn search_filters_by_range() { 305 fn search_filters_by_range() {
302 let code = r#" 306 let refs = get_all_refs(
303 fn foo() { 307 r#"
304 let spam<|> = 92; 308fn foo() {
305 spam + spam 309 let spam<|> = 92;
306 } 310 spam + spam
307 fn bar() { 311}
308 let spam = 92; 312fn bar() {
309 spam + spam 313 let spam = 92;
310 } 314 spam + spam
311 "#; 315}
312 let refs = get_all_refs(code); 316"#,
317 );
313 check_result( 318 check_result(
314 refs, 319 refs,
315 "spam BIND_PAT FileId(1) 44..48 Other", 320 "spam BIND_PAT FileId(1) 19..23 Other",
316 &["FileId(1) 71..75 Other Read", "FileId(1) 78..82 Other Read"], 321 &["FileId(1) 34..38 Other Read", "FileId(1) 41..45 Other Read"],
317 ); 322 );
318 } 323 }
319 324
320 #[test] 325 #[test]
321 fn test_find_all_refs_for_param_inside() { 326 fn test_find_all_refs_for_param_inside() {
322 let code = r#" 327 let refs = get_all_refs(
323 fn foo(i : u32) -> u32 { 328 r#"
324 i<|> 329fn foo(i : u32) -> u32 {
325 }"#; 330 i<|>
326 331}
327 let refs = get_all_refs(code); 332"#,
328 check_result(refs, "i BIND_PAT FileId(1) 12..13 Other", &["FileId(1) 38..39 Other Read"]); 333 );
334 check_result(refs, "i BIND_PAT FileId(1) 7..8 Other", &["FileId(1) 29..30 Other Read"]);
329 } 335 }
330 336
331 #[test] 337 #[test]
332 fn test_find_all_refs_for_fn_param() { 338 fn test_find_all_refs_for_fn_param() {
333 let code = r#" 339 let refs = get_all_refs(
334 fn foo(i<|> : u32) -> u32 { 340 r#"
335 i 341fn foo(i<|> : u32) -> u32 {
336 }"#; 342 i
337 343}
338 let refs = get_all_refs(code); 344"#,
339 check_result(refs, "i BIND_PAT FileId(1) 12..13 Other", &["FileId(1) 38..39 Other Read"]); 345 );
346 check_result(refs, "i BIND_PAT FileId(1) 7..8 Other", &["FileId(1) 29..30 Other Read"]);
340 } 347 }
341 348
342 #[test] 349 #[test]
343 fn test_find_all_refs_field_name() { 350 fn test_find_all_refs_field_name() {
344 let code = r#" 351 let refs = get_all_refs(
345 //- /lib.rs 352 r#"
346 struct Foo { 353//- /lib.rs
347 pub spam<|>: u32, 354struct Foo {
348 } 355 pub spam<|>: u32,
349 356}
350 fn main(s: Foo) {
351 let f = s.spam;
352 }
353 "#;
354 357
355 let refs = get_all_refs(code); 358fn main(s: Foo) {
359 let f = s.spam;
360}
361"#,
362 );
356 check_result( 363 check_result(
357 refs, 364 refs,
358 "spam RECORD_FIELD_DEF FileId(1) 66..79 70..74 Other", 365 "spam RECORD_FIELD_DEF FileId(1) 17..30 21..25 Other",
359 &["FileId(1) 152..156 Other Read"], 366 &["FileId(1) 67..71 Other Read"],
360 ); 367 );
361 } 368 }
362 369
363 #[test] 370 #[test]
364 fn test_find_all_refs_impl_item_name() { 371 fn test_find_all_refs_impl_item_name() {
365 let code = r#" 372 let refs = get_all_refs(
366 //- /lib.rs 373 r#"
367 struct Foo; 374struct Foo;
368 impl Foo { 375impl Foo {
369 fn f<|>(&self) { } 376 fn f<|>(&self) { }
370 } 377}
371 "#; 378"#,
372 379 );
373 let refs = get_all_refs(code); 380 check_result(refs, "f FN_DEF FileId(1) 27..43 30..31 Other", &[]);
374 check_result(refs, "f FN_DEF FileId(1) 88..104 91..92 Other", &[]);
375 } 381 }
376 382
377 #[test] 383 #[test]
378 fn test_find_all_refs_enum_var_name() { 384 fn test_find_all_refs_enum_var_name() {
379 let code = r#" 385 let refs = get_all_refs(
380 //- /lib.rs 386 r#"
381 enum Foo { 387enum Foo {
382 A, 388 A,
383 B<|>, 389 B<|>,
384 C, 390 C,
385 } 391}
386 "#; 392"#,
387 393 );
388 let refs = get_all_refs(code); 394 check_result(refs, "B ENUM_VARIANT FileId(1) 22..23 22..23 Other", &[]);
389 check_result(refs, "B ENUM_VARIANT FileId(1) 83..84 83..84 Other", &[]);
390 } 395 }
391 396
392 #[test] 397 #[test]
393 fn test_find_all_refs_two_modules() { 398 fn test_find_all_refs_two_modules() {
394 let code = r#" 399 let (analysis, pos) = analysis_and_position(
395 //- /lib.rs 400 r#"
396 pub mod foo; 401//- /lib.rs
397 pub mod bar; 402pub mod foo;
398 403pub mod bar;
399 fn f() { 404
400 let i = foo::Foo { n: 5 }; 405fn f() {
401 } 406 let i = foo::Foo { n: 5 };
402 407}
403 //- /foo.rs
404 use crate::bar;
405 408
406 pub struct Foo { 409//- /foo.rs
407 pub n: u32, 410use crate::bar;
408 }
409 411
410 fn f() { 412pub struct Foo {
411 let i = bar::Bar { n: 5 }; 413 pub n: u32,
412 } 414}
413 415
414 //- /bar.rs 416fn f() {
415 use crate::foo; 417 let i = bar::Bar { n: 5 };
418}
416 419
417 pub struct Bar { 420//- /bar.rs
418 pub n: u32, 421use crate::foo;
419 }
420 422
421 fn f() { 423pub struct Bar {
422 let i = foo::Foo<|> { n: 5 }; 424 pub n: u32,
423 } 425}
424 "#;
425 426
426 let (analysis, pos) = analysis_and_position(code); 427fn f() {
428 let i = foo::Foo<|> { n: 5 };
429}
430"#,
431 );
427 let refs = analysis.find_all_refs(pos, None).unwrap().unwrap(); 432 let refs = analysis.find_all_refs(pos, None).unwrap().unwrap();
428 check_result( 433 check_result(
429 refs, 434 refs,
@@ -437,48 +442,48 @@ mod tests {
437 // which is the whole `foo.rs`, and the second one is in `use foo::Foo`. 442 // which is the whole `foo.rs`, and the second one is in `use foo::Foo`.
438 #[test] 443 #[test]
439 fn test_find_all_refs_decl_module() { 444 fn test_find_all_refs_decl_module() {
440 let code = r#" 445 let (analysis, pos) = analysis_and_position(
441 //- /lib.rs 446 r#"
442 mod foo<|>; 447//- /lib.rs
448mod foo<|>;
443 449
444 use foo::Foo; 450use foo::Foo;
445 451
446 fn f() { 452fn f() {
447 let i = Foo { n: 5 }; 453 let i = Foo { n: 5 };
448 } 454}
449
450 //- /foo.rs
451 pub struct Foo {
452 pub n: u32,
453 }
454 "#;
455 455
456 let (analysis, pos) = analysis_and_position(code); 456//- /foo.rs
457pub struct Foo {
458 pub n: u32,
459}
460"#,
461 );
457 let refs = analysis.find_all_refs(pos, None).unwrap().unwrap(); 462 let refs = analysis.find_all_refs(pos, None).unwrap().unwrap();
458 check_result(refs, "foo SOURCE_FILE FileId(2) 0..35 Other", &["FileId(1) 14..17 Other"]); 463 check_result(refs, "foo SOURCE_FILE FileId(2) 0..35 Other", &["FileId(1) 14..17 Other"]);
459 } 464 }
460 465
461 #[test] 466 #[test]
462 fn test_find_all_refs_super_mod_vis() { 467 fn test_find_all_refs_super_mod_vis() {
463 let code = r#" 468 let (analysis, pos) = analysis_and_position(
464 //- /lib.rs 469 r#"
465 mod foo; 470//- /lib.rs
466 471mod foo;
467 //- /foo.rs
468 mod some;
469 use some::Foo;
470 472
471 fn f() { 473//- /foo.rs
472 let i = Foo { n: 5 }; 474mod some;
473 } 475use some::Foo;
474 476
475 //- /foo/some.rs 477fn f() {
476 pub(super) struct Foo<|> { 478 let i = Foo { n: 5 };
477 pub n: u32, 479}
478 }
479 "#;
480 480
481 let (analysis, pos) = analysis_and_position(code); 481//- /foo/some.rs
482pub(super) struct Foo<|> {
483 pub n: u32,
484}
485"#,
486 );
482 let refs = analysis.find_all_refs(pos, None).unwrap().unwrap(); 487 let refs = analysis.find_all_refs(pos, None).unwrap().unwrap();
483 check_result( 488 check_result(
484 refs, 489 refs,
@@ -525,93 +530,98 @@ mod tests {
525 530
526 #[test] 531 #[test]
527 fn test_find_all_refs_macro_def() { 532 fn test_find_all_refs_macro_def() {
528 let code = r#" 533 let refs = get_all_refs(
529 #[macro_export] 534 r#"
530 macro_rules! m1<|> { () => (()) } 535#[macro_export]
531 536macro_rules! m1<|> { () => (()) }
532 fn foo() { 537
533 m1(); 538fn foo() {
534 m1(); 539 m1();
535 }"#; 540 m1();
536 541}
537 let refs = get_all_refs(code); 542"#,
543 );
538 check_result( 544 check_result(
539 refs, 545 refs,
540 "m1 MACRO_CALL FileId(1) 9..63 46..48 Other", 546 "m1 MACRO_CALL FileId(1) 0..46 29..31 Other",
541 &["FileId(1) 96..98 StructLiteral", "FileId(1) 114..116 StructLiteral"], 547 &["FileId(1) 63..65 StructLiteral", "FileId(1) 73..75 StructLiteral"],
542 ); 548 );
543 } 549 }
544 550
545 #[test] 551 #[test]
546 fn test_basic_highlight_read_write() { 552 fn test_basic_highlight_read_write() {
547 let code = r#" 553 let refs = get_all_refs(
548 fn foo() { 554 r#"
549 let mut i<|> = 0; 555fn foo() {
550 i = i + 1; 556 let mut i<|> = 0;
551 }"#; 557 i = i + 1;
552 558}
553 let refs = get_all_refs(code); 559"#,
560 );
554 check_result( 561 check_result(
555 refs, 562 refs,
556 "i BIND_PAT FileId(1) 40..41 Other Write", 563 "i BIND_PAT FileId(1) 23..24 Other Write",
557 &["FileId(1) 59..60 Other Write", "FileId(1) 63..64 Other Read"], 564 &["FileId(1) 34..35 Other Write", "FileId(1) 38..39 Other Read"],
558 ); 565 );
559 } 566 }
560 567
561 #[test] 568 #[test]
562 fn test_basic_highlight_field_read_write() { 569 fn test_basic_highlight_field_read_write() {
563 let code = r#" 570 let refs = get_all_refs(
564 struct S { 571 r#"
565 f: u32, 572struct S {
566 } 573 f: u32,
567 574}
568 fn foo() {
569 let mut s = S{f: 0};
570 s.f<|> = 0;
571 }"#;
572 575
573 let refs = get_all_refs(code); 576fn foo() {
577 let mut s = S{f: 0};
578 s.f<|> = 0;
579}
580"#,
581 );
574 check_result( 582 check_result(
575 refs, 583 refs,
576 "f RECORD_FIELD_DEF FileId(1) 32..38 32..33 Other", 584 "f RECORD_FIELD_DEF FileId(1) 15..21 15..16 Other",
577 &["FileId(1) 96..97 Other Read", "FileId(1) 117..118 Other Write"], 585 &["FileId(1) 55..56 Other Read", "FileId(1) 68..69 Other Write"],
578 ); 586 );
579 } 587 }
580 588
581 #[test] 589 #[test]
582 fn test_basic_highlight_decl_no_write() { 590 fn test_basic_highlight_decl_no_write() {
583 let code = r#" 591 let refs = get_all_refs(
584 fn foo() { 592 r#"
585 let i<|>; 593fn foo() {
586 i = 1; 594 let i<|>;
587 }"#; 595 i = 1;
588 596}
589 let refs = get_all_refs(code); 597"#,
590 check_result(refs, "i BIND_PAT FileId(1) 36..37 Other", &["FileId(1) 51..52 Other Write"]); 598 );
599 check_result(refs, "i BIND_PAT FileId(1) 19..20 Other", &["FileId(1) 26..27 Other Write"]);
591 } 600 }
592 601
593 #[test] 602 #[test]
594 fn test_find_struct_function_refs_outside_module() { 603 fn test_find_struct_function_refs_outside_module() {
595 let code = r#" 604 let refs = get_all_refs(
596 mod foo { 605 r#"
597 pub struct Foo; 606mod foo {
607 pub struct Foo;
598 608
599 impl Foo { 609 impl Foo {
600 pub fn new<|>() -> Foo { 610 pub fn new<|>() -> Foo {
601 Foo 611 Foo
602 }
603 }
604 } 612 }
613 }
614}
605 615
606 fn main() { 616fn main() {
607 let _f = foo::Foo::new(); 617 let _f = foo::Foo::new();
608 }"#; 618}
609 619"#,
610 let refs = get_all_refs(code); 620 );
611 check_result( 621 check_result(
612 refs, 622 refs,
613 "new FN_DEF FileId(1) 87..150 94..97 Other", 623 "new FN_DEF FileId(1) 54..101 61..64 Other",
614 &["FileId(1) 227..230 StructLiteral"], 624 &["FileId(1) 146..149 StructLiteral"],
615 ); 625 );
616 } 626 }
617 627
@@ -642,8 +652,8 @@ mod tests {
642 ); 652 );
643 } 653 }
644 654
645 fn get_all_refs(text: &str) -> ReferenceSearchResult { 655 fn get_all_refs(ra_fixture: &str) -> ReferenceSearchResult {
646 let (analysis, position) = single_file_with_position(text); 656 let (analysis, position) = single_file_with_position(ra_fixture);
647 analysis.find_all_refs(position, None).unwrap().unwrap() 657 analysis.find_all_refs(position, None).unwrap().unwrap()
648 } 658 }
649 659