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_ide/src/mock_analysis.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/mock_analysis.rs b/crates/ra_ide/src/mock_analysis.rs index 2c13f206a..64c0684c5 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 (<|>)"); -- cgit v1.2.3 From 2dde9b19943d3f9557520428c92a52d75fb1deb3 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 16 May 2020 13:17:21 +0300 Subject: Use FixtureMeta in MockAnalysis --- crates/ra_ide/src/mock_analysis.rs | 93 +++++++++++++++++++++++++++++++------- 1 file changed, 77 insertions(+), 16 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/mock_analysis.rs b/crates/ra_ide/src/mock_analysis.rs index 64c0684c5..8d8e30714 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 <|>. -- 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_ide/src/call_hierarchy.rs | 29 +++++++++++++++++++++++++++++ crates/ra_ide/src/mock_analysis.rs | 31 +++++++++++++++++++++++++------ 2 files changed, 54 insertions(+), 6 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/call_hierarchy.rs b/crates/ra_ide/src/call_hierarchy.rs index 85d1f0cb1..defd8176f 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 8d8e30714..ad78d2d93 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(), ); -- cgit v1.2.3