Skip to content

Commit b64d0a3

Browse files
bors[bot]matklad
andauthored
Merge #4909
4909: Anchor file-system operations to the file, and not to the source root. r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents da34d63 + 3c72fc0 commit b64d0a3

File tree

8 files changed

+67
-81
lines changed

8 files changed

+67
-81
lines changed

crates/ra_hir_def/src/diagnostics.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use std::any::Any;
44

55
use hir_expand::diagnostics::Diagnostic;
6-
use ra_db::RelativePathBuf;
76
use ra_syntax::{ast, AstPtr, SyntaxNodePtr};
87

98
use hir_expand::{HirFileId, InFile};
@@ -12,7 +11,7 @@ use hir_expand::{HirFileId, InFile};
1211
pub struct UnresolvedModule {
1312
pub file: HirFileId,
1413
pub decl: AstPtr<ast::Module>,
15-
pub candidate: RelativePathBuf,
14+
pub candidate: String,
1615
}
1716

1817
impl Diagnostic for UnresolvedModule {

crates/ra_hir_def/src/nameres.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ pub enum ModuleSource {
296296

297297
mod diagnostics {
298298
use hir_expand::diagnostics::DiagnosticSink;
299-
use ra_db::RelativePathBuf;
300299
use ra_syntax::{ast, AstPtr};
301300

302301
use crate::{db::DefDatabase, diagnostics::UnresolvedModule, nameres::LocalModuleId, AstId};
@@ -306,7 +305,7 @@ mod diagnostics {
306305
UnresolvedModule {
307306
module: LocalModuleId,
308307
declaration: AstId<ast::Module>,
309-
candidate: RelativePathBuf,
308+
candidate: String,
310309
},
311310
}
312311

crates/ra_hir_def/src/nameres/mod_resolution.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ impl ModDir {
4444
file_id: HirFileId,
4545
name: &Name,
4646
attr_path: Option<&SmolStr>,
47-
) -> Result<(FileId, ModDir), RelativePathBuf> {
47+
) -> Result<(FileId, ModDir), String> {
4848
let file_id = file_id.original_file(db.upcast());
4949

5050
let mut candidate_files = Vec::new();
5151
match attr_to_path(attr_path) {
5252
Some(attr_path) => {
5353
let base =
5454
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())
5656
}
5757
None => {
58-
candidate_files.push(self.path.join(&format!("{}.rs", name)));
59-
candidate_files.push(self.path.join(&format!("{}/mod.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)).to_string());
6060
}
6161
};
6262

crates/ra_ide/src/diagnostics.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use hir::{
1111
Semantics,
1212
};
1313
use itertools::Itertools;
14-
use ra_db::{RelativePath, SourceDatabase, SourceDatabaseExt};
14+
use ra_db::SourceDatabase;
1515
use ra_ide_db::RootDatabase;
1616
use ra_prof::profile;
1717
use ra_syntax::{
@@ -57,14 +57,10 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
5757
})
5858
.on::<hir::diagnostics::UnresolvedModule, _>(|d| {
5959
let original_file = d.source().file_id.original_file(db);
60-
let source_root = db.file_source_root(original_file);
61-
let path = db
62-
.file_relative_path(original_file)
63-
.parent()
64-
.unwrap_or_else(|| RelativePath::new(""))
65-
.join(&d.candidate);
66-
let fix =
67-
Fix::new("Create module", FileSystemEdit::CreateFile { source_root, path }.into());
60+
let fix = Fix::new(
61+
"Create module",
62+
FileSystemEdit::CreateFile { anchor: original_file, dst: d.candidate.clone() }.into(),
63+
);
6864
res.borrow_mut().push(Diagnostic {
6965
range: sema.diagnostics_range(d).range,
7066
message: d.message(),
@@ -612,10 +608,10 @@ mod tests {
612608
source_file_edits: [],
613609
file_system_edits: [
614610
CreateFile {
615-
source_root: SourceRootId(
616-
0,
611+
anchor: FileId(
612+
1,
617613
),
618-
path: "foo.rs",
614+
dst: "foo.rs",
619615
},
620616
],
621617
is_snippet: false,

crates/ra_ide/src/references/rename.rs

Lines changed: 41 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! FIXME: write short doc here
22
33
use hir::{ModuleSource, Semantics};
4-
use ra_db::{RelativePath, RelativePathBuf, SourceDatabaseExt};
4+
use ra_db::{RelativePathBuf, SourceDatabaseExt};
55
use ra_ide_db::RootDatabase;
66
use 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,

crates/ra_ide_db/src/source_change.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//!
44
//! It can be viewed as a dual for `AnalysisChange`.
55
6-
use ra_db::{FileId, RelativePathBuf, SourceRootId};
6+
use ra_db::FileId;
77
use ra_text_edit::TextEdit;
88

99
#[derive(Debug, Clone)]
@@ -44,8 +44,8 @@ impl From<Vec<SourceFileEdit>> for SourceChange {
4444

4545
#[derive(Debug, Clone)]
4646
pub enum FileSystemEdit {
47-
CreateFile { source_root: SourceRootId, path: RelativePathBuf },
48-
MoveFile { src: FileId, dst_source_root: SourceRootId, dst_path: RelativePathBuf },
47+
CreateFile { anchor: FileId, dst: String },
48+
MoveFile { src: FileId, anchor: FileId, dst: String },
4949
}
5050

5151
impl From<FileSystemEdit> for SourceChange {

crates/rust-analyzer/src/global_state.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use ra_ide::{
1616
Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId,
1717
};
1818
use ra_project_model::{ProcMacroClient, ProjectWorkspace};
19-
use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch};
19+
use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsTask, Watch};
2020
use relative_path::RelativePathBuf;
2121
use stdx::format_to;
2222

@@ -298,9 +298,10 @@ impl GlobalStateSnapshot {
298298
self.vfs.read().file_line_endings(VfsFile(id.0))
299299
}
300300

301-
pub fn path_to_url(&self, root: SourceRootId, path: &RelativePathBuf) -> Url {
302-
let base = self.vfs.read().root2path(VfsRoot(root.0));
303-
let path = path.to_path(base);
301+
pub fn anchored_path(&self, file_id: FileId, path: &str) -> Url {
302+
let mut base = self.vfs.read().file2path(VfsFile(file_id.0));
303+
base.pop();
304+
let path = base.join(path);
304305
url_from_abs_path(&path)
305306
}
306307

crates/rust-analyzer/src/to_proto.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,13 +528,13 @@ pub(crate) fn resource_op(
528528
file_system_edit: FileSystemEdit,
529529
) -> lsp_types::ResourceOp {
530530
match file_system_edit {
531-
FileSystemEdit::CreateFile { source_root, path } => {
532-
let uri = snap.path_to_url(source_root, &path);
531+
FileSystemEdit::CreateFile { anchor, dst } => {
532+
let uri = snap.anchored_path(anchor, &dst);
533533
lsp_types::ResourceOp::Create(lsp_types::CreateFile { uri, options: None })
534534
}
535-
FileSystemEdit::MoveFile { src, dst_source_root, dst_path } => {
535+
FileSystemEdit::MoveFile { src, anchor, dst } => {
536536
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);
538538
lsp_types::ResourceOp::Rename(lsp_types::RenameFile { old_uri, new_uri, options: None })
539539
}
540540
}

0 commit comments

Comments
 (0)