Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion crates/ra_db/src/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
};
Expand Down Expand Up @@ -119,6 +127,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
Some(krate.clone()),
meta.cfg,
meta.env,
Default::default(),
);
let prev = crates.insert(krate.clone(), crate_id);
assert!(prev.is_none());
Expand Down Expand Up @@ -155,6 +164,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
None,
CfgOptions::default(),
Env::default(),
Default::default(),
);
} else {
for (from, to) in crate_deps {
Expand Down
43 changes: 43 additions & 0 deletions crates/ra_db/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub struct CrateData {
pub display_name: Option<String>,
pub cfg_options: CfgOptions,
pub env: Env,
pub extern_source: ExternSource,
pub dependencies: Vec<Dependency>,
}

Expand All @@ -122,11 +123,22 @@ 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>,
}

// 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<String, ExternSourceId>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, it's ... less then ideal that we need to single out OUT_DIR this way, but I guess that's the only way given current VFS API...

Copy link
Contributor Author

@edwin0cheng edwin0cheng Mar 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is the only solution I came up without altering too much code. But after some thoughts, I think I should separate this part to a new struct ExternPaths ExternSource and put it in CrateGraph instead.

[Edit] change to ExternSource

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Dependency {
pub crate_id: CrateId,
Expand All @@ -141,13 +153,15 @@ impl CrateGraph {
display_name: Option<String>,
cfg_options: CfgOptions,
env: Env,
extern_source: ExternSource,
) -> CrateId {
let data = CrateData {
root_file_id: file_id,
edition,
display_name,
cfg_options,
env,
extern_source,
dependencies: Vec::new(),
};
let crate_id = CrateId(self.arena.len() as u32);
Expand Down Expand Up @@ -271,6 +285,27 @@ impl Env {
}
}

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) {
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, root_path: &str, root: ExternSourceId) {
self.extern_paths.insert(root_path.to_owned(), root);
}
}

#[derive(Debug)]
pub struct ParseEditionError {
invalid_input: String,
Expand Down Expand Up @@ -300,20 +335,23 @@ mod tests {
None,
CfgOptions::default(),
Env::default(),
Default::default(),
);
let crate2 = graph.add_crate_root(
FileId(2u32),
Edition2018,
None,
CfgOptions::default(),
Env::default(),
Default::default(),
);
let crate3 = graph.add_crate_root(
FileId(3u32),
Edition2018,
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());
Expand All @@ -329,20 +367,23 @@ mod tests {
None,
CfgOptions::default(),
Env::default(),
Default::default(),
);
let crate2 = graph.add_crate_root(
FileId(2u32),
Edition2018,
None,
CfgOptions::default(),
Env::default(),
Default::default(),
);
let crate3 = graph.add_crate_root(
FileId(3u32),
Edition2018,
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());
Expand All @@ -357,13 +398,15 @@ mod tests {
None,
CfgOptions::default(),
Env::default(),
Default::default(),
);
let crate2 = graph.add_crate_root(
FileId(2u32),
Edition2018,
None,
CfgOptions::default(),
Env::default(),
Default::default(),
);
assert!(graph
.add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2)
Expand Down
18 changes: 17 additions & 1 deletion crates/ra_db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, ExternSource, ExternSourceId,
FileId, SourceRoot, SourceRootId,
},
};
pub use relative_path::{RelativePath, RelativePathBuf};
Expand Down Expand Up @@ -87,6 +88,12 @@ pub trait FileLoader {
fn resolve_relative_path(&self, anchor: FileId, relative_path: &RelativePath)
-> Option<FileId>;
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>>;

fn resolve_extern_path(
&self,
extern_id: ExternSourceId,
relative_path: &RelativePath,
) -> Option<FileId>;
}

/// Database which stores all significant input facts: source code and project
Expand Down Expand Up @@ -164,4 +171,13 @@ impl<T: SourceDatabaseExt> 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<FileId> {
let source_root = self.0.source_root(SourceRootId(extern_id.0));
source_root.file_by_relative_path(&relative_path)
}
}
10 changes: 9 additions & 1 deletion crates/ra_hir_def/src/test_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
};

use crate::db::DefDatabase;
use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath};
use ra_db::{salsa, CrateId, ExternSourceId, FileId, FileLoader, FileLoaderDelegate, RelativePath};

#[salsa::database(
ra_db::SourceDatabaseExtStorage,
Expand Down Expand Up @@ -52,6 +52,14 @@ impl FileLoader for TestDB {
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id)
}

fn resolve_extern_path(
&self,
extern_id: ExternSourceId,
relative_path: &RelativePath,
) -> Option<FileId> {
FileLoaderDelegate(self).resolve_extern_path(extern_id, relative_path)
}
}

impl TestDB {
Expand Down
Loading