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