aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide')
-rw-r--r--crates/ra_ide/src/references/rename.rs1043
1 files changed, 489 insertions, 554 deletions
diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs
index b6a2266b4..8735ec53c 100644
--- a/crates/ra_ide/src/references/rename.rs
+++ b/crates/ra_ide/src/references/rename.rs
@@ -116,8 +116,7 @@ fn rename_mod(
116 } else { 116 } else {
117 format!("{}.rs", new_name) 117 format!("{}.rs", new_name)
118 }; 118 };
119 let move_file = 119 let move_file = FileSystemEdit::MoveFile { src: file_id, anchor: file_id, dst };
120 FileSystemEdit::MoveFile { src: file_id, anchor: position.file_id, dst };
121 file_system_edits.push(move_file); 120 file_system_edits.push(move_file);
122 } 121 }
123 ModuleSource::Module(..) => {} 122 ModuleSource::Module(..) => {}
@@ -271,51 +270,51 @@ fn rename_reference(
271 270
272#[cfg(test)] 271#[cfg(test)]
273mod tests { 272mod tests {
274 use insta::assert_debug_snapshot; 273 use expect::{expect, Expect};
275 use ra_text_edit::TextEditBuilder; 274 use ra_text_edit::TextEditBuilder;
276 use stdx::trim_indent; 275 use stdx::trim_indent;
277 use test_utils::{assert_eq_text, mark}; 276 use test_utils::{assert_eq_text, mark};
278 277
279 use crate::{mock_analysis::analysis_and_position, FileId}; 278 use crate::{mock_analysis::analysis_and_position, FileId};
280 279
280 fn check(new_name: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
281 let ra_fixture_after = &trim_indent(ra_fixture_after);
282 let (analysis, position) = analysis_and_position(ra_fixture_before);
283 let source_change = analysis.rename(position, new_name).unwrap();
284 let mut text_edit_builder = TextEditBuilder::default();
285 let mut file_id: Option<FileId> = None;
286 if let Some(change) = source_change {
287 for edit in change.info.source_file_edits {
288 file_id = Some(edit.file_id);
289 for indel in edit.edit.into_iter() {
290 text_edit_builder.replace(indel.delete, indel.insert);
291 }
292 }
293 }
294 let mut result = analysis.file_text(file_id.unwrap()).unwrap().to_string();
295 text_edit_builder.finish().apply(&mut result);
296 assert_eq_text!(ra_fixture_after, &*result);
297 }
298
299 fn check_expect(new_name: &str, ra_fixture: &str, expect: Expect) {
300 let (analysis, position) = analysis_and_position(ra_fixture);
301 let source_change = analysis.rename(position, new_name).unwrap().unwrap();
302 expect.assert_debug_eq(&source_change)
303 }
304
281 #[test] 305 #[test]
282 fn test_rename_to_underscore() { 306 fn test_rename_to_underscore() {
283 test_rename( 307 check("_", r#"fn main() { let i<|> = 1; }"#, r#"fn main() { let _ = 1; }"#);
284 r#"
285 fn main() {
286 let i<|> = 1;
287 }"#,
288 "_",
289 r#"
290 fn main() {
291 let _ = 1;
292 }"#,
293 );
294 } 308 }
295 309
296 #[test] 310 #[test]
297 fn test_rename_to_raw_identifier() { 311 fn test_rename_to_raw_identifier() {
298 test_rename( 312 check("r#fn", r#"fn main() { let i<|> = 1; }"#, r#"fn main() { let r#fn = 1; }"#);
299 r#"
300 fn main() {
301 let i<|> = 1;
302 }"#,
303 "r#fn",
304 r#"
305 fn main() {
306 let r#fn = 1;
307 }"#,
308 );
309 } 313 }
310 314
311 #[test] 315 #[test]
312 fn test_rename_to_invalid_identifier() { 316 fn test_rename_to_invalid_identifier() {
313 let (analysis, position) = analysis_and_position( 317 let (analysis, position) = analysis_and_position(r#"fn main() { let i<|> = 1; }"#);
314 "
315 fn main() {
316 let i<|> = 1;
317 }",
318 );
319 let new_name = "invalid!"; 318 let new_name = "invalid!";
320 let source_change = analysis.rename(position, new_name).unwrap(); 319 let source_change = analysis.rename(position, new_name).unwrap();
321 assert!(source_change.is_none()); 320 assert!(source_change.is_none());
@@ -323,318 +322,269 @@ mod tests {
323 322
324 #[test] 323 #[test]
325 fn test_rename_for_local() { 324 fn test_rename_for_local() {
326 test_rename( 325 check(
326 "k",
327 r#" 327 r#"
328 fn main() { 328fn main() {
329 let mut i = 1; 329 let mut i = 1;
330 let j = 1; 330 let j = 1;
331 i = i<|> + j; 331 i = i<|> + j;
332 332
333 { 333 { i = 0; }
334 i = 0;
335 }
336 334
337 i = 5; 335 i = 5;
338 }"#, 336}
339 "k", 337"#,
340 r#" 338 r#"
341 fn main() { 339fn main() {
342 let mut k = 1; 340 let mut k = 1;
343 let j = 1; 341 let j = 1;
344 k = k + j; 342 k = k + j;
345 343
346 { 344 { k = 0; }
347 k = 0;
348 }
349 345
350 k = 5; 346 k = 5;
351 }"#, 347}
348"#,
352 ); 349 );
353 } 350 }
354 351
355 #[test] 352 #[test]
356 fn test_rename_for_macro_args() { 353 fn test_rename_for_macro_args() {
357 test_rename( 354 check(
358 r#"
359 macro_rules! foo {($i:ident) => {$i} }
360 fn main() {
361 let a<|> = "test";
362 foo!(a);
363 }"#,
364 "b", 355 "b",
365 r#" 356 r#"
366 macro_rules! foo {($i:ident) => {$i} } 357macro_rules! foo {($i:ident) => {$i} }
367 fn main() { 358fn main() {
368 let b = "test"; 359 let a<|> = "test";
369 foo!(b); 360 foo!(a);
370 }"#, 361}
362"#,
363 r#"
364macro_rules! foo {($i:ident) => {$i} }
365fn main() {
366 let b = "test";
367 foo!(b);
368}
369"#,
371 ); 370 );
372 } 371 }
373 372
374 #[test] 373 #[test]
375 fn test_rename_for_macro_args_rev() { 374 fn test_rename_for_macro_args_rev() {
376 test_rename( 375 check(
377 r#"
378 macro_rules! foo {($i:ident) => {$i} }
379 fn main() {
380 let a = "test";
381 foo!(a<|>);
382 }"#,
383 "b", 376 "b",
384 r#" 377 r#"
385 macro_rules! foo {($i:ident) => {$i} } 378macro_rules! foo {($i:ident) => {$i} }
386 fn main() { 379fn main() {
387 let b = "test"; 380 let a = "test";
388 foo!(b); 381 foo!(a<|>);
389 }"#, 382}
383"#,
384 r#"
385macro_rules! foo {($i:ident) => {$i} }
386fn main() {
387 let b = "test";
388 foo!(b);
389}
390"#,
390 ); 391 );
391 } 392 }
392 393
393 #[test] 394 #[test]
394 fn test_rename_for_macro_define_fn() { 395 fn test_rename_for_macro_define_fn() {
395 test_rename( 396 check(
396 r#"
397 macro_rules! define_fn {($id:ident) => { fn $id{} }}
398 define_fn!(foo);
399 fn main() {
400 fo<|>o();
401 }"#,
402 "bar", 397 "bar",
403 r#" 398 r#"
404 macro_rules! define_fn {($id:ident) => { fn $id{} }} 399macro_rules! define_fn {($id:ident) => { fn $id{} }}
405 define_fn!(bar); 400define_fn!(foo);
406 fn main() { 401fn main() {
407 bar(); 402 fo<|>o();
408 }"#, 403}
404"#,
405 r#"
406macro_rules! define_fn {($id:ident) => { fn $id{} }}
407define_fn!(bar);
408fn main() {
409 bar();
410}
411"#,
409 ); 412 );
410 } 413 }
411 414
412 #[test] 415 #[test]
413 fn test_rename_for_macro_define_fn_rev() { 416 fn test_rename_for_macro_define_fn_rev() {
414 test_rename( 417 check(
415 r#"
416 macro_rules! define_fn {($id:ident) => { fn $id{} }}
417 define_fn!(fo<|>o);
418 fn main() {
419 foo();
420 }"#,
421 "bar", 418 "bar",
422 r#" 419 r#"
423 macro_rules! define_fn {($id:ident) => { fn $id{} }} 420macro_rules! define_fn {($id:ident) => { fn $id{} }}
424 define_fn!(bar); 421define_fn!(fo<|>o);
425 fn main() { 422fn main() {
426 bar(); 423 foo();
427 }"#, 424}
425"#,
426 r#"
427macro_rules! define_fn {($id:ident) => { fn $id{} }}
428define_fn!(bar);
429fn main() {
430 bar();
431}
432"#,
428 ); 433 );
429 } 434 }
430 435
431 #[test] 436 #[test]
432 fn test_rename_for_param_inside() { 437 fn test_rename_for_param_inside() {
433 test_rename( 438 check("j", r#"fn foo(i : u32) -> u32 { i<|> }"#, r#"fn foo(j : u32) -> u32 { j }"#);
434 r#"
435 fn foo(i : u32) -> u32 {
436 i<|>
437 }"#,
438 "j",
439 r#"
440 fn foo(j : u32) -> u32 {
441 j
442 }"#,
443 );
444 } 439 }
445 440
446 #[test] 441 #[test]
447 fn test_rename_refs_for_fn_param() { 442 fn test_rename_refs_for_fn_param() {
448 test_rename( 443 check("j", r#"fn foo(i<|> : u32) -> u32 { i }"#, r#"fn foo(j : u32) -> u32 { j }"#);
449 r#"
450 fn foo(i<|> : u32) -> u32 {
451 i
452 }"#,
453 "new_name",
454 r#"
455 fn foo(new_name : u32) -> u32 {
456 new_name
457 }"#,
458 );
459 } 444 }
460 445
461 #[test] 446 #[test]
462 fn test_rename_for_mut_param() { 447 fn test_rename_for_mut_param() {
463 test_rename( 448 check("j", r#"fn foo(mut i<|> : u32) -> u32 { i }"#, r#"fn foo(mut j : u32) -> u32 { j }"#);
464 r#"
465 fn foo(mut i<|> : u32) -> u32 {
466 i
467 }"#,
468 "new_name",
469 r#"
470 fn foo(mut new_name : u32) -> u32 {
471 new_name
472 }"#,
473 );
474 } 449 }
475 450
476 #[test] 451 #[test]
477 fn test_rename_struct_field() { 452 fn test_rename_struct_field() {
478 test_rename( 453 check(
454 "j",
479 r#" 455 r#"
480 struct Foo { 456struct Foo { i<|>: i32 }
481 i<|>: i32,
482 }
483 457
484 impl Foo { 458impl Foo {
485 fn new(i: i32) -> Self { 459 fn new(i: i32) -> Self {
486 Self { i: i } 460 Self { i: i }
487 }
488 } 461 }
489 "#, 462}
490 "j", 463"#,
491 r#" 464 r#"
492 struct Foo { 465struct Foo { j: i32 }
493 j: i32,
494 }
495 466
496 impl Foo { 467impl Foo {
497 fn new(i: i32) -> Self { 468 fn new(i: i32) -> Self {
498 Self { j: i } 469 Self { j: i }
499 }
500 } 470 }
501 "#, 471}
472"#,
502 ); 473 );
503 } 474 }
504 475
505 #[test] 476 #[test]
506 fn test_rename_struct_field_for_shorthand() { 477 fn test_rename_struct_field_for_shorthand() {
507 mark::check!(test_rename_struct_field_for_shorthand); 478 mark::check!(test_rename_struct_field_for_shorthand);
508 test_rename( 479 check(
480 "j",
509 r#" 481 r#"
510 struct Foo { 482struct Foo { i<|>: i32 }
511 i<|>: i32,
512 }
513 483
514 impl Foo { 484impl Foo {
515 fn new(i: i32) -> Self { 485 fn new(i: i32) -> Self {
516 Self { i } 486 Self { i }
517 }
518 } 487 }
519 "#, 488}
520 "j", 489"#,
521 r#" 490 r#"
522 struct Foo { 491struct Foo { j: i32 }
523 j: i32,
524 }
525 492
526 impl Foo { 493impl Foo {
527 fn new(i: i32) -> Self { 494 fn new(i: i32) -> Self {
528 Self { j: i } 495 Self { j: i }
529 }
530 } 496 }
531 "#, 497}
498"#,
532 ); 499 );
533 } 500 }
534 501
535 #[test] 502 #[test]
536 fn test_rename_local_for_field_shorthand() { 503 fn test_rename_local_for_field_shorthand() {
537 mark::check!(test_rename_local_for_field_shorthand); 504 mark::check!(test_rename_local_for_field_shorthand);
538 test_rename( 505 check(
506 "j",
539 r#" 507 r#"
540 struct Foo { 508struct Foo { i: i32 }
541 i: i32,
542 }
543 509
544 impl Foo { 510impl Foo {
545 fn new(i<|>: i32) -> Self { 511 fn new(i<|>: i32) -> Self {
546 Self { i } 512 Self { i }
547 }
548 } 513 }
549 "#, 514}
550 "j", 515"#,
551 r#" 516 r#"
552 struct Foo { 517struct Foo { i: i32 }
553 i: i32,
554 }
555 518
556 impl Foo { 519impl Foo {
557 fn new(j: i32) -> Self { 520 fn new(j: i32) -> Self {
558 Self { i: j } 521 Self { i: j }
559 }
560 } 522 }
561 "#, 523}
524"#,
562 ); 525 );
563 } 526 }
564 527
565 #[test] 528 #[test]
566 fn test_field_shorthand_correct_struct() { 529 fn test_field_shorthand_correct_struct() {
567 test_rename( 530 check(
568 r#"
569 struct Foo {
570 i<|>: i32,
571 }
572
573 struct Bar {
574 i: i32,
575 }
576
577 impl Bar {
578 fn new(i: i32) -> Self {
579 Self { i }
580 }
581 }
582 "#,
583 "j", 531 "j",
584 r#" 532 r#"
585 struct Foo { 533struct Foo { i<|>: i32 }
586 j: i32, 534struct Bar { i: i32 }
587 }
588 535
589 struct Bar { 536impl Bar {
590 i: i32, 537 fn new(i: i32) -> Self {
538 Self { i }
591 } 539 }
540}
541"#,
542 r#"
543struct Foo { j: i32 }
544struct Bar { i: i32 }
592 545
593 impl Bar { 546impl Bar {
594 fn new(i: i32) -> Self { 547 fn new(i: i32) -> Self {
595 Self { i } 548 Self { i }
596 }
597 } 549 }
598 "#, 550}
551"#,
599 ); 552 );
600 } 553 }
601 554
602 #[test] 555 #[test]
603 fn test_shadow_local_for_struct_shorthand() { 556 fn test_shadow_local_for_struct_shorthand() {
604 test_rename( 557 check(
558 "j",
605 r#" 559 r#"
606 struct Foo { 560struct Foo { i: i32 }
607 i: i32,
608 }
609 561
610 fn baz(i<|>: i32) -> Self { 562fn baz(i<|>: i32) -> Self {
611 let x = Foo { i }; 563 let x = Foo { i };
612 { 564 {
613 let i = 0; 565 let i = 0;
614 Foo { i } 566 Foo { i }
615 }
616 } 567 }
617 "#, 568}
618 "j", 569"#,
619 r#" 570 r#"
620 struct Foo { 571struct Foo { i: i32 }
621 i: i32,
622 }
623 572
624 fn baz(j: i32) -> Self { 573fn baz(j: i32) -> Self {
625 let x = Foo { i: j }; 574 let x = Foo { i: j };
626 { 575 {
627 let i = 0; 576 let i = 0;
628 Foo { i } 577 Foo { i }
629 }
630 } 578 }
631 "#, 579}
580"#,
632 ); 581 );
633 } 582 }
634 583
635 #[test] 584 #[test]
636 fn test_rename_mod() { 585 fn test_rename_mod() {
637 let (analysis, position) = analysis_and_position( 586 check_expect(
587 "foo2",
638 r#" 588 r#"
639//- /lib.rs 589//- /lib.rs
640mod bar; 590mod bar;
@@ -643,53 +593,49 @@ mod bar;
643mod foo<|>; 593mod foo<|>;
644 594
645//- /bar/foo.rs 595//- /bar/foo.rs
646// emtpy 596// empty
647 "#, 597"#,
648 ); 598 expect![[r#"
649 let new_name = "foo2"; 599 RangeInfo {
650 let source_change = analysis.rename(position, new_name).unwrap(); 600 range: 4..7,
651 assert_debug_snapshot!(&source_change, 601 info: SourceChange {
652@r###" 602 source_file_edits: [
653 Some( 603 SourceFileEdit {
654 RangeInfo { 604 file_id: FileId(
655 range: 4..7, 605 2,
656 info: SourceChange { 606 ),
657 source_file_edits: [ 607 edit: TextEdit {
658 SourceFileEdit { 608 indels: [
659 file_id: FileId( 609 Indel {
660 2, 610 insert: "foo2",
661 ), 611 delete: 4..7,
662 edit: TextEdit { 612 },
663 indels: [ 613 ],
664 Indel { 614 },
665 insert: "foo2",
666 delete: 4..7,
667 },
668 ],
669 }, 615 },
670 }, 616 ],
671 ], 617 file_system_edits: [
672 file_system_edits: [ 618 MoveFile {
673 MoveFile { 619 src: FileId(
674 src: FileId( 620 3,
675 3, 621 ),
676 ), 622 anchor: FileId(
677 anchor: FileId( 623 3,
678 2, 624 ),
679 ), 625 dst: "foo2.rs",
680 dst: "foo2.rs", 626 },
681 }, 627 ],
682 ], 628 is_snippet: false,
683 is_snippet: false, 629 },
684 }, 630 }
685 }, 631 "#]],
686 ) 632 );
687 "###);
688 } 633 }
689 634
690 #[test] 635 #[test]
691 fn test_rename_mod_in_use_tree() { 636 fn test_rename_mod_in_use_tree() {
692 let (analysis, position) = analysis_and_position( 637 check_expect(
638 "quux",
693 r#" 639 r#"
694//- /main.rs 640//- /main.rs
695pub mod foo; 641pub mod foo;
@@ -701,140 +647,173 @@ pub struct FooContent;
701 647
702//- /bar.rs 648//- /bar.rs
703use crate::foo<|>::FooContent; 649use crate::foo<|>::FooContent;
704 "#, 650"#,
705 ); 651 expect![[r#"
706 let new_name = "qux"; 652 RangeInfo {
707 let source_change = analysis.rename(position, new_name).unwrap(); 653 range: 11..14,
708 assert_debug_snapshot!(&source_change, 654 info: SourceChange {
709@r###" 655 source_file_edits: [
710 Some( 656 SourceFileEdit {
711 RangeInfo { 657 file_id: FileId(
712 range: 11..14, 658 1,
713 info: SourceChange { 659 ),
714 source_file_edits: [ 660 edit: TextEdit {
715 SourceFileEdit { 661 indels: [
716 file_id: FileId( 662 Indel {
717 1, 663 insert: "quux",
718 ), 664 delete: 8..11,
719 edit: TextEdit { 665 },
720 indels: [ 666 ],
721 Indel { 667 },
722 insert: "qux",
723 delete: 8..11,
724 },
725 ],
726 }, 668 },
727 }, 669 SourceFileEdit {
728 SourceFileEdit { 670 file_id: FileId(
729 file_id: FileId( 671 3,
730 3, 672 ),
731 ), 673 edit: TextEdit {
732 edit: TextEdit { 674 indels: [
733 indels: [ 675 Indel {
734 Indel { 676 insert: "quux",
735 insert: "qux", 677 delete: 11..14,
736 delete: 11..14, 678 },
737 }, 679 ],
738 ], 680 },
739 }, 681 },
740 }, 682 ],
741 ], 683 file_system_edits: [
742 file_system_edits: [ 684 MoveFile {
743 MoveFile { 685 src: FileId(
744 src: FileId( 686 2,
745 2, 687 ),
746 ), 688 anchor: FileId(
747 anchor: FileId( 689 2,
748 3, 690 ),
749 ), 691 dst: "quux.rs",
750 dst: "qux.rs", 692 },
751 }, 693 ],
752 ], 694 is_snippet: false,
753 is_snippet: false, 695 },
754 }, 696 }
755 }, 697 "#]],
756 ) 698 );
757 "###);
758 } 699 }
759 700
760 #[test] 701 #[test]
761 fn test_rename_mod_in_dir() { 702 fn test_rename_mod_in_dir() {
762 let (analysis, position) = analysis_and_position( 703 check_expect(
704 "foo2",
763 r#" 705 r#"
764//- /lib.rs 706//- /lib.rs
765mod fo<|>o; 707mod fo<|>o;
766//- /foo/mod.rs 708//- /foo/mod.rs
767// emtpy 709// emtpy
768 "#, 710"#,
769 ); 711 expect![[r#"
770 let new_name = "foo2"; 712 RangeInfo {
771 let source_change = analysis.rename(position, new_name).unwrap(); 713 range: 4..7,
772 assert_debug_snapshot!(&source_change, 714 info: SourceChange {
773 @r###" 715 source_file_edits: [
774 Some( 716 SourceFileEdit {
775 RangeInfo { 717 file_id: FileId(
776 range: 4..7, 718 1,
777 info: SourceChange { 719 ),
778 source_file_edits: [ 720 edit: TextEdit {
779 SourceFileEdit { 721 indels: [
780 file_id: FileId( 722 Indel {
781 1, 723 insert: "foo2",
782 ), 724 delete: 4..7,
783 edit: TextEdit { 725 },
784 indels: [ 726 ],
785 Indel { 727 },
786 insert: "foo2",
787 delete: 4..7,
788 },
789 ],
790 }, 728 },
791 }, 729 ],
792 ], 730 file_system_edits: [
793 file_system_edits: [ 731 MoveFile {
794 MoveFile { 732 src: FileId(
795 src: FileId( 733 2,
796 2, 734 ),
797 ), 735 anchor: FileId(
798 anchor: FileId( 736 2,
799 1, 737 ),
800 ), 738 dst: "../foo2/mod.rs",
801 dst: "../foo2/mod.rs", 739 },
802 }, 740 ],
803 ], 741 is_snippet: false,
804 is_snippet: false, 742 },
805 }, 743 }
806 }, 744 "#]],
807 ) 745 );
808 "###
809 );
810 } 746 }
811 747
812 #[test] 748 #[test]
813 fn test_module_rename_in_path() { 749 fn test_rename_unusually_nested_mod() {
814 test_rename( 750 check_expect(
751 "bar",
815 r#" 752 r#"
816 mod <|>foo { 753//- /lib.rs
817 pub fn bar() {} 754mod outer { mod fo<|>o; }
755
756//- /outer/foo.rs
757// emtpy
758"#,
759 expect![[r#"
760 RangeInfo {
761 range: 16..19,
762 info: SourceChange {
763 source_file_edits: [
764 SourceFileEdit {
765 file_id: FileId(
766 1,
767 ),
768 edit: TextEdit {
769 indels: [
770 Indel {
771 insert: "bar",
772 delete: 16..19,
773 },
774 ],
775 },
776 },
777 ],
778 file_system_edits: [
779 MoveFile {
780 src: FileId(
781 2,
782 ),
783 anchor: FileId(
784 2,
785 ),
786 dst: "bar.rs",
787 },
788 ],
789 is_snippet: false,
790 },
791 }
792 "#]],
793 );
818 } 794 }
819 795
820 fn main() { 796 #[test]
821 foo::bar(); 797 fn test_module_rename_in_path() {
822 }"#, 798 check(
823 "baz", 799 "baz",
824 r#" 800 r#"
825 mod baz { 801mod <|>foo { pub fn bar() {} }
826 pub fn bar() {}
827 }
828 802
829 fn main() { 803fn main() { foo::bar(); }
830 baz::bar(); 804"#,
831 }"#, 805 r#"
806mod baz { pub fn bar() {} }
807
808fn main() { baz::bar(); }
809"#,
832 ); 810 );
833 } 811 }
834 812
835 #[test] 813 #[test]
836 fn test_rename_mod_filename_and_path() { 814 fn test_rename_mod_filename_and_path() {
837 let (analysis, position) = analysis_and_position( 815 check_expect(
816 "foo2",
838 r#" 817 r#"
839//- /lib.rs 818//- /lib.rs
840mod bar; 819mod bar;
@@ -847,229 +826,185 @@ pub mod foo<|>;
847 826
848//- /bar/foo.rs 827//- /bar/foo.rs
849// pub fn fun() {} 828// pub fn fun() {}
850 "#, 829"#,
851 ); 830 expect![[r#"
852 let new_name = "foo2"; 831 RangeInfo {
853 let source_change = analysis.rename(position, new_name).unwrap(); 832 range: 8..11,
854 assert_debug_snapshot!(&source_change, 833 info: SourceChange {
855@r###" 834 source_file_edits: [
856 Some( 835 SourceFileEdit {
857 RangeInfo { 836 file_id: FileId(
858 range: 8..11, 837 2,
859 info: SourceChange { 838 ),
860 source_file_edits: [ 839 edit: TextEdit {
861 SourceFileEdit { 840 indels: [
862 file_id: FileId( 841 Indel {
863 2, 842 insert: "foo2",
864 ), 843 delete: 8..11,
865 edit: TextEdit { 844 },
866 indels: [ 845 ],
867 Indel { 846 },
868 insert: "foo2",
869 delete: 8..11,
870 },
871 ],
872 }, 847 },
873 }, 848 SourceFileEdit {
874 SourceFileEdit { 849 file_id: FileId(
875 file_id: FileId( 850 1,
876 1, 851 ),
877 ), 852 edit: TextEdit {
878 edit: TextEdit { 853 indels: [
879 indels: [ 854 Indel {
880 Indel { 855 insert: "foo2",
881 insert: "foo2", 856 delete: 27..30,
882 delete: 27..30, 857 },
883 }, 858 ],
884 ], 859 },
885 }, 860 },
886 }, 861 ],
887 ], 862 file_system_edits: [
888 file_system_edits: [ 863 MoveFile {
889 MoveFile { 864 src: FileId(
890 src: FileId( 865 3,
891 3, 866 ),
892 ), 867 anchor: FileId(
893 anchor: FileId( 868 3,
894 2, 869 ),
895 ), 870 dst: "foo2.rs",
896 dst: "foo2.rs", 871 },
897 }, 872 ],
898 ], 873 is_snippet: false,
899 is_snippet: false, 874 },
900 }, 875 }
901 }, 876 "#]],
902 ) 877 );
903 "###);
904 } 878 }
905 879
906 #[test] 880 #[test]
907 fn test_enum_variant_from_module_1() { 881 fn test_enum_variant_from_module_1() {
908 test_rename( 882 check(
883 "Baz",
909 r#" 884 r#"
910 mod foo { 885mod foo {
911 pub enum Foo { 886 pub enum Foo { Bar<|> }
912 Bar<|>, 887}
913 }
914 }
915 888
916 fn func(f: foo::Foo) { 889fn func(f: foo::Foo) {
917 match f { 890 match f {
918 foo::Foo::Bar => {} 891 foo::Foo::Bar => {}
919 }
920 } 892 }
921 "#, 893}
922 "Baz", 894"#,
923 r#" 895 r#"
924 mod foo { 896mod foo {
925 pub enum Foo { 897 pub enum Foo { Baz }
926 Baz, 898}
927 }
928 }
929 899
930 fn func(f: foo::Foo) { 900fn func(f: foo::Foo) {
931 match f { 901 match f {
932 foo::Foo::Baz => {} 902 foo::Foo::Baz => {}
933 }
934 } 903 }
935 "#, 904}
905"#,
936 ); 906 );
937 } 907 }
938 908
939 #[test] 909 #[test]
940 fn test_enum_variant_from_module_2() { 910 fn test_enum_variant_from_module_2() {
941 test_rename( 911 check(
912 "baz",
942 r#" 913 r#"
943 mod foo { 914mod foo {
944 pub struct Foo { 915 pub struct Foo { pub bar<|>: uint }
945 pub bar<|>: uint, 916}
946 }
947 }
948 917
949 fn foo(f: foo::Foo) { 918fn foo(f: foo::Foo) {
950 let _ = f.bar; 919 let _ = f.bar;
951 } 920}
952 "#, 921"#,
953 "baz",
954 r#" 922 r#"
955 mod foo { 923mod foo {
956 pub struct Foo { 924 pub struct Foo { pub baz: uint }
957 pub baz: uint, 925}
958 }
959 }
960 926
961 fn foo(f: foo::Foo) { 927fn foo(f: foo::Foo) {
962 let _ = f.baz; 928 let _ = f.baz;
963 } 929}
964 "#, 930"#,
965 ); 931 );
966 } 932 }
967 933
968 #[test] 934 #[test]
969 fn test_parameter_to_self() { 935 fn test_parameter_to_self() {
970 test_rename( 936 check(
937 "self",
971 r#" 938 r#"
972 struct Foo { 939struct Foo { i: i32 }
973 i: i32,
974 }
975 940
976 impl Foo { 941impl Foo {
977 fn f(foo<|>: &mut Foo) -> i32 { 942 fn f(foo<|>: &mut Foo) -> i32 {
978 foo.i 943 foo.i
979 }
980 } 944 }
981 "#, 945}
982 "self", 946"#,
983 r#" 947 r#"
984 struct Foo { 948struct Foo { i: i32 }
985 i: i32,
986 }
987 949
988 impl Foo { 950impl Foo {
989 fn f(&mut self) -> i32 { 951 fn f(&mut self) -> i32 {
990 self.i 952 self.i
991 }
992 } 953 }
993 "#, 954}
955"#,
994 ); 956 );
995 } 957 }
996 958
997 #[test] 959 #[test]
998 fn test_self_to_parameter() { 960 fn test_self_to_parameter() {
999 test_rename( 961 check(
962 "foo",
1000 r#" 963 r#"
1001 struct Foo { 964struct Foo { i: i32 }
1002 i: i32,
1003 }
1004 965
1005 impl Foo { 966impl Foo {
1006 fn f(&mut <|>self) -> i32 { 967 fn f(&mut <|>self) -> i32 {
1007 self.i 968 self.i
1008 }
1009 } 969 }
1010 "#, 970}
1011 "foo", 971"#,
1012 r#" 972 r#"
1013 struct Foo { 973struct Foo { i: i32 }
1014 i: i32,
1015 }
1016 974
1017 impl Foo { 975impl Foo {
1018 fn f(foo: &mut Foo) -> i32 { 976 fn f(foo: &mut Foo) -> i32 {
1019 foo.i 977 foo.i
1020 }
1021 } 978 }
1022 "#, 979}
980"#,
1023 ); 981 );
1024 } 982 }
1025 983
1026 #[test] 984 #[test]
1027 fn test_self_in_path_to_parameter() { 985 fn test_self_in_path_to_parameter() {
1028 test_rename( 986 check(
987 "foo",
1029 r#" 988 r#"
1030 struct Foo { 989struct Foo { i: i32 }
1031 i: i32,
1032 }
1033 990
1034 impl Foo { 991impl Foo {
1035 fn f(&self) -> i32 { 992 fn f(&self) -> i32 {
1036 let self_var = 1; 993 let self_var = 1;
1037 self<|>.i 994 self<|>.i
1038 }
1039 } 995 }
1040 "#, 996}
1041 "foo", 997"#,
1042 r#" 998 r#"
1043 struct Foo { 999struct Foo { i: i32 }
1044 i: i32,
1045 }
1046 1000
1047 impl Foo { 1001impl Foo {
1048 fn f(foo: &Foo) -> i32 { 1002 fn f(foo: &Foo) -> i32 {
1049 let self_var = 1; 1003 let self_var = 1;
1050 foo.i 1004 foo.i
1051 }
1052 } 1005 }
1053 "#, 1006}
1007"#,
1054 ); 1008 );
1055 } 1009 }
1056
1057 fn test_rename(ra_fixture_before: &str, new_name: &str, ra_fixture_after: &str) {
1058 let ra_fixture_after = &trim_indent(ra_fixture_after);
1059 let (analysis, position) = analysis_and_position(ra_fixture_before);
1060 let source_change = analysis.rename(position, new_name).unwrap();
1061 let mut text_edit_builder = TextEditBuilder::default();
1062 let mut file_id: Option<FileId> = None;
1063 if let Some(change) = source_change {
1064 for edit in change.info.source_file_edits {
1065 file_id = Some(edit.file_id);
1066 for indel in edit.edit.into_iter() {
1067 text_edit_builder.replace(indel.delete, indel.insert);
1068 }
1069 }
1070 }
1071 let mut result = analysis.file_text(file_id.unwrap()).unwrap().to_string();
1072 text_edit_builder.finish().apply(&mut result);
1073 assert_eq_text!(ra_fixture_after, &*result);
1074 }
1075} 1010}