From dad1333b48c38bc7a5628fc0ff5304d003776a85 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 11 Jun 2020 11:04:09 +0200 Subject: New VFS --- crates/ra_db/src/fixture.rs | 32 ++++++++---------- crates/ra_db/src/input.rs | 79 ++++++--------------------------------------- crates/ra_db/src/lib.rs | 28 ++++------------ 3 files changed, 29 insertions(+), 110 deletions(-) (limited to 'crates/ra_db/src') diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index af8fe11ec..f7d9118a9 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs @@ -57,17 +57,16 @@ //! fn insert_source_code_here() {} //! " //! ``` - -use std::str::FromStr; -use std::sync::Arc; +use std::{str::FromStr, sync::Arc}; use ra_cfg::CfgOptions; use rustc_hash::FxHashMap; use test_utils::{extract_offset, parse_fixture, parse_single_fixture, FixtureMeta, CURSOR_MARKER}; +use vfs::{file_set::FileSet, VfsPath}; use crate::{ - input::CrateName, CrateGraph, CrateId, Edition, Env, FileId, FilePosition, RelativePathBuf, - SourceDatabaseExt, SourceRoot, SourceRootId, + input::CrateName, CrateGraph, CrateId, Edition, Env, FileId, FilePosition, SourceDatabaseExt, + SourceRoot, SourceRootId, }; pub const WORKSPACE: SourceRootId = SourceRootId(0); @@ -105,10 +104,10 @@ impl WithFixture for DB {} fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId { let file_id = FileId(0); - let rel_path: RelativePathBuf = "/main.rs".into(); + let mut file_set = vfs::file_set::FileSet::default(); + file_set.insert(file_id, vfs::VfsPath::new_virtual_path("/main.rs".to_string())); - let mut source_root = SourceRoot::new_local(); - source_root.insert_file(rel_path.clone(), file_id); + let source_root = SourceRoot::new_local(file_set); let fixture = parse_single_fixture(ra_fixture); @@ -128,7 +127,6 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId meta.cfg, meta.env, Default::default(), - Default::default(), ); crate_graph } else { @@ -140,13 +138,11 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); crate_graph }; db.set_file_text(file_id, Arc::new(ra_fixture.to_string())); - db.set_file_relative_path(file_id, rel_path); db.set_file_source_root(file_id, WORKSPACE); db.set_source_root(WORKSPACE, Arc::new(source_root)); db.set_crate_graph(Arc::new(crate_graph)); @@ -162,7 +158,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option = None; - let mut source_root = SourceRoot::new_local(); + let mut file_set = FileSet::default(); let mut source_root_id = WORKSPACE; let mut source_root_prefix = "/".to_string(); let mut file_id = FileId(0); @@ -172,8 +168,8 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option { - let source_root = std::mem::replace(&mut source_root, SourceRoot::new_local()); - db.set_source_root(source_root_id, Arc::new(source_root)); + let file_set = std::mem::replace(&mut file_set, FileSet::default()); + db.set_source_root(source_root_id, Arc::new(SourceRoot::new_local(file_set))); source_root_id.0 += 1; source_root_prefix = path; continue; @@ -190,7 +186,6 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option Option Option Option, + pub(crate) file_set: FileSet, } impl SourceRoot { - pub fn new_local() -> SourceRoot { - SourceRoot { is_library: false, files: Default::default() } - } - pub fn new_library() -> SourceRoot { - SourceRoot { is_library: true, files: Default::default() } - } - pub fn insert_file(&mut self, path: RelativePathBuf, file_id: FileId) { - self.files.insert(path, file_id); + pub fn new_local(file_set: FileSet) -> SourceRoot { + SourceRoot { is_library: false, file_set } } - pub fn remove_file(&mut self, path: &RelativePath) { - self.files.remove(path); + pub fn new_library(file_set: FileSet) -> SourceRoot { + SourceRoot { is_library: true, file_set } } - pub fn walk(&self) -> impl Iterator + '_ { - self.files.values().copied() - } - pub fn file_by_relative_path(&self, path: &RelativePath) -> Option { - self.files.get(path).copied() + pub fn iter(&self) -> impl Iterator + '_ { + self.file_set.iter() } } @@ -141,7 +120,6 @@ pub struct CrateData { pub display_name: Option, pub cfg_options: CfgOptions, pub env: Env, - pub extern_source: ExternSource, pub dependencies: Vec, pub proc_macro: Vec, } @@ -152,22 +130,11 @@ pub enum Edition { Edition2015, } -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct ExternSourceId(pub u32); - #[derive(Default, Debug, Clone, PartialEq, Eq)] pub struct Env { entries: FxHashMap, } -// FIXME: Redesign vfs for solve the following limitation ? -// Note: Some env variables (e.g. OUT_DIR) are located outside of the -// crate. We store a map to allow remap it to ExternSourceId -#[derive(Default, Debug, Clone, PartialEq, Eq)] -pub struct ExternSource { - extern_paths: FxHashMap, -} - #[derive(Debug, Clone, PartialEq, Eq)] pub struct Dependency { pub crate_id: CrateId, @@ -182,7 +149,6 @@ impl CrateGraph { display_name: Option, cfg_options: CfgOptions, env: Env, - extern_source: ExternSource, proc_macro: Vec<(SmolStr, Arc)>, ) -> CrateId { let proc_macro = @@ -194,7 +160,6 @@ impl CrateGraph { display_name, cfg_options, env, - extern_source, proc_macro, dependencies: Vec::new(), }; @@ -334,20 +299,6 @@ impl Env { } } -impl ExternSource { - pub fn extern_path(&self, path: &Path) -> Option<(ExternSourceId, RelativePathBuf)> { - self.extern_paths.iter().find_map(|(root_path, id)| { - let rel_path = path.strip_prefix(root_path).ok()?; - let rel_path = RelativePathBuf::from_path(rel_path).ok()?; - Some((*id, rel_path)) - }) - } - - pub fn set_extern_path(&mut self, root_path: &Path, root: ExternSourceId) { - self.extern_paths.insert(root_path.to_path_buf(), root); - } -} - #[derive(Debug)] pub struct ParseEditionError { invalid_input: String, @@ -378,7 +329,6 @@ mod tests { CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); let crate2 = graph.add_crate_root( FileId(2u32), @@ -387,7 +337,6 @@ mod tests { CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); let crate3 = graph.add_crate_root( FileId(3u32), @@ -396,7 +345,6 @@ mod tests { CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); @@ -413,7 +361,6 @@ mod tests { CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); let crate2 = graph.add_crate_root( FileId(2u32), @@ -422,7 +369,6 @@ mod tests { CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); assert!(graph.add_dep(crate2, CrateName::new("crate2").unwrap(), crate2).is_err()); @@ -438,7 +384,6 @@ mod tests { CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); let crate2 = graph.add_crate_root( FileId(2u32), @@ -447,7 +392,6 @@ mod tests { CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); let crate3 = graph.add_crate_root( FileId(3u32), @@ -456,7 +400,6 @@ mod tests { CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); @@ -472,7 +415,6 @@ mod tests { CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); let crate2 = graph.add_crate_root( FileId(2u32), @@ -481,7 +423,6 @@ mod tests { CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); assert!(graph .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index 80ddb6058..875290259 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -12,12 +12,13 @@ use rustc_hash::FxHashSet; pub use crate::{ cancellation::Canceled, input::{ - CrateData, CrateGraph, CrateId, CrateName, Dependency, Edition, Env, ExternSource, - ExternSourceId, FileId, ProcMacroId, SourceRoot, SourceRootId, + CrateData, CrateGraph, CrateId, CrateName, Dependency, Edition, Env, FileId, ProcMacroId, + SourceRoot, SourceRootId, }, }; pub use relative_path::{RelativePath, RelativePathBuf}; pub use salsa; +pub use vfs::{file_set::FileSet, AbsPathBuf, VfsPath}; #[macro_export] macro_rules! impl_intern_key { @@ -125,8 +126,6 @@ pub trait SourceDatabaseExt: SourceDatabase { #[salsa::input] fn file_text(&self, file_id: FileId) -> Arc; /// Path to a file, relative to the root of its source root. - #[salsa::input] - fn file_relative_path(&self, file_id: FileId) -> RelativePathBuf; /// Source root of the file. #[salsa::input] fn file_source_root(&self, file_id: FileId) -> SourceRootId; @@ -161,24 +160,9 @@ impl FileLoader for FileLoaderDelegate<&'_ T> { } fn resolve_path(&self, anchor: FileId, path: &str) -> Option { // FIXME: this *somehow* should be platform agnostic... - if std::path::Path::new(path).is_absolute() { - let krate = *self.relevant_crates(anchor).iter().next()?; - let (extern_source_id, relative_file) = - self.0.crate_graph()[krate].extern_source.extern_path(path.as_ref())?; - - let source_root = self.0.source_root(SourceRootId(extern_source_id.0)); - source_root.file_by_relative_path(&relative_file) - } else { - let rel_path = { - let mut rel_path = self.0.file_relative_path(anchor); - assert!(rel_path.pop()); - rel_path.push(path); - rel_path.normalize() - }; - let source_root = self.0.file_source_root(anchor); - let source_root = self.0.source_root(source_root); - source_root.file_by_relative_path(&rel_path) - } + let source_root = self.0.file_source_root(anchor); + let source_root = self.0.source_root(source_root); + source_root.file_set.resolve_path(anchor, path) } fn relevant_crates(&self, file_id: FileId) -> Arc> { -- cgit v1.2.3