diff options
Diffstat (limited to 'crates/ra_ide/src/references/rename.rs')
-rw-r--r-- | crates/ra_ide/src/references/rename.rs | 117 |
1 files changed, 54 insertions, 63 deletions
diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs index 546224b50..99c2581b7 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use hir::{Module, ModuleDef, ModuleSource, Semantics}; | 3 | use hir::{Module, ModuleDef, ModuleSource, Semantics}; |
4 | use ra_db::{RelativePath, RelativePathBuf, SourceDatabaseExt}; | 4 | use ra_db::{RelativePathBuf, SourceDatabaseExt}; |
5 | use ra_ide_db::{ | 5 | use ra_ide_db::{ |
6 | defs::{classify_name, classify_name_ref, Definition, NameClass, NameRefClass}, | 6 | defs::{classify_name, classify_name_ref, Definition, NameClass, NameRefClass}, |
7 | RootDatabase, | 7 | RootDatabase, |
@@ -111,23 +111,14 @@ fn rename_mod( | |||
111 | ModuleSource::SourceFile(..) => { | 111 | ModuleSource::SourceFile(..) => { |
112 | let mod_path: RelativePathBuf = db.file_relative_path(file_id); | 112 | let mod_path: RelativePathBuf = db.file_relative_path(file_id); |
113 | // mod is defined in path/to/dir/mod.rs | 113 | // mod is defined in path/to/dir/mod.rs |
114 | let dst_path = if mod_path.file_stem() == Some("mod") { | 114 | let dst = if mod_path.file_stem() == Some("mod") { |
115 | mod_path | 115 | format!("../{}/mod.rs", new_name) |
116 | .parent() | ||
117 | .and_then(|p| p.parent()) | ||
118 | .or_else(|| Some(RelativePath::new(""))) | ||
119 | .map(|p| p.join(new_name).join("mod.rs")) | ||
120 | } else { | 116 | } else { |
121 | Some(mod_path.with_file_name(new_name).with_extension("rs")) | 117 | format!("{}.rs", new_name) |
122 | }; | 118 | }; |
123 | if let Some(path) = dst_path { | 119 | let move_file = |
124 | let move_file = FileSystemEdit::MoveFile { | 120 | FileSystemEdit::MoveFile { src: file_id, anchor: position.file_id, dst }; |
125 | src: file_id, | 121 | file_system_edits.push(move_file); |
126 | dst_source_root: db.file_source_root(position.file_id), | ||
127 | dst_path: path, | ||
128 | }; | ||
129 | file_system_edits.push(move_file); | ||
130 | } | ||
131 | } | 122 | } |
132 | ModuleSource::Module(..) => {} | 123 | ModuleSource::Module(..) => {} |
133 | } | 124 | } |
@@ -644,16 +635,16 @@ mod tests { | |||
644 | #[test] | 635 | #[test] |
645 | fn test_rename_mod() { | 636 | fn test_rename_mod() { |
646 | let (analysis, position) = analysis_and_position( | 637 | let (analysis, position) = analysis_and_position( |
647 | " | 638 | r#" |
648 | //- /lib.rs | 639 | //- /lib.rs |
649 | mod bar; | 640 | mod bar; |
650 | 641 | ||
651 | //- /bar.rs | 642 | //- /bar.rs |
652 | mod foo<|>; | 643 | mod foo<|>; |
653 | 644 | ||
654 | //- /bar/foo.rs | 645 | //- /bar/foo.rs |
655 | // emtpy | 646 | // emtpy |
656 | ", | 647 | "#, |
657 | ); | 648 | ); |
658 | let new_name = "foo2"; | 649 | let new_name = "foo2"; |
659 | let source_change = analysis.rename(position, new_name).unwrap(); | 650 | let source_change = analysis.rename(position, new_name).unwrap(); |
@@ -683,10 +674,10 @@ mod tests { | |||
683 | src: FileId( | 674 | src: FileId( |
684 | 3, | 675 | 3, |
685 | ), | 676 | ), |
686 | dst_source_root: SourceRootId( | 677 | anchor: FileId( |
687 | 0, | 678 | 2, |
688 | ), | 679 | ), |
689 | dst_path: "bar/foo2.rs", | 680 | dst: "foo2.rs", |
690 | }, | 681 | }, |
691 | ], | 682 | ], |
692 | is_snippet: false, | 683 | is_snippet: false, |
@@ -699,18 +690,18 @@ mod tests { | |||
699 | #[test] | 690 | #[test] |
700 | fn test_rename_mod_in_use_tree() { | 691 | fn test_rename_mod_in_use_tree() { |
701 | let (analysis, position) = analysis_and_position( | 692 | let (analysis, position) = analysis_and_position( |
702 | " | 693 | r#" |
703 | //- /main.rs | 694 | //- /main.rs |
704 | pub mod foo; | 695 | pub mod foo; |
705 | pub mod bar; | 696 | pub mod bar; |
706 | fn main() {} | 697 | fn main() {} |
707 | 698 | ||
708 | //- /foo.rs | 699 | //- /foo.rs |
709 | pub struct FooContent; | 700 | pub struct FooContent; |
710 | 701 | ||
711 | //- /bar.rs | 702 | //- /bar.rs |
712 | use crate::foo<|>::FooContent; | 703 | use crate::foo<|>::FooContent; |
713 | ", | 704 | "#, |
714 | ); | 705 | ); |
715 | let new_name = "qux"; | 706 | let new_name = "qux"; |
716 | let source_change = analysis.rename(position, new_name).unwrap(); | 707 | let source_change = analysis.rename(position, new_name).unwrap(); |
@@ -753,10 +744,10 @@ mod tests { | |||
753 | src: FileId( | 744 | src: FileId( |
754 | 2, | 745 | 2, |
755 | ), | 746 | ), |
756 | dst_source_root: SourceRootId( | 747 | anchor: FileId( |
757 | 0, | 748 | 3, |
758 | ), | 749 | ), |
759 | dst_path: "qux.rs", | 750 | dst: "qux.rs", |
760 | }, | 751 | }, |
761 | ], | 752 | ], |
762 | is_snippet: false, | 753 | is_snippet: false, |
@@ -769,12 +760,12 @@ mod tests { | |||
769 | #[test] | 760 | #[test] |
770 | fn test_rename_mod_in_dir() { | 761 | fn test_rename_mod_in_dir() { |
771 | let (analysis, position) = analysis_and_position( | 762 | let (analysis, position) = analysis_and_position( |
772 | " | 763 | r#" |
773 | //- /lib.rs | 764 | //- /lib.rs |
774 | mod fo<|>o; | 765 | mod fo<|>o; |
775 | //- /foo/mod.rs | 766 | //- /foo/mod.rs |
776 | // emtpy | 767 | // emtpy |
777 | ", | 768 | "#, |
778 | ); | 769 | ); |
779 | let new_name = "foo2"; | 770 | let new_name = "foo2"; |
780 | let source_change = analysis.rename(position, new_name).unwrap(); | 771 | let source_change = analysis.rename(position, new_name).unwrap(); |
@@ -804,10 +795,10 @@ mod tests { | |||
804 | src: FileId( | 795 | src: FileId( |
805 | 2, | 796 | 2, |
806 | ), | 797 | ), |
807 | dst_source_root: SourceRootId( | 798 | anchor: FileId( |
808 | 0, | 799 | 1, |
809 | ), | 800 | ), |
810 | dst_path: "foo2/mod.rs", | 801 | dst: "../foo2/mod.rs", |
811 | }, | 802 | }, |
812 | ], | 803 | ], |
813 | is_snippet: false, | 804 | is_snippet: false, |
@@ -844,19 +835,19 @@ mod tests { | |||
844 | #[test] | 835 | #[test] |
845 | fn test_rename_mod_filename_and_path() { | 836 | fn test_rename_mod_filename_and_path() { |
846 | let (analysis, position) = analysis_and_position( | 837 | let (analysis, position) = analysis_and_position( |
847 | " | 838 | r#" |
848 | //- /lib.rs | 839 | //- /lib.rs |
849 | mod bar; | 840 | mod bar; |
850 | fn f() { | 841 | fn f() { |
851 | bar::foo::fun() | 842 | bar::foo::fun() |
852 | } | 843 | } |
853 | 844 | ||
854 | //- /bar.rs | 845 | //- /bar.rs |
855 | pub mod foo<|>; | 846 | pub mod foo<|>; |
856 | 847 | ||
857 | //- /bar/foo.rs | 848 | //- /bar/foo.rs |
858 | // pub fn fun() {} | 849 | // pub fn fun() {} |
859 | ", | 850 | "#, |
860 | ); | 851 | ); |
861 | let new_name = "foo2"; | 852 | let new_name = "foo2"; |
862 | let source_change = analysis.rename(position, new_name).unwrap(); | 853 | let source_change = analysis.rename(position, new_name).unwrap(); |
@@ -899,10 +890,10 @@ mod tests { | |||
899 | src: FileId( | 890 | src: FileId( |
900 | 3, | 891 | 3, |
901 | ), | 892 | ), |
902 | dst_source_root: SourceRootId( | 893 | anchor: FileId( |
903 | 0, | 894 | 2, |
904 | ), | 895 | ), |
905 | dst_path: "bar/foo2.rs", | 896 | dst: "foo2.rs", |
906 | }, | 897 | }, |
907 | ], | 898 | ], |
908 | is_snippet: false, | 899 | is_snippet: false, |