aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/references/rename.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/references/rename.rs')
-rw-r--r--crates/ra_ide/src/references/rename.rs117
1 files changed, 91 insertions, 26 deletions
diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs
index fd17bc9f2..2cbb82c1a 100644
--- a/crates/ra_ide/src/references/rename.rs
+++ b/crates/ra_ide/src/references/rename.rs
@@ -7,14 +7,13 @@ use ra_syntax::{
7 algo::find_node_at_offset, ast, lex_single_valid_syntax_kind, AstNode, SyntaxKind, SyntaxNode, 7 algo::find_node_at_offset, ast, lex_single_valid_syntax_kind, AstNode, SyntaxKind, SyntaxNode,
8}; 8};
9use ra_text_edit::TextEdit; 9use ra_text_edit::TextEdit;
10use test_utils::tested_by;
10 11
11use crate::{ 12use crate::{
12 FilePosition, FileSystemEdit, RangeInfo, Reference, ReferenceKind, SourceChange, 13 references::find_all_refs, FilePosition, FileSystemEdit, RangeInfo, Reference, ReferenceKind,
13 SourceFileEdit, TextRange, 14 SourceChange, SourceFileEdit, TextRange,
14}; 15};
15 16
16use super::find_all_refs;
17
18pub(crate) fn rename( 17pub(crate) fn rename(
19 db: &RootDatabase, 18 db: &RootDatabase,
20 position: FilePosition, 19 position: FilePosition,
@@ -52,11 +51,13 @@ fn source_edit_from_reference(reference: Reference, new_name: &str) -> SourceFil
52 let file_id = reference.file_range.file_id; 51 let file_id = reference.file_range.file_id;
53 let range = match reference.kind { 52 let range = match reference.kind {
54 ReferenceKind::FieldShorthandForField => { 53 ReferenceKind::FieldShorthandForField => {
54 tested_by!(test_rename_struct_field_for_shorthand);
55 replacement_text.push_str(new_name); 55 replacement_text.push_str(new_name);
56 replacement_text.push_str(": "); 56 replacement_text.push_str(": ");
57 TextRange::new(reference.file_range.range.start(), reference.file_range.range.start()) 57 TextRange::new(reference.file_range.range.start(), reference.file_range.range.start())
58 } 58 }
59 ReferenceKind::FieldShorthandForLocal => { 59 ReferenceKind::FieldShorthandForLocal => {
60 tested_by!(test_rename_local_for_field_shorthand);
60 replacement_text.push_str(": "); 61 replacement_text.push_str(": ");
61 replacement_text.push_str(new_name); 62 replacement_text.push_str(new_name);
62 TextRange::new(reference.file_range.range.end(), reference.file_range.range.end()) 63 TextRange::new(reference.file_range.range.end(), reference.file_range.range.end())
@@ -121,7 +122,7 @@ fn rename_mod(
121 source_file_edits.extend(ref_edits); 122 source_file_edits.extend(ref_edits);
122 } 123 }
123 124
124 Some(SourceChange::from_edits("rename", source_file_edits, file_system_edits)) 125 Some(SourceChange::from_edits("Rename", source_file_edits, file_system_edits))
125} 126}
126 127
127fn rename_reference( 128fn rename_reference(
@@ -140,14 +141,14 @@ fn rename_reference(
140 return None; 141 return None;
141 } 142 }
142 143
143 Some(RangeInfo::new(range, SourceChange::source_file_edits("rename", edit))) 144 Some(RangeInfo::new(range, SourceChange::source_file_edits("Rename", edit)))
144} 145}
145 146
146#[cfg(test)] 147#[cfg(test)]
147mod tests { 148mod tests {
148 use insta::assert_debug_snapshot; 149 use insta::assert_debug_snapshot;
149 use ra_text_edit::TextEditBuilder; 150 use ra_text_edit::TextEditBuilder;
150 use test_utils::assert_eq_text; 151 use test_utils::{assert_eq_text, covers};
151 152
152 use crate::{ 153 use crate::{
153 mock_analysis::analysis_and_position, mock_analysis::single_file_with_position, FileId, 154 mock_analysis::analysis_and_position, mock_analysis::single_file_with_position, FileId,
@@ -379,6 +380,7 @@ mod tests {
379 380
380 #[test] 381 #[test]
381 fn test_rename_struct_field_for_shorthand() { 382 fn test_rename_struct_field_for_shorthand() {
383 covers!(test_rename_struct_field_for_shorthand);
382 test_rename( 384 test_rename(
383 r#" 385 r#"
384 struct Foo { 386 struct Foo {
@@ -408,6 +410,7 @@ mod tests {
408 410
409 #[test] 411 #[test]
410 fn test_rename_local_for_field_shorthand() { 412 fn test_rename_local_for_field_shorthand() {
413 covers!(test_rename_local_for_field_shorthand);
411 test_rename( 414 test_rename(
412 r#" 415 r#"
413 struct Foo { 416 struct Foo {
@@ -527,17 +530,17 @@ mod tests {
527 RangeInfo { 530 RangeInfo {
528 range: 4..7, 531 range: 4..7,
529 info: SourceChange { 532 info: SourceChange {
530 label: "rename", 533 label: "Rename",
531 source_file_edits: [ 534 source_file_edits: [
532 SourceFileEdit { 535 SourceFileEdit {
533 file_id: FileId( 536 file_id: FileId(
534 2, 537 2,
535 ), 538 ),
536 edit: TextEdit { 539 edit: TextEdit {
537 atoms: [ 540 indels: [
538 AtomTextEdit { 541 Indel {
539 delete: 4..7,
540 insert: "foo2", 542 insert: "foo2",
543 delete: 4..7,
541 }, 544 },
542 ], 545 ],
543 }, 546 },
@@ -579,17 +582,17 @@ mod tests {
579 RangeInfo { 582 RangeInfo {
580 range: 4..7, 583 range: 4..7,
581 info: SourceChange { 584 info: SourceChange {
582 label: "rename", 585 label: "Rename",
583 source_file_edits: [ 586 source_file_edits: [
584 SourceFileEdit { 587 SourceFileEdit {
585 file_id: FileId( 588 file_id: FileId(
586 1, 589 1,
587 ), 590 ),
588 edit: TextEdit { 591 edit: TextEdit {
589 atoms: [ 592 indels: [
590 AtomTextEdit { 593 Indel {
591 delete: 4..7,
592 insert: "foo2", 594 insert: "foo2",
595 delete: 4..7,
593 }, 596 },
594 ], 597 ],
595 }, 598 },
@@ -662,17 +665,17 @@ mod tests {
662 RangeInfo { 665 RangeInfo {
663 range: 8..11, 666 range: 8..11,
664 info: SourceChange { 667 info: SourceChange {
665 label: "rename", 668 label: "Rename",
666 source_file_edits: [ 669 source_file_edits: [
667 SourceFileEdit { 670 SourceFileEdit {
668 file_id: FileId( 671 file_id: FileId(
669 2, 672 2,
670 ), 673 ),
671 edit: TextEdit { 674 edit: TextEdit {
672 atoms: [ 675 indels: [
673 AtomTextEdit { 676 Indel {
674 delete: 8..11,
675 insert: "foo2", 677 insert: "foo2",
678 delete: 8..11,
676 }, 679 },
677 ], 680 ],
678 }, 681 },
@@ -682,10 +685,10 @@ mod tests {
682 1, 685 1,
683 ), 686 ),
684 edit: TextEdit { 687 edit: TextEdit {
685 atoms: [ 688 indels: [
686 AtomTextEdit { 689 Indel {
687 delete: 27..30,
688 insert: "foo2", 690 insert: "foo2",
691 delete: 27..30,
689 }, 692 },
690 ], 693 ],
691 }, 694 },
@@ -709,6 +712,68 @@ mod tests {
709 "###); 712 "###);
710 } 713 }
711 714
715 #[test]
716 fn test_enum_variant_from_module_1() {
717 test_rename(
718 r#"
719 mod foo {
720 pub enum Foo {
721 Bar<|>,
722 }
723 }
724
725 fn func(f: foo::Foo) {
726 match f {
727 foo::Foo::Bar => {}
728 }
729 }
730 "#,
731 "Baz",
732 r#"
733 mod foo {
734 pub enum Foo {
735 Baz,
736 }
737 }
738
739 fn func(f: foo::Foo) {
740 match f {
741 foo::Foo::Baz => {}
742 }
743 }
744 "#,
745 );
746 }
747
748 #[test]
749 fn test_enum_variant_from_module_2() {
750 test_rename(
751 r#"
752 mod foo {
753 pub struct Foo {
754 pub bar<|>: uint,
755 }
756 }
757
758 fn foo(f: foo::Foo) {
759 let _ = f.bar;
760 }
761 "#,
762 "baz",
763 r#"
764 mod foo {
765 pub struct Foo {
766 pub baz: uint,
767 }
768 }
769
770 fn foo(f: foo::Foo) {
771 let _ = f.baz;
772 }
773 "#,
774 );
775 }
776
712 fn test_rename(text: &str, new_name: &str, expected: &str) { 777 fn test_rename(text: &str, new_name: &str, expected: &str) {
713 let (analysis, position) = single_file_with_position(text); 778 let (analysis, position) = single_file_with_position(text);
714 let source_change = analysis.rename(position, new_name).unwrap(); 779 let source_change = analysis.rename(position, new_name).unwrap();
@@ -717,13 +782,13 @@ mod tests {
717 if let Some(change) = source_change { 782 if let Some(change) = source_change {
718 for edit in change.info.source_file_edits { 783 for edit in change.info.source_file_edits {
719 file_id = Some(edit.file_id); 784 file_id = Some(edit.file_id);
720 for atom in edit.edit.as_atoms() { 785 for indel in edit.edit.as_indels() {
721 text_edit_builder.replace(atom.delete, atom.insert.clone()); 786 text_edit_builder.replace(indel.delete, indel.insert.clone());
722 } 787 }
723 } 788 }
724 } 789 }
725 let result = 790 let mut result = analysis.file_text(file_id.unwrap()).unwrap().to_string();
726 text_edit_builder.finish().apply(&*analysis.file_text(file_id.unwrap()).unwrap()); 791 text_edit_builder.finish().apply(&mut result);
727 assert_eq_text!(expected, &*result); 792 assert_eq_text!(expected, &*result);
728 } 793 }
729} 794}