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