From 2c48fa087b6cc79ebfd81af9daf4a07d8019fd67 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 Nov 2019 13:55:03 +0300 Subject: Add support for environment to CrateGraph --- crates/ra_db/src/fixture.rs | 19 +++++++++++++++---- crates/ra_db/src/input.rs | 34 ++++++++++++++++++++++++---------- crates/ra_db/src/lib.rs | 2 +- crates/ra_ide_api/src/lib.rs | 4 ++-- crates/ra_ide_api/src/mock_analysis.rs | 12 +++++++++--- crates/ra_ide_api/src/parent_module.rs | 11 +++++++++-- crates/ra_project_model/src/lib.rs | 25 +++++++++++++++++++------ 7 files changed, 79 insertions(+), 28 deletions(-) (limited to 'crates') diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index ade187629..e8f335e33 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs @@ -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 Option, +} + #[derive(Debug, Clone, PartialEq, Eq)] struct CrateData { file_id: FileId, edition: Edition, dependencies: Vec, 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()); } diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index b6bfd531d..f9d012cb0 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -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; diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 62ad996bc..cb6c24eaa 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -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); diff --git a/crates/ra_ide_api/src/mock_analysis.rs b/crates/ra_ide_api/src/mock_analysis.rs index 2b1c96dbf..bf8a54932 100644 --- a/crates/ra_ide_api/src/mock_analysis.rs +++ b/crates/ra_ide_api/src/mock_analysis.rs @@ -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(); diff --git a/crates/ra_ide_api/src/parent_module.rs b/crates/ra_ide_api/src/parent_module.rs index fa232a379..6027e7d54 100644 --- a/crates/ra_ide_api/src/parent_module.rs +++ b/crates/ra_ide_api/src/parent_module.rs @@ -34,12 +34,14 @@ pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec { #[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); diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 0e14f1b70..d04a8cbe2 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -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); -- cgit v1.2.3