diff options
author | Aleksey Kladov <[email protected]> | 2019-11-22 10:55:03 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-11-22 10:57:25 +0000 |
commit | 2c48fa087b6cc79ebfd81af9daf4a07d8019fd67 (patch) | |
tree | d2d9cfb6f56cfe83b801d6e118e03051207f75a5 /crates | |
parent | 5be7bd605a09fafbc2bb91ebc3c4c3e35cf24110 (diff) |
Add support for environment to CrateGraph
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_db/src/fixture.rs | 19 | ||||
-rw-r--r-- | crates/ra_db/src/input.rs | 34 | ||||
-rw-r--r-- | crates/ra_db/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_ide_api/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/ra_ide_api/src/mock_analysis.rs | 12 | ||||
-rw-r--r-- | crates/ra_ide_api/src/parent_module.rs | 11 | ||||
-rw-r--r-- | crates/ra_project_model/src/lib.rs | 25 |
7 files changed, 79 insertions, 28 deletions
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; | |||
8 | use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER}; | 8 | use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER}; |
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{ |
11 | CrateGraph, CrateId, Edition, FileId, FilePosition, RelativePathBuf, SourceDatabaseExt, | 11 | CrateGraph, CrateId, Edition, Env, FileId, FilePosition, RelativePathBuf, SourceDatabaseExt, |
12 | SourceRoot, SourceRootId, | 12 | SourceRoot, SourceRootId, |
13 | }; | 13 | }; |
14 | 14 | ||
@@ -53,7 +53,12 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, text: &str) -> FileId { | |||
53 | source_root.insert_file(rel_path.clone(), file_id); | 53 | source_root.insert_file(rel_path.clone(), file_id); |
54 | 54 | ||
55 | let mut crate_graph = CrateGraph::default(); | 55 | let mut crate_graph = CrateGraph::default(); |
56 | crate_graph.add_crate_root(file_id, Edition::Edition2018, CfgOptions::default()); | 56 | crate_graph.add_crate_root( |
57 | file_id, | ||
58 | Edition::Edition2018, | ||
59 | CfgOptions::default(), | ||
60 | Env::default(), | ||
61 | ); | ||
57 | 62 | ||
58 | db.set_file_text(file_id, Arc::new(text.to_string())); | 63 | db.set_file_text(file_id, Arc::new(text.to_string())); |
59 | db.set_file_relative_path(file_id, rel_path); | 64 | db.set_file_relative_path(file_id, rel_path); |
@@ -93,7 +98,8 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit | |||
93 | assert!(meta.path.starts_with(&source_root_prefix)); | 98 | assert!(meta.path.starts_with(&source_root_prefix)); |
94 | 99 | ||
95 | if let Some(krate) = meta.krate { | 100 | if let Some(krate) = meta.krate { |
96 | let crate_id = crate_graph.add_crate_root(file_id, meta.edition, meta.cfg); | 101 | let crate_id = |
102 | crate_graph.add_crate_root(file_id, meta.edition, meta.cfg, Env::default()); | ||
97 | let prev = crates.insert(krate.clone(), crate_id); | 103 | let prev = crates.insert(krate.clone(), crate_id); |
98 | assert!(prev.is_none()); | 104 | assert!(prev.is_none()); |
99 | for dep in meta.deps { | 105 | for dep in meta.deps { |
@@ -123,7 +129,12 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit | |||
123 | 129 | ||
124 | if crates.is_empty() { | 130 | if crates.is_empty() { |
125 | let crate_root = default_crate_root.unwrap(); | 131 | let crate_root = default_crate_root.unwrap(); |
126 | crate_graph.add_crate_root(crate_root, Edition::Edition2018, CfgOptions::default()); | 132 | crate_graph.add_crate_root( |
133 | crate_root, | ||
134 | Edition::Edition2018, | ||
135 | CfgOptions::default(), | ||
136 | Env::default(), | ||
137 | ); | ||
127 | } else { | 138 | } else { |
128 | for (from, to) in crate_deps { | 139 | for (from, to) in crate_deps { |
129 | let from_id = crates[&from]; | 140 | let from_id = crates[&from]; |
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index c0d95a13f..0015d6b5e 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs | |||
@@ -114,17 +114,23 @@ impl FromStr for Edition { | |||
114 | } | 114 | } |
115 | } | 115 | } |
116 | 116 | ||
117 | #[derive(Default, Debug, Clone, PartialEq, Eq)] | ||
118 | pub struct Env { | ||
119 | entries: FxHashMap<String, String>, | ||
120 | } | ||
121 | |||
117 | #[derive(Debug, Clone, PartialEq, Eq)] | 122 | #[derive(Debug, Clone, PartialEq, Eq)] |
118 | struct CrateData { | 123 | struct CrateData { |
119 | file_id: FileId, | 124 | file_id: FileId, |
120 | edition: Edition, | 125 | edition: Edition, |
121 | dependencies: Vec<Dependency>, | 126 | dependencies: Vec<Dependency>, |
122 | cfg_options: CfgOptions, | 127 | cfg_options: CfgOptions, |
128 | env: Env, | ||
123 | } | 129 | } |
124 | 130 | ||
125 | impl CrateData { | 131 | impl CrateData { |
126 | fn new(file_id: FileId, edition: Edition, cfg_options: CfgOptions) -> CrateData { | 132 | fn new(file_id: FileId, edition: Edition, cfg_options: CfgOptions, env: Env) -> CrateData { |
127 | CrateData { file_id, edition, dependencies: Vec::new(), cfg_options } | 133 | CrateData { file_id, edition, dependencies: Vec::new(), cfg_options, env } |
128 | } | 134 | } |
129 | 135 | ||
130 | fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { | 136 | fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { |
@@ -150,9 +156,11 @@ impl CrateGraph { | |||
150 | file_id: FileId, | 156 | file_id: FileId, |
151 | edition: Edition, | 157 | edition: Edition, |
152 | cfg_options: CfgOptions, | 158 | cfg_options: CfgOptions, |
159 | env: Env, | ||
153 | ) -> CrateId { | 160 | ) -> CrateId { |
161 | let data = CrateData::new(file_id, edition, cfg_options, env); | ||
154 | let crate_id = CrateId(self.arena.len() as u32); | 162 | let crate_id = CrateId(self.arena.len() as u32); |
155 | let prev = self.arena.insert(crate_id, CrateData::new(file_id, edition, cfg_options)); | 163 | let prev = self.arena.insert(crate_id, data); |
156 | assert!(prev.is_none()); | 164 | assert!(prev.is_none()); |
157 | crate_id | 165 | crate_id |
158 | } | 166 | } |
@@ -241,14 +249,17 @@ impl CrateGraph { | |||
241 | 249 | ||
242 | #[cfg(test)] | 250 | #[cfg(test)] |
243 | mod tests { | 251 | mod tests { |
244 | use super::{CfgOptions, CrateGraph, Edition::Edition2018, FileId, SmolStr}; | 252 | use super::{CfgOptions, CrateGraph, Edition::Edition2018, Env, FileId, SmolStr}; |
245 | 253 | ||
246 | #[test] | 254 | #[test] |
247 | fn it_should_panic_because_of_cycle_dependencies() { | 255 | fn it_should_panic_because_of_cycle_dependencies() { |
248 | let mut graph = CrateGraph::default(); | 256 | let mut graph = CrateGraph::default(); |
249 | let crate1 = graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default()); | 257 | let crate1 = |
250 | let crate2 = graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default()); | 258 | graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default()); |
251 | let crate3 = graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default()); | 259 | let crate2 = |
260 | graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); | ||
261 | let crate3 = | ||
262 | graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default()); | ||
252 | assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); | 263 | assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); |
253 | assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); | 264 | assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); |
254 | assert!(graph.add_dep(crate3, SmolStr::new("crate1"), crate1).is_err()); | 265 | assert!(graph.add_dep(crate3, SmolStr::new("crate1"), crate1).is_err()); |
@@ -257,9 +268,12 @@ mod tests { | |||
257 | #[test] | 268 | #[test] |
258 | fn it_works() { | 269 | fn it_works() { |
259 | let mut graph = CrateGraph::default(); | 270 | let mut graph = CrateGraph::default(); |
260 | let crate1 = graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default()); | 271 | let crate1 = |
261 | let crate2 = graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default()); | 272 | graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default()); |
262 | let crate3 = graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default()); | 273 | let crate2 = |
274 | graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); | ||
275 | let crate3 = | ||
276 | graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default()); | ||
263 | assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); | 277 | assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); |
264 | assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); | 278 | assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); |
265 | } | 279 | } |
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}; | |||
10 | 10 | ||
11 | pub use crate::{ | 11 | pub use crate::{ |
12 | cancellation::Canceled, | 12 | cancellation::Canceled, |
13 | input::{CrateGraph, CrateId, Dependency, Edition, FileId, SourceRoot, SourceRootId}, | 13 | input::{CrateGraph, CrateId, Dependency, Edition, Env, FileId, SourceRoot, SourceRootId}, |
14 | }; | 14 | }; |
15 | pub use relative_path::{RelativePath, RelativePathBuf}; | 15 | pub use relative_path::{RelativePath, RelativePathBuf}; |
16 | pub use salsa; | 16 | 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; | |||
54 | use ra_cfg::CfgOptions; | 54 | use ra_cfg::CfgOptions; |
55 | use ra_db::{ | 55 | use ra_db::{ |
56 | salsa::{self, ParallelDatabase}, | 56 | salsa::{self, ParallelDatabase}, |
57 | CheckCanceled, FileLoader, SourceDatabase, | 57 | CheckCanceled, Env, FileLoader, SourceDatabase, |
58 | }; | 58 | }; |
59 | use ra_syntax::{SourceFile, TextRange, TextUnit}; | 59 | use ra_syntax::{SourceFile, TextRange, TextUnit}; |
60 | 60 | ||
@@ -240,7 +240,7 @@ impl Analysis { | |||
240 | // Default to enable test for single file. | 240 | // Default to enable test for single file. |
241 | let mut cfg_options = CfgOptions::default(); | 241 | let mut cfg_options = CfgOptions::default(); |
242 | cfg_options.insert_atom("test".into()); | 242 | cfg_options.insert_atom("test".into()); |
243 | crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options); | 243 | crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options, Env::default()); |
244 | change.add_file(source_root, file_id, "main.rs".into(), Arc::new(text)); | 244 | change.add_file(source_root, file_id, "main.rs".into(), Arc::new(text)); |
245 | change.set_crate_graph(crate_graph); | 245 | change.set_crate_graph(crate_graph); |
246 | host.apply_change(change); | 246 | 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 @@ | |||
3 | use std::sync::Arc; | 3 | use std::sync::Arc; |
4 | 4 | ||
5 | use ra_cfg::CfgOptions; | 5 | use ra_cfg::CfgOptions; |
6 | use ra_db::RelativePathBuf; | 6 | use ra_db::{Env, RelativePathBuf}; |
7 | use test_utils::{extract_offset, extract_range, parse_fixture, CURSOR_MARKER}; | 7 | use test_utils::{extract_offset, extract_range, parse_fixture, CURSOR_MARKER}; |
8 | 8 | ||
9 | use crate::{ | 9 | use crate::{ |
@@ -96,9 +96,15 @@ impl MockAnalysis { | |||
96 | let file_id = FileId(i as u32 + 1); | 96 | let file_id = FileId(i as u32 + 1); |
97 | let cfg_options = CfgOptions::default(); | 97 | let cfg_options = CfgOptions::default(); |
98 | if path == "/lib.rs" || path == "/main.rs" { | 98 | if path == "/lib.rs" || path == "/main.rs" { |
99 | root_crate = Some(crate_graph.add_crate_root(file_id, Edition2018, cfg_options)); | 99 | root_crate = Some(crate_graph.add_crate_root( |
100 | file_id, | ||
101 | Edition2018, | ||
102 | cfg_options, | ||
103 | Env::default(), | ||
104 | )); | ||
100 | } else if path.ends_with("/lib.rs") { | 105 | } else if path.ends_with("/lib.rs") { |
101 | let other_crate = crate_graph.add_crate_root(file_id, Edition2018, cfg_options); | 106 | let other_crate = |
107 | crate_graph.add_crate_root(file_id, Edition2018, cfg_options, Env::default()); | ||
102 | let crate_name = path.parent().unwrap().file_name().unwrap(); | 108 | let crate_name = path.parent().unwrap().file_name().unwrap(); |
103 | if let Some(root_crate) = root_crate { | 109 | if let Some(root_crate) = root_crate { |
104 | crate_graph.add_dep(root_crate, crate_name.into(), other_crate).unwrap(); | 110 | 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<CrateId> { | |||
34 | 34 | ||
35 | #[cfg(test)] | 35 | #[cfg(test)] |
36 | mod tests { | 36 | mod tests { |
37 | use ra_cfg::CfgOptions; | ||
38 | use ra_db::Env; | ||
39 | |||
37 | use crate::{ | 40 | use crate::{ |
38 | mock_analysis::{analysis_and_position, MockAnalysis}, | 41 | mock_analysis::{analysis_and_position, MockAnalysis}, |
39 | AnalysisChange, CrateGraph, | 42 | AnalysisChange, CrateGraph, |
40 | Edition::Edition2018, | 43 | Edition::Edition2018, |
41 | }; | 44 | }; |
42 | use ra_cfg::CfgOptions; | ||
43 | 45 | ||
44 | #[test] | 46 | #[test] |
45 | fn test_resolve_parent_module() { | 47 | fn test_resolve_parent_module() { |
@@ -87,7 +89,12 @@ mod tests { | |||
87 | assert!(host.analysis().crate_for(mod_file).unwrap().is_empty()); | 89 | assert!(host.analysis().crate_for(mod_file).unwrap().is_empty()); |
88 | 90 | ||
89 | let mut crate_graph = CrateGraph::default(); | 91 | let mut crate_graph = CrateGraph::default(); |
90 | let crate_id = crate_graph.add_crate_root(root_file, Edition2018, CfgOptions::default()); | 92 | let crate_id = crate_graph.add_crate_root( |
93 | root_file, | ||
94 | Edition2018, | ||
95 | CfgOptions::default(), | ||
96 | Env::default(), | ||
97 | ); | ||
91 | let mut change = AnalysisChange::new(); | 98 | let mut change = AnalysisChange::new(); |
92 | change.set_crate_graph(crate_graph); | 99 | change.set_crate_graph(crate_graph); |
93 | host.apply_change(change); | 100 | 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::{ | |||
13 | }; | 13 | }; |
14 | 14 | ||
15 | use ra_cfg::CfgOptions; | 15 | use ra_cfg::CfgOptions; |
16 | use ra_db::{CrateGraph, CrateId, Edition, FileId}; | 16 | use ra_db::{CrateGraph, CrateId, Edition, Env, FileId}; |
17 | use rustc_hash::FxHashMap; | 17 | use rustc_hash::FxHashMap; |
18 | use serde_json::from_reader; | 18 | use serde_json::from_reader; |
19 | 19 | ||
@@ -146,7 +146,12 @@ impl ProjectWorkspace { | |||
146 | }; | 146 | }; |
147 | crates.insert( | 147 | crates.insert( |
148 | crate_id, | 148 | crate_id, |
149 | crate_graph.add_crate_root(file_id, edition, cfg_options), | 149 | crate_graph.add_crate_root( |
150 | file_id, | ||
151 | edition, | ||
152 | cfg_options, | ||
153 | Env::default(), | ||
154 | ), | ||
150 | ); | 155 | ); |
151 | } | 156 | } |
152 | } | 157 | } |
@@ -180,8 +185,12 @@ impl ProjectWorkspace { | |||
180 | opts | 185 | opts |
181 | }; | 186 | }; |
182 | 187 | ||
183 | let crate_id = | 188 | let crate_id = crate_graph.add_crate_root( |
184 | crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options); | 189 | file_id, |
190 | Edition::Edition2018, | ||
191 | cfg_options, | ||
192 | Env::default(), | ||
193 | ); | ||
185 | sysroot_crates.insert(krate, crate_id); | 194 | sysroot_crates.insert(krate, crate_id); |
186 | names.insert(crate_id, krate.name(&sysroot).to_string()); | 195 | names.insert(crate_id, krate.name(&sysroot).to_string()); |
187 | } | 196 | } |
@@ -216,8 +225,12 @@ impl ProjectWorkspace { | |||
216 | opts.insert_features(pkg.features(&cargo).iter().map(Into::into)); | 225 | opts.insert_features(pkg.features(&cargo).iter().map(Into::into)); |
217 | opts | 226 | opts |
218 | }; | 227 | }; |
219 | let crate_id = | 228 | let crate_id = crate_graph.add_crate_root( |
220 | crate_graph.add_crate_root(file_id, edition, cfg_options); | 229 | file_id, |
230 | edition, | ||
231 | cfg_options, | ||
232 | Env::default(), | ||
233 | ); | ||
221 | names.insert(crate_id, pkg.name(&cargo).to_string()); | 234 | names.insert(crate_id, pkg.name(&cargo).to_string()); |
222 | if tgt.kind(&cargo) == TargetKind::Lib { | 235 | if tgt.kind(&cargo) == TargetKind::Lib { |
223 | lib_tgt = Some(crate_id); | 236 | lib_tgt = Some(crate_id); |