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.rs665
1 files changed, 324 insertions, 341 deletions
diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs
index b6a2266b4..f4b11cbd0 100644
--- a/crates/ra_ide/src/references/rename.rs
+++ b/crates/ra_ide/src/references/rename.rs
@@ -278,43 +278,67 @@ mod tests {
278 278
279 use crate::{mock_analysis::analysis_and_position, FileId}; 279 use crate::{mock_analysis::analysis_and_position, FileId};
280 280
281 fn check(new_name: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
282 let ra_fixture_after = &trim_indent(ra_fixture_after);
283 let (analysis, position) = analysis_and_position(ra_fixture_before);
284 let source_change = analysis.rename(position, new_name).unwrap();
285 let mut text_edit_builder = TextEditBuilder::default();
286 let mut file_id: Option<FileId> = None;
287 if let Some(change) = source_change {
288 for edit in change.info.source_file_edits {
289 file_id = Some(edit.file_id);
290 for indel in edit.edit.into_iter() {
291 text_edit_builder.replace(indel.delete, indel.insert);
292 }
293 }
294 }
295 let mut result = analysis.file_text(file_id.unwrap()).unwrap().to_string();
296 text_edit_builder.finish().apply(&mut result);
297 assert_eq_text!(ra_fixture_after, &*result);
298 }
299
281 #[test] 300 #[test]
282 fn test_rename_to_underscore() { 301 fn test_rename_to_underscore() {
283 test_rename( 302 check(
284 r#"
285 fn main() {
286 let i<|> = 1;
287 }"#,
288 "_", 303 "_",
289 r#" 304 r#"
290 fn main() { 305fn main() {
291 let _ = 1; 306 let i<|> = 1;
292 }"#, 307}
308"#,
309 r#"
310fn main() {
311 let _ = 1;
312}
313"#,
293 ); 314 );
294 } 315 }
295 316
296 #[test] 317 #[test]
297 fn test_rename_to_raw_identifier() { 318 fn test_rename_to_raw_identifier() {
298 test_rename( 319 check(
299 r#"
300 fn main() {
301 let i<|> = 1;
302 }"#,
303 "r#fn", 320 "r#fn",
304 r#" 321 r#"
305 fn main() { 322fn main() {
306 let r#fn = 1; 323 let i<|> = 1;
307 }"#, 324}
325"#,
326 r#"
327fn main() {
328 let r#fn = 1;
329}
330"#,
308 ); 331 );
309 } 332 }
310 333
311 #[test] 334 #[test]
312 fn test_rename_to_invalid_identifier() { 335 fn test_rename_to_invalid_identifier() {
313 let (analysis, position) = analysis_and_position( 336 let (analysis, position) = analysis_and_position(
314 " 337 r#"
315 fn main() { 338fn main() {
316 let i<|> = 1; 339 let i<|> = 1;
317 }", 340}
341"#,
318 ); 342 );
319 let new_name = "invalid!"; 343 let new_name = "invalid!";
320 let source_change = analysis.rename(position, new_name).unwrap(); 344 let source_change = analysis.rename(position, new_name).unwrap();
@@ -323,312 +347,304 @@ mod tests {
323 347
324 #[test] 348 #[test]
325 fn test_rename_for_local() { 349 fn test_rename_for_local() {
326 test_rename( 350 check(
351 "k",
327 r#" 352 r#"
328 fn main() { 353fn main() {
329 let mut i = 1; 354 let mut i = 1;
330 let j = 1; 355 let j = 1;
331 i = i<|> + j; 356 i = i<|> + j;
332 357
333 { 358 {
334 i = 0; 359 i = 0;
335 } 360 }
336 361
337 i = 5; 362 i = 5;
338 }"#, 363}
339 "k", 364"#,
340 r#" 365 r#"
341 fn main() { 366fn main() {
342 let mut k = 1; 367 let mut k = 1;
343 let j = 1; 368 let j = 1;
344 k = k + j; 369 k = k + j;
345 370
346 { 371 {
347 k = 0; 372 k = 0;
348 } 373 }
349 374
350 k = 5; 375 k = 5;
351 }"#, 376}
377"#,
352 ); 378 );
353 } 379 }
354 380
355 #[test] 381 #[test]
356 fn test_rename_for_macro_args() { 382 fn test_rename_for_macro_args() {
357 test_rename( 383 check(
358 r#"
359 macro_rules! foo {($i:ident) => {$i} }
360 fn main() {
361 let a<|> = "test";
362 foo!(a);
363 }"#,
364 "b", 384 "b",
365 r#" 385 r#"
366 macro_rules! foo {($i:ident) => {$i} } 386macro_rules! foo {($i:ident) => {$i} }
367 fn main() { 387fn main() {
368 let b = "test"; 388 let a<|> = "test";
369 foo!(b); 389 foo!(a);
370 }"#, 390}
391"#,
392 r#"
393macro_rules! foo {($i:ident) => {$i} }
394fn main() {
395 let b = "test";
396 foo!(b);
397}
398"#,
371 ); 399 );
372 } 400 }
373 401
374 #[test] 402 #[test]
375 fn test_rename_for_macro_args_rev() { 403 fn test_rename_for_macro_args_rev() {
376 test_rename( 404 check(
377 r#"
378 macro_rules! foo {($i:ident) => {$i} }
379 fn main() {
380 let a = "test";
381 foo!(a<|>);
382 }"#,
383 "b", 405 "b",
384 r#" 406 r#"
385 macro_rules! foo {($i:ident) => {$i} } 407macro_rules! foo {($i:ident) => {$i} }
386 fn main() { 408fn main() {
387 let b = "test"; 409 let a = "test";
388 foo!(b); 410 foo!(a<|>);
389 }"#, 411}
412"#,
413 r#"
414macro_rules! foo {($i:ident) => {$i} }
415fn main() {
416 let b = "test";
417 foo!(b);
418}
419"#,
390 ); 420 );
391 } 421 }
392 422
393 #[test] 423 #[test]
394 fn test_rename_for_macro_define_fn() { 424 fn test_rename_for_macro_define_fn() {
395 test_rename( 425 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", 426 "bar",
403 r#" 427 r#"
404 macro_rules! define_fn {($id:ident) => { fn $id{} }} 428macro_rules! define_fn {($id:ident) => { fn $id{} }}
405 define_fn!(bar); 429define_fn!(foo);
406 fn main() { 430fn main() {
407 bar(); 431 fo<|>o();
408 }"#, 432}
433"#,
434 r#"
435macro_rules! define_fn {($id:ident) => { fn $id{} }}
436define_fn!(bar);
437fn main() {
438 bar();
439}
440"#,
409 ); 441 );
410 } 442 }
411 443
412 #[test] 444 #[test]
413 fn test_rename_for_macro_define_fn_rev() { 445 fn test_rename_for_macro_define_fn_rev() {
414 test_rename( 446 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", 447 "bar",
422 r#" 448 r#"
423 macro_rules! define_fn {($id:ident) => { fn $id{} }} 449macro_rules! define_fn {($id:ident) => { fn $id{} }}
424 define_fn!(bar); 450define_fn!(fo<|>o);
425 fn main() { 451fn main() {
426 bar(); 452 foo();
427 }"#, 453}
454"#,
455 r#"
456macro_rules! define_fn {($id:ident) => { fn $id{} }}
457define_fn!(bar);
458fn main() {
459 bar();
460}
461"#,
428 ); 462 );
429 } 463 }
430 464
431 #[test] 465 #[test]
432 fn test_rename_for_param_inside() { 466 fn test_rename_for_param_inside() {
433 test_rename( 467 check(
434 r#"
435 fn foo(i : u32) -> u32 {
436 i<|>
437 }"#,
438 "j", 468 "j",
439 r#" 469 r#"
440 fn foo(j : u32) -> u32 { 470fn foo(i : u32) -> u32 {
441 j 471 i<|>
442 }"#, 472}
473"#,
474 r#"
475fn foo(j : u32) -> u32 {
476 j
477}
478"#,
443 ); 479 );
444 } 480 }
445 481
446 #[test] 482 #[test]
447 fn test_rename_refs_for_fn_param() { 483 fn test_rename_refs_for_fn_param() {
448 test_rename( 484 check(
449 r#"
450 fn foo(i<|> : u32) -> u32 {
451 i
452 }"#,
453 "new_name", 485 "new_name",
454 r#" 486 r#"
455 fn foo(new_name : u32) -> u32 { 487fn foo(i<|> : u32) -> u32 {
456 new_name 488 i
457 }"#, 489}
490"#,
491 r#"
492fn foo(new_name : u32) -> u32 {
493 new_name
494}
495"#,
458 ); 496 );
459 } 497 }
460 498
461 #[test] 499 #[test]
462 fn test_rename_for_mut_param() { 500 fn test_rename_for_mut_param() {
463 test_rename( 501 check(
464 r#"
465 fn foo(mut i<|> : u32) -> u32 {
466 i
467 }"#,
468 "new_name", 502 "new_name",
469 r#" 503 r#"
470 fn foo(mut new_name : u32) -> u32 { 504fn foo(mut i<|> : u32) -> u32 {
471 new_name 505 i
472 }"#, 506}
507"#,
508 r#"
509fn foo(mut new_name : u32) -> u32 {
510 new_name
511}
512"#,
473 ); 513 );
474 } 514 }
475 515
476 #[test] 516 #[test]
477 fn test_rename_struct_field() { 517 fn test_rename_struct_field() {
478 test_rename( 518 check(
519 "j",
479 r#" 520 r#"
480 struct Foo { 521struct Foo { i<|>: i32 }
481 i<|>: i32,
482 }
483 522
484 impl Foo { 523impl Foo {
485 fn new(i: i32) -> Self { 524 fn new(i: i32) -> Self {
486 Self { i: i } 525 Self { i: i }
487 }
488 } 526 }
489 "#, 527}
490 "j", 528"#,
491 r#" 529 r#"
492 struct Foo { 530struct Foo { j: i32 }
493 j: i32,
494 }
495 531
496 impl Foo { 532impl Foo {
497 fn new(i: i32) -> Self { 533 fn new(i: i32) -> Self {
498 Self { j: i } 534 Self { j: i }
499 }
500 } 535 }
501 "#, 536}
537"#,
502 ); 538 );
503 } 539 }
504 540
505 #[test] 541 #[test]
506 fn test_rename_struct_field_for_shorthand() { 542 fn test_rename_struct_field_for_shorthand() {
507 mark::check!(test_rename_struct_field_for_shorthand); 543 mark::check!(test_rename_struct_field_for_shorthand);
508 test_rename( 544 check(
545 "j",
509 r#" 546 r#"
510 struct Foo { 547struct Foo { i<|>: i32 }
511 i<|>: i32,
512 }
513 548
514 impl Foo { 549impl Foo {
515 fn new(i: i32) -> Self { 550 fn new(i: i32) -> Self {
516 Self { i } 551 Self { i }
517 }
518 } 552 }
519 "#, 553}
520 "j", 554"#,
521 r#" 555 r#"
522 struct Foo { 556struct Foo { j: i32 }
523 j: i32,
524 }
525 557
526 impl Foo { 558impl Foo {
527 fn new(i: i32) -> Self { 559 fn new(i: i32) -> Self {
528 Self { j: i } 560 Self { j: i }
529 }
530 } 561 }
531 "#, 562}
563"#,
532 ); 564 );
533 } 565 }
534 566
535 #[test] 567 #[test]
536 fn test_rename_local_for_field_shorthand() { 568 fn test_rename_local_for_field_shorthand() {
537 mark::check!(test_rename_local_for_field_shorthand); 569 mark::check!(test_rename_local_for_field_shorthand);
538 test_rename( 570 check(
571 "j",
539 r#" 572 r#"
540 struct Foo { 573struct Foo { i: i32 }
541 i: i32,
542 }
543 574
544 impl Foo { 575impl Foo {
545 fn new(i<|>: i32) -> Self { 576 fn new(i<|>: i32) -> Self {
546 Self { i } 577 Self { i }
547 }
548 } 578 }
549 "#, 579}
550 "j", 580"#,
551 r#" 581 r#"
552 struct Foo { 582struct Foo { i: i32 }
553 i: i32,
554 }
555 583
556 impl Foo { 584impl Foo {
557 fn new(j: i32) -> Self { 585 fn new(j: i32) -> Self {
558 Self { i: j } 586 Self { i: j }
559 }
560 } 587 }
561 "#, 588}
589"#,
562 ); 590 );
563 } 591 }
564 592
565 #[test] 593 #[test]
566 fn test_field_shorthand_correct_struct() { 594 fn test_field_shorthand_correct_struct() {
567 test_rename( 595 check(
596 "j",
568 r#" 597 r#"
569 struct Foo { 598struct Foo { i<|>: i32 }
570 i<|>: i32,
571 }
572 599
573 struct Bar { 600struct Bar { i: i32 }
574 i: i32,
575 }
576 601
577 impl Bar { 602impl Bar {
578 fn new(i: i32) -> Self { 603 fn new(i: i32) -> Self {
579 Self { i } 604 Self { i }
580 }
581 } 605 }
582 "#, 606}
583 "j", 607"#,
584 r#" 608 r#"
585 struct Foo { 609struct Foo { j: i32 }
586 j: i32,
587 }
588 610
589 struct Bar { 611struct Bar { i: i32 }
590 i: i32,
591 }
592 612
593 impl Bar { 613impl Bar {
594 fn new(i: i32) -> Self { 614 fn new(i: i32) -> Self {
595 Self { i } 615 Self { i }
596 }
597 } 616 }
598 "#, 617}
618"#,
599 ); 619 );
600 } 620 }
601 621
602 #[test] 622 #[test]
603 fn test_shadow_local_for_struct_shorthand() { 623 fn test_shadow_local_for_struct_shorthand() {
604 test_rename( 624 check(
625 "j",
605 r#" 626 r#"
606 struct Foo { 627struct Foo { i: i32 }
607 i: i32,
608 }
609 628
610 fn baz(i<|>: i32) -> Self { 629fn baz(i<|>: i32) -> Self {
611 let x = Foo { i }; 630 let x = Foo { i };
612 { 631 {
613 let i = 0; 632 let i = 0;
614 Foo { i } 633 Foo { i }
615 }
616 } 634 }
617 "#, 635}
618 "j", 636"#,
619 r#" 637 r#"
620 struct Foo { 638struct Foo { i: i32 }
621 i: i32,
622 }
623 639
624 fn baz(j: i32) -> Self { 640fn baz(j: i32) -> Self {
625 let x = Foo { i: j }; 641 let x = Foo { i: j };
626 { 642 {
627 let i = 0; 643 let i = 0;
628 Foo { i } 644 Foo { i }
629 }
630 } 645 }
631 "#, 646}
647"#,
632 ); 648 );
633 } 649 }
634 650
@@ -811,24 +827,26 @@ mod fo<|>o;
811 827
812 #[test] 828 #[test]
813 fn test_module_rename_in_path() { 829 fn test_module_rename_in_path() {
814 test_rename( 830 check(
831 "baz",
815 r#" 832 r#"
816 mod <|>foo { 833mod <|>foo {
817 pub fn bar() {} 834 pub fn bar() {}
818 } 835}
819 836
820 fn main() { 837fn main() {
821 foo::bar(); 838 foo::bar();
822 }"#, 839}
823 "baz", 840"#,
824 r#" 841 r#"
825 mod baz { 842mod baz {
826 pub fn bar() {} 843 pub fn bar() {}
827 } 844}
828 845
829 fn main() { 846fn main() {
830 baz::bar(); 847 baz::bar();
831 }"#, 848}
849"#,
832 ); 850 );
833 } 851 }
834 852
@@ -905,171 +923,136 @@ pub mod foo<|>;
905 923
906 #[test] 924 #[test]
907 fn test_enum_variant_from_module_1() { 925 fn test_enum_variant_from_module_1() {
908 test_rename( 926 check(
927 "Baz",
909 r#" 928 r#"
910 mod foo { 929mod foo {
911 pub enum Foo { 930 pub enum Foo {
912 Bar<|>, 931 Bar<|>,
913 }
914 } 932 }
933}
915 934
916 fn func(f: foo::Foo) { 935fn func(f: foo::Foo) {
917 match f { 936 match f {
918 foo::Foo::Bar => {} 937 foo::Foo::Bar => {}
919 }
920 } 938 }
921 "#, 939}
922 "Baz", 940"#,
923 r#" 941 r#"
924 mod foo { 942mod foo {
925 pub enum Foo { 943 pub enum Foo {
926 Baz, 944 Baz,
927 }
928 } 945 }
946}
929 947
930 fn func(f: foo::Foo) { 948fn func(f: foo::Foo) {
931 match f { 949 match f {
932 foo::Foo::Baz => {} 950 foo::Foo::Baz => {}
933 }
934 } 951 }
935 "#, 952}
953"#,
936 ); 954 );
937 } 955 }
938 956
939 #[test] 957 #[test]
940 fn test_enum_variant_from_module_2() { 958 fn test_enum_variant_from_module_2() {
941 test_rename( 959 check(
960 "baz",
942 r#" 961 r#"
943 mod foo { 962mod foo {
944 pub struct Foo { 963 pub struct Foo { pub bar<|>: uint }
945 pub bar<|>: uint, 964}
946 }
947 }
948 965
949 fn foo(f: foo::Foo) { 966fn foo(f: foo::Foo) {
950 let _ = f.bar; 967 let _ = f.bar;
951 } 968}
952 "#, 969"#,
953 "baz",
954 r#" 970 r#"
955 mod foo { 971mod foo {
956 pub struct Foo { 972 pub struct Foo { pub baz: uint }
957 pub baz: uint, 973}
958 }
959 }
960 974
961 fn foo(f: foo::Foo) { 975fn foo(f: foo::Foo) {
962 let _ = f.baz; 976 let _ = f.baz;
963 } 977}
964 "#, 978"#,
965 ); 979 );
966 } 980 }
967 981
968 #[test] 982 #[test]
969 fn test_parameter_to_self() { 983 fn test_parameter_to_self() {
970 test_rename( 984 check(
985 "self",
971 r#" 986 r#"
972 struct Foo { 987struct Foo { i: i32 }
973 i: i32,
974 }
975 988
976 impl Foo { 989impl Foo {
977 fn f(foo<|>: &mut Foo) -> i32 { 990 fn f(foo<|>: &mut Foo) -> i32 {
978 foo.i 991 foo.i
979 }
980 } 992 }
981 "#, 993}
982 "self", 994"#,
983 r#" 995 r#"
984 struct Foo { 996struct Foo { i: i32 }
985 i: i32,
986 }
987 997
988 impl Foo { 998impl Foo {
989 fn f(&mut self) -> i32 { 999 fn f(&mut self) -> i32 {
990 self.i 1000 self.i
991 }
992 } 1001 }
993 "#, 1002}
1003"#,
994 ); 1004 );
995 } 1005 }
996 1006
997 #[test] 1007 #[test]
998 fn test_self_to_parameter() { 1008 fn test_self_to_parameter() {
999 test_rename( 1009 check(
1010 "foo",
1000 r#" 1011 r#"
1001 struct Foo { 1012struct Foo { i: i32 }
1002 i: i32,
1003 }
1004 1013
1005 impl Foo { 1014impl Foo {
1006 fn f(&mut <|>self) -> i32 { 1015 fn f(&mut <|>self) -> i32 {
1007 self.i 1016 self.i
1008 }
1009 } 1017 }
1010 "#, 1018}
1011 "foo", 1019"#,
1012 r#" 1020 r#"
1013 struct Foo { 1021struct Foo { i: i32 }
1014 i: i32,
1015 }
1016 1022
1017 impl Foo { 1023impl Foo {
1018 fn f(foo: &mut Foo) -> i32 { 1024 fn f(foo: &mut Foo) -> i32 {
1019 foo.i 1025 foo.i
1020 }
1021 } 1026 }
1022 "#, 1027}
1028"#,
1023 ); 1029 );
1024 } 1030 }
1025 1031
1026 #[test] 1032 #[test]
1027 fn test_self_in_path_to_parameter() { 1033 fn test_self_in_path_to_parameter() {
1028 test_rename( 1034 check(
1035 "foo",
1029 r#" 1036 r#"
1030 struct Foo { 1037struct Foo { i: i32 }
1031 i: i32,
1032 }
1033 1038
1034 impl Foo { 1039impl Foo {
1035 fn f(&self) -> i32 { 1040 fn f(&self) -> i32 {
1036 let self_var = 1; 1041 let self_var = 1;
1037 self<|>.i 1042 self<|>.i
1038 }
1039 } 1043 }
1040 "#, 1044}
1041 "foo", 1045"#,
1042 r#" 1046 r#"
1043 struct Foo { 1047struct Foo { i: i32 }
1044 i: i32,
1045 }
1046 1048
1047 impl Foo { 1049impl Foo {
1048 fn f(foo: &Foo) -> i32 { 1050 fn f(foo: &Foo) -> i32 {
1049 let self_var = 1; 1051 let self_var = 1;
1050 foo.i 1052 foo.i
1051 }
1052 } 1053 }
1053 "#, 1054}
1055"#,
1054 ); 1056 );
1055 } 1057 }
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} 1058}