rust/crates/ra_analysis/src/db.rs

99 lines
2.7 KiB
Rust
Raw Normal View History

2018-10-20 14:29:26 -05:00
use std::{
sync::Arc,
2018-10-07 05:18:25 -05:00
};
2018-10-20 14:29:26 -05:00
use ra_editor::LineIndex;
use ra_syntax::File;
use salsa;
2018-10-20 14:29:26 -05:00
use crate::{
2018-10-20 14:35:55 -05:00
db,
Cancelable, Canceled,
descriptors::module::{SubmodulesQuery, ModuleTreeQuery, ModulesDatabase},
2018-10-20 14:29:26 -05:00
symbol_index::SymbolIndex,
FileId,
};
2018-09-13 14:58:36 -05:00
2018-10-25 08:03:49 -05:00
#[derive(Default, Debug)]
2018-10-07 05:18:25 -05:00
pub(crate) struct RootDatabase {
2018-10-20 10:43:02 -05:00
runtime: salsa::Runtime<RootDatabase>,
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-09-13 14:58:36 -05:00
}
2018-10-25 09:22:31 -05:00
pub(crate) fn check_canceled(db: &impl salsa::Database) -> Cancelable<()> {
if db.salsa_runtime().is_current_revision_canceled() {
Err(Canceled)
} else {
Ok(())
}
2018-10-20 14:35:55 -05:00
}
2018-10-15 14:29:24 -05:00
impl salsa::ParallelDatabase for RootDatabase {
fn fork(&self) -> Self {
RootDatabase {
runtime: self.runtime.fork(),
}
}
}
impl Clone for RootDatabase {
fn clone(&self) -> RootDatabase {
salsa::ParallelDatabase::fork(self)
}
}
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;
fn libraries() for crate::input::LibrarieseQuery;
fn library_symbols() for crate::input::LibrarySymbolsQuery;
fn crate_graph() for crate::input::CrateGraphQuery;
2018-10-07 05:18:25 -05:00
}
impl SyntaxDatabase {
fn file_syntax() for FileSyntaxQuery;
fn file_lines() for FileLinesQuery;
fn file_symbols() for FileSymbolsQuery;
}
2018-10-08 05:18:47 -05:00
impl ModulesDatabase {
fn module_tree() for ModuleTreeQuery;
fn module_descriptor() for SubmodulesQuery;
2018-10-08 05:18:47 -05:00
}
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-10-25 09:52:50 -05:00
pub(crate) trait SyntaxDatabase: crate::input::FilesDatabase {
2018-10-07 05:18:25 -05:00
fn file_syntax(file_id: FileId) -> File {
type FileSyntaxQuery;
}
fn file_lines(file_id: FileId) -> Arc<LineIndex> {
type FileLinesQuery;
}
2018-10-20 14:29:26 -05:00
fn file_symbols(file_id: FileId) -> Cancelable<Arc<SymbolIndex>> {
2018-10-07 05:18:25 -05:00
type FileSymbolsQuery;
}
2018-09-15 09:21:47 -05:00
}
2018-09-13 14:58:36 -05:00
}
2018-10-07 05:18:25 -05:00
fn file_syntax(db: &impl SyntaxDatabase, file_id: FileId) -> File {
let text = db.file_text(file_id);
File::parse(&*text)
}
fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<LineIndex> {
let text = db.file_text(file_id);
Arc::new(LineIndex::new(&*text))
}
2018-10-20 14:29:26 -05:00
fn file_symbols(db: &impl SyntaxDatabase, file_id: FileId) -> Cancelable<Arc<SymbolIndex>> {
2018-10-20 14:35:55 -05:00
db::check_canceled(db)?;
2018-10-07 05:18:25 -05:00
let syntax = db.file_syntax(file_id);
2018-10-20 14:29:26 -05:00
Ok(Arc::new(SymbolIndex::for_file(file_id, syntax)))
2018-10-07 05:18:25 -05:00
}