Add support for environment to CrateGraph
This commit is contained in:
parent
5be7bd605a
commit
2c48fa087b
7 changed files with 79 additions and 28 deletions
|
@ -8,7 +8,7 @@ use rustc_hash::FxHashMap;
|
|||
use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER};
|
||||
|
||||
use crate::{
|
||||
CrateGraph, CrateId, Edition, FileId, FilePosition, RelativePathBuf, SourceDatabaseExt,
|
||||
CrateGraph, CrateId, Edition, Env, FileId, FilePosition, RelativePathBuf, SourceDatabaseExt,
|
||||
SourceRoot, SourceRootId,
|
||||
};
|
||||
|
||||
|
@ -53,7 +53,12 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, text: &str) -> FileId {
|
|||
source_root.insert_file(rel_path.clone(), file_id);
|
||||
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
crate_graph.add_crate_root(file_id, Edition::Edition2018, CfgOptions::default());
|
||||
crate_graph.add_crate_root(
|
||||
file_id,
|
||||
Edition::Edition2018,
|
||||
CfgOptions::default(),
|
||||
Env::default(),
|
||||
);
|
||||
|
||||
db.set_file_text(file_id, Arc::new(text.to_string()));
|
||||
db.set_file_relative_path(file_id, rel_path);
|
||||
|
@ -93,7 +98,8 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
|
|||
assert!(meta.path.starts_with(&source_root_prefix));
|
||||
|
||||
if let Some(krate) = meta.krate {
|
||||
let crate_id = crate_graph.add_crate_root(file_id, meta.edition, meta.cfg);
|
||||
let crate_id =
|
||||
crate_graph.add_crate_root(file_id, meta.edition, meta.cfg, Env::default());
|
||||
let prev = crates.insert(krate.clone(), crate_id);
|
||||
assert!(prev.is_none());
|
||||
for dep in meta.deps {
|
||||
|
@ -123,7 +129,12 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
|
|||
|
||||
if crates.is_empty() {
|
||||
let crate_root = default_crate_root.unwrap();
|
||||
crate_graph.add_crate_root(crate_root, Edition::Edition2018, CfgOptions::default());
|
||||
crate_graph.add_crate_root(
|
||||
crate_root,
|
||||
Edition::Edition2018,
|
||||
CfgOptions::default(),
|
||||
Env::default(),
|
||||
);
|
||||
} else {
|
||||
for (from, to) in crate_deps {
|
||||
let from_id = crates[&from];
|
||||
|
|
|
@ -114,17 +114,23 @@ impl FromStr for Edition {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Env {
|
||||
entries: FxHashMap<String, String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
struct CrateData {
|
||||
file_id: FileId,
|
||||
edition: Edition,
|
||||
dependencies: Vec<Dependency>,
|
||||
cfg_options: CfgOptions,
|
||||
env: Env,
|
||||
}
|
||||
|
||||
impl CrateData {
|
||||
fn new(file_id: FileId, edition: Edition, cfg_options: CfgOptions) -> CrateData {
|
||||
CrateData { file_id, edition, dependencies: Vec::new(), cfg_options }
|
||||
fn new(file_id: FileId, edition: Edition, cfg_options: CfgOptions, env: Env) -> CrateData {
|
||||
CrateData { file_id, edition, dependencies: Vec::new(), cfg_options, env }
|
||||
}
|
||||
|
||||
fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) {
|
||||
|
@ -150,9 +156,11 @@ impl CrateGraph {
|
|||
file_id: FileId,
|
||||
edition: Edition,
|
||||
cfg_options: CfgOptions,
|
||||
env: Env,
|
||||
) -> CrateId {
|
||||
let data = CrateData::new(file_id, edition, cfg_options, env);
|
||||
let crate_id = CrateId(self.arena.len() as u32);
|
||||
let prev = self.arena.insert(crate_id, CrateData::new(file_id, edition, cfg_options));
|
||||
let prev = self.arena.insert(crate_id, data);
|
||||
assert!(prev.is_none());
|
||||
crate_id
|
||||
}
|
||||
|
@ -241,14 +249,17 @@ impl CrateGraph {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{CfgOptions, CrateGraph, Edition::Edition2018, FileId, SmolStr};
|
||||
use super::{CfgOptions, CrateGraph, Edition::Edition2018, Env, FileId, SmolStr};
|
||||
|
||||
#[test]
|
||||
fn it_should_panic_because_of_cycle_dependencies() {
|
||||
let mut graph = CrateGraph::default();
|
||||
let crate1 = graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default());
|
||||
let crate2 = graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default());
|
||||
let crate3 = graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default());
|
||||
let crate1 =
|
||||
graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default());
|
||||
let crate2 =
|
||||
graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default());
|
||||
let crate3 =
|
||||
graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default());
|
||||
assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok());
|
||||
assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok());
|
||||
assert!(graph.add_dep(crate3, SmolStr::new("crate1"), crate1).is_err());
|
||||
|
@ -257,9 +268,12 @@ mod tests {
|
|||
#[test]
|
||||
fn it_works() {
|
||||
let mut graph = CrateGraph::default();
|
||||
let crate1 = graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default());
|
||||
let crate2 = graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default());
|
||||
let crate3 = graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default());
|
||||
let crate1 =
|
||||
graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default());
|
||||
let crate2 =
|
||||
graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default());
|
||||
let crate3 =
|
||||
graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default());
|
||||
assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok());
|
||||
assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok());
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ use ra_syntax::{ast, Parse, SourceFile, TextRange, TextUnit};
|
|||
|
||||
pub use crate::{
|
||||
cancellation::Canceled,
|
||||
input::{CrateGraph, CrateId, Dependency, Edition, FileId, SourceRoot, SourceRootId},
|
||||
input::{CrateGraph, CrateId, Dependency, Edition, Env, FileId, SourceRoot, SourceRootId},
|
||||
};
|
||||
pub use relative_path::{RelativePath, RelativePathBuf};
|
||||
pub use salsa;
|
||||
|
|
|
@ -54,7 +54,7 @@ use std::sync::Arc;
|
|||
use ra_cfg::CfgOptions;
|
||||
use ra_db::{
|
||||
salsa::{self, ParallelDatabase},
|
||||
CheckCanceled, FileLoader, SourceDatabase,
|
||||
CheckCanceled, Env, FileLoader, SourceDatabase,
|
||||
};
|
||||
use ra_syntax::{SourceFile, TextRange, TextUnit};
|
||||
|
||||
|
@ -240,7 +240,7 @@ impl Analysis {
|
|||
// Default to enable test for single file.
|
||||
let mut cfg_options = CfgOptions::default();
|
||||
cfg_options.insert_atom("test".into());
|
||||
crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options);
|
||||
crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options, Env::default());
|
||||
change.add_file(source_root, file_id, "main.rs".into(), Arc::new(text));
|
||||
change.set_crate_graph(crate_graph);
|
||||
host.apply_change(change);
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use ra_cfg::CfgOptions;
|
||||
use ra_db::RelativePathBuf;
|
||||
use ra_db::{Env, RelativePathBuf};
|
||||
use test_utils::{extract_offset, extract_range, parse_fixture, CURSOR_MARKER};
|
||||
|
||||
use crate::{
|
||||
|
@ -96,9 +96,15 @@ impl MockAnalysis {
|
|||
let file_id = FileId(i as u32 + 1);
|
||||
let cfg_options = CfgOptions::default();
|
||||
if path == "/lib.rs" || path == "/main.rs" {
|
||||
root_crate = Some(crate_graph.add_crate_root(file_id, Edition2018, cfg_options));
|
||||
root_crate = Some(crate_graph.add_crate_root(
|
||||
file_id,
|
||||
Edition2018,
|
||||
cfg_options,
|
||||
Env::default(),
|
||||
));
|
||||
} else if path.ends_with("/lib.rs") {
|
||||
let other_crate = crate_graph.add_crate_root(file_id, Edition2018, cfg_options);
|
||||
let other_crate =
|
||||
crate_graph.add_crate_root(file_id, Edition2018, cfg_options, Env::default());
|
||||
let crate_name = path.parent().unwrap().file_name().unwrap();
|
||||
if let Some(root_crate) = root_crate {
|
||||
crate_graph.add_dep(root_crate, crate_name.into(), other_crate).unwrap();
|
||||
|
|
|
@ -34,12 +34,14 @@ pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use ra_cfg::CfgOptions;
|
||||
use ra_db::Env;
|
||||
|
||||
use crate::{
|
||||
mock_analysis::{analysis_and_position, MockAnalysis},
|
||||
AnalysisChange, CrateGraph,
|
||||
Edition::Edition2018,
|
||||
};
|
||||
use ra_cfg::CfgOptions;
|
||||
|
||||
#[test]
|
||||
fn test_resolve_parent_module() {
|
||||
|
@ -87,7 +89,12 @@ mod tests {
|
|||
assert!(host.analysis().crate_for(mod_file).unwrap().is_empty());
|
||||
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
let crate_id = crate_graph.add_crate_root(root_file, Edition2018, CfgOptions::default());
|
||||
let crate_id = crate_graph.add_crate_root(
|
||||
root_file,
|
||||
Edition2018,
|
||||
CfgOptions::default(),
|
||||
Env::default(),
|
||||
);
|
||||
let mut change = AnalysisChange::new();
|
||||
change.set_crate_graph(crate_graph);
|
||||
host.apply_change(change);
|
||||
|
|
|
@ -13,7 +13,7 @@ use std::{
|
|||
};
|
||||
|
||||
use ra_cfg::CfgOptions;
|
||||
use ra_db::{CrateGraph, CrateId, Edition, FileId};
|
||||
use ra_db::{CrateGraph, CrateId, Edition, Env, FileId};
|
||||
use rustc_hash::FxHashMap;
|
||||
use serde_json::from_reader;
|
||||
|
||||
|
@ -146,7 +146,12 @@ impl ProjectWorkspace {
|
|||
};
|
||||
crates.insert(
|
||||
crate_id,
|
||||
crate_graph.add_crate_root(file_id, edition, cfg_options),
|
||||
crate_graph.add_crate_root(
|
||||
file_id,
|
||||
edition,
|
||||
cfg_options,
|
||||
Env::default(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -180,8 +185,12 @@ impl ProjectWorkspace {
|
|||
opts
|
||||
};
|
||||
|
||||
let crate_id =
|
||||
crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options);
|
||||
let crate_id = crate_graph.add_crate_root(
|
||||
file_id,
|
||||
Edition::Edition2018,
|
||||
cfg_options,
|
||||
Env::default(),
|
||||
);
|
||||
sysroot_crates.insert(krate, crate_id);
|
||||
names.insert(crate_id, krate.name(&sysroot).to_string());
|
||||
}
|
||||
|
@ -216,8 +225,12 @@ impl ProjectWorkspace {
|
|||
opts.insert_features(pkg.features(&cargo).iter().map(Into::into));
|
||||
opts
|
||||
};
|
||||
let crate_id =
|
||||
crate_graph.add_crate_root(file_id, edition, cfg_options);
|
||||
let crate_id = crate_graph.add_crate_root(
|
||||
file_id,
|
||||
edition,
|
||||
cfg_options,
|
||||
Env::default(),
|
||||
);
|
||||
names.insert(crate_id, pkg.name(&cargo).to_string());
|
||||
if tgt.kind(&cargo) == TargetKind::Lib {
|
||||
lib_tgt = Some(crate_id);
|
||||
|
|
Loading…
Add table
Reference in a new issue