Skip to content

Commit 2c00bd8

Browse files
committed
Propogate fixture meta to AnalysisHost
Except crate name.
1 parent 2dde9b1 commit 2c00bd8

File tree

5 files changed

+113
-16
lines changed

5 files changed

+113
-16
lines changed

crates/ra_db/src/fixture.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,14 @@ impl From<&FixtureMeta> for ParsedMeta {
254254
}
255255
FixtureMeta::File(f) => Self::File(FileMeta {
256256
path: f.path.to_owned().into(),
257-
krate: f.krate.to_owned().into(),
257+
krate: f.crate_name.to_owned().into(),
258258
deps: f.deps.to_owned(),
259259
cfg: f.cfg.to_owned(),
260260
edition: f
261261
.edition
262262
.as_ref()
263263
.map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()),
264-
env: {
265-
let mut env = Env::default();
266-
for (k, v) in &f.env {
267-
env.set(&k, v.to_owned());
268-
}
269-
env
270-
},
264+
env: Env::from(f.env.iter()),
271265
}),
272266
}
273267
}

crates/ra_db/src/input.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,21 @@ impl fmt::Display for Edition {
311311
}
312312
}
313313

314+
impl<'a, T> From<T> for Env
315+
where
316+
T: Iterator<Item = (&'a String, &'a String)>,
317+
{
318+
fn from(iter: T) -> Self {
319+
let mut result = Self::default();
320+
321+
for (k, v) in iter {
322+
result.entries.insert(k.to_owned(), v.to_owned());
323+
}
324+
325+
result
326+
}
327+
}
328+
314329
impl Env {
315330
pub fn set(&mut self, env: &str, value: String) {
316331
self.entries.insert(env.to_owned(), value);

crates/ra_ide/src/call_hierarchy.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,35 @@ mod tests {
245245
);
246246
}
247247

248+
#[test]
249+
fn test_call_hierarchy_in_tests_mod() {
250+
check_hierarchy(
251+
r#"
252+
//- /lib.rs cfg:test
253+
fn callee() {}
254+
fn caller1() {
255+
call<|>ee();
256+
}
257+
258+
#[cfg(test)]
259+
mod tests {
260+
use super::*;
261+
262+
#[test]
263+
fn test_caller() {
264+
callee();
265+
}
266+
}
267+
"#,
268+
"callee FN_DEF FileId(1) 0..14 3..9",
269+
&[
270+
"caller1 FN_DEF FileId(1) 15..45 18..25 : [34..40]",
271+
"test_caller FN_DEF FileId(1) 93..147 108..119 : [132..138]",
272+
],
273+
&[],
274+
);
275+
}
276+
248277
#[test]
249278
fn test_call_hierarchy_in_different_files() {
250279
check_hierarchy(

crates/ra_ide/src/mock_analysis.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
//! FIXME: write short doc here
22
3+
use std::str::FromStr;
34
use std::sync::Arc;
45

56
use ra_cfg::CfgOptions;
67
use ra_db::{CrateName, Env, RelativePathBuf};
78
use test_utils::{extract_offset, extract_range, parse_fixture, FixtureEntry, CURSOR_MARKER};
89

910
use crate::{
10-
Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition::Edition2018, FileId, FilePosition,
11-
FileRange, SourceRootId,
11+
Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition, FileId, FilePosition, FileRange,
12+
SourceRootId,
1213
};
1314

1415
#[derive(Debug)]
@@ -46,6 +47,22 @@ impl MockFileData {
4647
_ => CfgOptions::default(),
4748
}
4849
}
50+
51+
fn edition(&self) -> Edition {
52+
match self {
53+
MockFileData::Fixture(f) => {
54+
f.meta.edition().map_or(Edition::Edition2018, |v| Edition::from_str(v).unwrap())
55+
}
56+
_ => Edition::Edition2018,
57+
}
58+
}
59+
60+
fn env(&self) -> Env {
61+
match self {
62+
MockFileData::Fixture(f) => Env::from(f.meta.env()),
63+
_ => Env::default(),
64+
}
65+
}
4966
}
5067

5168
impl From<FixtureEntry> for MockFileData {
@@ -153,24 +170,26 @@ impl MockAnalysis {
153170
let path = RelativePathBuf::from_path(&path[1..]).unwrap();
154171
let cfg_options = data.cfg_options();
155172
let file_id = FileId(i as u32 + 1);
173+
let edition = data.edition();
174+
let env = data.env();
156175
if path == "/lib.rs" || path == "/main.rs" {
157176
root_crate = Some(crate_graph.add_crate_root(
158177
file_id,
159-
Edition2018,
178+
edition,
160179
None,
161180
cfg_options,
162-
Env::default(),
181+
env,
163182
Default::default(),
164183
Default::default(),
165184
));
166185
} else if path.ends_with("/lib.rs") {
167186
let crate_name = path.parent().unwrap().file_name().unwrap();
168187
let other_crate = crate_graph.add_crate_root(
169188
file_id,
170-
Edition2018,
189+
edition,
171190
Some(CrateName::new(crate_name).unwrap()),
172191
cfg_options,
173-
Env::default(),
192+
env,
174193
Default::default(),
175194
Default::default(),
176195
);

crates/test_utils/src/lib.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ pub enum FixtureMeta {
174174
#[derive(Debug, Eq, PartialEq)]
175175
pub struct FileMeta {
176176
pub path: RelativePathBuf,
177-
pub krate: Option<String>,
177+
pub crate_name: Option<String>,
178178
pub deps: Vec<String>,
179179
pub cfg: CfgOptions,
180180
pub edition: Option<String>,
@@ -189,12 +189,52 @@ impl FixtureMeta {
189189
}
190190
}
191191

192+
pub fn crate_name(&self) -> Option<&String> {
193+
match self {
194+
FixtureMeta::File(f) => f.crate_name.as_ref(),
195+
_ => None,
196+
}
197+
}
198+
192199
pub fn cfg_options(&self) -> Option<&CfgOptions> {
193200
match self {
194201
FixtureMeta::File(f) => Some(&f.cfg),
195202
_ => None,
196203
}
197204
}
205+
206+
pub fn edition(&self) -> Option<&String> {
207+
match self {
208+
FixtureMeta::File(f) => f.edition.as_ref(),
209+
_ => None,
210+
}
211+
}
212+
213+
pub fn env(&self) -> impl Iterator<Item = (&String, &String)> {
214+
struct EnvIter<'a> {
215+
iter: Option<std::collections::hash_map::Iter<'a, String, String>>,
216+
}
217+
218+
impl<'a> EnvIter<'a> {
219+
fn new(meta: &'a FixtureMeta) -> Self {
220+
Self {
221+
iter: match meta {
222+
FixtureMeta::File(f) => Some(f.env.iter()),
223+
_ => None,
224+
},
225+
}
226+
}
227+
}
228+
229+
impl<'a> Iterator for EnvIter<'a> {
230+
type Item = (&'a String, &'a String);
231+
fn next(&mut self) -> Option<Self::Item> {
232+
self.iter.as_mut().and_then(|i| i.next())
233+
}
234+
}
235+
236+
EnvIter::new(self)
237+
}
198238
}
199239

200240
/// Parses text which looks like this:
@@ -289,7 +329,7 @@ fn parse_meta(meta: &str) -> FixtureMeta {
289329
}
290330
}
291331

292-
FixtureMeta::File(FileMeta { path, krate, deps, edition, cfg, env })
332+
FixtureMeta::File(FileMeta { path, crate_name: krate, deps, edition, cfg, env })
293333
}
294334

295335
fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> {

0 commit comments

Comments
 (0)