Propogate fixture meta to AnalysisHost

Except crate name.
This commit is contained in:
vsrs 2020-05-16 15:23:43 +03:00
parent 2dde9b1994
commit 2c00bd8c6a
5 changed files with 113 additions and 16 deletions

View file

@ -254,20 +254,14 @@ impl From<&FixtureMeta> for ParsedMeta {
} }
FixtureMeta::File(f) => Self::File(FileMeta { FixtureMeta::File(f) => Self::File(FileMeta {
path: f.path.to_owned().into(), path: f.path.to_owned().into(),
krate: f.krate.to_owned().into(), krate: f.crate_name.to_owned().into(),
deps: f.deps.to_owned(), deps: f.deps.to_owned(),
cfg: f.cfg.to_owned(), cfg: f.cfg.to_owned(),
edition: f edition: f
.edition .edition
.as_ref() .as_ref()
.map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()), .map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()),
env: { env: Env::from(f.env.iter()),
let mut env = Env::default();
for (k, v) in &f.env {
env.set(&k, v.to_owned());
}
env
},
}), }),
} }
} }

View file

@ -311,6 +311,21 @@ impl fmt::Display for Edition {
} }
} }
impl<'a, T> From<T> for Env
where
T: Iterator<Item = (&'a String, &'a String)>,
{
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 { impl Env {
pub fn set(&mut self, env: &str, value: String) { pub fn set(&mut self, env: &str, value: String) {
self.entries.insert(env.to_owned(), value); self.entries.insert(env.to_owned(), value);

View file

@ -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] #[test]
fn test_call_hierarchy_in_different_files() { fn test_call_hierarchy_in_different_files() {
check_hierarchy( check_hierarchy(

View file

@ -1,5 +1,6 @@
//! FIXME: write short doc here //! FIXME: write short doc here
use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
use ra_cfg::CfgOptions; 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 test_utils::{extract_offset, extract_range, parse_fixture, FixtureEntry, CURSOR_MARKER};
use crate::{ use crate::{
Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition::Edition2018, FileId, FilePosition, Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition, FileId, FilePosition, FileRange,
FileRange, SourceRootId, SourceRootId,
}; };
#[derive(Debug)] #[derive(Debug)]
@ -46,6 +47,22 @@ impl MockFileData {
_ => CfgOptions::default(), _ => 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<FixtureEntry> for MockFileData { impl From<FixtureEntry> for MockFileData {
@ -153,13 +170,15 @@ impl MockAnalysis {
let path = RelativePathBuf::from_path(&path[1..]).unwrap(); let path = RelativePathBuf::from_path(&path[1..]).unwrap();
let cfg_options = data.cfg_options(); let cfg_options = data.cfg_options();
let file_id = FileId(i as u32 + 1); let file_id = FileId(i as u32 + 1);
let edition = data.edition();
let env = data.env();
if path == "/lib.rs" || path == "/main.rs" { if path == "/lib.rs" || path == "/main.rs" {
root_crate = Some(crate_graph.add_crate_root( root_crate = Some(crate_graph.add_crate_root(
file_id, file_id,
Edition2018, edition,
None, None,
cfg_options, cfg_options,
Env::default(), env,
Default::default(), Default::default(),
Default::default(), Default::default(),
)); ));
@ -167,10 +186,10 @@ impl MockAnalysis {
let crate_name = path.parent().unwrap().file_name().unwrap(); let crate_name = path.parent().unwrap().file_name().unwrap();
let other_crate = crate_graph.add_crate_root( let other_crate = crate_graph.add_crate_root(
file_id, file_id,
Edition2018, edition,
Some(CrateName::new(crate_name).unwrap()), Some(CrateName::new(crate_name).unwrap()),
cfg_options, cfg_options,
Env::default(), env,
Default::default(), Default::default(),
Default::default(), Default::default(),
); );

View file

@ -174,7 +174,7 @@ pub enum FixtureMeta {
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub struct FileMeta { pub struct FileMeta {
pub path: RelativePathBuf, pub path: RelativePathBuf,
pub krate: Option<String>, pub crate_name: Option<String>,
pub deps: Vec<String>, pub deps: Vec<String>,
pub cfg: CfgOptions, pub cfg: CfgOptions,
pub edition: Option<String>, pub edition: Option<String>,
@ -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> { pub fn cfg_options(&self) -> Option<&CfgOptions> {
match self { match self {
FixtureMeta::File(f) => Some(&f.cfg), FixtureMeta::File(f) => Some(&f.cfg),
_ => None, _ => None,
} }
} }
pub fn edition(&self) -> Option<&String> {
match self {
FixtureMeta::File(f) => f.edition.as_ref(),
_ => None,
}
}
pub fn env(&self) -> impl Iterator<Item = (&String, &String)> {
struct EnvIter<'a> {
iter: Option<std::collections::hash_map::Iter<'a, String, String>>,
}
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::Item> {
self.iter.as_mut().and_then(|i| i.next())
}
}
EnvIter::new(self)
}
} }
/// Parses text which looks like this: /// 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)> { fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> {