aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorvsrs <[email protected]>2020-05-16 09:57:41 +0100
committervsrs <[email protected]>2020-05-16 09:57:41 +0100
commitd901e0e709258f71183455865cb6f9e07b3dd5d3 (patch)
tree18c39839206d8c10bf2539cd0ae66162f02b3563 /crates
parenteeb98237d1cf5134ca3e41b85e1e9e07cab83c75 (diff)
Reimplement ra_db::fixture::ParsedMeta
in terms of test_utils::FixtureMeta
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_db/src/fixture.rs74
-rw-r--r--crates/test_utils/src/lib.rs5
2 files changed, 28 insertions, 51 deletions
diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs
index 51d4c493e..6e2c7ff72 100644
--- a/crates/ra_db/src/fixture.rs
+++ b/crates/ra_db/src/fixture.rs
@@ -49,7 +49,7 @@ use std::sync::Arc;
49 49
50use ra_cfg::CfgOptions; 50use ra_cfg::CfgOptions;
51use rustc_hash::FxHashMap; 51use rustc_hash::FxHashMap;
52use test_utils::{extract_offset, parse_fixture, parse_single_fixture, CURSOR_MARKER}; 52use test_utils::{extract_offset, parse_fixture, parse_single_fixture, FixtureMeta, CURSOR_MARKER};
53 53
54use crate::{ 54use crate::{
55 input::CrateName, CrateGraph, CrateId, Edition, Env, FileId, FilePosition, RelativePathBuf, 55 input::CrateName, CrateGraph, CrateId, Edition, Env, FileId, FilePosition, RelativePathBuf,
@@ -99,7 +99,7 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId
99 let fixture = parse_single_fixture(ra_fixture); 99 let fixture = parse_single_fixture(ra_fixture);
100 100
101 let crate_graph = if let Some(entry) = fixture { 101 let crate_graph = if let Some(entry) = fixture {
102 let meta = match parse_meta(&entry.meta) { 102 let meta = match ParsedMeta::from(&entry.parsed_meta) {
103 ParsedMeta::File(it) => it, 103 ParsedMeta::File(it) => it,
104 _ => panic!("with_single_file only support file meta"), 104 _ => panic!("with_single_file only support file meta"),
105 }; 105 };
@@ -156,7 +156,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
156 let mut file_position = None; 156 let mut file_position = None;
157 157
158 for entry in fixture.iter() { 158 for entry in fixture.iter() {
159 let meta = match parse_meta(&entry.meta) { 159 let meta = match ParsedMeta::from(&entry.parsed_meta) {
160 ParsedMeta::Root { path } => { 160 ParsedMeta::Root { path } => {
161 let source_root = std::mem::replace(&mut source_root, SourceRoot::new_local()); 161 let source_root = std::mem::replace(&mut source_root, SourceRoot::new_local());
162 db.set_source_root(source_root_id, Arc::new(source_root)); 162 db.set_source_root(source_root_id, Arc::new(source_root));
@@ -244,53 +244,31 @@ struct FileMeta {
244 env: Env, 244 env: Env,
245} 245}
246 246
247//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo) 247impl From<&FixtureMeta> for ParsedMeta {
248fn parse_meta(meta: &str) -> ParsedMeta { 248 fn from(meta: &FixtureMeta) -> Self {
249 let components = meta.split_ascii_whitespace().collect::<Vec<_>>(); 249 match meta {
250 250 FixtureMeta::Root { path } => {
251 if components[0] == "root" { 251 // `Self::Root` causes a false warning: 'variant is never constructed: `Root` '
252 let path: RelativePathBuf = components[1].into(); 252 // see https://github.com/rust-lang/rust/issues/69018
253 assert!(path.starts_with("/") && path.ends_with("/")); 253 ParsedMeta::Root { path: path.to_owned() }
254 return ParsedMeta::Root { path };
255 }
256
257 let path: RelativePathBuf = components[0].into();
258 assert!(path.starts_with("/"));
259
260 let mut krate = None;
261 let mut deps = Vec::new();
262 let mut edition = Edition::Edition2018;
263 let mut cfg = CfgOptions::default();
264 let mut env = Env::default();
265 for component in components[1..].iter() {
266 let (key, value) = split1(component, ':').unwrap();
267 match key {
268 "crate" => krate = Some(value.to_string()),
269 "deps" => deps = value.split(',').map(|it| it.to_string()).collect(),
270 "edition" => edition = Edition::from_str(&value).unwrap(),
271 "cfg" => {
272 for key in value.split(',') {
273 match split1(key, '=') {
274 None => cfg.insert_atom(key.into()),
275 Some((k, v)) => cfg.insert_key_value(k.into(), v.into()),
276 }
277 }
278 } 254 }
279 "env" => { 255 FixtureMeta::File(f) => Self::File(FileMeta {
280 for key in value.split(',') { 256 path: f.path.to_owned().into(),
281 if let Some((k, v)) = split1(key, '=') { 257 krate: f.krate.to_owned().into(),
282 env.set(k, v.into()); 258 deps: f.deps.to_owned(),
259 cfg: f.cfg.to_owned(),
260 edition: f
261 .edition
262 .as_ref()
263 .map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()),
264 env: {
265 let mut env = Env::default();
266 for (k, v) in &f.env {
267 env.set(&k, v.to_owned());
283 } 268 }
284 } 269 env
285 } 270 },
286 _ => panic!("bad component: {:?}", component), 271 }),
287 } 272 }
288 } 273 }
289
290 ParsedMeta::File(FileMeta { path, krate, deps, edition, cfg, env })
291}
292
293fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> {
294 let idx = haystack.find(delim)?;
295 Some((&haystack[..idx], &haystack[idx + delim.len_utf8()..]))
296} 274}
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs
index 12ae5f451..0c367ce71 100644
--- a/crates/test_utils/src/lib.rs
+++ b/crates/test_utils/src/lib.rs
@@ -16,10 +16,10 @@ use std::{
16 16
17pub use ra_cfg::CfgOptions; 17pub use ra_cfg::CfgOptions;
18 18
19use serde_json::Value;
20use text_size::{TextRange, TextSize};
21pub use relative_path::{RelativePath, RelativePathBuf}; 19pub use relative_path::{RelativePath, RelativePathBuf};
22pub use rustc_hash::FxHashMap; 20pub use rustc_hash::FxHashMap;
21use serde_json::Value;
22use text_size::{TextRange, TextSize};
23 23
24pub use difference::Changeset as __Changeset; 24pub use difference::Changeset as __Changeset;
25 25
@@ -292,7 +292,6 @@ fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> {
292 Some((&haystack[..idx], &haystack[idx + delim.len_utf8()..])) 292 Some((&haystack[..idx], &haystack[idx + delim.len_utf8()..]))
293} 293}
294 294
295
296/// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines. 295/// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines.
297/// This allows fixtures to start off in a different indentation, e.g. to align the first line with 296/// This allows fixtures to start off in a different indentation, e.g. to align the first line with
298/// the other lines visually: 297/// the other lines visually: