From e5101ae150db5faa7ddf8393e375032ef371f5e5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 24 Jun 2020 00:30:34 +0200 Subject: Centralize fixture parsing for assists --- crates/ra_db/src/fixture.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'crates/ra_db/src') diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index 3959d7efe..541815fe7 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs @@ -61,7 +61,7 @@ use std::{str::FromStr, sync::Arc}; use ra_cfg::CfgOptions; use rustc_hash::FxHashMap; -use test_utils::{extract_offset, Fixture, CURSOR_MARKER}; +use test_utils::{extract_range_or_offset, Fixture, RangeOrOffset, CURSOR_MARKER}; use vfs::{file_set::FileSet, VfsPath}; use crate::{ @@ -86,9 +86,19 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static { } fn with_position(ra_fixture: &str) -> (Self, FilePosition) { + let (db, file_id, range_or_offset) = Self::with_range_or_offset(ra_fixture); + let offset = match range_or_offset { + RangeOrOffset::Range(_) => panic!(), + RangeOrOffset::Offset(it) => it, + }; + (db, FilePosition { file_id, offset }) + } + + fn with_range_or_offset(ra_fixture: &str) -> (Self, FileId, RangeOrOffset) { let mut db = Self::default(); let (pos, _) = with_files(&mut db, ra_fixture); - (db, pos.unwrap()) + let (file_id, range_or_offset) = pos.unwrap(); + (db, file_id, range_or_offset) } fn test_crate(&self) -> CrateId { @@ -151,7 +161,7 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId fn with_files( db: &mut dyn SourceDatabaseExt, fixture: &str, -) -> (Option, Vec) { +) -> (Option<(FileId, RangeOrOffset)>, Vec) { let fixture = Fixture::parse(fixture); let mut files = Vec::new(); @@ -193,9 +203,9 @@ fn with_files( } let text = if entry.text.contains(CURSOR_MARKER) { - let (offset, text) = extract_offset(&entry.text); + let (range_or_offset, text) = extract_range_or_offset(&entry.text); assert!(file_position.is_none()); - file_position = Some(FilePosition { file_id, offset }); + file_position = Some((file_id, range_or_offset)); text.to_string() } else { entry.text.to_string() -- cgit v1.2.3 From d016cb486738c1ab2574a322924183fa8a870b06 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 24 Jun 2020 00:48:38 +0200 Subject: Use only one code-path for parsing fixtures This removes leading newlines everywhere, shifting all ranges in tests by one --- crates/ra_db/src/fixture.rs | 51 +++------------------------------------------ 1 file changed, 3 insertions(+), 48 deletions(-) (limited to 'crates/ra_db/src') diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index 541815fe7..ddf46e6c4 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs @@ -74,8 +74,9 @@ 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_eq!(files.len(), 1); + (db, files[0]) } fn with_files(ra_fixture: &str) -> Self { @@ -112,52 +113,6 @@ 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 crate_graph = if let Some(meta) = ra_fixture.lines().find(|it| it.contains("//-")) { - let entry = Fixture::parse_meta_line(meta.trim()); - 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, -- cgit v1.2.3