aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/references
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/references')
-rw-r--r--crates/ra_ide/src/references/rename.rs91
1 files changed, 41 insertions, 50 deletions
diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs
index 915d4f4d3..c4f07f905 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
3use hir::{ModuleSource, Semantics}; 3use hir::{ModuleSource, Semantics};
4use ra_db::{RelativePath, RelativePathBuf, SourceDatabaseExt}; 4use ra_db::{RelativePathBuf, SourceDatabaseExt};
5use ra_ide_db::RootDatabase; 5use ra_ide_db::RootDatabase;
6use ra_syntax::{ 6use ra_syntax::{
7 algo::find_node_at_offset, ast, ast::TypeAscriptionOwner, lex_single_valid_syntax_kind, 7 algo::find_node_at_offset, ast, ast::TypeAscriptionOwner, lex_single_valid_syntax_kind,
@@ -92,23 +92,14 @@ fn rename_mod(
92 ModuleSource::SourceFile(..) => { 92 ModuleSource::SourceFile(..) => {
93 let mod_path: RelativePathBuf = sema.db.file_relative_path(file_id); 93 let mod_path: RelativePathBuf = sema.db.file_relative_path(file_id);
94 // mod is defined in path/to/dir/mod.rs 94 // mod is defined in path/to/dir/mod.rs
95 let dst_path = if mod_path.file_stem() == Some("mod") { 95 let dst = if mod_path.file_stem() == Some("mod") {
96 mod_path 96 format!("../{}/mod.rs", new_name)
97 .parent()
98 .and_then(|p| p.parent())
99 .or_else(|| Some(RelativePath::new("")))
100 .map(|p| p.join(new_name).join("mod.rs"))
101 } else { 97 } else {
102 Some(mod_path.with_file_name(new_name).with_extension("rs")) 98 format!("{}.rs", new_name)
103 }; 99 };
104 if let Some(path) = dst_path { 100 let move_file =
105 let move_file = FileSystemEdit::MoveFile { 101 FileSystemEdit::MoveFile { src: file_id, anchor: position.file_id, dst };
106 src: file_id, 102 file_system_edits.push(move_file);
107 dst_source_root: sema.db.file_source_root(position.file_id),
108 dst_path: path,
109 };
110 file_system_edits.push(move_file);
111 }
112 } 103 }
113 ModuleSource::Module(..) => {} 104 ModuleSource::Module(..) => {}
114 } 105 }
@@ -623,16 +614,16 @@ mod tests {
623 #[test] 614 #[test]
624 fn test_rename_mod() { 615 fn test_rename_mod() {
625 let (analysis, position) = analysis_and_position( 616 let (analysis, position) = analysis_and_position(
626 " 617 r#"
627 //- /lib.rs 618//- /lib.rs
628 mod bar; 619mod bar;
629 620
630 //- /bar.rs 621//- /bar.rs
631 mod foo<|>; 622mod foo<|>;
632 623
633 //- /bar/foo.rs 624//- /bar/foo.rs
634 // emtpy 625// emtpy
635 ", 626 "#,
636 ); 627 );
637 let new_name = "foo2"; 628 let new_name = "foo2";
638 let source_change = analysis.rename(position, new_name).unwrap(); 629 let source_change = analysis.rename(position, new_name).unwrap();
@@ -662,10 +653,10 @@ mod tests {
662 src: FileId( 653 src: FileId(
663 3, 654 3,
664 ), 655 ),
665 dst_source_root: SourceRootId( 656 anchor: FileId(
666 0, 657 2,
667 ), 658 ),
668 dst_path: "bar/foo2.rs", 659 dst: "foo2.rs",
669 }, 660 },
670 ], 661 ],
671 is_snippet: false, 662 is_snippet: false,
@@ -678,12 +669,12 @@ mod tests {
678 #[test] 669 #[test]
679 fn test_rename_mod_in_dir() { 670 fn test_rename_mod_in_dir() {
680 let (analysis, position) = analysis_and_position( 671 let (analysis, position) = analysis_and_position(
681 " 672 r#"
682 //- /lib.rs 673//- /lib.rs
683 mod fo<|>o; 674mod fo<|>o;
684 //- /foo/mod.rs 675//- /foo/mod.rs
685 // emtpy 676// emtpy
686 ", 677 "#,
687 ); 678 );
688 let new_name = "foo2"; 679 let new_name = "foo2";
689 let source_change = analysis.rename(position, new_name).unwrap(); 680 let source_change = analysis.rename(position, new_name).unwrap();
@@ -713,10 +704,10 @@ mod tests {
713 src: FileId( 704 src: FileId(
714 2, 705 2,
715 ), 706 ),
716 dst_source_root: SourceRootId( 707 anchor: FileId(
717 0, 708 1,
718 ), 709 ),
719 dst_path: "foo2/mod.rs", 710 dst: "../foo2/mod.rs",
720 }, 711 },
721 ], 712 ],
722 is_snippet: false, 713 is_snippet: false,
@@ -753,19 +744,19 @@ mod tests {
753 #[test] 744 #[test]
754 fn test_rename_mod_filename_and_path() { 745 fn test_rename_mod_filename_and_path() {
755 let (analysis, position) = analysis_and_position( 746 let (analysis, position) = analysis_and_position(
756 " 747 r#"
757 //- /lib.rs 748//- /lib.rs
758 mod bar; 749mod bar;
759 fn f() { 750fn f() {
760 bar::foo::fun() 751 bar::foo::fun()
761 } 752}
762 753
763 //- /bar.rs 754//- /bar.rs
764 pub mod foo<|>; 755pub mod foo<|>;
765 756
766 //- /bar/foo.rs 757//- /bar/foo.rs
767 // pub fn fun() {} 758// pub fn fun() {}
768 ", 759 "#,
769 ); 760 );
770 let new_name = "foo2"; 761 let new_name = "foo2";
771 let source_change = analysis.rename(position, new_name).unwrap(); 762 let source_change = analysis.rename(position, new_name).unwrap();
@@ -808,10 +799,10 @@ mod tests {
808 src: FileId( 799 src: FileId(
809 3, 800 3,
810 ), 801 ),
811 dst_source_root: SourceRootId( 802 anchor: FileId(
812 0, 803 2,
813 ), 804 ),
814 dst_path: "bar/foo2.rs", 805 dst: "foo2.rs",
815 }, 806 },
816 ], 807 ],
817 is_snippet: false, 808 is_snippet: false,