mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Simplify
This commit is contained in:
parent
5e7a1a1203
commit
0a2b6087ec
2 changed files with 18 additions and 67 deletions
|
@ -1,5 +1,5 @@
|
||||||
//! FIXME: write short doc here
|
//! FIXME: write short doc here
|
||||||
use std::{str::FromStr, sync::Arc};
|
use std::sync::Arc;
|
||||||
|
|
||||||
use ra_cfg::CfgOptions;
|
use ra_cfg::CfgOptions;
|
||||||
use ra_db::{CrateName, Env, FileSet, SourceRoot, VfsPath};
|
use ra_db::{CrateName, Env, FileSet, SourceRoot, VfsPath};
|
||||||
|
@ -9,61 +9,11 @@ use crate::{
|
||||||
Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition, FileId, FilePosition, FileRange,
|
Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition, FileId, FilePosition, FileRange,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
enum MockFileData {
|
|
||||||
Fixture(Fixture),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MockFileData {
|
|
||||||
fn path(&self) -> &str {
|
|
||||||
match self {
|
|
||||||
MockFileData::Fixture(f) => f.path.as_str(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn content(&self) -> &str {
|
|
||||||
match self {
|
|
||||||
MockFileData::Fixture(f) => f.text.as_str(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cfg_options(&self) -> CfgOptions {
|
|
||||||
match self {
|
|
||||||
MockFileData::Fixture(f) => {
|
|
||||||
let mut cfg = CfgOptions::default();
|
|
||||||
f.cfg_atoms.iter().for_each(|it| cfg.insert_atom(it.into()));
|
|
||||||
f.cfg_key_values.iter().for_each(|(k, v)| cfg.insert_key_value(k.into(), v.into()));
|
|
||||||
cfg
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn edition(&self) -> Edition {
|
|
||||||
match self {
|
|
||||||
MockFileData::Fixture(f) => {
|
|
||||||
f.edition.as_ref().map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn env(&self) -> Env {
|
|
||||||
match self {
|
|
||||||
MockFileData::Fixture(f) => Env::from(f.env.iter()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Fixture> for MockFileData {
|
|
||||||
fn from(fixture: Fixture) -> 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<MockFileData>,
|
files: Vec<Fixture>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MockAnalysis {
|
impl MockAnalysis {
|
||||||
|
@ -113,8 +63,8 @@ impl MockAnalysis {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_file_fixture(&mut self, fixture: Fixture) -> FileId {
|
fn add_file_fixture(&mut self, fixture: Fixture) -> FileId {
|
||||||
let file_id = self.next_id();
|
let file_id = FileId((self.files.len() + 1) as u32);
|
||||||
self.files.push(MockFileData::from(fixture));
|
self.files.push(fixture);
|
||||||
file_id
|
file_id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,7 +73,7 @@ impl MockAnalysis {
|
||||||
.files
|
.files
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.find(|(_, data)| path == data.path())
|
.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)
|
||||||
}
|
}
|
||||||
|
@ -134,18 +84,23 @@ impl MockAnalysis {
|
||||||
let mut crate_graph = CrateGraph::default();
|
let mut crate_graph = CrateGraph::default();
|
||||||
let mut root_crate = None;
|
let mut root_crate = None;
|
||||||
for (i, data) in self.files.into_iter().enumerate() {
|
for (i, data) in self.files.into_iter().enumerate() {
|
||||||
let path = data.path();
|
let path = data.path;
|
||||||
assert!(path.starts_with('/'));
|
assert!(path.starts_with('/'));
|
||||||
let cfg_options = data.cfg_options();
|
|
||||||
|
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);
|
||||||
|
|
||||||
let file_id = FileId(i as u32 + 1);
|
let file_id = FileId(i as u32 + 1);
|
||||||
let edition = data.edition();
|
let env = Env::from(data.env.iter());
|
||||||
let env = data.env();
|
|
||||||
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,
|
||||||
edition,
|
edition,
|
||||||
None,
|
None,
|
||||||
cfg_options,
|
cfg,
|
||||||
env,
|
env,
|
||||||
Default::default(),
|
Default::default(),
|
||||||
));
|
));
|
||||||
|
@ -156,7 +111,7 @@ impl MockAnalysis {
|
||||||
file_id,
|
file_id,
|
||||||
edition,
|
edition,
|
||||||
Some(CrateName::new(crate_name).unwrap()),
|
Some(CrateName::new(crate_name).unwrap()),
|
||||||
cfg_options,
|
cfg,
|
||||||
env,
|
env,
|
||||||
Default::default(),
|
Default::default(),
|
||||||
);
|
);
|
||||||
|
@ -168,7 +123,7 @@ impl MockAnalysis {
|
||||||
}
|
}
|
||||||
let path = VfsPath::new_virtual_path(path.to_string());
|
let path = VfsPath::new_virtual_path(path.to_string());
|
||||||
file_set.insert(file_id, path);
|
file_set.insert(file_id, path);
|
||||||
change.change_file(file_id, Some(Arc::new(data.content().to_owned())));
|
change.change_file(file_id, Some(Arc::new(data.text).to_owned()));
|
||||||
}
|
}
|
||||||
change.set_crate_graph(crate_graph);
|
change.set_crate_graph(crate_graph);
|
||||||
change.set_roots(vec![SourceRoot::new_local(file_set)]);
|
change.set_roots(vec![SourceRoot::new_local(file_set)]);
|
||||||
|
@ -178,10 +133,6 @@ 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 <|>.
|
||||||
|
|
|
@ -104,7 +104,7 @@ fn syntax_tree_for_token(node: &SyntaxToken, text_range: TextRange) -> Option<St
|
||||||
mod tests {
|
mod tests {
|
||||||
use test_utils::assert_eq_text;
|
use test_utils::assert_eq_text;
|
||||||
|
|
||||||
use crate::mock_analysis::{single_file, analysis_and_range};
|
use crate::mock_analysis::{analysis_and_range, single_file};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_syntax_tree_without_range() {
|
fn test_syntax_tree_without_range() {
|
||||||
|
|
Loading…
Reference in a new issue