diff options
Diffstat (limited to 'crates/ra_hir/src/mock.rs')
-rw-r--r-- | crates/ra_hir/src/mock.rs | 74 |
1 files changed, 3 insertions, 71 deletions
diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index 35dfaf3ba..ab97a09b9 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs | |||
@@ -2,17 +2,17 @@ | |||
2 | 2 | ||
3 | use std::{panic, sync::Arc}; | 3 | use std::{panic, sync::Arc}; |
4 | 4 | ||
5 | use hir_expand::diagnostics::DiagnosticSink; | ||
5 | use parking_lot::Mutex; | 6 | use parking_lot::Mutex; |
6 | use ra_cfg::CfgOptions; | 7 | use ra_cfg::CfgOptions; |
7 | use ra_db::{ | 8 | use ra_db::{ |
8 | salsa, CrateGraph, CrateId, Edition, FileId, FileLoader, FileLoaderDelegate, FilePosition, | 9 | salsa, CrateGraph, CrateId, Edition, FileId, FileLoader, FileLoaderDelegate, FilePosition, |
9 | SourceDatabase, SourceDatabaseExt, SourceRoot, SourceRootId, | 10 | RelativePath, RelativePathBuf, SourceDatabase, SourceDatabaseExt, SourceRoot, SourceRootId, |
10 | }; | 11 | }; |
11 | use relative_path::{RelativePath, RelativePathBuf}; | ||
12 | use rustc_hash::FxHashMap; | 12 | use rustc_hash::FxHashMap; |
13 | use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER}; | 13 | use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER}; |
14 | 14 | ||
15 | use crate::{db, debug::HirDebugHelper, diagnostics::DiagnosticSink}; | 15 | use crate::{db, debug::HirDebugHelper}; |
16 | 16 | ||
17 | pub const WORKSPACE: SourceRootId = SourceRootId(0); | 17 | pub const WORKSPACE: SourceRootId = SourceRootId(0); |
18 | 18 | ||
@@ -77,12 +77,6 @@ impl MockDatabase { | |||
77 | (db, source_root, file_id) | 77 | (db, source_root, file_id) |
78 | } | 78 | } |
79 | 79 | ||
80 | pub fn with_position(fixture: &str) -> (MockDatabase, FilePosition) { | ||
81 | let (db, position) = MockDatabase::from_fixture(fixture); | ||
82 | let position = position.expect("expected a marker ( <|> )"); | ||
83 | (db, position) | ||
84 | } | ||
85 | |||
86 | pub fn file_id_of(&self, path: &str) -> FileId { | 80 | pub fn file_id_of(&self, path: &str) -> FileId { |
87 | match self.files.get(path) { | 81 | match self.files.get(path) { |
88 | Some(it) => *it, | 82 | Some(it) => *it, |
@@ -90,25 +84,6 @@ impl MockDatabase { | |||
90 | } | 84 | } |
91 | } | 85 | } |
92 | 86 | ||
93 | pub fn set_crate_graph_from_fixture(&mut self, graph: CrateGraphFixture) { | ||
94 | let mut ids = FxHashMap::default(); | ||
95 | let mut crate_graph = CrateGraph::default(); | ||
96 | for (crate_name, (crate_root, edition, cfg_options, _)) in graph.0.iter() { | ||
97 | let crate_root = self.file_id_of(&crate_root); | ||
98 | let crate_id = crate_graph.add_crate_root(crate_root, *edition, cfg_options.clone()); | ||
99 | Arc::make_mut(&mut self.crate_names).insert(crate_id, crate_name.clone()); | ||
100 | ids.insert(crate_name, crate_id); | ||
101 | } | ||
102 | for (crate_name, (_, _, _, deps)) in graph.0.iter() { | ||
103 | let from = ids[crate_name]; | ||
104 | for dep in deps { | ||
105 | let to = ids[dep]; | ||
106 | crate_graph.add_dep(from, dep.as_str().into(), to).unwrap(); | ||
107 | } | ||
108 | } | ||
109 | self.set_crate_graph(Arc::new(crate_graph)) | ||
110 | } | ||
111 | |||
112 | pub fn diagnostics(&self) -> String { | 87 | pub fn diagnostics(&self) -> String { |
113 | let mut buf = String::new(); | 88 | let mut buf = String::new(); |
114 | let mut files: Vec<FileId> = self.files.values().copied().collect(); | 89 | let mut files: Vec<FileId> = self.files.values().copied().collect(); |
@@ -285,46 +260,3 @@ impl MockDatabase { | |||
285 | .collect() | 260 | .collect() |
286 | } | 261 | } |
287 | } | 262 | } |
288 | |||
289 | #[derive(Default)] | ||
290 | pub struct CrateGraphFixture(pub Vec<(String, (String, Edition, CfgOptions, Vec<String>))>); | ||
291 | |||
292 | #[macro_export] | ||
293 | macro_rules! crate_graph { | ||
294 | ($( | ||
295 | $crate_name:literal: ( | ||
296 | $crate_path:literal, | ||
297 | $($edition:literal,)? | ||
298 | [$($dep:literal),*] | ||
299 | $(, cfg = { | ||
300 | $($key:literal $(= $value:literal)?),* | ||
301 | $(,)? | ||
302 | })? | ||
303 | ), | ||
304 | )*) => {{ | ||
305 | let mut res = $crate::mock::CrateGraphFixture::default(); | ||
306 | $( | ||
307 | #[allow(unused_mut, unused_assignments)] | ||
308 | let mut edition = ra_db::Edition::Edition2018; | ||
309 | $(edition = ra_db::Edition::from_string($edition);)? | ||
310 | let cfg_options = { | ||
311 | #[allow(unused_mut)] | ||
312 | let mut cfg = ::ra_cfg::CfgOptions::default(); | ||
313 | $( | ||
314 | $( | ||
315 | if 0 == 0 $(+ { drop($value); 1})? { | ||
316 | cfg.insert_atom($key.into()); | ||
317 | } | ||
318 | $(cfg.insert_key_value($key.into(), $value.into());)? | ||
319 | )* | ||
320 | )? | ||
321 | cfg | ||
322 | }; | ||
323 | res.0.push(( | ||
324 | $crate_name.to_string(), | ||
325 | ($crate_path.to_string(), edition, cfg_options, vec![$($dep.to_string()),*]) | ||
326 | )); | ||
327 | )* | ||
328 | res | ||
329 | }} | ||
330 | } | ||