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 +++++++++++++ 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(-) (limited to 'crates') 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); 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(), ); diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 584ca5c39..e7fa201e9 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)> { -- cgit v1.2.3