2020-02-06 13:43:46 +00:00
|
|
|
//! This crate defines the core datastructure representing IDE state -- `RootDatabase`.
|
|
|
|
//!
|
|
|
|
//! It is mainly a `HirDatabase` for semantic analysis, plus a `SymbolsDatabase`, for fuzzy search.
|
2020-02-06 11:07:06 +00:00
|
|
|
|
2020-10-02 13:45:09 +00:00
|
|
|
mod apply_change;
|
2020-08-18 14:41:21 +00:00
|
|
|
pub mod label;
|
2020-02-06 11:17:40 +00:00
|
|
|
pub mod line_index;
|
2020-02-06 11:22:35 +00:00
|
|
|
pub mod symbol_index;
|
2020-02-06 15:23:28 +00:00
|
|
|
pub mod defs;
|
2020-03-04 11:05:14 +00:00
|
|
|
pub mod search;
|
2020-02-06 15:26:43 +00:00
|
|
|
pub mod imports_locator;
|
2020-05-06 09:31:26 +00:00
|
|
|
pub mod source_change;
|
2020-02-06 11:17:40 +00:00
|
|
|
|
2020-07-07 08:14:48 +00:00
|
|
|
use std::{fmt, sync::Arc};
|
2020-02-06 11:08:08 +00:00
|
|
|
|
2020-08-13 14:25:38 +00:00
|
|
|
use base_db::{
|
2020-07-07 08:14:48 +00:00
|
|
|
salsa::{self, Durability},
|
2020-06-05 14:45:20 +00:00
|
|
|
Canceled, CheckCanceled, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase,
|
2020-06-11 11:26:48 +00:00
|
|
|
Upcast,
|
2020-02-06 11:08:08 +00:00
|
|
|
};
|
2020-08-13 14:25:38 +00:00
|
|
|
use hir::db::{AstDatabase, DefDatabase, HirDatabase};
|
2020-06-11 11:26:48 +00:00
|
|
|
use rustc_hash::FxHashSet;
|
2020-02-06 11:08:08 +00:00
|
|
|
|
2020-03-10 17:56:15 +00:00
|
|
|
use crate::{line_index::LineIndex, symbol_index::SymbolsDatabase};
|
2020-02-06 11:08:08 +00:00
|
|
|
|
|
|
|
#[salsa::database(
|
2020-08-13 14:25:38 +00:00
|
|
|
base_db::SourceDatabaseStorage,
|
|
|
|
base_db::SourceDatabaseExtStorage,
|
2020-02-06 11:08:08 +00:00
|
|
|
LineIndexDatabaseStorage,
|
|
|
|
symbol_index::SymbolsDatabaseStorage,
|
|
|
|
hir::db::InternDatabaseStorage,
|
|
|
|
hir::db::AstDatabaseStorage,
|
|
|
|
hir::db::DefDatabaseStorage,
|
|
|
|
hir::db::HirDatabaseStorage
|
|
|
|
)]
|
2020-02-06 11:43:56 +00:00
|
|
|
pub struct RootDatabase {
|
2020-07-07 08:14:48 +00:00
|
|
|
storage: salsa::Storage<RootDatabase>,
|
2020-02-06 11:08:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 08:14:48 +00:00
|
|
|
impl fmt::Debug for RootDatabase {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.debug_struct("RootDatabase").finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 15:05:46 +00:00
|
|
|
impl Upcast<dyn AstDatabase> for RootDatabase {
|
|
|
|
fn upcast(&self) -> &(dyn AstDatabase + 'static) {
|
|
|
|
&*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Upcast<dyn DefDatabase> for RootDatabase {
|
|
|
|
fn upcast(&self) -> &(dyn DefDatabase + 'static) {
|
|
|
|
&*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 09:43:36 +00:00
|
|
|
impl Upcast<dyn HirDatabase> for RootDatabase {
|
|
|
|
fn upcast(&self) -> &(dyn HirDatabase + 'static) {
|
|
|
|
&*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 11:08:08 +00:00
|
|
|
impl FileLoader for RootDatabase {
|
|
|
|
fn file_text(&self, file_id: FileId) -> Arc<String> {
|
|
|
|
FileLoaderDelegate(self).file_text(file_id)
|
|
|
|
}
|
2020-06-05 13:07:30 +00:00
|
|
|
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
|
|
|
|
FileLoaderDelegate(self).resolve_path(anchor, path)
|
2020-02-06 11:08:08 +00:00
|
|
|
}
|
2020-06-11 09:30:06 +00:00
|
|
|
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
|
2020-02-06 11:08:08 +00:00
|
|
|
FileLoaderDelegate(self).relevant_crates(file_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl salsa::Database for RootDatabase {
|
|
|
|
fn on_propagated_panic(&self) -> ! {
|
|
|
|
Canceled::throw()
|
|
|
|
}
|
2020-07-07 08:14:48 +00:00
|
|
|
fn salsa_event(&self, event: salsa::Event) {
|
|
|
|
match event.kind {
|
2020-02-06 11:08:08 +00:00
|
|
|
salsa::EventKind::DidValidateMemoizedValue { .. }
|
|
|
|
| salsa::EventKind::WillExecute { .. } => {
|
|
|
|
self.check_canceled();
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for RootDatabase {
|
|
|
|
fn default() -> RootDatabase {
|
2020-03-10 17:56:15 +00:00
|
|
|
RootDatabase::new(None)
|
2020-02-06 11:08:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RootDatabase {
|
2020-03-10 17:56:15 +00:00
|
|
|
pub fn new(lru_capacity: Option<usize>) -> RootDatabase {
|
2020-09-29 19:13:58 +00:00
|
|
|
let mut db = RootDatabase { storage: salsa::Storage::default() };
|
2020-02-06 11:08:08 +00:00
|
|
|
db.set_crate_graph_with_durability(Default::default(), Durability::HIGH);
|
|
|
|
db.set_local_roots_with_durability(Default::default(), Durability::HIGH);
|
|
|
|
db.set_library_roots_with_durability(Default::default(), Durability::HIGH);
|
2020-03-20 20:09:23 +00:00
|
|
|
db.update_lru_capacity(lru_capacity);
|
2020-02-06 11:08:08 +00:00
|
|
|
db
|
|
|
|
}
|
2020-03-20 20:09:23 +00:00
|
|
|
|
|
|
|
pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) {
|
2020-08-13 14:25:38 +00:00
|
|
|
let lru_capacity = lru_capacity.unwrap_or(base_db::DEFAULT_LRU_CAP);
|
|
|
|
base_db::ParseQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
|
2020-07-07 08:14:48 +00:00
|
|
|
hir::db::ParseMacroQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
|
|
|
|
hir::db::MacroExpandQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
|
2020-03-20 20:09:23 +00:00
|
|
|
}
|
2020-02-06 11:08:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl salsa::ParallelDatabase for RootDatabase {
|
|
|
|
fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
|
2020-09-29 19:13:58 +00:00
|
|
|
salsa::Snapshot::new(RootDatabase { storage: self.storage.snapshot() })
|
2020-02-06 11:08:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::query_group(LineIndexDatabaseStorage)]
|
2020-08-13 14:25:38 +00:00
|
|
|
pub trait LineIndexDatabase: base_db::SourceDatabase + CheckCanceled {
|
2020-02-06 11:08:08 +00:00
|
|
|
fn line_index(&self, file_id: FileId) -> Arc<LineIndex>;
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:14:48 +00:00
|
|
|
fn line_index(db: &dyn LineIndexDatabase, file_id: FileId) -> Arc<LineIndex> {
|
2020-02-06 11:08:08 +00:00
|
|
|
let text = db.file_text(file_id);
|
|
|
|
Arc::new(LineIndex::new(&*text))
|
|
|
|
}
|