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.rs119
1 files changed, 0 insertions, 119 deletions
diff --git a/crates/ra_hir/src/test_db.rs b/crates/ra_hir/src/test_db.rs
deleted file mode 100644
index efee2f658..000000000
--- a/crates/ra_hir/src/test_db.rs
+++ /dev/null
@@ -1,119 +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_event(&self, event: impl Fn() -> salsa::Event<TestDB>) {
32 let mut events = self.events.lock();
33 if let Some(events) = &mut *events {
34 events.push(event());
35 }
36 }
37}
38
39impl salsa::ParallelDatabase for TestDB {
40 fn snapshot(&self) -> salsa::Snapshot<TestDB> {
41 salsa::Snapshot::new(TestDB {
42 events: Default::default(),
43 runtime: self.runtime.snapshot(self),
44 })
45 }
46}
47
48impl panic::RefUnwindSafe for TestDB {}
49
50impl FileLoader for TestDB {
51 fn file_text(&self, file_id: FileId) -> Arc<String> {
52 FileLoaderDelegate(self).file_text(file_id)
53 }
54 fn resolve_relative_path(
55 &self,
56 anchor: FileId,
57 relative_path: &RelativePath,
58 ) -> Option<FileId> {
59 FileLoaderDelegate(self).resolve_relative_path(anchor, relative_path)
60 }
61 fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
62 FileLoaderDelegate(self).relevant_crates(file_id)
63 }
64}
65
66// FIXME: improve `WithFixture` to bring useful hir debugging back
67impl HirDebugHelper for TestDB {
68 fn crate_name(&self, _krate: CrateId) -> Option<String> {
69 None
70 }
71
72 fn file_path(&self, _file_id: FileId) -> Option<String> {
73 None
74 }
75}
76
77impl TestDB {
78 pub fn diagnostics(&self) -> String {
79 let mut buf = String::new();
80 let crate_graph = self.crate_graph();
81 for krate in crate_graph.iter().next() {
82 let crate_def_map = self.crate_def_map(krate);
83 for (module_id, _) in crate_def_map.modules.iter() {
84 let module_id = ModuleId { krate, module_id };
85 let module = crate::Module::from(module_id);
86 module.diagnostics(
87 self,
88 &mut DiagnosticSink::new(|d| {
89 buf += &format!("{:?}: {}\n", d.syntax_node(self).text(), d.message());
90 }),
91 )
92 }
93 }
94 buf
95 }
96}
97
98impl TestDB {
99 pub fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event<TestDB>> {
100 *self.events.lock() = Some(Vec::new());
101 f();
102 self.events.lock().take().unwrap()
103 }
104
105 pub fn log_executed(&self, f: impl FnOnce()) -> Vec<String> {
106 let events = self.log(f);
107 events
108 .into_iter()
109 .filter_map(|e| match e.kind {
110 // This pretty horrible, but `Debug` is the only way to inspect
111 // QueryDescriptor at the moment.
112 salsa::EventKind::WillExecute { database_key } => {
113 Some(format!("{:?}", database_key))
114 }
115 _ => None,
116 })
117 .collect()
118 }
119}