2018-10-20 19:29:26 +00:00
|
|
|
use std::{
|
|
|
|
fmt,
|
|
|
|
hash::{Hash, Hasher},
|
|
|
|
sync::Arc,
|
2018-10-07 10:18:25 +00:00
|
|
|
};
|
2018-10-20 19:29:26 +00:00
|
|
|
|
2018-10-15 21:44:23 +00:00
|
|
|
use ra_editor::LineIndex;
|
|
|
|
use ra_syntax::File;
|
|
|
|
use rustc_hash::FxHashSet;
|
|
|
|
use salsa;
|
|
|
|
|
2018-10-20 19:29:26 +00:00
|
|
|
use crate::{
|
2018-10-20 19:35:55 +00:00
|
|
|
db,
|
|
|
|
Cancelable, Canceled,
|
2018-10-20 19:29:26 +00:00
|
|
|
module_map::{ModuleDescriptorQuery, ModuleTreeQuery, ModulesDatabase},
|
|
|
|
symbol_index::SymbolIndex,
|
|
|
|
FileId, FileResolverImp,
|
2018-10-15 21:44:23 +00:00
|
|
|
};
|
2018-09-13 19:58:36 +00:00
|
|
|
|
2018-10-07 10:18:25 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub(crate) struct RootDatabase {
|
2018-10-20 15:43:02 +00:00
|
|
|
runtime: salsa::Runtime<RootDatabase>,
|
2018-09-13 19:58:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-07 10:18:25 +00:00
|
|
|
impl fmt::Debug for RootDatabase {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt.write_str("RootDatabase { ... }")
|
|
|
|
}
|
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-10-20 19:35:55 +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-15 19:29:24 +00: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 10:18:25 +00:00
|
|
|
salsa::database_storage! {
|
|
|
|
pub(crate) struct RootDatabaseStorage for RootDatabase {
|
|
|
|
impl FilesDatabase {
|
|
|
|
fn file_text() for FileTextQuery;
|
|
|
|
fn file_set() for FileSetQuery;
|
|
|
|
}
|
|
|
|
impl SyntaxDatabase {
|
|
|
|
fn file_syntax() for FileSyntaxQuery;
|
|
|
|
fn file_lines() for FileLinesQuery;
|
|
|
|
fn file_symbols() for FileSymbolsQuery;
|
|
|
|
}
|
2018-10-08 10:18:47 +00:00
|
|
|
impl ModulesDatabase {
|
|
|
|
fn module_tree() for ModuleTreeQuery;
|
|
|
|
fn module_descriptor() for ModuleDescriptorQuery;
|
|
|
|
}
|
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! {
|
|
|
|
pub(crate) trait FilesDatabase: salsa::Database {
|
|
|
|
fn file_text(file_id: FileId) -> Arc<String> {
|
|
|
|
type FileTextQuery;
|
|
|
|
storage input;
|
|
|
|
}
|
2018-10-20 15:43:02 +00:00
|
|
|
fn file_set() -> Arc<FileSet> {
|
2018-10-07 10:18:25 +00:00
|
|
|
type FileSetQuery;
|
|
|
|
storage input;
|
|
|
|
}
|
2018-09-15 20:19:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 23:15:22 +00:00
|
|
|
#[derive(Default, Debug, Eq)]
|
2018-10-07 10:18:25 +00:00
|
|
|
pub(crate) struct FileSet {
|
2018-10-15 18:25:54 +00:00
|
|
|
pub(crate) files: FxHashSet<FileId>,
|
2018-10-07 10:18:25 +00:00
|
|
|
pub(crate) resolver: FileResolverImp,
|
2018-09-13 19:58:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 23:15:22 +00:00
|
|
|
impl PartialEq for FileSet {
|
|
|
|
fn eq(&self, other: &FileSet) -> bool {
|
|
|
|
self.files == other.files && self.resolver == other.resolver
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-07 10:18:25 +00:00
|
|
|
impl Hash for FileSet {
|
|
|
|
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
|
|
|
let mut files = self.files.iter().cloned().collect::<Vec<_>>();
|
|
|
|
files.sort();
|
|
|
|
files.hash(hasher);
|
2018-09-15 14:21:47 +00:00
|
|
|
}
|
2018-10-07 10:18:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
salsa::query_group! {
|
|
|
|
pub(crate) trait SyntaxDatabase: FilesDatabase {
|
|
|
|
fn file_syntax(file_id: FileId) -> File {
|
|
|
|
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-09-15 14:21:47 +00:00
|
|
|
}
|
2018-09-13 19:58:36 +00:00
|
|
|
}
|
2018-10-07 10:18:25 +00: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 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
|
|
|
}
|