mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-10 15:14:32 +00:00
Simplify
This commit is contained in:
parent
21f751a0e5
commit
3486b47e5c
3 changed files with 14 additions and 71 deletions
|
@ -61,9 +61,7 @@ use std::{str::FromStr, sync::Arc};
|
|||
|
||||
use ra_cfg::CfgOptions;
|
||||
use rustc_hash::FxHashMap;
|
||||
use test_utils::{
|
||||
extract_offset, parse_fixture, parse_single_fixture, FixtureEntry, CURSOR_MARKER,
|
||||
};
|
||||
use test_utils::{extract_offset, parse_fixture, FixtureEntry, CURSOR_MARKER};
|
||||
use vfs::{file_set::FileSet, VfsPath};
|
||||
|
||||
use crate::{
|
||||
|
@ -76,20 +74,21 @@ pub const WORKSPACE: SourceRootId = SourceRootId(0);
|
|||
pub trait WithFixture: Default + SourceDatabaseExt + 'static {
|
||||
fn with_single_file(text: &str) -> (Self, FileId) {
|
||||
let mut db = Self::default();
|
||||
let file_id = with_single_file(&mut db, text);
|
||||
(db, file_id)
|
||||
let (_, files) = with_files(&mut db, text);
|
||||
assert!(files.len() == 1);
|
||||
(db, files[0])
|
||||
}
|
||||
|
||||
fn with_files(ra_fixture: &str) -> Self {
|
||||
let mut db = Self::default();
|
||||
let pos = with_files(&mut db, ra_fixture);
|
||||
let (pos, _) = with_files(&mut db, ra_fixture);
|
||||
assert!(pos.is_none());
|
||||
db
|
||||
}
|
||||
|
||||
fn with_position(ra_fixture: &str) -> (Self, FilePosition) {
|
||||
let mut db = Self::default();
|
||||
let pos = with_files(&mut db, ra_fixture);
|
||||
let (pos, _) = with_files(&mut db, ra_fixture);
|
||||
(db, pos.unwrap())
|
||||
}
|
||||
|
||||
|
@ -104,54 +103,11 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static {
|
|||
|
||||
impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {}
|
||||
|
||||
fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId {
|
||||
let file_id = FileId(0);
|
||||
let mut file_set = vfs::file_set::FileSet::default();
|
||||
file_set.insert(file_id, vfs::VfsPath::new_virtual_path("/main.rs".to_string()));
|
||||
|
||||
let source_root = SourceRoot::new_local(file_set);
|
||||
|
||||
let fixture = parse_single_fixture(ra_fixture);
|
||||
|
||||
let crate_graph = if let Some(entry) = fixture {
|
||||
let meta = match ParsedMeta::from(&entry) {
|
||||
ParsedMeta::File(it) => it,
|
||||
};
|
||||
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
crate_graph.add_crate_root(
|
||||
file_id,
|
||||
meta.edition,
|
||||
meta.krate.map(|name| {
|
||||
CrateName::new(&name).expect("Fixture crate name should not contain dashes")
|
||||
}),
|
||||
meta.cfg,
|
||||
meta.env,
|
||||
Default::default(),
|
||||
);
|
||||
crate_graph
|
||||
} else {
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
crate_graph.add_crate_root(
|
||||
file_id,
|
||||
Edition::Edition2018,
|
||||
None,
|
||||
CfgOptions::default(),
|
||||
Env::default(),
|
||||
Default::default(),
|
||||
);
|
||||
crate_graph
|
||||
};
|
||||
|
||||
db.set_file_text(file_id, Arc::new(ra_fixture.to_string()));
|
||||
db.set_file_source_root(file_id, WORKSPACE);
|
||||
db.set_source_root(WORKSPACE, Arc::new(source_root));
|
||||
db.set_crate_graph(Arc::new(crate_graph));
|
||||
|
||||
file_id
|
||||
}
|
||||
|
||||
fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosition> {
|
||||
fn with_files(
|
||||
db: &mut dyn SourceDatabaseExt,
|
||||
fixture: &str,
|
||||
) -> (Option<FilePosition>, Vec<FileId>) {
|
||||
let mut files = Vec::new();
|
||||
let fixture = parse_fixture(fixture);
|
||||
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
|
@ -204,7 +160,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
|
|||
db.set_file_source_root(file_id, source_root_id);
|
||||
let path = VfsPath::new_virtual_path(meta.path);
|
||||
file_set.insert(file_id, path.into());
|
||||
|
||||
files.push(file_id);
|
||||
file_id.0 += 1;
|
||||
}
|
||||
|
||||
|
@ -229,7 +185,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
|
|||
db.set_source_root(source_root_id, Arc::new(SourceRoot::new_local(file_set)));
|
||||
db.set_crate_graph(Arc::new(crate_graph));
|
||||
|
||||
file_position
|
||||
(file_position, files)
|
||||
}
|
||||
|
||||
enum ParsedMeta {
|
||||
|
|
|
@ -13,19 +13,6 @@ pub struct FixtureEntry {
|
|||
pub env: FxHashMap<String, String>,
|
||||
}
|
||||
|
||||
/// Same as `parse_fixture`, except it allow empty fixture
|
||||
pub fn parse_single_fixture(ra_fixture: &str) -> Option<FixtureEntry> {
|
||||
if !ra_fixture.lines().any(|it| it.trim_start().starts_with("//-")) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let fixtures = parse_fixture(ra_fixture);
|
||||
if fixtures.len() > 1 {
|
||||
panic!("too many fixtures");
|
||||
}
|
||||
fixtures.into_iter().nth(0)
|
||||
}
|
||||
|
||||
/// Parses text which looks like this:
|
||||
///
|
||||
/// ```not_rust
|
||||
|
|
|
@ -22,7 +22,7 @@ pub use difference::Changeset as __Changeset;
|
|||
pub use ra_cfg::CfgOptions;
|
||||
pub use rustc_hash::FxHashMap;
|
||||
|
||||
pub use crate::fixture::{parse_fixture, parse_single_fixture, FixtureEntry};
|
||||
pub use crate::fixture::{parse_fixture, FixtureEntry};
|
||||
|
||||
pub const CURSOR_MARKER: &str = "<|>";
|
||||
|
||||
|
|
Loading…
Reference in a new issue