rust-analyzer/crates/libanalysis/src/db/mod.rs

110 lines
3.1 KiB
Rust
Raw Normal View History

2018-09-15 14:21:47 +00:00
mod imp;
2018-09-13 19:58:36 +00:00
use std::{
sync::Arc,
};
use im;
use salsa;
2018-09-15 14:21:47 +00:00
use {FileId, imp::FileResolverImp};
2018-09-13 19:58:36 +00:00
2018-09-15 14:21:47 +00:00
#[derive(Debug, Default, Clone)]
2018-09-13 19:58:36 +00:00
pub(crate) struct State {
2018-09-15 14:21:47 +00:00
pub(crate) file_map: im::HashMap<FileId, Arc<String>>,
pub(crate) file_resolver: FileResolverImp
2018-09-13 19:58:36 +00:00
}
2018-09-15 14:21:47 +00:00
#[derive(Debug)]
2018-09-13 19:58:36 +00:00
pub(crate) struct Db {
2018-09-15 14:21:47 +00:00
imp: imp::Db,
2018-09-13 19:58:36 +00:00
}
2018-09-15 14:21:47 +00:00
#[derive(Clone, Copy)]
pub(crate) struct QueryCtx<'a> {
imp: &'a salsa::QueryCtx<State, imp::Data>,
2018-09-13 19:58:36 +00:00
}
2018-09-15 14:21:47 +00:00
pub(crate) struct Query<T, R>(pub(crate) u16, pub(crate) fn(QueryCtx, &T) -> R);
pub(crate) struct QueryRegistry {
imp: imp::QueryRegistry,
2018-09-13 19:58:36 +00:00
}
impl Db {
2018-09-15 10:55:34 +00:00
pub(crate) fn new() -> Db {
2018-09-15 14:21:47 +00:00
let reg = QueryRegistry::new();
Db { imp: imp::Db::new(reg.imp) }
2018-09-13 19:58:36 +00:00
}
pub(crate) fn state(&self) -> &State {
2018-09-15 14:21:47 +00:00
self.imp.imp.ground_data()
2018-09-13 19:58:36 +00:00
}
2018-09-15 14:21:47 +00:00
pub(crate) fn with_changes(&self, new_state: State, changed_files: &[FileId], resolver_changed: bool) -> Db {
Db { imp: self.imp.with_changes(new_state, changed_files, resolver_changed) }
2018-09-13 19:58:36 +00:00
}
2018-09-15 14:21:47 +00:00
pub(crate) fn make_query<F: FnOnce(QueryCtx) -> R, R>(&self, f: F) -> R {
let ctx = QueryCtx { imp: &self.imp.imp.query_ctx() };
f(ctx)
}
pub(crate) fn trace_query<F: FnOnce(QueryCtx) -> R, R>(&self, f: F) -> (R, Vec<&'static str>) {
let ctx = QueryCtx { imp: &self.imp.imp.query_ctx() };
let res = f(ctx);
let trace = self.imp.extract_trace(ctx.imp);
(res, trace)
2018-09-13 19:58:36 +00:00
}
}
impl<'a> QueryCtx<'a> {
2018-09-15 14:21:47 +00:00
pub(crate) fn get<Q: imp::EvalQuery>(&self, q: Q, params: Q::Params) -> Arc<Q::Output> {
q.get(self, params)
2018-09-13 19:58:36 +00:00
}
}
2018-09-15 14:21:47 +00:00
pub(crate) fn file_text(ctx: QueryCtx, file_id: FileId) -> Arc<String> {
imp::file_text(ctx, file_id)
2018-09-13 19:58:36 +00:00
}
2018-09-15 14:21:47 +00:00
pub(crate) fn file_set(ctx: QueryCtx) -> Arc<(Vec<FileId>, FileResolverImp)> {
imp::file_set(ctx)
2018-09-13 19:58:36 +00:00
}
2018-09-15 14:21:47 +00:00
pub(crate) use self::queries::file_syntax;
2018-09-13 19:58:36 +00:00
2018-09-15 14:21:47 +00:00
mod queries {
2018-09-15 17:29:22 +00:00
use std::sync::Arc;
2018-09-15 14:21:47 +00:00
use libsyntax2::File;
2018-09-15 17:29:22 +00:00
use libeditor::LineIndex;
2018-09-15 14:21:47 +00:00
use {FileId};
use super::{Query, QueryCtx, QueryRegistry, file_text};
2018-09-13 19:58:36 +00:00
2018-09-15 14:21:47 +00:00
pub(crate) fn register_queries(reg: &mut QueryRegistry) {
2018-09-15 17:29:22 +00:00
reg.add(FILE_SYNTAX, "FILE_SYNTAX");
reg.add(FILE_LINES, "FILE_LINES");
2018-09-15 14:21:47 +00:00
}
2018-09-13 19:58:36 +00:00
2018-09-15 14:21:47 +00:00
pub(crate) fn file_syntax(ctx: QueryCtx, file_id: FileId) -> File {
(&*ctx.get(FILE_SYNTAX, file_id)).clone()
2018-09-13 19:58:36 +00:00
}
2018-09-15 17:29:22 +00:00
pub(crate) fn file_lines(ctx: QueryCtx, file_id: FileId) -> Arc<LineIndex> {
ctx.get(FILE_LINES, file_id)
}
2018-09-15 14:21:47 +00:00
pub(super) const FILE_SYNTAX: Query<FileId, File> = Query(16, |ctx, file_id: &FileId| {
let text = file_text(ctx, *file_id);
File::parse(&*text)
});
2018-09-15 17:29:22 +00:00
pub(super) const FILE_LINES: Query<FileId, LineIndex> = Query(17, |ctx, file_id: &FileId| {
let text = file_text(ctx, *file_id);
LineIndex::new(&*text)
});
2018-09-13 19:58:36 +00:00
}
2018-09-15 14:21:47 +00:00
impl QueryRegistry {
fn new() -> QueryRegistry {
let mut reg = QueryRegistry { imp: imp::QueryRegistry::new() };
queries::register_queries(&mut reg);
::module_map_db::register_queries(&mut reg);
reg
}
pub(crate) fn add<Q: imp::EvalQuery>(&mut self, q: Q, name: &'static str) {
self.imp.add(q, name)
}
2018-09-13 19:58:36 +00:00
}