2019-09-30 03:58:53 -05:00
|
|
|
//! FIXME: write short doc here
|
2020-06-24 05:06:53 -05:00
|
|
|
use std::sync::Arc;
|
2019-01-08 13:33:36 -06:00
|
|
|
|
2020-08-13 09:25:38 -05:00
|
|
|
use base_db::{CrateName, FileSet, SourceRoot, VfsPath};
|
2020-08-13 03:19:09 -05:00
|
|
|
use cfg::CfgOptions;
|
2020-06-30 06:03:08 -05:00
|
|
|
use test_utils::{
|
|
|
|
extract_annotations, extract_range_or_offset, Fixture, RangeOrOffset, CURSOR_MARKER,
|
|
|
|
};
|
2019-01-08 13:33:36 -06:00
|
|
|
|
2019-07-04 15:05:17 -05:00
|
|
|
use crate::{
|
2020-05-16 07:23:43 -05:00
|
|
|
Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition, FileId, FilePosition, FileRange,
|
2019-07-04 15:05:17 -05:00
|
|
|
};
|
2019-01-08 13:33:36 -06:00
|
|
|
|
|
|
|
/// Mock analysis is used in test to bootstrap an AnalysisHost/Analysis
|
|
|
|
/// from a set of in-memory files.
|
|
|
|
#[derive(Debug, Default)]
|
2020-10-02 07:26:40 -05:00
|
|
|
pub(crate) struct MockAnalysis {
|
2020-06-24 05:06:53 -05:00
|
|
|
files: Vec<Fixture>,
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MockAnalysis {
|
|
|
|
/// Creates `MockAnalysis` using a fixture data in the following format:
|
|
|
|
///
|
2019-02-19 10:51:48 -06:00
|
|
|
/// ```not_rust
|
2019-01-08 13:33:36 -06:00
|
|
|
/// //- /main.rs
|
|
|
|
/// mod foo;
|
|
|
|
/// fn main() {}
|
|
|
|
///
|
|
|
|
/// //- /foo.rs
|
|
|
|
/// struct Baz;
|
|
|
|
/// ```
|
2020-10-02 07:26:40 -05:00
|
|
|
pub(crate) fn with_files(ra_fixture: &str) -> MockAnalysis {
|
2020-06-24 05:01:17 -05:00
|
|
|
let (res, pos) = MockAnalysis::with_fixture(ra_fixture);
|
|
|
|
assert!(pos.is_none());
|
2019-01-08 13:33:36 -06:00
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Same as `with_files`, but requires that a single file contains a `<|>` marker,
|
|
|
|
/// whose position is also returned.
|
2020-10-02 07:26:40 -05:00
|
|
|
pub(crate) fn with_files_and_position(fixture: &str) -> (MockAnalysis, FilePosition) {
|
2020-06-24 05:01:17 -05:00
|
|
|
let (res, position) = MockAnalysis::with_fixture(fixture);
|
|
|
|
let (file_id, range_or_offset) = position.expect("expected a marker (<|>)");
|
|
|
|
let offset = match range_or_offset {
|
|
|
|
RangeOrOffset::Range(_) => panic!(),
|
|
|
|
RangeOrOffset::Offset(it) => it,
|
|
|
|
};
|
|
|
|
(res, FilePosition { file_id, offset })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_fixture(fixture: &str) -> (MockAnalysis, Option<(FileId, RangeOrOffset)>) {
|
2019-01-08 13:33:36 -06:00
|
|
|
let mut position = None;
|
2020-06-24 04:51:45 -05:00
|
|
|
let mut res = MockAnalysis::default();
|
2020-06-24 04:48:44 -05:00
|
|
|
for mut entry in Fixture::parse(fixture) {
|
2019-01-08 13:33:36 -06:00
|
|
|
if entry.text.contains(CURSOR_MARKER) {
|
2019-02-08 05:49:43 -06:00
|
|
|
assert!(position.is_none(), "only one marker (<|>) per fixture is allowed");
|
2020-06-24 05:01:17 -05:00
|
|
|
let (range_or_offset, text) = extract_range_or_offset(&entry.text);
|
2020-06-24 04:48:44 -05:00
|
|
|
entry.text = text;
|
|
|
|
let file_id = res.add_file_fixture(entry);
|
2020-06-24 05:01:17 -05:00
|
|
|
position = Some((file_id, range_or_offset));
|
2019-01-08 13:33:36 -06:00
|
|
|
} else {
|
2020-05-16 05:17:21 -05:00
|
|
|
res.add_file_fixture(entry);
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
(res, position)
|
|
|
|
}
|
|
|
|
|
2020-06-24 04:05:47 -05:00
|
|
|
fn add_file_fixture(&mut self, fixture: Fixture) -> FileId {
|
2020-06-24 05:06:53 -05:00
|
|
|
let file_id = FileId((self.files.len() + 1) as u32);
|
|
|
|
self.files.push(fixture);
|
2020-05-16 05:17:21 -05:00
|
|
|
file_id
|
|
|
|
}
|
|
|
|
|
2020-10-02 07:26:40 -05:00
|
|
|
pub(crate) fn id_of(&self, path: &str) -> FileId {
|
2020-07-09 07:33:03 -05:00
|
|
|
let (file_id, _) =
|
|
|
|
self.files().find(|(_, data)| path == data.path).expect("no file in this mock");
|
|
|
|
file_id
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2020-10-02 07:26:40 -05:00
|
|
|
pub(crate) fn annotations(&self) -> Vec<(FileRange, String)> {
|
2020-07-09 07:33:03 -05:00
|
|
|
self.files()
|
|
|
|
.flat_map(|(file_id, fixture)| {
|
2020-06-30 06:03:08 -05:00
|
|
|
let annotations = extract_annotations(&fixture.text);
|
2020-06-30 06:20:16 -05:00
|
|
|
annotations
|
|
|
|
.into_iter()
|
|
|
|
.map(move |(range, data)| (FileRange { file_id, range }, data))
|
2020-06-30 06:03:08 -05:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
2020-10-02 07:26:40 -05:00
|
|
|
pub(crate) fn files(&self) -> impl Iterator<Item = (FileId, &Fixture)> + '_ {
|
2020-07-09 07:33:03 -05:00
|
|
|
self.files.iter().enumerate().map(|(idx, fixture)| (FileId(idx as u32 + 1), fixture))
|
|
|
|
}
|
2020-10-02 07:26:40 -05:00
|
|
|
pub(crate) fn annotation(&self) -> (FileRange, String) {
|
2020-06-30 06:20:16 -05:00
|
|
|
let mut all = self.annotations();
|
2020-06-30 06:03:08 -05:00
|
|
|
assert_eq!(all.len(), 1);
|
2020-06-30 06:20:16 -05:00
|
|
|
all.pop().unwrap()
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2020-10-02 07:26:40 -05:00
|
|
|
pub(crate) fn analysis_host(self) -> AnalysisHost {
|
2019-01-08 13:33:36 -06:00
|
|
|
let mut host = AnalysisHost::default();
|
|
|
|
let mut change = AnalysisChange::new();
|
2020-06-11 04:04:09 -05:00
|
|
|
let mut file_set = FileSet::default();
|
2019-01-08 13:33:36 -06:00
|
|
|
let mut crate_graph = CrateGraph::default();
|
2019-02-04 14:42:37 -06:00
|
|
|
let mut root_crate = None;
|
2020-05-16 05:17:21 -05:00
|
|
|
for (i, data) in self.files.into_iter().enumerate() {
|
2020-06-24 05:06:53 -05:00
|
|
|
let path = data.path;
|
2019-01-08 13:33:36 -06:00
|
|
|
assert!(path.starts_with('/'));
|
2020-06-24 05:06:53 -05:00
|
|
|
|
|
|
|
let mut cfg = CfgOptions::default();
|
|
|
|
data.cfg_atoms.iter().for_each(|it| cfg.insert_atom(it.into()));
|
|
|
|
data.cfg_key_values.iter().for_each(|(k, v)| cfg.insert_key_value(k.into(), v.into()));
|
|
|
|
let edition: Edition =
|
|
|
|
data.edition.and_then(|it| it.parse().ok()).unwrap_or(Edition::Edition2018);
|
|
|
|
|
2019-01-27 09:07:45 -06:00
|
|
|
let file_id = FileId(i as u32 + 1);
|
2020-07-21 10:17:21 -05:00
|
|
|
let env = data.env.into_iter().collect();
|
2019-01-08 13:33:36 -06:00
|
|
|
if path == "/lib.rs" || path == "/main.rs" {
|
2019-11-22 04:55:03 -06:00
|
|
|
root_crate = Some(crate_graph.add_crate_root(
|
|
|
|
file_id,
|
2020-05-16 07:23:43 -05:00
|
|
|
edition,
|
2020-07-30 21:12:44 -05:00
|
|
|
Some("test".to_string()),
|
2020-06-24 05:06:53 -05:00
|
|
|
cfg,
|
2020-05-16 07:23:43 -05:00
|
|
|
env,
|
2020-03-10 22:04:02 -05:00
|
|
|
Default::default(),
|
2019-11-22 04:55:03 -06:00
|
|
|
));
|
2019-02-04 14:42:37 -06:00
|
|
|
} else if path.ends_with("/lib.rs") {
|
2020-06-22 10:30:23 -05:00
|
|
|
let base = &path[..path.len() - "/lib.rs".len()];
|
|
|
|
let crate_name = &base[base.rfind('/').unwrap() + '/'.len_utf8()..];
|
2020-03-08 08:26:57 -05:00
|
|
|
let other_crate = crate_graph.add_crate_root(
|
|
|
|
file_id,
|
2020-05-16 07:23:43 -05:00
|
|
|
edition,
|
2020-07-01 03:03:07 -05:00
|
|
|
Some(crate_name.to_string()),
|
2020-06-24 05:06:53 -05:00
|
|
|
cfg,
|
2020-05-16 07:23:43 -05:00
|
|
|
env,
|
2020-03-10 22:04:02 -05:00
|
|
|
Default::default(),
|
2020-03-08 08:26:57 -05:00
|
|
|
);
|
2019-02-04 14:42:37 -06:00
|
|
|
if let Some(root_crate) = root_crate {
|
2020-02-05 04:47:28 -06:00
|
|
|
crate_graph
|
|
|
|
.add_dep(root_crate, CrateName::new(crate_name).unwrap(), other_crate)
|
|
|
|
.unwrap();
|
2019-02-04 14:42:37 -06:00
|
|
|
}
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2020-06-11 04:04:09 -05:00
|
|
|
let path = VfsPath::new_virtual_path(path.to_string());
|
|
|
|
file_set.insert(file_id, path);
|
2020-06-24 05:06:53 -05:00
|
|
|
change.change_file(file_id, Some(Arc::new(data.text).to_owned()));
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
|
|
|
change.set_crate_graph(crate_graph);
|
2020-06-11 04:04:09 -05:00
|
|
|
change.set_roots(vec![SourceRoot::new_local(file_set)]);
|
2019-01-08 13:33:36 -06:00
|
|
|
host.apply_change(change);
|
|
|
|
host
|
|
|
|
}
|
2020-10-02 07:26:40 -05:00
|
|
|
pub(crate) fn analysis(self) -> Analysis {
|
2019-01-08 13:33:36 -06:00
|
|
|
self.analysis_host().analysis()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates analysis from a multi-file fixture, returns positions marked with <|>.
|
2020-10-02 07:26:40 -05:00
|
|
|
pub(crate) fn analysis_and_position(ra_fixture: &str) -> (Analysis, FilePosition) {
|
2020-02-27 09:05:35 -06:00
|
|
|
let (mock, position) = MockAnalysis::with_files_and_position(ra_fixture);
|
2019-01-08 13:33:36 -06:00
|
|
|
(mock.analysis(), position)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates analysis for a single file.
|
2020-10-02 07:26:40 -05:00
|
|
|
pub(crate) fn single_file(ra_fixture: &str) -> (Analysis, FileId) {
|
2020-06-24 04:05:47 -05:00
|
|
|
let mock = MockAnalysis::with_files(ra_fixture);
|
|
|
|
let file_id = mock.id_of("/main.rs");
|
2019-01-08 13:33:36 -06:00
|
|
|
(mock.analysis(), file_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates analysis for a single file, returns range marked with a pair of <|>.
|
2020-10-02 07:26:40 -05:00
|
|
|
pub(crate) fn analysis_and_range(ra_fixture: &str) -> (Analysis, FileRange) {
|
2020-06-24 05:01:17 -05:00
|
|
|
let (res, position) = MockAnalysis::with_fixture(ra_fixture);
|
|
|
|
let (file_id, range_or_offset) = position.expect("expected a marker (<|>)");
|
|
|
|
let range = match range_or_offset {
|
|
|
|
RangeOrOffset::Range(it) => it,
|
|
|
|
RangeOrOffset::Offset(_) => panic!(),
|
|
|
|
};
|
|
|
|
(res.analysis(), FileRange { file_id, range })
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|