rust/crates/ra_analysis/src/db.rs

171 lines
5.1 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;
use ra_editor::LineIndex;
2018-11-27 17:15:21 -06:00
use ra_syntax::{SourceFileNode};
2018-11-04 05:09:21 -06:00
use salsa::{self, Database};
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,
2018-11-27 17:49:28 -06:00
loc2id::{IdMaps, LocationIntener, DefId, DefLoc, FnId},
2018-10-31 15:41:43 -05:00
Cancelable, Canceled, FileId,
};
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(crate::input::SourceRootQuery)
.set(crate::input::WORKSPACE, Default::default());
db.query_mut(crate::input::CrateGraphQuery)
.set((), Default::default());
db.query_mut(crate::input::LibrariesQuery)
.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
}
}
2018-11-27 14:33:36 -06:00
pub(crate) trait BaseDatabase: salsa::Database {
fn id_maps(&self) -> &IdMaps;
fn check_canceled(&self) -> Cancelable<()> {
2018-11-27 16:38:39 -06:00
if self.salsa_runtime().is_current_revision_canceled() {
Err(Canceled)
} else {
Ok(())
}
2018-11-27 14:33:36 -06:00
}
}
impl BaseDatabase for RootDatabase {
2018-11-18 07:21:23 -06:00
fn id_maps(&self) -> &IdMaps {
&self.id_maps
}
}
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 {
2018-10-25 09:52:50 -05:00
impl crate::input::FilesDatabase {
fn file_text() for crate::input::FileTextQuery;
fn file_source_root() for crate::input::FileSourceRootQuery;
fn source_root() for crate::input::SourceRootQuery;
2018-10-31 13:05:14 -05:00
fn libraries() for crate::input::LibrariesQuery;
2018-10-25 09:52:50 -05:00
fn crate_graph() for crate::input::CrateGraphQuery;
2018-10-07 05:18:25 -05:00
}
impl SyntaxDatabase {
2018-11-27 17:25:03 -06:00
fn source_file() for SourceFileQuery;
2018-10-07 05:18:25 -05:00
fn file_lines() for 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
}
2018-10-07 05:18:25 -05:00
salsa::query_group! {
2018-11-27 14:33:36 -06:00
pub(crate) trait SyntaxDatabase: crate::input::FilesDatabase + BaseDatabase {
2018-11-27 17:25:03 -06:00
fn source_file(file_id: FileId) -> SourceFileNode {
type SourceFileQuery;
2018-10-07 05:18:25 -05:00
}
fn file_lines(file_id: FileId) -> Arc<LineIndex> {
type FileLinesQuery;
}
2018-09-15 09:21:47 -05:00
}
2018-09-13 14:58:36 -05:00
}
2018-10-07 05:18:25 -05:00
2018-11-27 17:25:03 -06:00
fn source_file(db: &impl SyntaxDatabase, file_id: FileId) -> SourceFileNode {
2018-10-07 05:18:25 -05:00
let text = db.file_text(file_id);
2018-11-07 09:32:33 -06:00
SourceFileNode::parse(&*text)
2018-10-07 05:18:25 -05:00
}
fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<LineIndex> {
let text = db.file_text(file_id);
Arc::new(LineIndex::new(&*text))
}