encapsulate hir locations

This commit is contained in:
Aleksey Kladov 2019-01-24 12:41:08 +03:00
parent 6a0a4a564a
commit 9fe09db771
7 changed files with 45 additions and 74 deletions

View file

@ -1,7 +1,7 @@
use std::{fmt, sync::Arc};
use std::sync::Arc;
use ra_db::{
LocationIntener, BaseDatabase, FileId, Canceled,
BaseDatabase, FileId, Canceled,
salsa::{self, Database},
};
@ -10,21 +10,7 @@ use crate::{symbol_index, LineIndex};
#[derive(Debug)]
pub(crate) struct RootDatabase {
runtime: salsa::Runtime<RootDatabase>,
id_maps: Arc<IdMaps>,
}
#[derive(Default)]
struct IdMaps {
defs: LocationIntener<hir::DefLoc, hir::DefId>,
macros: LocationIntener<hir::MacroCallLoc, hir::MacroCallId>,
}
impl fmt::Debug for IdMaps {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("IdMaps")
.field("n_defs", &self.defs.len())
.finish()
}
interner: Arc<hir::HirInterner>,
}
impl salsa::Database for RootDatabase {
@ -40,7 +26,7 @@ impl Default for RootDatabase {
fn default() -> RootDatabase {
let mut db = RootDatabase {
runtime: salsa::Runtime::default(),
id_maps: Default::default(),
interner: Default::default(),
};
db.query_mut(ra_db::CrateGraphQuery)
.set((), Default::default());
@ -56,22 +42,16 @@ impl salsa::ParallelDatabase for RootDatabase {
fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
salsa::Snapshot::new(RootDatabase {
runtime: self.runtime.snapshot(self),
id_maps: self.id_maps.clone(),
interner: Arc::clone(&self.interner),
})
}
}
impl BaseDatabase for RootDatabase {}
impl AsRef<LocationIntener<hir::DefLoc, hir::DefId>> for RootDatabase {
fn as_ref(&self) -> &LocationIntener<hir::DefLoc, hir::DefId> {
&self.id_maps.defs
}
}
impl AsRef<LocationIntener<hir::MacroCallLoc, hir::MacroCallId>> for RootDatabase {
fn as_ref(&self) -> &LocationIntener<hir::MacroCallLoc, hir::MacroCallId> {
&self.id_maps.macros
impl AsRef<hir::HirInterner> for RootDatabase {
fn as_ref(&self) -> &hir::HirInterner {
&self.interner
}
}

View file

@ -1,5 +1,5 @@
use ra_db::{
LocationIntener, SourceFileQuery,
SourceFileQuery,
salsa::{Database, debug::DebugQueryTable},
};
@ -8,7 +8,7 @@ use crate::db::RootDatabase;
pub(crate) fn status(db: &RootDatabase) -> String {
let n_parsed_files = db.query(SourceFileQuery).keys::<Vec<_>>().len();
let n_defs = {
let interner: &LocationIntener<hir::DefLoc, hir::DefId> = db.as_ref();
let interner: &hir::HirInterner = db.as_ref();
interner.len()
};
format!("#n_parsed_files {}\n#n_defs {}\n", n_parsed_files, n_defs)