diff options
Diffstat (limited to 'crates/ra_db/src/fixture.rs')
-rw-r--r-- | crates/ra_db/src/fixture.rs | 70 |
1 files changed, 55 insertions, 15 deletions
diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index da7af110c..3dc86ca2d 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs | |||
@@ -5,7 +5,7 @@ use std::sync::Arc; | |||
5 | 5 | ||
6 | use ra_cfg::CfgOptions; | 6 | use ra_cfg::CfgOptions; |
7 | use rustc_hash::FxHashMap; | 7 | use rustc_hash::FxHashMap; |
8 | use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER}; | 8 | use test_utils::{extract_offset, parse_fixture, parse_single_fixture, CURSOR_MARKER}; |
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{ |
11 | input::CrateName, CrateGraph, CrateId, Edition, Env, FileId, FilePosition, RelativePathBuf, | 11 | input::CrateName, CrateGraph, CrateId, Edition, Env, FileId, FilePosition, RelativePathBuf, |
@@ -45,22 +45,45 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static { | |||
45 | 45 | ||
46 | impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {} | 46 | impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {} |
47 | 47 | ||
48 | fn with_single_file(db: &mut dyn SourceDatabaseExt, text: &str) -> FileId { | 48 | fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId { |
49 | let file_id = FileId(0); | 49 | let file_id = FileId(0); |
50 | let rel_path: RelativePathBuf = "/main.rs".into(); | 50 | let rel_path: RelativePathBuf = "/main.rs".into(); |
51 | 51 | ||
52 | let mut source_root = SourceRoot::new_local(); | 52 | let mut source_root = SourceRoot::new_local(); |
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 fixture = parse_single_fixture(ra_fixture); |
56 | crate_graph.add_crate_root( | 56 | |
57 | file_id, | 57 | let crate_graph = if let Some(entry) = fixture { |
58 | Edition::Edition2018, | 58 | let meta = match parse_meta(&entry.meta) { |
59 | CfgOptions::default(), | 59 | ParsedMeta::File(it) => it, |
60 | Env::default(), | 60 | _ => panic!("with_single_file only support file meta"), |
61 | ); | 61 | }; |
62 | 62 | ||
63 | db.set_file_text(file_id, Arc::new(text.to_string())); | 63 | let mut crate_graph = CrateGraph::default(); |
64 | crate_graph.add_crate_root( | ||
65 | file_id, | ||
66 | meta.edition, | ||
67 | meta.krate, | ||
68 | meta.cfg, | ||
69 | meta.env, | ||
70 | Default::default(), | ||
71 | ); | ||
72 | crate_graph | ||
73 | } else { | ||
74 | let mut crate_graph = CrateGraph::default(); | ||
75 | crate_graph.add_crate_root( | ||
76 | file_id, | ||
77 | Edition::Edition2018, | ||
78 | None, | ||
79 | CfgOptions::default(), | ||
80 | Env::default(), | ||
81 | Default::default(), | ||
82 | ); | ||
83 | crate_graph | ||
84 | }; | ||
85 | |||
86 | db.set_file_text(file_id, Arc::new(ra_fixture.to_string())); | ||
64 | db.set_file_relative_path(file_id, rel_path); | 87 | db.set_file_relative_path(file_id, rel_path); |
65 | db.set_file_source_root(file_id, WORKSPACE); | 88 | db.set_file_source_root(file_id, WORKSPACE); |
66 | db.set_source_root(WORKSPACE, Arc::new(source_root)); | 89 | db.set_source_root(WORKSPACE, Arc::new(source_root)); |
@@ -98,8 +121,14 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit | |||
98 | assert!(meta.path.starts_with(&source_root_prefix)); | 121 | assert!(meta.path.starts_with(&source_root_prefix)); |
99 | 122 | ||
100 | if let Some(krate) = meta.krate { | 123 | if let Some(krate) = meta.krate { |
101 | let crate_id = | 124 | let crate_id = crate_graph.add_crate_root( |
102 | crate_graph.add_crate_root(file_id, meta.edition, meta.cfg, Env::default()); | 125 | file_id, |
126 | meta.edition, | ||
127 | Some(krate.clone()), | ||
128 | meta.cfg, | ||
129 | meta.env, | ||
130 | Default::default(), | ||
131 | ); | ||
103 | let prev = crates.insert(krate.clone(), crate_id); | 132 | let prev = crates.insert(krate.clone(), crate_id); |
104 | assert!(prev.is_none()); | 133 | assert!(prev.is_none()); |
105 | for dep in meta.deps { | 134 | for dep in meta.deps { |
@@ -132,8 +161,10 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit | |||
132 | crate_graph.add_crate_root( | 161 | crate_graph.add_crate_root( |
133 | crate_root, | 162 | crate_root, |
134 | Edition::Edition2018, | 163 | Edition::Edition2018, |
164 | None, | ||
135 | CfgOptions::default(), | 165 | CfgOptions::default(), |
136 | Env::default(), | 166 | Env::default(), |
167 | Default::default(), | ||
137 | ); | 168 | ); |
138 | } else { | 169 | } else { |
139 | for (from, to) in crate_deps { | 170 | for (from, to) in crate_deps { |
@@ -160,9 +191,10 @@ struct FileMeta { | |||
160 | deps: Vec<String>, | 191 | deps: Vec<String>, |
161 | cfg: CfgOptions, | 192 | cfg: CfgOptions, |
162 | edition: Edition, | 193 | edition: Edition, |
194 | env: Env, | ||
163 | } | 195 | } |
164 | 196 | ||
165 | //- /lib.rs crate:foo deps:bar,baz | 197 | //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo) |
166 | fn parse_meta(meta: &str) -> ParsedMeta { | 198 | fn parse_meta(meta: &str) -> ParsedMeta { |
167 | let components = meta.split_ascii_whitespace().collect::<Vec<_>>(); | 199 | let components = meta.split_ascii_whitespace().collect::<Vec<_>>(); |
168 | 200 | ||
@@ -179,6 +211,7 @@ fn parse_meta(meta: &str) -> ParsedMeta { | |||
179 | let mut deps = Vec::new(); | 211 | let mut deps = Vec::new(); |
180 | let mut edition = Edition::Edition2018; | 212 | let mut edition = Edition::Edition2018; |
181 | let mut cfg = CfgOptions::default(); | 213 | let mut cfg = CfgOptions::default(); |
214 | let mut env = Env::default(); | ||
182 | for component in components[1..].iter() { | 215 | for component in components[1..].iter() { |
183 | let (key, value) = split1(component, ':').unwrap(); | 216 | let (key, value) = split1(component, ':').unwrap(); |
184 | match key { | 217 | match key { |
@@ -193,11 +226,18 @@ fn parse_meta(meta: &str) -> ParsedMeta { | |||
193 | } | 226 | } |
194 | } | 227 | } |
195 | } | 228 | } |
229 | "env" => { | ||
230 | for key in value.split(',') { | ||
231 | if let Some((k, v)) = split1(key, '=') { | ||
232 | env.set(k.into(), v.into()); | ||
233 | } | ||
234 | } | ||
235 | } | ||
196 | _ => panic!("bad component: {:?}", component), | 236 | _ => panic!("bad component: {:?}", component), |
197 | } | 237 | } |
198 | } | 238 | } |
199 | 239 | ||
200 | ParsedMeta::File(FileMeta { path, krate, deps, edition, cfg }) | 240 | ParsedMeta::File(FileMeta { path, krate, deps, edition, cfg, env }) |
201 | } | 241 | } |
202 | 242 | ||
203 | fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> { | 243 | fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> { |