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