From 3486b47e5c4f71479cc3c876da1fd1dcbfcab257 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 23 Jun 2020 18:43:55 +0200 Subject: Simplify --- crates/ra_db/src/fixture.rs | 70 ++++++++-------------------------------- crates/test_utils/src/fixture.rs | 13 -------- crates/test_utils/src/lib.rs | 2 +- 3 files changed, 14 insertions(+), 71 deletions(-) diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index d65536bbc..f786fb87f 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs @@ -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 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 { +fn with_files( + db: &mut dyn SourceDatabaseExt, + fixture: &str, +) -> (Option, Vec) { + 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 Option, } -/// Same as `parse_fixture`, except it allow empty fixture -pub fn parse_single_fixture(ra_fixture: &str) -> Option { - 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 diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 0fdd1a36b..d44b2f9ab 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -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 = "<|>"; -- cgit v1.2.3