rust-analyzer/crates/ra_ide_api/src/db.rs

69 lines
1.8 KiB
Rust
Raw Normal View History

2019-01-26 17:33:33 +00:00
use std::{
sync::Arc,
time,
};
2019-01-08 19:33:36 +00:00
2019-01-17 11:11:00 +00:00
use ra_db::{
2019-01-26 08:20:30 +00:00
CheckCanceled, FileId, Canceled, SourceDatabase,
2019-01-26 08:17:05 +00:00
salsa,
2019-01-17 11:11:00 +00:00
};
2019-01-08 19:33:36 +00:00
2019-01-26 08:17:05 +00:00
use crate::{LineIndex, symbol_index::{self, SymbolsDatabase}};
2019-01-08 19:33:36 +00:00
2019-01-25 12:16:50 +00:00
#[salsa::database(
2019-01-26 08:20:30 +00:00
ra_db::SourceDatabaseStorage,
2019-01-25 20:27:16 +00:00
LineIndexDatabaseStorage,
symbol_index::SymbolsDatabaseStorage,
2019-02-01 10:33:41 +00:00
hir::db::HirDatabaseStorage,
hir::db::DefDatabaseStorage
2019-01-25 12:16:50 +00:00
)]
2019-01-08 19:33:36 +00:00
#[derive(Debug)]
pub(crate) struct RootDatabase {
runtime: salsa::Runtime<RootDatabase>,
2019-01-26 17:33:33 +00:00
pub(crate) last_gc: time::Instant,
pub(crate) last_gc_check: time::Instant,
2019-01-08 19:33:36 +00:00
}
impl salsa::Database for RootDatabase {
fn salsa_runtime(&self) -> &salsa::Runtime<RootDatabase> {
&self.runtime
}
2019-01-10 09:20:32 +00:00
fn on_propagated_panic(&self) -> ! {
Canceled::throw()
}
2019-01-08 19:33:36 +00:00
}
impl Default for RootDatabase {
fn default() -> RootDatabase {
let mut db = RootDatabase {
runtime: salsa::Runtime::default(),
2019-01-26 17:33:33 +00:00
last_gc: time::Instant::now(),
last_gc_check: time::Instant::now(),
2019-01-08 19:33:36 +00:00
};
2019-01-26 08:17:05 +00:00
db.set_crate_graph(Default::default());
db.set_local_roots(Default::default());
db.set_library_roots(Default::default());
2019-01-08 19:33:36 +00:00
db
}
}
impl salsa::ParallelDatabase for RootDatabase {
fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
salsa::Snapshot::new(RootDatabase {
runtime: self.runtime.snapshot(self),
2019-01-26 17:33:33 +00:00
last_gc: self.last_gc.clone(),
last_gc_check: self.last_gc_check.clone(),
2019-01-08 19:33:36 +00:00
})
}
}
2019-01-25 20:27:16 +00:00
#[salsa::query_group(LineIndexDatabaseStorage)]
2019-01-26 08:20:30 +00:00
pub(crate) trait LineIndexDatabase: ra_db::SourceDatabase + CheckCanceled {
2019-01-17 11:11:00 +00:00
fn line_index(&self, file_id: FileId) -> Arc<LineIndex>;
2019-01-08 19:33:36 +00:00
}
2019-01-26 08:20:30 +00:00
fn line_index(db: &impl ra_db::SourceDatabase, file_id: FileId) -> Arc<LineIndex> {
2019-01-08 19:33:36 +00:00
let text = db.file_text(file_id);
Arc::new(LineIndex::new(&*text))
}