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

131 lines
4.1 KiB
Rust
Raw Normal View History

2018-10-31 20:41:43 +00:00
use std::sync::Arc;
2018-10-20 19:29:26 +00:00
use ra_editor::LineIndex;
2018-11-07 15:32:33 +00:00
use ra_syntax::{SourceFileNode, SyntaxNode};
2018-11-04 11:09:21 +00:00
use salsa::{self, Database};
2018-10-20 19:29:26 +00:00
use crate::{
2018-10-20 19:35:55 +00:00
db,
2018-10-31 07:56:31 +00:00
descriptors::{
2018-10-31 20:41:43 +00:00
DescriptorDatabase, FnScopesQuery, FnSyntaxQuery, ModuleScopeQuery, ModuleTreeQuery,
2018-11-21 08:16:46 +00:00
SubmodulesQuery, ItemMapQuery, InputModuleItemsQuery,
2018-10-31 07:56:31 +00:00
},
2018-10-20 19:29:26 +00:00
symbol_index::SymbolIndex,
2018-11-01 10:15:11 +00:00
syntax_ptr::SyntaxPtr,
2018-11-18 13:21:23 +00:00
loc2id::{IdMaps, IdDatabase},
2018-10-31 20:41:43 +00:00
Cancelable, Canceled, FileId,
};
2018-09-13 19:58:36 +00:00
2018-11-04 11:09:21 +00:00
#[derive(Debug)]
2018-10-07 10:18:25 +00:00
pub(crate) struct RootDatabase {
2018-10-20 15:43:02 +00:00
runtime: salsa::Runtime<RootDatabase>,
2018-11-18 13:21:23 +00:00
id_maps: IdMaps,
2018-09-13 19:58:36 +00:00
}
2018-10-07 10:18:25 +00:00
impl salsa::Database for RootDatabase {
2018-10-20 15:43:02 +00:00
fn salsa_runtime(&self) -> &salsa::Runtime<RootDatabase> {
2018-10-07 10:18:25 +00:00
&self.runtime
}
2018-09-13 19:58:36 +00:00
}
2018-11-04 11:09:21 +00:00
impl Default for RootDatabase {
fn default() -> RootDatabase {
let mut db = RootDatabase {
2018-11-18 13:21:23 +00:00
runtime: salsa::Runtime::default(),
id_maps: IdMaps::default(),
2018-11-04 11:09:21 +00: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-25 14:22:31 +00: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 19:35:55 +00:00
}
2018-10-15 19:29:24 +00:00
impl salsa::ParallelDatabase for RootDatabase {
2018-11-01 12:29:23 +00:00
fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
salsa::Snapshot::new(RootDatabase {
runtime: self.runtime.snapshot(self),
2018-11-18 13:21:23 +00:00
id_maps: self.id_maps.clone(),
2018-11-01 12:29:23 +00:00
})
2018-10-15 19:29:24 +00:00
}
}
2018-11-18 13:21:23 +00:00
impl IdDatabase for RootDatabase {
fn id_maps(&self) -> &IdMaps {
&self.id_maps
}
}
2018-10-07 10:18:25 +00:00
salsa::database_storage! {
pub(crate) struct RootDatabaseStorage for RootDatabase {
2018-10-25 14:52:50 +00: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 18:05:14 +00:00
fn libraries() for crate::input::LibrariesQuery;
2018-10-25 14:52:50 +00:00
fn library_symbols() for crate::input::LibrarySymbolsQuery;
fn crate_graph() for crate::input::CrateGraphQuery;
2018-10-07 10:18:25 +00:00
}
impl SyntaxDatabase {
fn file_syntax() for FileSyntaxQuery;
fn file_lines() for FileLinesQuery;
fn file_symbols() for FileSymbolsQuery;
2018-11-01 10:15:11 +00:00
fn resolve_syntax_ptr() for ResolveSyntaxPtrQuery;
2018-10-07 10:18:25 +00:00
}
2018-10-31 07:56:31 +00:00
impl DescriptorDatabase {
2018-10-08 10:18:47 +00:00
fn module_tree() for ModuleTreeQuery;
2018-10-31 07:56:31 +00:00
fn fn_scopes() for FnScopesQuery;
2018-11-21 08:16:46 +00:00
fn _input_module_items() for InputModuleItemsQuery;
2018-11-20 21:39:50 +00:00
fn _item_map() for ItemMapQuery;
fn _module_scope() for ModuleScopeQuery;
fn _fn_syntax() for FnSyntaxQuery;
fn _submodules() for SubmodulesQuery;
}
2018-10-07 10:18:25 +00:00
}
2018-09-13 19:58:36 +00:00
}
2018-10-07 10:18:25 +00:00
salsa::query_group! {
2018-10-25 14:52:50 +00:00
pub(crate) trait SyntaxDatabase: crate::input::FilesDatabase {
2018-11-07 15:32:33 +00:00
fn file_syntax(file_id: FileId) -> SourceFileNode {
2018-10-07 10:18:25 +00:00
type FileSyntaxQuery;
}
fn file_lines(file_id: FileId) -> Arc<LineIndex> {
type FileLinesQuery;
}
2018-10-20 19:29:26 +00:00
fn file_symbols(file_id: FileId) -> Cancelable<Arc<SymbolIndex>> {
2018-10-07 10:18:25 +00:00
type FileSymbolsQuery;
}
2018-11-01 10:15:11 +00:00
fn resolve_syntax_ptr(ptr: SyntaxPtr) -> SyntaxNode {
type ResolveSyntaxPtrQuery;
// Don't retain syntax trees in memory
storage volatile;
use fn crate::syntax_ptr::resolve_syntax_ptr;
}
2018-09-15 14:21:47 +00:00
}
2018-09-13 19:58:36 +00:00
}
2018-10-07 10:18:25 +00:00
2018-11-07 15:32:33 +00:00
fn file_syntax(db: &impl SyntaxDatabase, file_id: FileId) -> SourceFileNode {
2018-10-07 10:18:25 +00:00
let text = db.file_text(file_id);
2018-11-07 15:32:33 +00:00
SourceFileNode::parse(&*text)
2018-10-07 10:18:25 +00:00
}
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 19:29:26 +00:00
fn file_symbols(db: &impl SyntaxDatabase, file_id: FileId) -> Cancelable<Arc<SymbolIndex>> {
2018-10-20 19:35:55 +00:00
db::check_canceled(db)?;
2018-10-07 10:18:25 +00:00
let syntax = db.file_syntax(file_id);
2018-10-20 19:29:26 +00:00
Ok(Arc::new(SymbolIndex::for_file(file_id, syntax)))
2018-10-07 10:18:25 +00:00
}