diff options
Diffstat (limited to 'crates/ra_db/src')
-rw-r--r-- | crates/ra_db/src/fixture.rs | 77 |
1 files changed, 34 insertions, 43 deletions
diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index f7d9118a9..20f291568 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}; | |||
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::{extract_offset, parse_fixture, parse_single_fixture, FixtureMeta, CURSOR_MARKER}; | 64 | use test_utils::{extract_offset, Fixture, CURSOR_MARKER}; |
65 | use vfs::{file_set::FileSet, VfsPath}; | 65 | use vfs::{file_set::FileSet, VfsPath}; |
66 | 66 | ||
67 | use crate::{ | 67 | use crate::{ |
@@ -80,14 +80,14 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static { | |||
80 | 80 | ||
81 | fn with_files(ra_fixture: &str) -> Self { | 81 | fn with_files(ra_fixture: &str) -> Self { |
82 | let mut db = Self::default(); | 82 | let mut db = Self::default(); |
83 | let pos = with_files(&mut db, ra_fixture); | 83 | let (pos, _) = with_files(&mut db, ra_fixture); |
84 | assert!(pos.is_none()); | 84 | assert!(pos.is_none()); |
85 | db | 85 | db |
86 | } | 86 | } |
87 | 87 | ||
88 | fn with_position(ra_fixture: &str) -> (Self, FilePosition) { | 88 | fn with_position(ra_fixture: &str) -> (Self, FilePosition) { |
89 | let mut db = Self::default(); | 89 | let mut db = Self::default(); |
90 | let pos = with_files(&mut db, ra_fixture); | 90 | let (pos, _) = with_files(&mut db, ra_fixture); |
91 | (db, pos.unwrap()) | 91 | (db, pos.unwrap()) |
92 | } | 92 | } |
93 | 93 | ||
@@ -109,12 +109,10 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId | |||
109 | 109 | ||
110 | let source_root = SourceRoot::new_local(file_set); | 110 | let source_root = SourceRoot::new_local(file_set); |
111 | 111 | ||
112 | let fixture = parse_single_fixture(ra_fixture); | 112 | let crate_graph = if let Some(meta) = ra_fixture.lines().find(|it| it.contains("//-")) { |
113 | 113 | let entry = Fixture::parse_single(meta.trim()); | |
114 | let crate_graph = if let Some(entry) = fixture { | 114 | let meta = match ParsedMeta::from(&entry) { |
115 | let meta = match ParsedMeta::from(&entry.meta) { | ||
116 | ParsedMeta::File(it) => it, | 115 | ParsedMeta::File(it) => it, |
117 | _ => panic!("with_single_file only support file meta"), | ||
118 | }; | 116 | }; |
119 | 117 | ||
120 | let mut crate_graph = CrateGraph::default(); | 118 | let mut crate_graph = CrateGraph::default(); |
@@ -150,30 +148,27 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId | |||
150 | file_id | 148 | file_id |
151 | } | 149 | } |
152 | 150 | ||
153 | fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosition> { | 151 | fn with_files( |
154 | let fixture = parse_fixture(fixture); | 152 | db: &mut dyn SourceDatabaseExt, |
153 | fixture: &str, | ||
154 | ) -> (Option<FilePosition>, Vec<FileId>) { | ||
155 | let fixture = Fixture::parse(fixture); | ||
155 | 156 | ||
157 | let mut files = Vec::new(); | ||
156 | let mut crate_graph = CrateGraph::default(); | 158 | let mut crate_graph = CrateGraph::default(); |
157 | let mut crates = FxHashMap::default(); | 159 | let mut crates = FxHashMap::default(); |
158 | let mut crate_deps = Vec::new(); | 160 | let mut crate_deps = Vec::new(); |
159 | let mut default_crate_root: Option<FileId> = None; | 161 | let mut default_crate_root: Option<FileId> = None; |
160 | 162 | ||
161 | let mut file_set = FileSet::default(); | 163 | let mut file_set = FileSet::default(); |
162 | let mut source_root_id = WORKSPACE; | 164 | let source_root_id = WORKSPACE; |
163 | let mut source_root_prefix = "/".to_string(); | 165 | let source_root_prefix = "/".to_string(); |
164 | let mut file_id = FileId(0); | 166 | let mut file_id = FileId(0); |
165 | 167 | ||
166 | let mut file_position = None; | 168 | let mut file_position = None; |
167 | 169 | ||
168 | for entry in fixture.iter() { | 170 | for entry in fixture.iter() { |
169 | let meta = match ParsedMeta::from(&entry.meta) { | 171 | let meta = match ParsedMeta::from(entry) { |
170 | ParsedMeta::Root { path } => { | ||
171 | let file_set = std::mem::replace(&mut file_set, FileSet::default()); | ||
172 | db.set_source_root(source_root_id, Arc::new(SourceRoot::new_local(file_set))); | ||
173 | source_root_id.0 += 1; | ||
174 | source_root_prefix = path; | ||
175 | continue; | ||
176 | } | ||
177 | ParsedMeta::File(it) => it, | 172 | ParsedMeta::File(it) => it, |
178 | }; | 173 | }; |
179 | assert!(meta.path.starts_with(&source_root_prefix)); | 174 | assert!(meta.path.starts_with(&source_root_prefix)); |
@@ -210,7 +205,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit | |||
210 | db.set_file_source_root(file_id, source_root_id); | 205 | db.set_file_source_root(file_id, source_root_id); |
211 | let path = VfsPath::new_virtual_path(meta.path); | 206 | let path = VfsPath::new_virtual_path(meta.path); |
212 | file_set.insert(file_id, path.into()); | 207 | file_set.insert(file_id, path.into()); |
213 | 208 | files.push(file_id); | |
214 | file_id.0 += 1; | 209 | file_id.0 += 1; |
215 | } | 210 | } |
216 | 211 | ||
@@ -235,11 +230,10 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit | |||
235 | db.set_source_root(source_root_id, Arc::new(SourceRoot::new_local(file_set))); | 230 | db.set_source_root(source_root_id, Arc::new(SourceRoot::new_local(file_set))); |
236 | db.set_crate_graph(Arc::new(crate_graph)); | 231 | db.set_crate_graph(Arc::new(crate_graph)); |
237 | 232 | ||
238 | file_position | 233 | (file_position, files) |
239 | } | 234 | } |
240 | 235 | ||
241 | enum ParsedMeta { | 236 | enum ParsedMeta { |
242 | Root { path: String }, | ||
243 | File(FileMeta), | 237 | File(FileMeta), |
244 | } | 238 | } |
245 | 239 | ||
@@ -252,25 +246,22 @@ struct FileMeta { | |||
252 | env: Env, | 246 | env: Env, |
253 | } | 247 | } |
254 | 248 | ||
255 | impl From<&FixtureMeta> for ParsedMeta { | 249 | impl From<&Fixture> for ParsedMeta { |
256 | fn from(meta: &FixtureMeta) -> Self { | 250 | fn from(f: &Fixture) -> Self { |
257 | match meta { | 251 | let mut cfg = CfgOptions::default(); |
258 | FixtureMeta::Root { path } => { | 252 | f.cfg_atoms.iter().for_each(|it| cfg.insert_atom(it.into())); |
259 | // `Self::Root` causes a false warning: 'variant is never constructed: `Root` ' | 253 | f.cfg_key_values.iter().for_each(|(k, v)| cfg.insert_key_value(k.into(), v.into())); |
260 | // see https://github.com/rust-lang/rust/issues/69018 | 254 | |
261 | ParsedMeta::Root { path: path.to_owned() } | 255 | Self::File(FileMeta { |
262 | } | 256 | path: f.path.to_owned(), |
263 | FixtureMeta::File(f) => Self::File(FileMeta { | 257 | krate: f.crate_name.to_owned(), |
264 | path: f.path.to_owned(), | 258 | deps: f.deps.to_owned(), |
265 | krate: f.crate_name.to_owned(), | 259 | cfg, |
266 | deps: f.deps.to_owned(), | 260 | edition: f |
267 | cfg: f.cfg.to_owned(), | 261 | .edition |
268 | edition: f | 262 | .as_ref() |
269 | .edition | 263 | .map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()), |
270 | .as_ref() | 264 | env: Env::from(f.env.iter()), |
271 | .map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()), | 265 | }) |
272 | env: Env::from(f.env.iter()), | ||
273 | }), | ||
274 | } | ||
275 | } | 266 | } |
276 | } | 267 | } |