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.rs25
1 files changed, 23 insertions, 2 deletions
diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs
index 972f0ece5..8dcea5071 100644
--- a/crates/ra_hir/src/mock.rs
+++ b/crates/ra_hir/src/mock.rs
@@ -2,13 +2,14 @@ use std::{panic, sync::Arc};
2 2
3use parking_lot::Mutex; 3use parking_lot::Mutex;
4use ra_db::{ 4use ra_db::{
5 salsa, CrateGraph, Edition, FileId, FilePosition, SourceDatabase, SourceRoot, SourceRootId, 5 salsa, CrateGraph, CrateId, Edition, FileId, FilePosition, SourceDatabase, SourceRoot,
6 SourceRootId,
6}; 7};
7use relative_path::RelativePathBuf; 8use relative_path::RelativePathBuf;
8use rustc_hash::FxHashMap; 9use rustc_hash::FxHashMap;
9use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER}; 10use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER};
10 11
11use crate::{db, diagnostics::DiagnosticSink}; 12use crate::{db, debug::HirDebugHelper, diagnostics::DiagnosticSink};
12 13
13pub const WORKSPACE: SourceRootId = SourceRootId(0); 14pub const WORKSPACE: SourceRootId = SourceRootId(0);
14 15
@@ -24,10 +25,22 @@ pub struct MockDatabase {
24 events: Mutex<Option<Vec<salsa::Event<MockDatabase>>>>, 25 events: Mutex<Option<Vec<salsa::Event<MockDatabase>>>>,
25 runtime: salsa::Runtime<MockDatabase>, 26 runtime: salsa::Runtime<MockDatabase>,
26 files: FxHashMap<String, FileId>, 27 files: FxHashMap<String, FileId>,
28 crate_names: Arc<FxHashMap<CrateId, String>>,
29 file_paths: Arc<FxHashMap<FileId, String>>,
27} 30}
28 31
29impl panic::RefUnwindSafe for MockDatabase {} 32impl panic::RefUnwindSafe for MockDatabase {}
30 33
34impl HirDebugHelper for MockDatabase {
35 fn crate_name(&self, krate: CrateId) -> Option<String> {
36 self.crate_names.get(&krate).cloned()
37 }
38
39 fn file_path(&self, file_id: FileId) -> Option<String> {
40 self.file_paths.get(&file_id).cloned()
41 }
42}
43
31impl MockDatabase { 44impl MockDatabase {
32 pub fn with_files(fixture: &str) -> MockDatabase { 45 pub fn with_files(fixture: &str) -> MockDatabase {
33 let (db, position) = MockDatabase::from_fixture(fixture); 46 let (db, position) = MockDatabase::from_fixture(fixture);
@@ -62,6 +75,7 @@ impl MockDatabase {
62 for (crate_name, (crate_root, edition, _)) in graph.0.iter() { 75 for (crate_name, (crate_root, edition, _)) in graph.0.iter() {
63 let crate_root = self.file_id_of(&crate_root); 76 let crate_root = self.file_id_of(&crate_root);
64 let crate_id = crate_graph.add_crate_root(crate_root, *edition); 77 let crate_id = crate_graph.add_crate_root(crate_root, *edition);
78 Arc::make_mut(&mut self.crate_names).insert(crate_id, crate_name.clone());
65 ids.insert(crate_name, crate_id); 79 ids.insert(crate_name, crate_id);
66 } 80 }
67 for (crate_name, (_, _, deps)) in graph.0.iter() { 81 for (crate_name, (_, _, deps)) in graph.0.iter() {
@@ -151,8 +165,11 @@ impl MockDatabase {
151 let is_crate_root = rel_path == "lib.rs" || rel_path == "/main.rs"; 165 let is_crate_root = rel_path == "lib.rs" || rel_path == "/main.rs";
152 166
153 let file_id = FileId(self.files.len() as u32); 167 let file_id = FileId(self.files.len() as u32);
168
154 let prev = self.files.insert(path.to_string(), file_id); 169 let prev = self.files.insert(path.to_string(), file_id);
155 assert!(prev.is_none(), "duplicate files in the text fixture"); 170 assert!(prev.is_none(), "duplicate files in the text fixture");
171 Arc::make_mut(&mut self.file_paths).insert(file_id, path.to_string());
172
156 let text = Arc::new(text.to_string()); 173 let text = Arc::new(text.to_string());
157 self.set_file_text(file_id, text); 174 self.set_file_text(file_id, text);
158 self.set_file_relative_path(file_id, rel_path.clone()); 175 self.set_file_relative_path(file_id, rel_path.clone());
@@ -200,6 +217,8 @@ impl Default for MockDatabase {
200 events: Default::default(), 217 events: Default::default(),
201 runtime: salsa::Runtime::default(), 218 runtime: salsa::Runtime::default(),
202 files: FxHashMap::default(), 219 files: FxHashMap::default(),
220 crate_names: Default::default(),
221 file_paths: Default::default(),
203 }; 222 };
204 db.set_crate_graph(Default::default()); 223 db.set_crate_graph(Default::default());
205 db 224 db
@@ -213,6 +232,8 @@ impl salsa::ParallelDatabase for MockDatabase {
213 runtime: self.runtime.snapshot(self), 232 runtime: self.runtime.snapshot(self),
214 // only the root database can be used to get file_id by path. 233 // only the root database can be used to get file_id by path.
215 files: FxHashMap::default(), 234 files: FxHashMap::default(),
235 file_paths: Arc::clone(&self.file_paths),
236 crate_names: Arc::clone(&self.crate_names),
216 }) 237 })
217 } 238 }
218} 239}