diff options
author | Aleksey Kladov <[email protected]> | 2020-06-16 17:45:58 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-06-16 17:45:58 +0100 |
commit | 3c72fc05738b8a08dbf90dab18a15b9894d9e2a1 (patch) | |
tree | 18501e20ac9848d31019c572170aee9259f1d0ea /crates/ra_ide/src | |
parent | da34d630b878f14e85be02b8ffdb702edf736539 (diff) |
Anchor file-system operations to the file, and not to the source root.
Anchoring to the SourceRoot wont' work if the path is absolute:
#[path = "/tmp/foo.rs"]
mod foo;
Anchoring to a file will.
However, we *should* anchor, instead of just producing an abs path.
I can imagine a situation where, for example, rust-analyzer processes
crates from different machines (or, for example, from in-memory git
branch), where the same absolute path in different crates might refer
to different files in the end!
Diffstat (limited to 'crates/ra_ide/src')
-rw-r--r-- | crates/ra_ide/src/diagnostics.rs | 20 | ||||
-rw-r--r-- | crates/ra_ide/src/references/rename.rs | 91 |
2 files changed, 49 insertions, 62 deletions
diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index e1bfd72f9..fd9abb55b 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs | |||
@@ -11,7 +11,7 @@ use hir::{ | |||
11 | Semantics, | 11 | Semantics, |
12 | }; | 12 | }; |
13 | use itertools::Itertools; | 13 | use itertools::Itertools; |
14 | use ra_db::{RelativePath, SourceDatabase, SourceDatabaseExt}; | 14 | use ra_db::SourceDatabase; |
15 | use ra_ide_db::RootDatabase; | 15 | use ra_ide_db::RootDatabase; |
16 | use ra_prof::profile; | 16 | use ra_prof::profile; |
17 | use ra_syntax::{ | 17 | use ra_syntax::{ |
@@ -57,14 +57,10 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic> | |||
57 | }) | 57 | }) |
58 | .on::<hir::diagnostics::UnresolvedModule, _>(|d| { | 58 | .on::<hir::diagnostics::UnresolvedModule, _>(|d| { |
59 | let original_file = d.source().file_id.original_file(db); | 59 | let original_file = d.source().file_id.original_file(db); |
60 | let source_root = db.file_source_root(original_file); | 60 | let fix = Fix::new( |
61 | let path = db | 61 | "Create module", |
62 | .file_relative_path(original_file) | 62 | FileSystemEdit::CreateFile { anchor: original_file, dst: d.candidate.clone() }.into(), |
63 | .parent() | 63 | ); |
64 | .unwrap_or_else(|| RelativePath::new("")) | ||
65 | .join(&d.candidate); | ||
66 | let fix = | ||
67 | Fix::new("Create module", FileSystemEdit::CreateFile { source_root, path }.into()); | ||
68 | res.borrow_mut().push(Diagnostic { | 64 | res.borrow_mut().push(Diagnostic { |
69 | range: sema.diagnostics_range(d).range, | 65 | range: sema.diagnostics_range(d).range, |
70 | message: d.message(), | 66 | message: d.message(), |
@@ -612,10 +608,10 @@ mod tests { | |||
612 | source_file_edits: [], | 608 | source_file_edits: [], |
613 | file_system_edits: [ | 609 | file_system_edits: [ |
614 | CreateFile { | 610 | CreateFile { |
615 | source_root: SourceRootId( | 611 | anchor: FileId( |
616 | 0, | 612 | 1, |
617 | ), | 613 | ), |
618 | path: "foo.rs", | 614 | dst: "foo.rs", |
619 | }, | 615 | }, |
620 | ], | 616 | ], |
621 | is_snippet: false, | 617 | is_snippet: false, |
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 | ||
3 | use hir::{ModuleSource, Semantics}; | 3 | use hir::{ModuleSource, Semantics}; |
4 | use ra_db::{RelativePath, RelativePathBuf, SourceDatabaseExt}; | 4 | use ra_db::{RelativePathBuf, SourceDatabaseExt}; |
5 | use ra_ide_db::RootDatabase; | 5 | use ra_ide_db::RootDatabase; |
6 | use ra_syntax::{ | 6 | use 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; | 619 | mod bar; |
629 | 620 | ||
630 | //- /bar.rs | 621 | //- /bar.rs |
631 | mod foo<|>; | 622 | mod 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; | 674 | mod 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; | 749 | mod bar; |
759 | fn f() { | 750 | fn f() { |
760 | bar::foo::fun() | 751 | bar::foo::fun() |
761 | } | 752 | } |
762 | 753 | ||
763 | //- /bar.rs | 754 | //- /bar.rs |
764 | pub mod foo<|>; | 755 | pub 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, |