diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index 8b62fe9aae93..fd535ab15d4e 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs @@ -254,20 +254,14 @@ fn from(meta: &FixtureMeta) -> Self { } 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 ab14e2d5e64d..4d2d3b48a0c0 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -311,6 +311,21 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { } } +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 85d1f0cb15e7..defd8176ff87 100644 --- a/crates/ra_ide/src/call_hierarchy.rs +++ b/crates/ra_ide/src/call_hierarchy.rs @@ -245,6 +245,35 @@ fn caller2() { ); } + #[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 8d8e30714854..ad78d2d93fd4 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 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 @@ fn cfg_options(&self) -> CfgOptions { _ => 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 @@ pub fn analysis_host(self) -> AnalysisHost { 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 @@ pub fn analysis_host(self) -> AnalysisHost { 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 584ca5c39155..e7fa201e9e7f 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 @@ pub fn path(&self) -> &RelativePath { } } + 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)> {