rust-analyzer/crates/hir_expand/src/test_db.rs

50 lines
1.3 KiB
Rust
Raw Normal View History

2019-11-22 17:11:33 +00:00
//! Database used for testing `hir_expand`.
use std::{
fmt, panic,
2019-11-22 17:11:33 +00:00
sync::{Arc, Mutex},
};
use base_db::{salsa, AnchoredPath, CrateId, FileId, FileLoader, FileLoaderDelegate};
2020-06-11 09:30:06 +00:00
use rustc_hash::FxHashSet;
2019-11-22 17:11:33 +00:00
#[salsa::database(
2020-08-13 14:25:38 +00:00
base_db::SourceDatabaseExtStorage,
base_db::SourceDatabaseStorage,
2019-11-22 17:11:33 +00:00
crate::db::AstDatabaseStorage
)]
#[derive(Default)]
2020-11-02 15:31:38 +00:00
pub(crate) struct TestDB {
storage: salsa::Storage<TestDB>,
events: Mutex<Option<Vec<salsa::Event>>>,
2019-11-22 17:11:33 +00:00
}
impl fmt::Debug for TestDB {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TestDB").finish()
2019-11-26 08:29:20 +00:00
}
}
2019-11-26 08:29:20 +00:00
impl salsa::Database for TestDB {
fn salsa_event(&self, event: salsa::Event) {
2019-11-22 17:11:33 +00:00
let mut events = self.events.lock().unwrap();
if let Some(events) = &mut *events {
events.push(event);
2019-11-22 17:11:33 +00:00
}
}
}
impl panic::RefUnwindSafe for TestDB {}
impl FileLoader for TestDB {
fn file_text(&self, file_id: FileId) -> Arc<String> {
FileLoaderDelegate(self).file_text(file_id)
}
fn resolve_path(&self, path: AnchoredPath) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(path)
2019-11-22 17:11:33 +00:00
}
2020-06-11 09:30:06 +00:00
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
2019-11-22 17:11:33 +00:00
FileLoaderDelegate(self).relevant_crates(file_id)
}
}