From d901e0e709258f71183455865cb6f9e07b3dd5d3 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 16 May 2020 11:57:41 +0300 Subject: Reimplement ra_db::fixture::ParsedMeta in terms of test_utils::FixtureMeta --- crates/ra_db/src/fixture.rs | 74 ++++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 48 deletions(-) (limited to 'crates/ra_db') 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; use ra_cfg::CfgOptions; use rustc_hash::FxHashMap; -use test_utils::{extract_offset, parse_fixture, parse_single_fixture, CURSOR_MARKER}; +use test_utils::{extract_offset, parse_fixture, parse_single_fixture, FixtureMeta, CURSOR_MARKER}; use crate::{ 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 let fixture = parse_single_fixture(ra_fixture); let crate_graph = if let Some(entry) = fixture { - let meta = match parse_meta(&entry.meta) { + let meta = match ParsedMeta::from(&entry.parsed_meta) { ParsedMeta::File(it) => it, _ => panic!("with_single_file only support file meta"), }; @@ -156,7 +156,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option { let source_root = std::mem::replace(&mut source_root, SourceRoot::new_local()); db.set_source_root(source_root_id, Arc::new(source_root)); @@ -244,53 +244,31 @@ struct FileMeta { env: Env, } -//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo) -fn parse_meta(meta: &str) -> ParsedMeta { - let components = meta.split_ascii_whitespace().collect::>(); - - if components[0] == "root" { - let path: RelativePathBuf = components[1].into(); - assert!(path.starts_with("/") && path.ends_with("/")); - return ParsedMeta::Root { path }; - } - - let path: RelativePathBuf = components[0].into(); - assert!(path.starts_with("/")); - - let mut krate = None; - let mut deps = Vec::new(); - let mut edition = Edition::Edition2018; - let mut cfg = CfgOptions::default(); - let mut env = Env::default(); - for component in components[1..].iter() { - let (key, value) = split1(component, ':').unwrap(); - match key { - "crate" => krate = Some(value.to_string()), - "deps" => deps = value.split(',').map(|it| it.to_string()).collect(), - "edition" => edition = Edition::from_str(&value).unwrap(), - "cfg" => { - for key in value.split(',') { - match split1(key, '=') { - None => cfg.insert_atom(key.into()), - Some((k, v)) => cfg.insert_key_value(k.into(), v.into()), - } - } +impl From<&FixtureMeta> for ParsedMeta { + fn from(meta: &FixtureMeta) -> Self { + match meta { + FixtureMeta::Root { path } => { + // `Self::Root` causes a false warning: 'variant is never constructed: `Root` ' + // see https://github.com/rust-lang/rust/issues/69018 + ParsedMeta::Root { path: path.to_owned() } } - "env" => { - for key in value.split(',') { - if let Some((k, v)) = split1(key, '=') { - env.set(k, v.into()); + FixtureMeta::File(f) => Self::File(FileMeta { + path: f.path.to_owned().into(), + krate: f.krate.to_owned().into(), + deps: f.deps.to_owned(), + cfg: f.cfg.to_owned(), + edition: f + .edition + .as_ref() + .map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()), + env: { + let mut env = Env::default(); + for (k, v) in &f.env { + env.set(&k, v.to_owned()); } - } - } - _ => panic!("bad component: {:?}", component), + env + }, + }), } } - - ParsedMeta::File(FileMeta { path, krate, deps, edition, cfg, env }) -} - -fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> { - let idx = haystack.find(delim)?; - Some((&haystack[..idx], &haystack[idx + delim.len_utf8()..])) } -- cgit v1.2.3 From 256fb7556e9f4a329e673851427942c6403bacb6 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 16 May 2020 12:25:26 +0300 Subject: Remove temporary FixtureEntry parsed_meta field. --- crates/ra_db/src/fixture.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index 6e2c7ff72..8b62fe9aa 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs @@ -99,7 +99,7 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId let fixture = parse_single_fixture(ra_fixture); let crate_graph = if let Some(entry) = fixture { - let meta = match ParsedMeta::from(&entry.parsed_meta) { + let meta = match ParsedMeta::from(&entry.meta) { ParsedMeta::File(it) => it, _ => panic!("with_single_file only support file meta"), }; @@ -156,7 +156,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option { let source_root = std::mem::replace(&mut source_root, SourceRoot::new_local()); db.set_source_root(source_root_id, Arc::new(source_root)); -- cgit v1.2.3 From 2c00bd8c6a9476dbac427c84941fe6f54a8a95b1 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 16 May 2020 15:23:43 +0300 Subject: Propogate fixture meta to AnalysisHost Except crate name. --- crates/ra_db/src/fixture.rs | 10 ++-------- crates/ra_db/src/input.rs | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 8 deletions(-) (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index 8b62fe9aa..fd535ab15 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs @@ -254,20 +254,14 @@ impl From<&FixtureMeta> for ParsedMeta { } FixtureMeta::File(f) => Self::File(FileMeta { path: f.path.to_owned().into(), - krate: f.krate.to_owned().into(), + krate: f.crate_name.to_owned().into(), deps: f.deps.to_owned(), cfg: f.cfg.to_owned(), edition: f .edition .as_ref() .map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()), - env: { - let mut env = Env::default(); - for (k, v) in &f.env { - env.set(&k, v.to_owned()); - } - env - }, + env: Env::from(f.env.iter()), }), } } diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index ab14e2d5e..4d2d3b48a 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -311,6 +311,21 @@ impl fmt::Display for Edition { } } +impl<'a, T> From for Env +where + T: Iterator, +{ + fn from(iter: T) -> Self { + let mut result = Self::default(); + + for (k, v) in iter { + result.entries.insert(k.to_owned(), v.to_owned()); + } + + result + } +} + impl Env { pub fn set(&mut self, env: &str, value: String) { self.entries.insert(env.to_owned(), value); -- cgit v1.2.3