From 8153a0b3ef84b8ad2f26bba14a094752797a5ada Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 10 Mar 2020 21:59:12 +0800 Subject: Add ExternSourceId and env functions --- crates/ra_db/src/input.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 1a1c64202..18d8e2a53 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -122,9 +122,16 @@ 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, + + // Note: Some env variables (e.g. OUT_DIR) are located outside of the + // crate. We store a map to allow remap it to ExternSourceId + extern_paths: FxHashMap, } #[derive(Debug, Clone, PartialEq, Eq)] @@ -269,6 +276,26 @@ impl Env { pub fn get(&self, env: &str) -> Option { self.entries.get(env).cloned() } + + pub fn extern_path(&self, path: &str) -> Option<(ExternSourceId, RelativePathBuf)> { + self.extern_paths.iter().find_map(|(root_path, id)| { + if path.starts_with(root_path) { + let mut rel_path = &path[root_path.len()..]; + if rel_path.starts_with("/") { + rel_path = &rel_path[1..]; + } + let rel_path = RelativePathBuf::from_path(rel_path).ok()?; + Some((id.clone(), rel_path)) + } else { + None + } + }) + } + + pub fn set_extern_path(&mut self, env: &str, root_path: &str, root: ExternSourceId) { + self.entries.insert(env.to_owned(), root_path.to_owned()); + self.extern_paths.insert(root_path.to_owned(), root); + } } #[derive(Debug)] -- cgit v1.2.3 From 22f064cca7651eaf2980fcfa27618d99c633a589 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 10 Mar 2020 22:00:31 +0800 Subject: Add resolve_extern_path in DB --- crates/ra_db/src/lib.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index fb002d717..9bf3fe248 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -11,7 +11,8 @@ use ra_syntax::{ast, Parse, SourceFile, TextRange, TextUnit}; pub use crate::{ cancellation::Canceled, input::{ - CrateGraph, CrateId, CrateName, Dependency, Edition, Env, FileId, SourceRoot, SourceRootId, + CrateGraph, CrateId, CrateName, Dependency, Edition, Env, ExternSourceId, FileId, + SourceRoot, SourceRootId, }, }; pub use relative_path::{RelativePath, RelativePathBuf}; @@ -87,6 +88,12 @@ pub trait FileLoader { fn resolve_relative_path(&self, anchor: FileId, relative_path: &RelativePath) -> Option; fn relevant_crates(&self, file_id: FileId) -> Arc>; + + fn resolve_extern_path( + &self, + extern_id: ExternSourceId, + relative_path: &RelativePath, + ) -> Option; } /// Database which stores all significant input facts: source code and project @@ -164,4 +171,13 @@ impl FileLoader for FileLoaderDelegate<&'_ T> { let source_root = self.0.file_source_root(file_id); self.0.source_root_crates(source_root) } + + fn resolve_extern_path( + &self, + extern_id: ExternSourceId, + relative_path: &RelativePath, + ) -> Option { + let source_root = self.0.source_root(SourceRootId(extern_id.0)); + source_root.file_by_relative_path(&relative_path) + } } -- cgit v1.2.3 From 6ea7c319154f9ec10721f4041afc9d07d6b2476b Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 11 Mar 2020 11:04:02 +0800 Subject: Add extern source --- crates/ra_db/src/fixture.rs | 12 +++++++++++- crates/ra_db/src/input.rs | 24 ++++++++++++++++++++---- crates/ra_db/src/lib.rs | 4 ++-- 3 files changed, 33 insertions(+), 7 deletions(-) (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index 7f43c2971..3dc86ca2d 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs @@ -61,7 +61,14 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId }; let mut crate_graph = CrateGraph::default(); - crate_graph.add_crate_root(file_id, meta.edition, meta.krate, meta.cfg, meta.env); + crate_graph.add_crate_root( + file_id, + meta.edition, + meta.krate, + meta.cfg, + meta.env, + Default::default(), + ); crate_graph } else { let mut crate_graph = CrateGraph::default(); @@ -71,6 +78,7 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId None, CfgOptions::default(), Env::default(), + Default::default(), ); crate_graph }; @@ -119,6 +127,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option Option, pub cfg_options: CfgOptions, pub env: Env, + pub extern_source: ExternSource, pub dependencies: Vec, } @@ -128,9 +129,13 @@ pub struct ExternSourceId(pub u32); #[derive(Default, Debug, Clone, PartialEq, Eq)] pub struct Env { entries: FxHashMap, +} - // Note: Some env variables (e.g. OUT_DIR) are located outside of the - // crate. We store a map to allow remap it to ExternSourceId +// 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, } @@ -148,6 +153,7 @@ impl CrateGraph { display_name: Option, cfg_options: CfgOptions, env: Env, + extern_source: ExternSource, ) -> CrateId { let data = CrateData { root_file_id: file_id, @@ -155,6 +161,7 @@ impl CrateGraph { display_name, cfg_options, env, + extern_source, dependencies: Vec::new(), }; let crate_id = CrateId(self.arena.len() as u32); @@ -276,7 +283,9 @@ impl Env { pub fn get(&self, env: &str) -> Option { self.entries.get(env).cloned() } +} +impl ExternSource { pub fn extern_path(&self, path: &str) -> Option<(ExternSourceId, RelativePathBuf)> { self.extern_paths.iter().find_map(|(root_path, id)| { if path.starts_with(root_path) { @@ -292,8 +301,7 @@ impl Env { }) } - pub fn set_extern_path(&mut self, env: &str, root_path: &str, root: ExternSourceId) { - self.entries.insert(env.to_owned(), root_path.to_owned()); + pub fn set_extern_path(&mut self, root_path: &str, root: ExternSourceId) { self.extern_paths.insert(root_path.to_owned(), root); } } @@ -327,6 +335,7 @@ mod tests { None, CfgOptions::default(), Env::default(), + Default::default(), ); let crate2 = graph.add_crate_root( FileId(2u32), @@ -334,6 +343,7 @@ mod tests { None, CfgOptions::default(), Env::default(), + Default::default(), ); let crate3 = graph.add_crate_root( FileId(3u32), @@ -341,6 +351,7 @@ mod tests { None, CfgOptions::default(), Env::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()); @@ -356,6 +367,7 @@ mod tests { None, CfgOptions::default(), Env::default(), + Default::default(), ); let crate2 = graph.add_crate_root( FileId(2u32), @@ -363,6 +375,7 @@ mod tests { None, CfgOptions::default(), Env::default(), + Default::default(), ); let crate3 = graph.add_crate_root( FileId(3u32), @@ -370,6 +383,7 @@ mod tests { None, CfgOptions::default(), Env::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()); @@ -384,6 +398,7 @@ mod tests { None, CfgOptions::default(), Env::default(), + Default::default(), ); let crate2 = graph.add_crate_root( FileId(2u32), @@ -391,6 +406,7 @@ mod tests { None, CfgOptions::default(), Env::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 9bf3fe248..d500d5e85 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -11,8 +11,8 @@ use ra_syntax::{ast, Parse, SourceFile, TextRange, TextUnit}; pub use crate::{ cancellation::Canceled, input::{ - CrateGraph, CrateId, CrateName, Dependency, Edition, Env, ExternSourceId, FileId, - SourceRoot, SourceRootId, + CrateGraph, CrateId, CrateName, Dependency, Edition, Env, ExternSource, ExternSourceId, + FileId, SourceRoot, SourceRootId, }, }; pub use relative_path::{RelativePath, RelativePathBuf}; -- cgit v1.2.3