aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir_def/src/diagnostics.rs3
-rw-r--r--crates/ra_hir_def/src/nameres.rs3
-rw-r--r--crates/ra_hir_def/src/nameres/mod_resolution.rs8
-rw-r--r--crates/ra_ide/src/diagnostics.rs20
-rw-r--r--crates/ra_ide/src/references/rename.rs91
-rw-r--r--crates/ra_ide_db/src/source_change.rs6
-rw-r--r--crates/rust-analyzer/src/global_state.rs9
-rw-r--r--crates/rust-analyzer/src/to_proto.rs8
8 files changed, 67 insertions, 81 deletions
diff --git a/crates/ra_hir_def/src/diagnostics.rs b/crates/ra_hir_def/src/diagnostics.rs
index 510c5e064..30db48f86 100644
--- a/crates/ra_hir_def/src/diagnostics.rs
+++ b/crates/ra_hir_def/src/diagnostics.rs
@@ -3,7 +3,6 @@
3use std::any::Any; 3use std::any::Any;
4 4
5use hir_expand::diagnostics::Diagnostic; 5use hir_expand::diagnostics::Diagnostic;
6use ra_db::RelativePathBuf;
7use ra_syntax::{ast, AstPtr, SyntaxNodePtr}; 6use ra_syntax::{ast, AstPtr, SyntaxNodePtr};
8 7
9use hir_expand::{HirFileId, InFile}; 8use hir_expand::{HirFileId, InFile};
@@ -12,7 +11,7 @@ use hir_expand::{HirFileId, InFile};
12pub struct UnresolvedModule { 11pub struct UnresolvedModule {
13 pub file: HirFileId, 12 pub file: HirFileId,
14 pub decl: AstPtr<ast::Module>, 13 pub decl: AstPtr<ast::Module>,
15 pub candidate: RelativePathBuf, 14 pub candidate: String,
16} 15}
17 16
18impl Diagnostic for UnresolvedModule { 17impl Diagnostic for UnresolvedModule {
diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs
index f279c2ad4..b3e5f491a 100644
--- a/crates/ra_hir_def/src/nameres.rs
+++ b/crates/ra_hir_def/src/nameres.rs
@@ -296,7 +296,6 @@ pub enum ModuleSource {
296 296
297mod diagnostics { 297mod diagnostics {
298 use hir_expand::diagnostics::DiagnosticSink; 298 use hir_expand::diagnostics::DiagnosticSink;
299 use ra_db::RelativePathBuf;
300 use ra_syntax::{ast, AstPtr}; 299 use ra_syntax::{ast, AstPtr};
301 300
302 use crate::{db::DefDatabase, diagnostics::UnresolvedModule, nameres::LocalModuleId, AstId}; 301 use crate::{db::DefDatabase, diagnostics::UnresolvedModule, nameres::LocalModuleId, AstId};
@@ -306,7 +305,7 @@ mod diagnostics {
306 UnresolvedModule { 305 UnresolvedModule {
307 module: LocalModuleId, 306 module: LocalModuleId,
308 declaration: AstId<ast::Module>, 307 declaration: AstId<ast::Module>,
309 candidate: RelativePathBuf, 308 candidate: String,
310 }, 309 },
311 } 310 }
312 311
diff --git a/crates/ra_hir_def/src/nameres/mod_resolution.rs b/crates/ra_hir_def/src/nameres/mod_resolution.rs
index cede4a6fc..19fe0615a 100644
--- a/crates/ra_hir_def/src/nameres/mod_resolution.rs
+++ b/crates/ra_hir_def/src/nameres/mod_resolution.rs
@@ -44,7 +44,7 @@ impl ModDir {
44 file_id: HirFileId, 44 file_id: HirFileId,
45 name: &Name, 45 name: &Name,
46 attr_path: Option<&SmolStr>, 46 attr_path: Option<&SmolStr>,
47 ) -> Result<(FileId, ModDir), RelativePathBuf> { 47 ) -> Result<(FileId, ModDir), String> {
48 let file_id = file_id.original_file(db.upcast()); 48 let file_id = file_id.original_file(db.upcast());
49 49
50 let mut candidate_files = Vec::new(); 50 let mut candidate_files = Vec::new();
@@ -52,11 +52,11 @@ impl ModDir {
52 Some(attr_path) => { 52 Some(attr_path) => {
53 let base = 53 let base =
54 if self.root_non_dir_owner { self.path.parent().unwrap() } else { &self.path }; 54 if self.root_non_dir_owner { self.path.parent().unwrap() } else { &self.path };
55 candidate_files.push(base.join(attr_path)) 55 candidate_files.push(base.join(attr_path).to_string())
56 } 56 }
57 None => { 57 None => {
58 candidate_files.push(self.path.join(&format!("{}.rs", name))); 58 candidate_files.push(self.path.join(&format!("{}.rs", name)).to_string());
59 candidate_files.push(self.path.join(&format!("{}/mod.rs", name))); 59 candidate_files.push(self.path.join(&format!("{}/mod.rs", name)).to_string());
60 } 60 }
61 }; 61 };
62 62
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};
13use itertools::Itertools; 13use itertools::Itertools;
14use ra_db::{RelativePath, SourceDatabase, SourceDatabaseExt}; 14use ra_db::SourceDatabase;
15use ra_ide_db::RootDatabase; 15use ra_ide_db::RootDatabase;
16use ra_prof::profile; 16use ra_prof::profile;
17use ra_syntax::{ 17use 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
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,
diff --git a/crates/ra_ide_db/src/source_change.rs b/crates/ra_ide_db/src/source_change.rs
index f40ae8304..0bbd3c3e5 100644
--- a/crates/ra_ide_db/src/source_change.rs
+++ b/crates/ra_ide_db/src/source_change.rs
@@ -3,7 +3,7 @@
3//! 3//!
4//! It can be viewed as a dual for `AnalysisChange`. 4//! It can be viewed as a dual for `AnalysisChange`.
5 5
6use ra_db::{FileId, RelativePathBuf, SourceRootId}; 6use ra_db::FileId;
7use ra_text_edit::TextEdit; 7use ra_text_edit::TextEdit;
8 8
9#[derive(Debug, Clone)] 9#[derive(Debug, Clone)]
@@ -44,8 +44,8 @@ impl From<Vec<SourceFileEdit>> for SourceChange {
44 44
45#[derive(Debug, Clone)] 45#[derive(Debug, Clone)]
46pub enum FileSystemEdit { 46pub enum FileSystemEdit {
47 CreateFile { source_root: SourceRootId, path: RelativePathBuf }, 47 CreateFile { anchor: FileId, dst: String },
48 MoveFile { src: FileId, dst_source_root: SourceRootId, dst_path: RelativePathBuf }, 48 MoveFile { src: FileId, anchor: FileId, dst: String },
49} 49}
50 50
51impl From<FileSystemEdit> for SourceChange { 51impl From<FileSystemEdit> for SourceChange {
diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs
index 9d5685d88..73ca2a709 100644
--- a/crates/rust-analyzer/src/global_state.rs
+++ b/crates/rust-analyzer/src/global_state.rs
@@ -16,7 +16,7 @@ use ra_ide::{
16 Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId, 16 Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId,
17}; 17};
18use ra_project_model::{ProcMacroClient, ProjectWorkspace}; 18use ra_project_model::{ProcMacroClient, ProjectWorkspace};
19use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch}; 19use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsTask, Watch};
20use relative_path::RelativePathBuf; 20use relative_path::RelativePathBuf;
21use stdx::format_to; 21use stdx::format_to;
22 22
@@ -298,9 +298,10 @@ impl GlobalStateSnapshot {
298 self.vfs.read().file_line_endings(VfsFile(id.0)) 298 self.vfs.read().file_line_endings(VfsFile(id.0))
299 } 299 }
300 300
301 pub fn path_to_url(&self, root: SourceRootId, path: &RelativePathBuf) -> Url { 301 pub fn anchored_path(&self, file_id: FileId, path: &str) -> Url {
302 let base = self.vfs.read().root2path(VfsRoot(root.0)); 302 let mut base = self.vfs.read().file2path(VfsFile(file_id.0));
303 let path = path.to_path(base); 303 base.pop();
304 let path = base.join(path);
304 url_from_abs_path(&path) 305 url_from_abs_path(&path)
305 } 306 }
306 307
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs
index 881aa1c55..2851b4d31 100644
--- a/crates/rust-analyzer/src/to_proto.rs
+++ b/crates/rust-analyzer/src/to_proto.rs
@@ -528,13 +528,13 @@ pub(crate) fn resource_op(
528 file_system_edit: FileSystemEdit, 528 file_system_edit: FileSystemEdit,
529) -> lsp_types::ResourceOp { 529) -> lsp_types::ResourceOp {
530 match file_system_edit { 530 match file_system_edit {
531 FileSystemEdit::CreateFile { source_root, path } => { 531 FileSystemEdit::CreateFile { anchor, dst } => {
532 let uri = snap.path_to_url(source_root, &path); 532 let uri = snap.anchored_path(anchor, &dst);
533 lsp_types::ResourceOp::Create(lsp_types::CreateFile { uri, options: None }) 533 lsp_types::ResourceOp::Create(lsp_types::CreateFile { uri, options: None })
534 } 534 }
535 FileSystemEdit::MoveFile { src, dst_source_root, dst_path } => { 535 FileSystemEdit::MoveFile { src, anchor, dst } => {
536 let old_uri = snap.file_id_to_url(src); 536 let old_uri = snap.file_id_to_url(src);
537 let new_uri = snap.path_to_url(dst_source_root, &dst_path); 537 let new_uri = snap.anchored_path(anchor, &dst);
538 lsp_types::ResourceOp::Rename(lsp_types::RenameFile { old_uri, new_uri, options: None }) 538 lsp_types::ResourceOp::Rename(lsp_types::RenameFile { old_uri, new_uri, options: None })
539 } 539 }
540 } 540 }