rust/crates/ra_analysis/src/db.rs

134 lines
4.0 KiB
Rust
Raw Normal View History

2018-10-31 15:41:43 -05:00
use std::sync::Arc;
2018-11-25 10:02:14 -06:00
#[cfg(test)]
2018-11-21 11:44:05 -06:00
use parking_lot::Mutex;
2018-11-04 05:09:21 -06:00
use salsa::{self, Database};
use ra_db::{LocationIntener, BaseDatabase};
2018-10-20 14:29:26 -05:00
use crate::{
2018-11-27 12:45:42 -06:00
hir,
2018-11-27 17:22:25 -06:00
symbol_index,
loc2id::{IdMaps, DefId, DefLoc, FnId},
};
2018-09-13 14:58:36 -05:00
2018-11-04 05:09:21 -06:00
#[derive(Debug)]
2018-10-07 05:18:25 -05:00
pub(crate) struct RootDatabase {
2018-11-21 11:44:05 -06:00
#[cfg(test)]
events: Mutex<Option<Vec<salsa::Event<RootDatabase>>>>,
#[cfg(not(test))]
events: (),
2018-10-20 10:43:02 -05:00
runtime: salsa::Runtime<RootDatabase>,
2018-11-27 17:49:28 -06:00
id_maps: Arc<IdMaps>,
2018-09-13 14:58:36 -05:00
}
2018-10-07 05:18:25 -05:00
impl salsa::Database for RootDatabase {
2018-10-20 10:43:02 -05:00
fn salsa_runtime(&self) -> &salsa::Runtime<RootDatabase> {
2018-10-07 05:18:25 -05:00
&self.runtime
}
2018-11-21 11:44:05 -06:00
2018-11-25 10:02:14 -06:00
#[allow(unused)]
2018-11-21 11:44:05 -06:00
fn salsa_event(&self, event: impl Fn() -> salsa::Event<RootDatabase>) {
#[cfg(test)]
{
let mut events = self.events.lock();
if let Some(events) = &mut *events {
events.push(event());
}
}
}
2018-09-13 14:58:36 -05:00
}
2018-11-04 05:09:21 -06:00
impl Default for RootDatabase {
fn default() -> RootDatabase {
let mut db = RootDatabase {
2018-11-21 11:44:05 -06:00
events: Default::default(),
2018-11-18 07:21:23 -06:00
runtime: salsa::Runtime::default(),
2018-11-27 17:49:28 -06:00
id_maps: Default::default(),
2018-11-04 05:09:21 -06:00
};
db.query_mut(ra_db::SourceRootQuery)
.set(ra_db::WORKSPACE, Default::default());
db.query_mut(ra_db::CrateGraphQuery)
2018-11-04 05:09:21 -06:00
.set((), Default::default());
db.query_mut(ra_db::LibrariesQuery)
2018-11-04 05:09:21 -06:00
.set((), Default::default());
db
}
}
2018-10-15 14:29:24 -05:00
impl salsa::ParallelDatabase for RootDatabase {
2018-11-01 07:29:23 -05:00
fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
salsa::Snapshot::new(RootDatabase {
2018-11-21 11:44:05 -06:00
events: Default::default(),
2018-11-01 07:29:23 -05:00
runtime: self.runtime.snapshot(self),
2018-11-18 07:21:23 -06:00
id_maps: self.id_maps.clone(),
2018-11-01 07:29:23 -05:00
})
2018-10-15 14:29:24 -05:00
}
}
impl BaseDatabase for RootDatabase {}
2018-11-18 07:21:23 -06:00
2018-11-27 17:49:28 -06:00
impl AsRef<LocationIntener<DefLoc, DefId>> for RootDatabase {
fn as_ref(&self) -> &LocationIntener<DefLoc, DefId> {
&self.id_maps.defs
}
}
impl AsRef<LocationIntener<hir::SourceItemId, FnId>> for RootDatabase {
fn as_ref(&self) -> &LocationIntener<hir::SourceItemId, FnId> {
&self.id_maps.fns
}
}
2018-11-21 11:44:05 -06:00
#[cfg(test)]
impl RootDatabase {
pub(crate) fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event<RootDatabase>> {
*self.events.lock() = Some(Vec::new());
f();
let events = self.events.lock().take().unwrap();
events
}
pub(crate) fn log_executed(&self, f: impl FnOnce()) -> Vec<String> {
let events = self.log(f);
events
.into_iter()
.filter_map(|e| match e.kind {
// This pretty horrible, but `Debug` is the only way to inspect
// QueryDescriptor at the moment.
salsa::EventKind::WillExecute { descriptor } => Some(format!("{:?}", descriptor)),
_ => None,
})
.collect()
}
}
2018-10-07 05:18:25 -05:00
salsa::database_storage! {
pub(crate) struct RootDatabaseStorage for RootDatabase {
impl ra_db::FilesDatabase {
fn file_text() for ra_db::FileTextQuery;
fn file_source_root() for ra_db::FileSourceRootQuery;
fn source_root() for ra_db::SourceRootQuery;
fn libraries() for ra_db::LibrariesQuery;
fn crate_graph() for ra_db::CrateGraphQuery;
2018-10-07 05:18:25 -05:00
}
impl ra_db::SyntaxDatabase {
fn source_file() for ra_db::SourceFileQuery;
fn file_lines() for ra_db::FileLinesQuery;
2018-11-27 17:22:25 -06:00
}
impl symbol_index::SymbolsDatabase {
fn file_symbols() for symbol_index::FileSymbolsQuery;
fn library_symbols() for symbol_index::LibrarySymbolsQuery;
2018-10-07 05:18:25 -05:00
}
2018-11-27 14:03:08 -06:00
impl hir::db::HirDatabase {
fn module_tree() for hir::db::ModuleTreeQuery;
fn fn_scopes() for hir::db::FnScopesQuery;
2018-11-27 17:22:25 -06:00
fn file_items() for hir::db::SourceFileItemsQuery;
fn file_item() for hir::db::FileItemQuery;
fn input_module_items() for hir::db::InputModuleItemsQuery;
fn item_map() for hir::db::ItemMapQuery;
fn fn_syntax() for hir::db::FnSyntaxQuery;
fn submodules() for hir::db::SubmodulesQuery;
}
2018-10-07 05:18:25 -05:00
}
2018-09-13 14:58:36 -05:00
}