rust/crates/ra_ide_db/src/lib.rs

135 lines
4.1 KiB
Rust
Raw Normal View History

2020-02-06 07:43:46 -06: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 05:07:06 -06:00
2020-02-06 05:17:40 -06:00
pub mod line_index;
2020-02-06 05:22:35 -06:00
pub mod symbol_index;
2020-02-06 05:35:40 -06:00
pub mod change;
2020-02-06 09:23:28 -06:00
pub mod defs;
2020-03-04 05:05:14 -06:00
pub mod search;
2020-02-06 09:26:43 -06:00
pub mod imports_locator;
2020-05-06 04:31:26 -05:00
pub mod source_change;
2020-02-06 05:43:56 -06:00
mod wasm_shims;
2020-02-06 05:17:40 -06:00
2020-02-06 05:08:08 -06:00
use std::sync::Arc;
use hir::db::{AstDatabase, DefDatabase};
2020-02-06 05:08:08 -06:00
use ra_db::{
salsa::{self, Database, Durability},
2020-06-05 09:45:20 -05:00
Canceled, CheckCanceled, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase,
2020-06-11 06:26:48 -05:00
Upcast,
2020-02-06 05:08:08 -06:00
};
2020-06-11 06:26:48 -05:00
use rustc_hash::FxHashSet;
2020-02-06 05:08:08 -06:00
2020-03-10 12:56:15 -05:00
use crate::{line_index::LineIndex, symbol_index::SymbolsDatabase};
2020-02-06 05:08:08 -06:00
#[salsa::database(
ra_db::SourceDatabaseStorage,
ra_db::SourceDatabaseExtStorage,
LineIndexDatabaseStorage,
symbol_index::SymbolsDatabaseStorage,
hir::db::InternDatabaseStorage,
hir::db::AstDatabaseStorage,
hir::db::DefDatabaseStorage,
hir::db::HirDatabaseStorage
)]
#[derive(Debug)]
2020-02-06 05:43:56 -06:00
pub struct RootDatabase {
2020-02-06 05:08:08 -06:00
runtime: salsa::Runtime<RootDatabase>,
2020-02-06 05:43:56 -06:00
pub last_gc: crate::wasm_shims::Instant,
pub last_gc_check: crate::wasm_shims::Instant,
2020-02-06 05:08:08 -06: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-02-06 05:08:08 -06:00
impl FileLoader for RootDatabase {
fn file_text(&self, file_id: FileId) -> Arc<String> {
FileLoaderDelegate(self).file_text(file_id)
}
2020-06-05 08:07:30 -05:00
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(anchor, path)
2020-02-06 05:08:08 -06:00
}
2020-06-11 04:30:06 -05:00
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
2020-02-06 05:08:08 -06:00
FileLoaderDelegate(self).relevant_crates(file_id)
}
}
impl salsa::Database for RootDatabase {
fn salsa_runtime(&self) -> &salsa::Runtime<RootDatabase> {
&self.runtime
}
fn salsa_runtime_mut(&mut self) -> &mut salsa::Runtime<Self> {
&mut self.runtime
}
fn on_propagated_panic(&self) -> ! {
Canceled::throw()
}
fn salsa_event(&self, event: impl Fn() -> salsa::Event<RootDatabase>) {
match event().kind {
salsa::EventKind::DidValidateMemoizedValue { .. }
| salsa::EventKind::WillExecute { .. } => {
self.check_canceled();
}
_ => (),
}
}
}
impl Default for RootDatabase {
fn default() -> RootDatabase {
2020-03-10 12:56:15 -05:00
RootDatabase::new(None)
2020-02-06 05:08:08 -06:00
}
}
impl RootDatabase {
2020-03-10 12:56:15 -05:00
pub fn new(lru_capacity: Option<usize>) -> RootDatabase {
2020-02-06 05:08:08 -06:00
let mut db = RootDatabase {
runtime: salsa::Runtime::default(),
last_gc: crate::wasm_shims::Instant::now(),
last_gc_check: crate::wasm_shims::Instant::now(),
};
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);
db.update_lru_capacity(lru_capacity);
2020-02-06 05:08:08 -06:00
db
}
pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) {
let lru_capacity = lru_capacity.unwrap_or(ra_db::DEFAULT_LRU_CAP);
self.query_mut(ra_db::ParseQuery).set_lru_capacity(lru_capacity);
self.query_mut(hir::db::ParseMacroQuery).set_lru_capacity(lru_capacity);
self.query_mut(hir::db::MacroExpandQuery).set_lru_capacity(lru_capacity);
}
2020-02-06 05:08:08 -06:00
}
impl salsa::ParallelDatabase for RootDatabase {
fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
salsa::Snapshot::new(RootDatabase {
runtime: self.runtime.snapshot(self),
last_gc: self.last_gc,
last_gc_check: self.last_gc_check,
})
}
}
#[salsa::query_group(LineIndexDatabaseStorage)]
2020-02-06 05:43:56 -06:00
pub trait LineIndexDatabase: ra_db::SourceDatabase + CheckCanceled {
2020-02-06 05:08:08 -06:00
fn line_index(&self, file_id: FileId) -> Arc<LineIndex>;
}
fn line_index(db: &impl LineIndexDatabase, file_id: FileId) -> Arc<LineIndex> {
let text = db.file_text(file_id);
Arc::new(LineIndex::new(&*text))
}