diff options
Diffstat (limited to 'crates/ra_hir/src/mock.rs')
-rw-r--r-- | crates/ra_hir/src/mock.rs | 134 |
1 files changed, 0 insertions, 134 deletions
diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs deleted file mode 100644 index ff1f3fa75..000000000 --- a/crates/ra_hir/src/mock.rs +++ /dev/null | |||
@@ -1,134 +0,0 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use std::{panic, sync::Arc}; | ||
4 | |||
5 | use hir_def::{db::DefDatabase2, ModuleId}; | ||
6 | use hir_expand::diagnostics::DiagnosticSink; | ||
7 | use parking_lot::Mutex; | ||
8 | use ra_db::{ | ||
9 | salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, SourceDatabase, | ||
10 | SourceRootId, | ||
11 | }; | ||
12 | |||
13 | use crate::{db, debug::HirDebugHelper}; | ||
14 | |||
15 | pub const WORKSPACE: SourceRootId = SourceRootId(0); | ||
16 | |||
17 | #[salsa::database( | ||
18 | ra_db::SourceDatabaseExtStorage, | ||
19 | ra_db::SourceDatabaseStorage, | ||
20 | db::InternDatabaseStorage, | ||
21 | db::AstDatabaseStorage, | ||
22 | db::DefDatabaseStorage, | ||
23 | db::DefDatabase2Storage, | ||
24 | db::HirDatabaseStorage | ||
25 | )] | ||
26 | #[derive(Debug)] | ||
27 | pub struct MockDatabase { | ||
28 | events: Mutex<Option<Vec<salsa::Event<MockDatabase>>>>, | ||
29 | runtime: salsa::Runtime<MockDatabase>, | ||
30 | } | ||
31 | |||
32 | impl panic::RefUnwindSafe for MockDatabase {} | ||
33 | |||
34 | impl FileLoader for MockDatabase { | ||
35 | fn file_text(&self, file_id: FileId) -> Arc<String> { | ||
36 | FileLoaderDelegate(self).file_text(file_id) | ||
37 | } | ||
38 | fn resolve_relative_path( | ||
39 | &self, | ||
40 | anchor: FileId, | ||
41 | relative_path: &RelativePath, | ||
42 | ) -> Option<FileId> { | ||
43 | FileLoaderDelegate(self).resolve_relative_path(anchor, relative_path) | ||
44 | } | ||
45 | fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { | ||
46 | FileLoaderDelegate(self).relevant_crates(file_id) | ||
47 | } | ||
48 | } | ||
49 | |||
50 | // FIXME: improve `WithFixture` to bring useful hir debugging back | ||
51 | impl HirDebugHelper for MockDatabase { | ||
52 | fn crate_name(&self, _krate: CrateId) -> Option<String> { | ||
53 | None | ||
54 | } | ||
55 | |||
56 | fn file_path(&self, _file_id: FileId) -> Option<String> { | ||
57 | None | ||
58 | } | ||
59 | } | ||
60 | |||
61 | impl MockDatabase { | ||
62 | pub fn diagnostics(&self) -> String { | ||
63 | let mut buf = String::new(); | ||
64 | let crate_graph = self.crate_graph(); | ||
65 | for krate in crate_graph.iter().next() { | ||
66 | let crate_def_map = self.crate_def_map(krate); | ||
67 | for (module_id, _) in crate_def_map.modules.iter() { | ||
68 | let module_id = ModuleId { krate, module_id }; | ||
69 | let module = crate::Module::from(module_id); | ||
70 | module.diagnostics( | ||
71 | self, | ||
72 | &mut DiagnosticSink::new(|d| { | ||
73 | buf += &format!("{:?}: {}\n", d.syntax_node(self).text(), d.message()); | ||
74 | }), | ||
75 | ) | ||
76 | } | ||
77 | } | ||
78 | buf | ||
79 | } | ||
80 | } | ||
81 | |||
82 | impl salsa::Database for MockDatabase { | ||
83 | fn salsa_runtime(&self) -> &salsa::Runtime<MockDatabase> { | ||
84 | &self.runtime | ||
85 | } | ||
86 | |||
87 | fn salsa_event(&self, event: impl Fn() -> salsa::Event<MockDatabase>) { | ||
88 | let mut events = self.events.lock(); | ||
89 | if let Some(events) = &mut *events { | ||
90 | events.push(event()); | ||
91 | } | ||
92 | } | ||
93 | } | ||
94 | |||
95 | impl Default for MockDatabase { | ||
96 | fn default() -> MockDatabase { | ||
97 | let mut db = | ||
98 | MockDatabase { events: Default::default(), runtime: salsa::Runtime::default() }; | ||
99 | db.set_crate_graph(Default::default()); | ||
100 | db | ||
101 | } | ||
102 | } | ||
103 | |||
104 | impl salsa::ParallelDatabase for MockDatabase { | ||
105 | fn snapshot(&self) -> salsa::Snapshot<MockDatabase> { | ||
106 | salsa::Snapshot::new(MockDatabase { | ||
107 | events: Default::default(), | ||
108 | runtime: self.runtime.snapshot(self), | ||
109 | }) | ||
110 | } | ||
111 | } | ||
112 | |||
113 | impl MockDatabase { | ||
114 | pub fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event<MockDatabase>> { | ||
115 | *self.events.lock() = Some(Vec::new()); | ||
116 | f(); | ||
117 | self.events.lock().take().unwrap() | ||
118 | } | ||
119 | |||
120 | pub fn log_executed(&self, f: impl FnOnce()) -> Vec<String> { | ||
121 | let events = self.log(f); | ||
122 | events | ||
123 | .into_iter() | ||
124 | .filter_map(|e| match e.kind { | ||
125 | // This pretty horrible, but `Debug` is the only way to inspect | ||
126 | // QueryDescriptor at the moment. | ||
127 | salsa::EventKind::WillExecute { database_key } => { | ||
128 | Some(format!("{:?}", database_key)) | ||
129 | } | ||
130 | _ => None, | ||
131 | }) | ||
132 | .collect() | ||
133 | } | ||
134 | } | ||