rust/crates/ra_ide/src/mock_analysis.rs

163 lines
5.9 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
2019-01-08 13:33:36 -06:00
use std::sync::Arc;
use ra_cfg::CfgOptions;
2020-02-05 04:47:28 -06:00
use ra_db::{CrateName, Env, RelativePathBuf};
2019-01-08 13:33:36 -06:00
use test_utils::{extract_offset, extract_range, parse_fixture, CURSOR_MARKER};
use crate::{
Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition::Edition2018, FileId, FilePosition,
FileRange, SourceRootId,
};
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 {
files: Vec<(String, String)>,
}
impl MockAnalysis {
pub fn new() -> MockAnalysis {
MockAnalysis::default()
}
/// Creates `MockAnalysis` using a fixture data in the following format:
///
/// ```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) {
res.add_file(entry.meta.path().as_str(), &entry.text);
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");
position =
Some(res.add_file_with_position(&entry.meta.path().as_str(), &entry.text));
2019-01-08 13:33:36 -06:00
} else {
res.add_file(&entry.meta.path().as_str(), &entry.text);
2019-01-08 13:33:36 -06:00
}
}
let position = position.expect("expected a marker (<|>)");
(res, position)
}
pub fn add_file(&mut self, path: &str, text: &str) -> FileId {
let file_id = FileId((self.files.len() + 1) as u32);
self.files.push((path.to_string(), text.to_string()));
file_id
}
pub fn add_file_with_position(&mut self, path: &str, text: &str) -> FilePosition {
let (offset, text) = extract_offset(text);
let file_id = FileId((self.files.len() + 1) as u32);
2019-07-04 22:41:53 -05:00
self.files.push((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);
let file_id = FileId((self.files.len() + 1) as u32);
2019-07-04 22:41:53 -05:00
self.files.push((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()
.find(|(_, (p, _text))| path == p)
.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();
let mut root_crate = None;
2019-01-27 09:07:45 -06:00
for (i, (path, contents)) in self.files.into_iter().enumerate() {
2019-01-08 13:33:36 -06:00
assert!(path.starts_with('/'));
let path = RelativePathBuf::from_path(&path[1..]).unwrap();
2019-01-27 09:07:45 -06:00
let file_id = FileId(i as u32 + 1);
let cfg_options = CfgOptions::default();
2019-01-08 13:33:36 -06:00
if path == "/lib.rs" || path == "/main.rs" {
root_crate = Some(crate_graph.add_crate_root(
file_id,
Edition2018,
2020-03-08 08:26:57 -05:00
None,
cfg_options,
Env::default(),
2020-03-10 22:04:02 -05:00
Default::default(),
2020-03-18 07:56:46 -05:00
Default::default(),
));
} 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
);
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-01-08 13:33:36 -06:00
}
change.add_file(source_root, file_id, path, Arc::new(contents));
}
change.set_crate_graph(crate_graph);
host.apply_change(change);
host
}
pub fn analysis(self) -> Analysis {
self.analysis_host().analysis()
}
}
/// 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)
}