mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 21:54:42 +00:00
Add ExternSourceId and env functions
This commit is contained in:
parent
c1db5d26a0
commit
8153a0b3ef
1 changed files with 27 additions and 0 deletions
|
@ -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<String, String>,
|
||||
|
||||
// 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<String, ExternSourceId>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
@ -269,6 +276,26 @@ impl Env {
|
|||
pub fn get(&self, env: &str) -> Option<String> {
|
||||
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)]
|
||||
|
|
Loading…
Reference in a new issue