From eeb98237d1cf5134ca3e41b85e1e9e07cab83c75 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 16 May 2020 11:16:32 +0300 Subject: [PATCH 1/7] parse fixture meta in test_utils crate --- crates/test_utils/Cargo.toml | 4 ++ crates/test_utils/src/lib.rs | 98 +++++++++++++++++++++++++++++++++++- 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/crates/test_utils/Cargo.toml b/crates/test_utils/Cargo.toml index 8ec986bcb0..4d185b01c7 100644 --- a/crates/test_utils/Cargo.toml +++ b/crates/test_utils/Cargo.toml @@ -11,3 +11,7 @@ doctest = false difference = "2.0.0" text-size = "1.0.0" serde_json = "1.0.48" +relative-path = "1.0.0" +rustc-hash = "1.1.0" + +ra_cfg = { path = "../ra_cfg" } \ No newline at end of file diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index b1e3c328f3..12ae5f4513 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -14,8 +14,12 @@ use std::{ path::{Path, PathBuf}, }; +pub use ra_cfg::CfgOptions; + use serde_json::Value; use text_size::{TextRange, TextSize}; +pub use relative_path::{RelativePath, RelativePathBuf}; +pub use rustc_hash::FxHashMap; pub use difference::Changeset as __Changeset; @@ -159,6 +163,33 @@ pub fn add_cursor(text: &str, offset: TextSize) -> String { pub struct FixtureEntry { pub meta: String, pub text: String, + + pub parsed_meta: FixtureMeta, +} + +#[derive(Debug, Eq, PartialEq)] +pub enum FixtureMeta { + Root { path: RelativePathBuf }, + File(FileMeta), +} + +#[derive(Debug, Eq, PartialEq)] +pub struct FileMeta { + pub path: RelativePathBuf, + pub krate: Option, + pub deps: Vec, + pub cfg: ra_cfg::CfgOptions, + pub edition: Option, + pub env: FxHashMap, +} + +impl FixtureMeta { + pub fn path(&self) -> &RelativePath { + match self { + FixtureMeta::Root { path } => &path, + FixtureMeta::File(f) => &f.path, + } + } } /// Parses text which looks like this: @@ -200,7 +231,8 @@ The offending line: {:?}"#, for line in lines.by_ref() { if line.starts_with("//-") { let meta = line["//-".len()..].trim().to_string(); - res.push(FixtureEntry { meta, text: String::new() }) + let parsed_meta = parse_meta(&meta); + res.push(FixtureEntry { meta, parsed_meta, text: String::new() }) } else if let Some(entry) = res.last_mut() { entry.text.push_str(line); entry.text.push('\n'); @@ -209,6 +241,58 @@ The offending line: {:?}"#, res } +//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo +fn parse_meta(meta: &str) -> FixtureMeta { + 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 FixtureMeta::Root { path }; + } + + let path: RelativePathBuf = components[0].into(); + assert!(path.starts_with("/")); + + let mut krate = None; + let mut deps = Vec::new(); + let mut edition = None; + let mut cfg = CfgOptions::default(); + let mut env = FxHashMap::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 = Some(value.to_string()), + "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()), + } + } + } + "env" => { + for key in value.split(',') { + if let Some((k, v)) = split1(key, '=') { + env.insert(k.into(), v.into()); + } + } + } + _ => panic!("bad component: {:?}", component), + } + } + + FixtureMeta::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()..])) +} + + /// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines. /// This allows fixtures to start off in a different indentation, e.g. to align the first line with /// the other lines visually: @@ -288,6 +372,18 @@ struct Bar; ) } +#[test] +fn parse_fixture_gets_full_meta() { + let fixture = r" + //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo + "; + let parsed = parse_fixture(fixture); + assert_eq!(1, parsed.len()); + + let parsed = &parsed[0]; + assert_eq!("\n", parsed.text); +} + /// Same as `parse_fixture`, except it allow empty fixture pub fn parse_single_fixture(fixture: &str) -> Option { if !fixture.lines().any(|it| it.trim_start().starts_with("//-")) { From d901e0e709258f71183455865cb6f9e07b3dd5d3 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 16 May 2020 11:57:41 +0300 Subject: [PATCH 2/7] Reimplement ra_db::fixture::ParsedMeta in terms of test_utils::FixtureMeta --- crates/ra_db/src/fixture.rs | 74 +++++++++++++----------------------- crates/test_utils/src/lib.rs | 5 +-- 2 files changed, 28 insertions(+), 51 deletions(-) diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index 51d4c493e3..6e2c7ff725 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()..])) } diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 12ae5f4513..0c367ce71a 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -16,10 +16,10 @@ use std::{ pub use ra_cfg::CfgOptions; -use serde_json::Value; -use text_size::{TextRange, TextSize}; pub use relative_path::{RelativePath, RelativePathBuf}; pub use rustc_hash::FxHashMap; +use serde_json::Value; +use text_size::{TextRange, TextSize}; pub use difference::Changeset as __Changeset; @@ -292,7 +292,6 @@ fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> { Some((&haystack[..idx], &haystack[idx + delim.len_utf8()..])) } - /// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines. /// This allows fixtures to start off in a different indentation, e.g. to align the first line with /// the other lines visually: From 256fb7556e9f4a329e673851427942c6403bacb6 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 16 May 2020 12:25:26 +0300 Subject: [PATCH 3/7] Remove temporary FixtureEntry parsed_meta field. --- crates/ra_db/src/fixture.rs | 4 ++-- crates/ra_ide/src/mock_analysis.rs | 7 ++++--- crates/rust-analyzer/tests/heavy_tests/support.rs | 2 +- crates/test_utils/src/lib.rs | 8 +++----- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index 6e2c7ff725..8b62fe9aae 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)); diff --git a/crates/ra_ide/src/mock_analysis.rs b/crates/ra_ide/src/mock_analysis.rs index 2c13f206a1..64c0684c5f 100644 --- a/crates/ra_ide/src/mock_analysis.rs +++ b/crates/ra_ide/src/mock_analysis.rs @@ -35,7 +35,7 @@ impl MockAnalysis { pub fn with_files(fixture: &str) -> MockAnalysis { let mut res = MockAnalysis::new(); for entry in parse_fixture(fixture) { - res.add_file(&entry.meta, &entry.text); + res.add_file(entry.meta.path().as_str(), &entry.text); } res } @@ -48,9 +48,10 @@ impl MockAnalysis { for entry in parse_fixture(fixture) { if entry.text.contains(CURSOR_MARKER) { assert!(position.is_none(), "only one marker (<|>) per fixture is allowed"); - position = Some(res.add_file_with_position(&entry.meta, &entry.text)); + position = + Some(res.add_file_with_position(&entry.meta.path().as_str(), &entry.text)); } else { - res.add_file(&entry.meta, &entry.text); + res.add_file(&entry.meta.path().as_str(), &entry.text); } } let position = position.expect("expected a marker (<|>)"); diff --git a/crates/rust-analyzer/tests/heavy_tests/support.rs b/crates/rust-analyzer/tests/heavy_tests/support.rs index 8756ad4a3c..7679e9ad7c 100644 --- a/crates/rust-analyzer/tests/heavy_tests/support.rs +++ b/crates/rust-analyzer/tests/heavy_tests/support.rs @@ -68,7 +68,7 @@ impl<'a> Project<'a> { let mut paths = vec![]; for entry in parse_fixture(self.fixture) { - let path = tmp_dir.path().join(entry.meta); + let path = tmp_dir.path().join(entry.meta.path().as_str()); fs::create_dir_all(path.parent().unwrap()).unwrap(); fs::write(path.as_path(), entry.text.as_bytes()).unwrap(); paths.push((path, entry.text)); diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 0c367ce71a..6a8d06ea7a 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -161,10 +161,8 @@ pub fn add_cursor(text: &str, offset: TextSize) -> String { #[derive(Debug, Eq, PartialEq)] pub struct FixtureEntry { - pub meta: String, + pub meta: FixtureMeta, pub text: String, - - pub parsed_meta: FixtureMeta, } #[derive(Debug, Eq, PartialEq)] @@ -231,8 +229,8 @@ The offending line: {:?}"#, for line in lines.by_ref() { if line.starts_with("//-") { let meta = line["//-".len()..].trim().to_string(); - let parsed_meta = parse_meta(&meta); - res.push(FixtureEntry { meta, parsed_meta, text: String::new() }) + let meta = parse_meta(&meta); + res.push(FixtureEntry { meta, text: String::new() }) } else if let Some(entry) = res.last_mut() { entry.text.push_str(line); entry.text.push('\n'); From 2dde9b19943d3f9557520428c92a52d75fb1deb3 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 16 May 2020 13:17:21 +0300 Subject: [PATCH 4/7] Use FixtureMeta in MockAnalysis --- crates/ra_ide/src/mock_analysis.rs | 93 +++++++++++++++++++++++++----- crates/test_utils/src/lib.rs | 9 ++- 2 files changed, 85 insertions(+), 17 deletions(-) diff --git a/crates/ra_ide/src/mock_analysis.rs b/crates/ra_ide/src/mock_analysis.rs index 64c0684c5f..8d8e307148 100644 --- a/crates/ra_ide/src/mock_analysis.rs +++ b/crates/ra_ide/src/mock_analysis.rs @@ -4,18 +4,61 @@ use std::sync::Arc; use ra_cfg::CfgOptions; use ra_db::{CrateName, Env, RelativePathBuf}; -use test_utils::{extract_offset, extract_range, parse_fixture, CURSOR_MARKER}; +use test_utils::{extract_offset, extract_range, parse_fixture, FixtureEntry, CURSOR_MARKER}; use crate::{ Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition::Edition2018, FileId, FilePosition, FileRange, SourceRootId, }; +#[derive(Debug)] +enum MockFileData { + Plain { path: String, content: String }, + Fixture(FixtureEntry), +} + +impl MockFileData { + fn new(path: String, content: String) -> Self { + // `Self::Plain` causes a false warning: 'variant is never constructed: `Plain` ' + // see https://github.com/rust-lang/rust/issues/69018 + MockFileData::Plain { path, content } + } + + fn path(&self) -> &str { + match self { + MockFileData::Plain { path, .. } => path.as_str(), + MockFileData::Fixture(f) => f.meta.path().as_str(), + } + } + + fn content(&self) -> &str { + match self { + MockFileData::Plain { content, .. } => content, + MockFileData::Fixture(f) => f.text.as_str(), + } + } + + fn cfg_options(&self) -> CfgOptions { + match self { + MockFileData::Fixture(f) => { + f.meta.cfg_options().map_or_else(Default::default, |o| o.clone()) + } + _ => CfgOptions::default(), + } + } +} + +impl From for MockFileData { + fn from(fixture: FixtureEntry) -> Self { + Self::Fixture(fixture) + } +} + /// Mock analysis is used in test to bootstrap an AnalysisHost/Analysis /// from a set of in-memory files. #[derive(Debug, Default)] pub struct MockAnalysis { - files: Vec<(String, String)>, + files: Vec, } impl MockAnalysis { @@ -35,7 +78,7 @@ impl MockAnalysis { pub fn with_files(fixture: &str) -> MockAnalysis { let mut res = MockAnalysis::new(); for entry in parse_fixture(fixture) { - res.add_file(entry.meta.path().as_str(), &entry.text); + res.add_file_fixture(entry); } res } @@ -48,31 +91,44 @@ impl MockAnalysis { for entry in parse_fixture(fixture) { if entry.text.contains(CURSOR_MARKER) { assert!(position.is_none(), "only one marker (<|>) per fixture is allowed"); - position = - Some(res.add_file_with_position(&entry.meta.path().as_str(), &entry.text)); + position = Some(res.add_file_fixture_with_position(entry)); } else { - res.add_file(&entry.meta.path().as_str(), &entry.text); + res.add_file_fixture(entry); } } let position = position.expect("expected a marker (<|>)"); (res, position) } + pub fn add_file_fixture(&mut self, fixture: FixtureEntry) -> FileId { + let file_id = self.next_id(); + self.files.push(MockFileData::from(fixture)); + file_id + } + + pub fn add_file_fixture_with_position(&mut self, mut fixture: FixtureEntry) -> FilePosition { + let (offset, text) = extract_offset(&fixture.text); + fixture.text = text; + let file_id = self.next_id(); + self.files.push(MockFileData::from(fixture)); + FilePosition { file_id, offset } + } + pub fn add_file(&mut self, path: &str, text: &str) -> FileId { - let file_id = FileId((self.files.len() + 1) as u32); - self.files.push((path.to_string(), text.to_string())); + let file_id = self.next_id(); + self.files.push(MockFileData::new(path.to_string(), text.to_string())); file_id } pub fn add_file_with_position(&mut self, path: &str, text: &str) -> FilePosition { let (offset, text) = extract_offset(text); - let file_id = FileId((self.files.len() + 1) as u32); - self.files.push((path.to_string(), text)); + let file_id = self.next_id(); + self.files.push(MockFileData::new(path.to_string(), text)); FilePosition { file_id, offset } } pub fn add_file_with_range(&mut self, path: &str, text: &str) -> FileRange { let (range, text) = extract_range(text); - let file_id = FileId((self.files.len() + 1) as u32); - self.files.push((path.to_string(), text)); + let file_id = self.next_id(); + self.files.push(MockFileData::new(path.to_string(), text)); FileRange { file_id, range } } pub fn id_of(&self, path: &str) -> FileId { @@ -80,7 +136,7 @@ impl MockAnalysis { .files .iter() .enumerate() - .find(|(_, (p, _text))| path == p) + .find(|(_, data)| path == data.path()) .expect("no file in this mock"); FileId(idx as u32 + 1) } @@ -91,11 +147,12 @@ impl MockAnalysis { change.add_root(source_root, true); let mut crate_graph = CrateGraph::default(); let mut root_crate = None; - for (i, (path, contents)) in self.files.into_iter().enumerate() { + for (i, data) in self.files.into_iter().enumerate() { + let path = data.path(); assert!(path.starts_with('/')); let path = RelativePathBuf::from_path(&path[1..]).unwrap(); + let cfg_options = data.cfg_options(); 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, @@ -123,7 +180,7 @@ impl MockAnalysis { .unwrap(); } } - change.add_file(source_root, file_id, path, Arc::new(contents)); + change.add_file(source_root, file_id, path, Arc::new(data.content().to_owned())); } change.set_crate_graph(crate_graph); host.apply_change(change); @@ -132,6 +189,10 @@ impl MockAnalysis { pub fn analysis(self) -> Analysis { self.analysis_host().analysis() } + + fn next_id(&self) -> FileId { + FileId((self.files.len() + 1) as u32) + } } /// Creates analysis from a multi-file fixture, returns positions marked with <|>. diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 6a8d06ea7a..584ca5c391 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -176,7 +176,7 @@ pub struct FileMeta { pub path: RelativePathBuf, pub krate: Option, pub deps: Vec, - pub cfg: ra_cfg::CfgOptions, + pub cfg: CfgOptions, pub edition: Option, pub env: FxHashMap, } @@ -188,6 +188,13 @@ impl FixtureMeta { FixtureMeta::File(f) => &f.path, } } + + pub fn cfg_options(&self) -> Option<&CfgOptions> { + match self { + FixtureMeta::File(f) => Some(&f.cfg), + _ => None, + } + } } /// Parses text which looks like this: From 2c00bd8c6a9476dbac427c84941fe6f54a8a95b1 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 16 May 2020 15:23:43 +0300 Subject: [PATCH 5/7] Propogate fixture meta to AnalysisHost Except crate name. --- crates/ra_db/src/fixture.rs | 10 ++----- crates/ra_db/src/input.rs | 15 ++++++++++ crates/ra_ide/src/call_hierarchy.rs | 29 +++++++++++++++++++ crates/ra_ide/src/mock_analysis.rs | 31 ++++++++++++++++---- crates/test_utils/src/lib.rs | 44 +++++++++++++++++++++++++++-- 5 files changed, 113 insertions(+), 16 deletions(-) diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index 8b62fe9aae..fd535ab15d 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 ab14e2d5e6..4d2d3b48a0 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); diff --git a/crates/ra_ide/src/call_hierarchy.rs b/crates/ra_ide/src/call_hierarchy.rs index 85d1f0cb15..defd8176ff 100644 --- a/crates/ra_ide/src/call_hierarchy.rs +++ b/crates/ra_ide/src/call_hierarchy.rs @@ -245,6 +245,35 @@ mod tests { ); } + #[test] + fn test_call_hierarchy_in_tests_mod() { + check_hierarchy( + r#" + //- /lib.rs cfg:test + fn callee() {} + fn caller1() { + call<|>ee(); + } + + #[cfg(test)] + mod tests { + use super::*; + + #[test] + fn test_caller() { + callee(); + } + } + "#, + "callee FN_DEF FileId(1) 0..14 3..9", + &[ + "caller1 FN_DEF FileId(1) 15..45 18..25 : [34..40]", + "test_caller FN_DEF FileId(1) 93..147 108..119 : [132..138]", + ], + &[], + ); + } + #[test] fn test_call_hierarchy_in_different_files() { check_hierarchy( diff --git a/crates/ra_ide/src/mock_analysis.rs b/crates/ra_ide/src/mock_analysis.rs index 8d8e307148..ad78d2d93f 100644 --- a/crates/ra_ide/src/mock_analysis.rs +++ b/crates/ra_ide/src/mock_analysis.rs @@ -1,5 +1,6 @@ //! FIXME: write short doc here +use std::str::FromStr; use std::sync::Arc; use ra_cfg::CfgOptions; @@ -7,8 +8,8 @@ use ra_db::{CrateName, Env, RelativePathBuf}; use test_utils::{extract_offset, extract_range, parse_fixture, FixtureEntry, CURSOR_MARKER}; use crate::{ - Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition::Edition2018, FileId, FilePosition, - FileRange, SourceRootId, + Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition, FileId, FilePosition, FileRange, + SourceRootId, }; #[derive(Debug)] @@ -46,6 +47,22 @@ impl MockFileData { _ => CfgOptions::default(), } } + + fn edition(&self) -> Edition { + match self { + MockFileData::Fixture(f) => { + f.meta.edition().map_or(Edition::Edition2018, |v| Edition::from_str(v).unwrap()) + } + _ => Edition::Edition2018, + } + } + + fn env(&self) -> Env { + match self { + MockFileData::Fixture(f) => Env::from(f.meta.env()), + _ => Env::default(), + } + } } impl From for MockFileData { @@ -153,13 +170,15 @@ impl MockAnalysis { let path = RelativePathBuf::from_path(&path[1..]).unwrap(); let cfg_options = data.cfg_options(); let file_id = FileId(i as u32 + 1); + let edition = data.edition(); + let env = data.env(); if path == "/lib.rs" || path == "/main.rs" { root_crate = Some(crate_graph.add_crate_root( file_id, - Edition2018, + edition, None, cfg_options, - Env::default(), + env, Default::default(), Default::default(), )); @@ -167,10 +186,10 @@ impl MockAnalysis { let crate_name = path.parent().unwrap().file_name().unwrap(); let other_crate = crate_graph.add_crate_root( file_id, - Edition2018, + edition, Some(CrateName::new(crate_name).unwrap()), cfg_options, - Env::default(), + env, Default::default(), Default::default(), ); diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 584ca5c391..e7fa201e9e 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -174,7 +174,7 @@ pub enum FixtureMeta { #[derive(Debug, Eq, PartialEq)] pub struct FileMeta { pub path: RelativePathBuf, - pub krate: Option, + pub crate_name: Option, pub deps: Vec, pub cfg: CfgOptions, pub edition: Option, @@ -189,12 +189,52 @@ impl FixtureMeta { } } + pub fn crate_name(&self) -> Option<&String> { + match self { + FixtureMeta::File(f) => f.crate_name.as_ref(), + _ => None, + } + } + pub fn cfg_options(&self) -> Option<&CfgOptions> { match self { FixtureMeta::File(f) => Some(&f.cfg), _ => None, } } + + pub fn edition(&self) -> Option<&String> { + match self { + FixtureMeta::File(f) => f.edition.as_ref(), + _ => None, + } + } + + pub fn env(&self) -> impl Iterator { + struct EnvIter<'a> { + iter: Option>, + } + + impl<'a> EnvIter<'a> { + fn new(meta: &'a FixtureMeta) -> Self { + Self { + iter: match meta { + FixtureMeta::File(f) => Some(f.env.iter()), + _ => None, + }, + } + } + } + + impl<'a> Iterator for EnvIter<'a> { + type Item = (&'a String, &'a String); + fn next(&mut self) -> Option { + self.iter.as_mut().and_then(|i| i.next()) + } + } + + EnvIter::new(self) + } } /// Parses text which looks like this: @@ -289,7 +329,7 @@ fn parse_meta(meta: &str) -> FixtureMeta { } } - FixtureMeta::File(FileMeta { path, krate, deps, edition, cfg, env }) + FixtureMeta::File(FileMeta { path, crate_name: krate, deps, edition, cfg, env }) } fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> { From 7e9c7ac4ee5c9295d811670277bc1aeb9775998c Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 16 May 2020 16:56:41 +0300 Subject: [PATCH 6/7] smoke test --- crates/test_utils/src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index e7fa201e9e..fc5ae8f073 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -426,6 +426,12 @@ fn parse_fixture_gets_full_meta() { let parsed = &parsed[0]; assert_eq!("\n", parsed.text); + + let meta = &parsed.meta; + assert_eq!("foo", meta.crate_name().unwrap()); + assert_eq!("/lib.rs", meta.path()); + assert!(meta.cfg_options().is_some()); + assert_eq!(2, meta.env().count()); } /// Same as `parse_fixture`, except it allow empty fixture From cd45c73b660f85acc9b564e536bc407f0836891d Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 16 May 2020 17:25:12 +0300 Subject: [PATCH 7/7] JFF, ra_fixture arg name for a code highlighting --- crates/test_utils/src/lib.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index fc5ae8f073..45abfead99 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -245,8 +245,8 @@ impl FixtureMeta { /// line 2 /// // - other meta /// ``` -pub fn parse_fixture(fixture: &str) -> Vec { - let fixture = indent_first_line(fixture); +pub fn parse_fixture(ra_fixture: &str) -> Vec { + let fixture = indent_first_line(ra_fixture); let margin = fixture_margin(&fixture); let mut lines = fixture @@ -418,14 +418,16 @@ struct Bar; #[test] fn parse_fixture_gets_full_meta() { - let fixture = r" + let parsed = parse_fixture( + r" //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo - "; - let parsed = parse_fixture(fixture); + mod m; + ", + ); assert_eq!(1, parsed.len()); let parsed = &parsed[0]; - assert_eq!("\n", parsed.text); + assert_eq!("mod m;\n\n", parsed.text); let meta = &parsed.meta; assert_eq!("foo", meta.crate_name().unwrap()); @@ -435,12 +437,12 @@ fn parse_fixture_gets_full_meta() { } /// Same as `parse_fixture`, except it allow empty fixture -pub fn parse_single_fixture(fixture: &str) -> Option { - if !fixture.lines().any(|it| it.trim_start().starts_with("//-")) { +pub fn parse_single_fixture(ra_fixture: &str) -> Option { + if !ra_fixture.lines().any(|it| it.trim_start().starts_with("//-")) { return None; } - let fixtures = parse_fixture(fixture); + let fixtures = parse_fixture(ra_fixture); if fixtures.len() > 1 { panic!("too many fixtures"); }