11//! FIXME: write short doc here
22
33use hir:: { ModuleSource , Semantics } ;
4- use ra_db:: { RelativePath , RelativePathBuf , SourceDatabaseExt } ;
4+ use ra_db:: { RelativePathBuf , SourceDatabaseExt } ;
55use ra_ide_db:: RootDatabase ;
66use ra_syntax:: {
77 algo:: find_node_at_offset, ast, ast:: TypeAscriptionOwner , lex_single_valid_syntax_kind,
@@ -92,23 +92,14 @@ fn rename_mod(
9292 ModuleSource :: SourceFile ( ..) => {
9393 let mod_path: RelativePathBuf = sema. db . file_relative_path ( file_id) ;
9494 // mod is defined in path/to/dir/mod.rs
95- let dst_path = if mod_path. file_stem ( ) == Some ( "mod" ) {
96- mod_path
97- . parent ( )
98- . and_then ( |p| p. parent ( ) )
99- . or_else ( || Some ( RelativePath :: new ( "" ) ) )
100- . map ( |p| p. join ( new_name) . join ( "mod.rs" ) )
95+ let dst = if mod_path. file_stem ( ) == Some ( "mod" ) {
96+ format ! ( "../{}/mod.rs" , new_name)
10197 } else {
102- Some ( mod_path . with_file_name ( new_name ) . with_extension ( " rs") )
98+ format ! ( "{}. rs", new_name )
10399 } ;
104- if let Some ( path) = dst_path {
105- let move_file = FileSystemEdit :: MoveFile {
106- src : file_id,
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- }
100+ let move_file =
101+ FileSystemEdit :: MoveFile { src : file_id, anchor : position. file_id , dst } ;
102+ file_system_edits. push ( move_file) ;
112103 }
113104 ModuleSource :: Module ( ..) => { }
114105 }
@@ -623,16 +614,16 @@ mod tests {
623614 #[ test]
624615 fn test_rename_mod ( ) {
625616 let ( analysis, position) = analysis_and_position (
626- "
627- //- /lib.rs
628- mod bar;
617+ r# "
618+ //- /lib.rs
619+ mod bar;
629620
630- //- /bar.rs
631- mod foo<|>;
621+ //- /bar.rs
622+ mod foo<|>;
632623
633- //- /bar/foo.rs
634- // emtpy
635- " ,
624+ //- /bar/foo.rs
625+ // emtpy
626+ "# ,
636627 ) ;
637628 let new_name = "foo2" ;
638629 let source_change = analysis. rename ( position, new_name) . unwrap ( ) ;
@@ -662,10 +653,10 @@ mod tests {
662653 src: FileId(
663654 3,
664655 ),
665- dst_source_root: SourceRootId (
666- 0 ,
656+ anchor: FileId (
657+ 2 ,
667658 ),
668- dst_path : "bar/ foo2.rs",
659+ dst : "foo2.rs",
669660 },
670661 ],
671662 is_snippet: false,
@@ -678,12 +669,12 @@ mod tests {
678669 #[ test]
679670 fn test_rename_mod_in_dir ( ) {
680671 let ( analysis, position) = analysis_and_position (
681- "
682- //- /lib.rs
683- mod fo<|>o;
684- //- /foo/mod.rs
685- // emtpy
686- " ,
672+ r# "
673+ //- /lib.rs
674+ mod fo<|>o;
675+ //- /foo/mod.rs
676+ // emtpy
677+ "# ,
687678 ) ;
688679 let new_name = "foo2" ;
689680 let source_change = analysis. rename ( position, new_name) . unwrap ( ) ;
@@ -713,10 +704,10 @@ mod tests {
713704 src: FileId(
714705 2,
715706 ),
716- dst_source_root: SourceRootId (
717- 0 ,
707+ anchor: FileId (
708+ 1 ,
718709 ),
719- dst_path : "foo2/mod.rs",
710+ dst : "../ foo2/mod.rs",
720711 },
721712 ],
722713 is_snippet: false,
@@ -753,19 +744,19 @@ mod tests {
753744 #[ test]
754745 fn test_rename_mod_filename_and_path ( ) {
755746 let ( analysis, position) = analysis_and_position (
756- "
757- //- /lib.rs
758- mod bar;
759- fn f() {
760- bar::foo::fun()
761- }
747+ r# "
748+ //- /lib.rs
749+ mod bar;
750+ fn f() {
751+ bar::foo::fun()
752+ }
762753
763- //- /bar.rs
764- pub mod foo<|>;
754+ //- /bar.rs
755+ pub mod foo<|>;
765756
766- //- /bar/foo.rs
767- // pub fn fun() {}
768- " ,
757+ //- /bar/foo.rs
758+ // pub fn fun() {}
759+ "# ,
769760 ) ;
770761 let new_name = "foo2" ;
771762 let source_change = analysis. rename ( position, new_name) . unwrap ( ) ;
@@ -808,10 +799,10 @@ mod tests {
808799 src: FileId(
809800 3,
810801 ),
811- dst_source_root: SourceRootId (
812- 0 ,
802+ anchor: FileId (
803+ 2 ,
813804 ),
814- dst_path : "bar/ foo2.rs",
805+ dst : "foo2.rs",
815806 },
816807 ],
817808 is_snippet: false,
0 commit comments