mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 20:43:21 +00:00
Use FixtureMeta in MockAnalysis
This commit is contained in:
parent
256fb7556e
commit
2dde9b1994
2 changed files with 85 additions and 17 deletions
|
@ -4,18 +4,61 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use ra_cfg::CfgOptions;
|
use ra_cfg::CfgOptions;
|
||||||
use ra_db::{CrateName, Env, RelativePathBuf};
|
use ra_db::{CrateName, Env, RelativePathBuf};
|
||||||
use test_utils::{extract_offset, extract_range, parse_fixture, 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::Edition2018, FileId, FilePosition,
|
||||||
FileRange, SourceRootId,
|
FileRange, SourceRootId,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Mock analysis is used in test to bootstrap an AnalysisHost/Analysis
|
/// Mock analysis is used in test to bootstrap an AnalysisHost/Analysis
|
||||||
/// from a set of in-memory files.
|
/// from a set of in-memory files.
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct MockAnalysis {
|
pub struct MockAnalysis {
|
||||||
files: Vec<(String, String)>,
|
files: Vec<MockFileData>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MockAnalysis {
|
impl MockAnalysis {
|
||||||
|
@ -35,7 +78,7 @@ impl MockAnalysis {
|
||||||
pub fn with_files(fixture: &str) -> MockAnalysis {
|
pub fn with_files(fixture: &str) -> MockAnalysis {
|
||||||
let mut res = MockAnalysis::new();
|
let mut res = MockAnalysis::new();
|
||||||
for entry in parse_fixture(fixture) {
|
for entry in parse_fixture(fixture) {
|
||||||
res.add_file(entry.meta.path().as_str(), &entry.text);
|
res.add_file_fixture(entry);
|
||||||
}
|
}
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
@ -48,31 +91,44 @@ impl MockAnalysis {
|
||||||
for entry in parse_fixture(fixture) {
|
for entry in parse_fixture(fixture) {
|
||||||
if entry.text.contains(CURSOR_MARKER) {
|
if entry.text.contains(CURSOR_MARKER) {
|
||||||
assert!(position.is_none(), "only one marker (<|>) per fixture is allowed");
|
assert!(position.is_none(), "only one marker (<|>) per fixture is allowed");
|
||||||
position =
|
position = Some(res.add_file_fixture_with_position(entry));
|
||||||
Some(res.add_file_with_position(&entry.meta.path().as_str(), &entry.text));
|
|
||||||
} else {
|
} else {
|
||||||
res.add_file(&entry.meta.path().as_str(), &entry.text);
|
res.add_file_fixture(entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let position = position.expect("expected a marker (<|>)");
|
let position = position.expect("expected a marker (<|>)");
|
||||||
(res, position)
|
(res, position)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 }
|
||||||
|
}
|
||||||
|
|
||||||
pub fn add_file(&mut self, path: &str, text: &str) -> FileId {
|
pub fn add_file(&mut self, path: &str, text: &str) -> FileId {
|
||||||
let file_id = FileId((self.files.len() + 1) as u32);
|
let file_id = self.next_id();
|
||||||
self.files.push((path.to_string(), text.to_string()));
|
self.files.push(MockFileData::new(path.to_string(), text.to_string()));
|
||||||
file_id
|
file_id
|
||||||
}
|
}
|
||||||
pub fn add_file_with_position(&mut self, path: &str, text: &str) -> FilePosition {
|
pub fn add_file_with_position(&mut self, path: &str, text: &str) -> FilePosition {
|
||||||
let (offset, text) = extract_offset(text);
|
let (offset, text) = extract_offset(text);
|
||||||
let file_id = FileId((self.files.len() + 1) as u32);
|
let file_id = self.next_id();
|
||||||
self.files.push((path.to_string(), text));
|
self.files.push(MockFileData::new(path.to_string(), text));
|
||||||
FilePosition { file_id, offset }
|
FilePosition { file_id, offset }
|
||||||
}
|
}
|
||||||
pub fn add_file_with_range(&mut self, path: &str, text: &str) -> FileRange {
|
pub fn add_file_with_range(&mut self, path: &str, text: &str) -> FileRange {
|
||||||
let (range, text) = extract_range(text);
|
let (range, text) = extract_range(text);
|
||||||
let file_id = FileId((self.files.len() + 1) as u32);
|
let file_id = self.next_id();
|
||||||
self.files.push((path.to_string(), text));
|
self.files.push(MockFileData::new(path.to_string(), text));
|
||||||
FileRange { file_id, range }
|
FileRange { file_id, range }
|
||||||
}
|
}
|
||||||
pub fn id_of(&self, path: &str) -> FileId {
|
pub fn id_of(&self, path: &str) -> FileId {
|
||||||
|
@ -80,7 +136,7 @@ impl MockAnalysis {
|
||||||
.files
|
.files
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.find(|(_, (p, _text))| path == p)
|
.find(|(_, data)| path == data.path())
|
||||||
.expect("no file in this mock");
|
.expect("no file in this mock");
|
||||||
FileId(idx as u32 + 1)
|
FileId(idx as u32 + 1)
|
||||||
}
|
}
|
||||||
|
@ -91,11 +147,12 @@ impl MockAnalysis {
|
||||||
change.add_root(source_root, true);
|
change.add_root(source_root, true);
|
||||||
let mut crate_graph = CrateGraph::default();
|
let mut crate_graph = CrateGraph::default();
|
||||||
let mut root_crate = None;
|
let mut root_crate = None;
|
||||||
for (i, (path, contents)) in self.files.into_iter().enumerate() {
|
for (i, data) in self.files.into_iter().enumerate() {
|
||||||
|
let path = data.path();
|
||||||
assert!(path.starts_with('/'));
|
assert!(path.starts_with('/'));
|
||||||
let path = RelativePathBuf::from_path(&path[1..]).unwrap();
|
let path = RelativePathBuf::from_path(&path[1..]).unwrap();
|
||||||
|
let cfg_options = data.cfg_options();
|
||||||
let file_id = FileId(i as u32 + 1);
|
let file_id = FileId(i as u32 + 1);
|
||||||
let cfg_options = CfgOptions::default();
|
|
||||||
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,
|
||||||
|
@ -123,7 +180,7 @@ impl MockAnalysis {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
change.add_file(source_root, file_id, path, Arc::new(contents));
|
change.add_file(source_root, file_id, path, Arc::new(data.content().to_owned()));
|
||||||
}
|
}
|
||||||
change.set_crate_graph(crate_graph);
|
change.set_crate_graph(crate_graph);
|
||||||
host.apply_change(change);
|
host.apply_change(change);
|
||||||
|
@ -132,6 +189,10 @@ impl MockAnalysis {
|
||||||
pub fn analysis(self) -> Analysis {
|
pub fn analysis(self) -> Analysis {
|
||||||
self.analysis_host().analysis()
|
self.analysis_host().analysis()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn next_id(&self) -> FileId {
|
||||||
|
FileId((self.files.len() + 1) as u32)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates analysis from a multi-file fixture, returns positions marked with <|>.
|
/// Creates analysis from a multi-file fixture, returns positions marked with <|>.
|
||||||
|
|
|
@ -176,7 +176,7 @@ pub struct FileMeta {
|
||||||
pub path: RelativePathBuf,
|
pub path: RelativePathBuf,
|
||||||
pub krate: Option<String>,
|
pub krate: Option<String>,
|
||||||
pub deps: Vec<String>,
|
pub deps: Vec<String>,
|
||||||
pub cfg: ra_cfg::CfgOptions,
|
pub cfg: CfgOptions,
|
||||||
pub edition: Option<String>,
|
pub edition: Option<String>,
|
||||||
pub env: FxHashMap<String, String>,
|
pub env: FxHashMap<String, String>,
|
||||||
}
|
}
|
||||||
|
@ -188,6 +188,13 @@ impl FixtureMeta {
|
||||||
FixtureMeta::File(f) => &f.path,
|
FixtureMeta::File(f) => &f.path,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn cfg_options(&self) -> Option<&CfgOptions> {
|
||||||
|
match self {
|
||||||
|
FixtureMeta::File(f) => Some(&f.cfg),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses text which looks like this:
|
/// Parses text which looks like this:
|
||||||
|
|
Loading…
Reference in a new issue