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(+) 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