Skip to content

Commit 8153a0b

Browse files
committed
Add ExternSourceId and env functions
1 parent c1db5d2 commit 8153a0b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

crates/ra_db/src/input.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,16 @@ pub enum Edition {
122122
Edition2015,
123123
}
124124

125+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
126+
pub struct ExternSourceId(pub u32);
127+
125128
#[derive(Default, Debug, Clone, PartialEq, Eq)]
126129
pub struct Env {
127130
entries: FxHashMap<String, String>,
131+
132+
// Note: Some env variables (e.g. OUT_DIR) are located outside of the
133+
// crate. We store a map to allow remap it to ExternSourceId
134+
extern_paths: FxHashMap<String, ExternSourceId>,
128135
}
129136

130137
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -269,6 +276,26 @@ impl Env {
269276
pub fn get(&self, env: &str) -> Option<String> {
270277
self.entries.get(env).cloned()
271278
}
279+
280+
pub fn extern_path(&self, path: &str) -> Option<(ExternSourceId, RelativePathBuf)> {
281+
self.extern_paths.iter().find_map(|(root_path, id)| {
282+
if path.starts_with(root_path) {
283+
let mut rel_path = &path[root_path.len()..];
284+
if rel_path.starts_with("/") {
285+
rel_path = &rel_path[1..];
286+
}
287+
let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
288+
Some((id.clone(), rel_path))
289+
} else {
290+
None
291+
}
292+
})
293+
}
294+
295+
pub fn set_extern_path(&mut self, env: &str, root_path: &str, root: ExternSourceId) {
296+
self.entries.insert(env.to_owned(), root_path.to_owned());
297+
self.extern_paths.insert(root_path.to_owned(), root);
298+
}
272299
}
273300

274301
#[derive(Debug)]

0 commit comments

Comments
 (0)