2019-09-30 03:58:53 -05:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2019-09-29 18:38:16 -05:00
|
|
|
use ra_cfg::CfgOptions;
|
2020-02-05 04:47:28 -06:00
|
|
|
use ra_db::{CrateName, Env, RelativePathBuf};
|
2020-05-16 05:17:21 -05:00
|
|
|
use test_utils::{extract_offset, extract_range, parse_fixture, FixtureEntry, CURSOR_MARKER};
|
2019-01-08 13:33:36 -06:00
|
|
|
|
2019-07-04 15:05:17 -05:00
|
|
|
use crate::{
|
|
|
|
Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition::Edition2018, FileId, FilePosition,
|
|
|
|
FileRange, SourceRootId,
|
|
|
|
};
|
2019-01-08 13:33:36 -06:00
|
|
|
|
2020-05-16 05:17:21 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum MockFileData {
|
|
|
|
Plain { path: String, content: String },
|
|
|
|
Fixture(FixtureEntry),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MockFileData {
|
|
|
|
fn new(path: String, content: String) -> Self {
|
|
|
|
// `Self::Plain` causes a false warning: 'variant is never constructed: `Plain` '
|
|
|
|
// see https://github.com/rust-lang/rust/issues/69018
|
|
|
|
MockFileData::Plain { path, content }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
MockFileData::Plain { path, .. } => path.as_str(),
|
|
|
|
MockFileData::Fixture(f) => f.meta.path().as_str(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn content(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
MockFileData::Plain { content, .. } => content,
|
|
|
|
MockFileData::Fixture(f) => f.text.as_str(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cfg_options(&self) -> CfgOptions {
|
|
|
|
match self {
|
|
|
|
MockFileData::Fixture(f) => {
|
|
|
|
f.meta.cfg_options().map_or_else(Default::default, |o| o.clone())
|
|
|
|
}
|
|
|
|
_ => CfgOptions::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<FixtureEntry> for MockFileData {
|
|
|
|
fn from(fixture: FixtureEntry) -> Self {
|
|
|
|
Self::Fixture(fixture)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)]
|
|
|
|
pub struct MockAnalysis {
|
2020-05-16 05:17:21 -05:00
|
|
|
files: Vec<MockFileData>,
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MockAnalysis {
|
|
|
|
pub fn new() -> MockAnalysis {
|
|
|
|
MockAnalysis::default()
|
|
|
|
}
|
|
|
|
/// 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;
|
|
|
|
/// ```
|
|
|
|
pub fn with_files(fixture: &str) -> MockAnalysis {
|
|
|
|
let mut res = MockAnalysis::new();
|
|
|
|
for entry in parse_fixture(fixture) {
|
2020-05-16 05:17:21 -05:00
|
|
|
res.add_file_fixture(entry);
|
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.
|
|
|
|
pub fn with_files_and_position(fixture: &str) -> (MockAnalysis, FilePosition) {
|
|
|
|
let mut position = None;
|
|
|
|
let mut res = MockAnalysis::new();
|
|
|
|
for entry in parse_fixture(fixture) {
|
|
|
|
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-05-16 05:17:21 -05:00
|
|
|
position = Some(res.add_file_fixture_with_position(entry));
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
let position = position.expect("expected a marker (<|>)");
|
|
|
|
(res, position)
|
|
|
|
}
|
|
|
|
|
2020-05-16 05:17:21 -05:00
|
|
|
pub fn add_file_fixture(&mut self, fixture: FixtureEntry) -> FileId {
|
|
|
|
let file_id = self.next_id();
|
|
|
|
self.files.push(MockFileData::from(fixture));
|
|
|
|
file_id
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_file_fixture_with_position(&mut self, mut fixture: FixtureEntry) -> FilePosition {
|
|
|
|
let (offset, text) = extract_offset(&fixture.text);
|
|
|
|
fixture.text = text;
|
|
|
|
let file_id = self.next_id();
|
|
|
|
self.files.push(MockFileData::from(fixture));
|
|
|
|
FilePosition { file_id, offset }
|
|
|
|
}
|
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
pub fn add_file(&mut self, path: &str, text: &str) -> FileId {
|
2020-05-16 05:17:21 -05:00
|
|
|
let file_id = self.next_id();
|
|
|
|
self.files.push(MockFileData::new(path.to_string(), text.to_string()));
|
2019-01-08 13:33:36 -06:00
|
|
|
file_id
|
|
|
|
}
|
|
|
|
pub fn add_file_with_position(&mut self, path: &str, text: &str) -> FilePosition {
|
|
|
|
let (offset, text) = extract_offset(text);
|
2020-05-16 05:17:21 -05:00
|
|
|
let file_id = self.next_id();
|
|
|
|
self.files.push(MockFileData::new(path.to_string(), text));
|
2019-01-08 13:33:36 -06:00
|
|
|
FilePosition { file_id, offset }
|
|
|
|
}
|
|
|
|
pub fn add_file_with_range(&mut self, path: &str, text: &str) -> FileRange {
|
|
|
|
let (range, text) = extract_range(text);
|
2020-05-16 05:17:21 -05:00
|
|
|
let file_id = self.next_id();
|
|
|
|
self.files.push(MockFileData::new(path.to_string(), text));
|
2019-01-08 13:33:36 -06:00
|
|
|
FileRange { file_id, range }
|
|
|
|
}
|
|
|
|
pub fn id_of(&self, path: &str) -> FileId {
|
|
|
|
let (idx, _) = self
|
|
|
|
.files
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2020-05-16 05:17:21 -05:00
|
|
|
.find(|(_, data)| path == data.path())
|
2019-01-08 13:33:36 -06:00
|
|
|
.expect("no file in this mock");
|
|
|
|
FileId(idx as u32 + 1)
|
|
|
|
}
|
|
|
|
pub fn analysis_host(self) -> AnalysisHost {
|
|
|
|
let mut host = AnalysisHost::default();
|
|
|
|
let source_root = SourceRootId(0);
|
|
|
|
let mut change = AnalysisChange::new();
|
|
|
|
change.add_root(source_root, true);
|
|
|
|
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() {
|
|
|
|
let path = data.path();
|
2019-01-08 13:33:36 -06:00
|
|
|
assert!(path.starts_with('/'));
|
|
|
|
let path = RelativePathBuf::from_path(&path[1..]).unwrap();
|
2020-05-16 05:17:21 -05:00
|
|
|
let cfg_options = data.cfg_options();
|
2019-01-27 09:07:45 -06:00
|
|
|
let file_id = FileId(i as u32 + 1);
|
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,
|
|
|
|
Edition2018,
|
2020-03-08 08:26:57 -05:00
|
|
|
None,
|
2019-11-22 04:55:03 -06:00
|
|
|
cfg_options,
|
|
|
|
Env::default(),
|
2020-03-10 22:04:02 -05:00
|
|
|
Default::default(),
|
2020-03-18 07:56:46 -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") {
|
|
|
|
let crate_name = path.parent().unwrap().file_name().unwrap();
|
2020-03-08 08:26:57 -05:00
|
|
|
let other_crate = crate_graph.add_crate_root(
|
|
|
|
file_id,
|
|
|
|
Edition2018,
|
2020-03-16 04:47:52 -05:00
|
|
|
Some(CrateName::new(crate_name).unwrap()),
|
2020-03-08 08:26:57 -05:00
|
|
|
cfg_options,
|
|
|
|
Env::default(),
|
2020-03-10 22:04:02 -05:00
|
|
|
Default::default(),
|
2020-03-18 07:56:46 -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-05-16 05:17:21 -05:00
|
|
|
change.add_file(source_root, file_id, path, Arc::new(data.content().to_owned()));
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
|
|
|
change.set_crate_graph(crate_graph);
|
|
|
|
host.apply_change(change);
|
|
|
|
host
|
|
|
|
}
|
|
|
|
pub fn analysis(self) -> Analysis {
|
|
|
|
self.analysis_host().analysis()
|
|
|
|
}
|
2020-05-16 05:17:21 -05:00
|
|
|
|
|
|
|
fn next_id(&self) -> FileId {
|
|
|
|
FileId((self.files.len() + 1) as u32)
|
|
|
|
}
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates analysis from a multi-file fixture, returns positions marked with <|>.
|
2020-02-27 09:05:35 -06:00
|
|
|
pub fn analysis_and_position(ra_fixture: &str) -> (Analysis, FilePosition) {
|
|
|
|
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-02-27 09:05:35 -06:00
|
|
|
pub fn single_file(ra_fixture: &str) -> (Analysis, FileId) {
|
2019-01-08 13:33:36 -06:00
|
|
|
let mut mock = MockAnalysis::new();
|
2020-02-27 09:05:35 -06:00
|
|
|
let file_id = mock.add_file("/main.rs", ra_fixture);
|
2019-01-08 13:33:36 -06:00
|
|
|
(mock.analysis(), file_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates analysis for a single file, returns position marked with <|>.
|
2020-02-27 09:05:35 -06:00
|
|
|
pub fn single_file_with_position(ra_fixture: &str) -> (Analysis, FilePosition) {
|
2019-01-08 13:33:36 -06:00
|
|
|
let mut mock = MockAnalysis::new();
|
2020-02-27 09:05:35 -06:00
|
|
|
let pos = mock.add_file_with_position("/main.rs", ra_fixture);
|
2019-01-08 13:33:36 -06:00
|
|
|
(mock.analysis(), pos)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates analysis for a single file, returns range marked with a pair of <|>.
|
2020-02-27 09:05:35 -06:00
|
|
|
pub fn single_file_with_range(ra_fixture: &str) -> (Analysis, FileRange) {
|
2019-01-08 13:33:36 -06:00
|
|
|
let mut mock = MockAnalysis::new();
|
2020-02-27 09:05:35 -06:00
|
|
|
let pos = mock.add_file_with_range("/main.rs", ra_fixture);
|
2019-01-08 13:33:36 -06:00
|
|
|
(mock.analysis(), pos)
|
|
|
|
}
|