diff options
| author | Aleksey Kladov <[email protected]> | 2020-06-23 17:43:55 +0100 |
|---|---|---|
| committer | Aleksey Kladov <[email protected]> | 2020-06-23 17:43:55 +0100 |
| commit | 3486b47e5c4f71479cc3c876da1fd1dcbfcab257 (patch) | |
| tree | d7e2a7e46f9a234bcd8edb1e95865be262c2930d /crates/ra_db/src | |
| parent | 21f751a0e5da5dd488612e25abfc545c259050e7 (diff) | |
Simplify
Diffstat (limited to 'crates/ra_db/src')
| -rw-r--r-- | crates/ra_db/src/fixture.rs | 70 |
1 files changed, 13 insertions, 57 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}; | |||
| 61 | 61 | ||
| 62 | use ra_cfg::CfgOptions; | 62 | use ra_cfg::CfgOptions; |
| 63 | use rustc_hash::FxHashMap; | 63 | use rustc_hash::FxHashMap; |
| 64 | use test_utils::{ | 64 | use test_utils::{extract_offset, parse_fixture, FixtureEntry, CURSOR_MARKER}; |
| 65 | extract_offset, parse_fixture, parse_single_fixture, FixtureEntry, CURSOR_MARKER, | ||
| 66 | }; | ||
| 67 | use vfs::{file_set::FileSet, VfsPath}; | 65 | use vfs::{file_set::FileSet, VfsPath}; |
| 68 | 66 | ||
| 69 | use crate::{ | 67 | use crate::{ |
| @@ -76,20 +74,21 @@ pub const WORKSPACE: SourceRootId = SourceRootId(0); | |||
| 76 | pub trait WithFixture: Default + SourceDatabaseExt + 'static { | 74 | pub trait WithFixture: Default + SourceDatabaseExt + 'static { |
| 77 | fn with_single_file(text: &str) -> (Self, FileId) { | 75 | fn with_single_file(text: &str) -> (Self, FileId) { |
| 78 | let mut db = Self::default(); | 76 | let mut db = Self::default(); |
| 79 | let file_id = with_single_file(&mut db, text); | 77 | let (_, files) = with_files(&mut db, text); |
| 80 | (db, file_id) | 78 | assert!(files.len() == 1); |
| 79 | (db, files[0]) | ||
| 81 | } | 80 | } |
| 82 | 81 | ||
| 83 | fn with_files(ra_fixture: &str) -> Self { | 82 | fn with_files(ra_fixture: &str) -> Self { |
| 84 | let mut db = Self::default(); | 83 | let mut db = Self::default(); |
| 85 | let pos = with_files(&mut db, ra_fixture); | 84 | let (pos, _) = with_files(&mut db, ra_fixture); |
| 86 | assert!(pos.is_none()); | 85 | assert!(pos.is_none()); |
| 87 | db | 86 | db |
| 88 | } | 87 | } |
| 89 | 88 | ||
| 90 | fn with_position(ra_fixture: &str) -> (Self, FilePosition) { | 89 | fn with_position(ra_fixture: &str) -> (Self, FilePosition) { |
| 91 | let mut db = Self::default(); | 90 | let mut db = Self::default(); |
| 92 | let pos = with_files(&mut db, ra_fixture); | 91 | let (pos, _) = with_files(&mut db, ra_fixture); |
| 93 | (db, pos.unwrap()) | 92 | (db, pos.unwrap()) |
| 94 | } | 93 | } |
| 95 | 94 | ||
| @@ -104,54 +103,11 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static { | |||
| 104 | 103 | ||
| 105 | impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {} | 104 | impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {} |
| 106 | 105 | ||
| 107 | fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId { | 106 | fn with_files( |
| 108 | let file_id = FileId(0); | 107 | db: &mut dyn SourceDatabaseExt, |
| 109 | let mut file_set = vfs::file_set::FileSet::default(); | 108 | fixture: &str, |
| 110 | file_set.insert(file_id, vfs::VfsPath::new_virtual_path("/main.rs".to_string())); | 109 | ) -> (Option<FilePosition>, Vec<FileId>) { |
| 111 | 110 | let mut files = Vec::new(); | |
| 112 | let source_root = SourceRoot::new_local(file_set); | ||
| 113 | |||
| 114 | let fixture = parse_single_fixture(ra_fixture); | ||
| 115 | |||
| 116 | let crate_graph = if let Some(entry) = fixture { | ||
| 117 | let meta = match ParsedMeta::from(&entry) { | ||
| 118 | ParsedMeta::File(it) => it, | ||
| 119 | }; | ||
| 120 | |||
| 121 | let mut crate_graph = CrateGraph::default(); | ||
| 122 | crate_graph.add_crate_root( | ||
| 123 | file_id, | ||
| 124 | meta.edition, | ||
| 125 | meta.krate.map(|name| { | ||
| 126 | CrateName::new(&name).expect("Fixture crate name should not contain dashes") | ||
| 127 | }), | ||
| 128 | meta.cfg, | ||
| 129 | meta.env, | ||
| 130 | Default::default(), | ||
| 131 | ); | ||
| 132 | crate_graph | ||
| 133 | } else { | ||
| 134 | let mut crate_graph = CrateGraph::default(); | ||
| 135 | crate_graph.add_crate_root( | ||
| 136 | file_id, | ||
| 137 | Edition::Edition2018, | ||
| 138 | None, | ||
| 139 | CfgOptions::default(), | ||
| 140 | Env::default(), | ||
| 141 | Default::default(), | ||
| 142 | ); | ||
| 143 | crate_graph | ||
| 144 | }; | ||
| 145 | |||
| 146 | db.set_file_text(file_id, Arc::new(ra_fixture.to_string())); | ||
| 147 | db.set_file_source_root(file_id, WORKSPACE); | ||
| 148 | db.set_source_root(WORKSPACE, Arc::new(source_root)); | ||
| 149 | db.set_crate_graph(Arc::new(crate_graph)); | ||
| 150 | |||
| 151 | file_id | ||
| 152 | } | ||
| 153 | |||
| 154 | fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosition> { | ||
| 155 | let fixture = parse_fixture(fixture); | 111 | let fixture = parse_fixture(fixture); |
| 156 | 112 | ||
| 157 | let mut crate_graph = CrateGraph::default(); | 113 | let mut crate_graph = CrateGraph::default(); |
| @@ -204,7 +160,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit | |||
| 204 | db.set_file_source_root(file_id, source_root_id); | 160 | db.set_file_source_root(file_id, source_root_id); |
| 205 | let path = VfsPath::new_virtual_path(meta.path); | 161 | let path = VfsPath::new_virtual_path(meta.path); |
| 206 | file_set.insert(file_id, path.into()); | 162 | file_set.insert(file_id, path.into()); |
| 207 | 163 | files.push(file_id); | |
| 208 | file_id.0 += 1; | 164 | file_id.0 += 1; |
| 209 | } | 165 | } |
| 210 | 166 | ||
| @@ -229,7 +185,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit | |||
| 229 | db.set_source_root(source_root_id, Arc::new(SourceRoot::new_local(file_set))); | 185 | db.set_source_root(source_root_id, Arc::new(SourceRoot::new_local(file_set))); |
| 230 | db.set_crate_graph(Arc::new(crate_graph)); | 186 | db.set_crate_graph(Arc::new(crate_graph)); |
| 231 | 187 | ||
| 232 | file_position | 188 | (file_position, files) |
| 233 | } | 189 | } |
| 234 | 190 | ||
| 235 | enum ParsedMeta { | 191 | enum ParsedMeta { |
