From 6e24321e4579d25686982002ed18f321db23cb9f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 9 Dec 2020 19:01:15 +0300 Subject: Introduce anchored_path They allow to represent paths like `#[path = "C:\path.rs"] mod foo;` in a lossless cross-platform & network-transparent way. --- crates/ide/src/diagnostics.rs | 10 ++++--- crates/ide/src/diagnostics/fixes.rs | 8 ++++-- crates/ide/src/references/rename.rs | 57 ++++++++++++++++++++++--------------- 3 files changed, 45 insertions(+), 30 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index c8453edb3..d09f3a0a1 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs @@ -610,10 +610,12 @@ fn test_fn() { source_file_edits: [], file_system_edits: [ CreateFile { - anchor: FileId( - 0, - ), - dst: "foo.rs", + dst: AnchoredPathBuf { + anchor: FileId( + 0, + ), + path: "foo.rs", + }, }, ], is_snippet: false, diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs index 24d08651e..29c7a040b 100644 --- a/crates/ide/src/diagnostics/fixes.rs +++ b/crates/ide/src/diagnostics/fixes.rs @@ -8,7 +8,7 @@ use hir::{ }, HasSource, HirDisplay, Semantics, VariantDef, }; -use ide_db::base_db::FileId; +use ide_db::base_db::{AnchoredPathBuf, FileId}; use ide_db::{ source_change::{FileSystemEdit, SourceFileEdit}, RootDatabase, @@ -36,8 +36,10 @@ impl DiagnosticWithFix for UnresolvedModule { Some(Fix::new( "Create module", FileSystemEdit::CreateFile { - anchor: self.file.original_file(sema.db), - dst: self.candidate.clone(), + dst: AnchoredPathBuf { + anchor: self.file.original_file(sema.db), + path: self.candidate.clone(), + }, } .into(), unresolved_module.syntax().text_range(), diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs index 64fe8bd65..44081f210 100644 --- a/crates/ide/src/references/rename.rs +++ b/crates/ide/src/references/rename.rs @@ -6,7 +6,7 @@ use std::{ }; use hir::{Module, ModuleDef, ModuleSource, Semantics}; -use ide_db::base_db::{FileRange, SourceDatabaseExt}; +use ide_db::base_db::{AnchoredPathBuf, FileRange, SourceDatabaseExt}; use ide_db::{ defs::{Definition, NameClass, NameRefClass}, RootDatabase, @@ -182,12 +182,13 @@ fn rename_mod( match src.value { ModuleSource::SourceFile(..) => { // mod is defined in path/to/dir/mod.rs - let dst = if module.is_mod_rs(sema.db) { + let path = if module.is_mod_rs(sema.db) { format!("../{}/mod.rs", new_name) } else { format!("{}.rs", new_name) }; - let move_file = FileSystemEdit::MoveFile { src: file_id, anchor: file_id, dst }; + let dst = AnchoredPathBuf { anchor: file_id, path }; + let move_file = FileSystemEdit::MoveFile { src: file_id, dst }; file_system_edits.push(move_file); } ModuleSource::Module(..) => {} @@ -771,10 +772,12 @@ mod foo<|>; src: FileId( 2, ), - anchor: FileId( - 2, - ), - dst: "foo2.rs", + dst: AnchoredPathBuf { + anchor: FileId( + 2, + ), + path: "foo2.rs", + }, }, ], is_snippet: false, @@ -837,10 +840,12 @@ use crate::foo<|>::FooContent; src: FileId( 1, ), - anchor: FileId( - 1, - ), - dst: "quux.rs", + dst: AnchoredPathBuf { + anchor: FileId( + 1, + ), + path: "quux.rs", + }, }, ], is_snippet: false, @@ -884,10 +889,12 @@ mod fo<|>o; src: FileId( 1, ), - anchor: FileId( - 1, - ), - dst: "../foo2/mod.rs", + dst: AnchoredPathBuf { + anchor: FileId( + 1, + ), + path: "../foo2/mod.rs", + }, }, ], is_snippet: false, @@ -932,10 +939,12 @@ mod outer { mod fo<|>o; } src: FileId( 1, ), - anchor: FileId( - 1, - ), - dst: "bar.rs", + dst: AnchoredPathBuf { + anchor: FileId( + 1, + ), + path: "bar.rs", + }, }, ], is_snippet: false, @@ -1016,10 +1025,12 @@ pub mod foo<|>; src: FileId( 2, ), - anchor: FileId( - 2, - ), - dst: "foo2.rs", + dst: AnchoredPathBuf { + anchor: FileId( + 2, + ), + path: "foo2.rs", + }, }, ], is_snippet: false, -- cgit v1.2.3