kill old module_map

This commit is contained in:
Aleksey Kladov 2018-09-15 23:19:41 +03:00
parent 3ebeb0db8d
commit e69ff21207
9 changed files with 251 additions and 552 deletions

View file

@ -122,9 +122,6 @@ impl QueryRegistry {
let config = config.with_query(id, q.f()); let config = config.with_query(id, q.f());
self.config= Some(config); self.config= Some(config);
} }
pub(super) fn finish(mut self) -> salsa::QueryConfig<State, Data> {
self.config.take().unwrap()
}
} }
fn hash<T: Hash>(x: &T) -> u64 { fn hash<T: Hash>(x: &T) -> u64 {

View file

@ -29,6 +29,12 @@ pub(crate) struct QueryRegistry {
imp: imp::QueryRegistry, imp: imp::QueryRegistry,
} }
impl Default for Db {
fn default() -> Db {
Db::new()
}
}
impl Db { impl Db {
pub(crate) fn new() -> Db { pub(crate) fn new() -> Db {
let reg = QueryRegistry::new(); let reg = QueryRegistry::new();
@ -44,6 +50,7 @@ impl Db {
let ctx = QueryCtx { imp: &self.imp.imp.query_ctx() }; let ctx = QueryCtx { imp: &self.imp.imp.query_ctx() };
f(ctx) f(ctx)
} }
#[allow(unused)]
pub(crate) fn trace_query<F: FnOnce(QueryCtx) -> R, R>(&self, f: F) -> (R, Vec<&'static str>) { 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 ctx = QueryCtx { imp: &self.imp.imp.query_ctx() };
let res = f(ctx); let res = f(ctx);
@ -65,42 +72,11 @@ pub(crate) fn file_text(ctx: QueryCtx, file_id: FileId) -> Arc<String> {
pub(crate) fn file_set(ctx: QueryCtx) -> Arc<(Vec<FileId>, FileResolverImp)> { pub(crate) fn file_set(ctx: QueryCtx) -> Arc<(Vec<FileId>, FileResolverImp)> {
imp::file_set(ctx) imp::file_set(ctx)
} }
pub(crate) use self::queries::file_syntax;
mod queries {
use std::sync::Arc;
use libsyntax2::File;
use libeditor::LineIndex;
use {FileId};
use super::{Query, QueryCtx, QueryRegistry, file_text};
pub(crate) fn register_queries(reg: &mut QueryRegistry) {
reg.add(FILE_SYNTAX, "FILE_SYNTAX");
reg.add(FILE_LINES, "FILE_LINES");
}
pub(crate) fn file_syntax(ctx: QueryCtx, file_id: FileId) -> File {
(&*ctx.get(FILE_SYNTAX, file_id)).clone()
}
pub(crate) fn file_lines(ctx: QueryCtx, file_id: FileId) -> Arc<LineIndex> {
ctx.get(FILE_LINES, file_id)
}
pub(super) const FILE_SYNTAX: Query<FileId, File> = Query(16, |ctx, file_id: &FileId| {
let text = file_text(ctx, *file_id);
File::parse(&*text)
});
pub(super) const FILE_LINES: Query<FileId, LineIndex> = Query(17, |ctx, file_id: &FileId| {
let text = file_text(ctx, *file_id);
LineIndex::new(&*text)
});
}
impl QueryRegistry { impl QueryRegistry {
fn new() -> QueryRegistry { fn new() -> QueryRegistry {
let mut reg = QueryRegistry { imp: imp::QueryRegistry::new() }; let mut reg = QueryRegistry { imp: imp::QueryRegistry::new() };
queries::register_queries(&mut reg); ::queries::register_queries(&mut reg);
::module_map_db::register_queries(&mut reg); ::module_map::register_queries(&mut reg);
reg reg
} }
pub(crate) fn add<Q: imp::EvalQuery>(&mut self, q: Q, name: &'static str) { pub(crate) fn add<Q: imp::EvalQuery>(&mut self, q: Q, name: &'static str) {

View file

@ -140,7 +140,7 @@ impl ModuleTreeDescriptor {
.links .links
.iter() .iter()
.filter(|it| it.name(self) == name) .filter(|it| it.name(self) == name)
.map(|link| link.owner(self)) .flat_map(|link| link.points_to(self).iter().map(|&node| self.node(node).file_id))
.collect() .collect()
} }
pub(crate) fn problems<'a, 'b>(&'b self, file_id: FileId, root: ast::Root<'a>) -> Vec<(ast::Name<'a>, &'b Problem)> { pub(crate) fn problems<'a, 'b>(&'b self, file_id: FileId, root: ast::Root<'a>) -> Vec<(ast::Name<'a>, &'b Problem)> {
@ -172,6 +172,9 @@ impl Link {
let owner = tree.link(self).owner; let owner = tree.link(self).owner;
tree.node(owner).file_id tree.node(owner).file_id
} }
fn points_to(self, tree: &ModuleTreeDescriptor) -> &[Node] {
&tree.link(self).points_to
}
pub(crate) fn bind_source<'a>(self, tree: &ModuleTreeDescriptor, root: ast::Root<'a>) -> ast::Module<'a> { pub(crate) fn bind_source<'a>(self, tree: &ModuleTreeDescriptor, root: ast::Root<'a>) -> ast::Module<'a> {
modules(root) modules(root)
.filter(|(name, _)| name == &tree.link(self).name) .filter(|(name, _)| name == &tree.link(self).name)

View file

@ -5,6 +5,7 @@ use std::{
}, },
fmt, fmt,
collections::{HashSet, VecDeque}, collections::{HashSet, VecDeque},
iter,
}; };
use relative_path::RelativePath; use relative_path::RelativePath;
@ -75,14 +76,12 @@ impl AnalysisHostImpl {
} }
pub fn change_files(&mut self, changes: &mut dyn Iterator<Item=(FileId, Option<String>)>) { pub fn change_files(&mut self, changes: &mut dyn Iterator<Item=(FileId, Option<String>)>) {
let data = self.data_mut(); let data = self.data_mut();
for (file_id, text) in changes { data.root = Arc::new(data.root.apply_changes(changes, None));
data.root.update(file_id, text);
}
} }
pub fn set_file_resolver(&mut self, resolver: FileResolverImp) { pub fn set_file_resolver(&mut self, resolver: FileResolverImp) {
let data = self.data_mut(); let data = self.data_mut();
data.file_resolver = resolver.clone(); data.file_resolver = resolver.clone();
data.root.set_file_resolver(resolver); data.root = Arc::new(data.root.apply_changes(&mut iter::empty(), Some(resolver)));
} }
pub fn set_crate_graph(&mut self, graph: CrateGraph) { pub fn set_crate_graph(&mut self, graph: CrateGraph) {
let mut visited = HashSet::new(); let mut visited = HashSet::new();
@ -124,18 +123,17 @@ impl Clone for AnalysisImpl {
impl AnalysisImpl { impl AnalysisImpl {
fn root(&self, file_id: FileId) -> &SourceRoot { fn root(&self, file_id: FileId) -> &SourceRoot {
if self.data.root.contains(file_id) { if self.data.root.contains(file_id) {
return &self.data.root; return &*self.data.root;
} }
&**self.data.libs.iter().find(|it| it.contains(file_id)).unwrap() &**self.data.libs.iter().find(|it| it.contains(file_id)).unwrap()
} }
pub fn file_syntax(&self, file_id: FileId) -> &File { pub fn file_syntax(&self, file_id: FileId) -> File {
self.root(file_id).syntax(file_id) self.root(file_id).syntax(file_id)
} }
pub fn file_line_index(&self, file_id: FileId) -> &LineIndex { pub fn file_line_index(&self, file_id: FileId) -> Arc<LineIndex> {
self.root(file_id).lines(file_id) self.root(file_id).lines(file_id)
} }
pub fn world_symbols(&self, query: Query, token: &JobToken) -> Vec<(FileId, FileSymbol)> { pub fn world_symbols(&self, query: Query, token: &JobToken) -> Vec<(FileId, FileSymbol)> {
self.reindex();
let mut buf = Vec::new(); let mut buf = Vec::new();
if query.libs { if query.libs {
self.data.libs.iter() self.data.libs.iter()
@ -308,19 +306,13 @@ impl AnalysisImpl {
}; };
module_tree.child_module_by_name(file_id, name.as_str()) module_tree.child_module_by_name(file_id, name.as_str())
} }
fn reindex(&self) {
if self.needs_reindex.compare_and_swap(true, false, SeqCst) {
self.data.root.reindex();
}
}
} }
#[derive(Default, Clone, Debug)] #[derive(Default, Clone, Debug)]
struct WorldData { struct WorldData {
file_resolver: FileResolverImp, file_resolver: FileResolverImp,
crate_graph: CrateGraph, crate_graph: CrateGraph,
root: WritableSourceRoot, root: Arc<WritableSourceRoot>,
libs: Vec<Arc<ReadonlySourceRoot>>, libs: Vec<Arc<ReadonlySourceRoot>>,
} }

View file

@ -14,11 +14,11 @@ extern crate salsa;
mod symbol_index; mod symbol_index;
mod module_map; mod module_map;
pub(crate) mod module_map_db;
mod imp; mod imp;
mod job; mod job;
mod roots; mod roots;
mod db; mod db;
mod queries;
mod descriptors; mod descriptors;
use std::{ use std::{
@ -166,8 +166,8 @@ impl Analysis {
pub fn file_syntax(&self, file_id: FileId) -> File { pub fn file_syntax(&self, file_id: FileId) -> File {
self.imp.file_syntax(file_id).clone() self.imp.file_syntax(file_id).clone()
} }
pub fn file_line_index(&self, file_id: FileId) -> LineIndex { pub fn file_line_index(&self, file_id: FileId) -> Arc<LineIndex> {
self.imp.file_line_index(file_id).clone() self.imp.file_line_index(file_id)
} }
pub fn extend_selection(&self, file: &File, range: TextRange) -> TextRange { pub fn extend_selection(&self, file: &File, range: TextRange) -> TextRange {
libeditor::extend_selection(file, range).unwrap_or(range) libeditor::extend_selection(file, range).unwrap_or(range)
@ -177,19 +177,19 @@ impl Analysis {
} }
pub fn syntax_tree(&self, file_id: FileId) -> String { pub fn syntax_tree(&self, file_id: FileId) -> String {
let file = self.imp.file_syntax(file_id); let file = self.imp.file_syntax(file_id);
libeditor::syntax_tree(file) libeditor::syntax_tree(&file)
} }
pub fn join_lines(&self, file_id: FileId, range: TextRange) -> SourceChange { pub fn join_lines(&self, file_id: FileId, range: TextRange) -> SourceChange {
let file = self.imp.file_syntax(file_id); let file = self.imp.file_syntax(file_id);
SourceChange::from_local_edit(file_id, "join lines", libeditor::join_lines(file, range)) SourceChange::from_local_edit(file_id, "join lines", libeditor::join_lines(&file, range))
} }
pub fn on_eq_typed(&self, file_id: FileId, offset: TextUnit) -> Option<SourceChange> { pub fn on_eq_typed(&self, file_id: FileId, offset: TextUnit) -> Option<SourceChange> {
let file = self.imp.file_syntax(file_id); let file = self.imp.file_syntax(file_id);
Some(SourceChange::from_local_edit(file_id, "add semicolon", libeditor::on_eq_typed(file, offset)?)) Some(SourceChange::from_local_edit(file_id, "add semicolon", libeditor::on_eq_typed(&file, offset)?))
} }
pub fn file_structure(&self, file_id: FileId) -> Vec<StructureNode> { pub fn file_structure(&self, file_id: FileId) -> Vec<StructureNode> {
let file = self.imp.file_syntax(file_id); let file = self.imp.file_syntax(file_id);
libeditor::file_structure(file) libeditor::file_structure(&file)
} }
pub fn symbol_search(&self, query: Query, token: &JobToken) -> Vec<(FileId, FileSymbol)> { pub fn symbol_search(&self, query: Query, token: &JobToken) -> Vec<(FileId, FileSymbol)> {
self.imp.world_symbols(query, token) self.imp.world_symbols(query, token)
@ -208,15 +208,15 @@ impl Analysis {
} }
pub fn runnables(&self, file_id: FileId) -> Vec<Runnable> { pub fn runnables(&self, file_id: FileId) -> Vec<Runnable> {
let file = self.imp.file_syntax(file_id); let file = self.imp.file_syntax(file_id);
libeditor::runnables(file) libeditor::runnables(&file)
} }
pub fn highlight(&self, file_id: FileId) -> Vec<HighlightedRange> { pub fn highlight(&self, file_id: FileId) -> Vec<HighlightedRange> {
let file = self.imp.file_syntax(file_id); let file = self.imp.file_syntax(file_id);
libeditor::highlight(file) libeditor::highlight(&file)
} }
pub fn completions(&self, file_id: FileId, offset: TextUnit) -> Option<Vec<CompletionItem>> { pub fn completions(&self, file_id: FileId, offset: TextUnit) -> Option<Vec<CompletionItem>> {
let file = self.imp.file_syntax(file_id); let file = self.imp.file_syntax(file_id);
libeditor::scope_completion(file, offset) libeditor::scope_completion(&file, offset)
} }
pub fn assists(&self, file_id: FileId, range: TextRange) -> Vec<SourceChange> { pub fn assists(&self, file_id: FileId, range: TextRange) -> Vec<SourceChange> {
self.imp.assists(file_id, range) self.imp.assists(file_id, range)

View file

@ -1,281 +1,157 @@
use relative_path::RelativePathBuf; use std::sync::Arc;
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard}; use {
use libsyntax2::{ FileId,
File, db::{
ast::{self, AstNode, NameOwner}, Query, QueryRegistry, QueryCtx,
SyntaxNode, SmolStr, file_set
};
use {FileId, imp::FileResolverImp};
type SyntaxProvider<'a> = dyn Fn(FileId) -> &'a File + 'a;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct ModuleId(FileId);
#[derive(Debug, Default)]
pub struct ModuleMap {
state: RwLock<State>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChangeKind {
Delete, Insert, Update
}
impl Clone for ModuleMap {
fn clone(&self) -> ModuleMap {
let state = self.state.read().clone();
ModuleMap { state: RwLock::new(state) }
}
}
#[derive(Clone, Debug, Default)]
struct State {
file_resolver: FileResolverImp,
changes: Vec<(FileId, ChangeKind)>,
links: Vec<Link>,
}
#[derive(Clone, Debug)]
struct Link {
owner: ModuleId,
syntax: SyntaxNode,
points_to: Vec<ModuleId>,
problem: Option<Problem>,
}
#[derive(Clone, Debug)]
pub enum Problem {
UnresolvedModule {
candidate: RelativePathBuf,
}, },
NotDirOwner { queries::file_syntax,
move_to: RelativePathBuf, descriptors::{ModuleDescriptor, ModuleTreeDescriptor},
candidate: RelativePathBuf, };
}
pub(crate) fn register_queries(reg: &mut QueryRegistry) {
reg.add(MODULE_DESCR, "MODULE_DESCR");
reg.add(MODULE_TREE, "MODULE_TREE");
} }
impl ModuleMap { pub(crate) fn module_tree(ctx: QueryCtx) -> Arc<ModuleTreeDescriptor> {
pub fn new() -> ModuleMap { ctx.get(MODULE_TREE, ())
Default::default() }
const MODULE_DESCR: Query<FileId, ModuleDescriptor> = Query(30, |ctx, &file_id| {
let file = file_syntax(ctx, file_id);
ModuleDescriptor::new(file.ast())
});
const MODULE_TREE: Query<(), ModuleTreeDescriptor> = Query(31, |ctx, _| {
let file_set = file_set(ctx);
let mut files = Vec::new();
for &file_id in file_set.0.iter() {
let module_descr = ctx.get(MODULE_DESCR, file_id);
files.push((file_id, module_descr));
} }
pub fn update_file(&mut self, file_id: FileId, change_kind: ChangeKind) { ModuleTreeDescriptor::new(files.iter().map(|(file_id, descr)| (*file_id, &**descr)), &file_set.1)
self.state.get_mut().changes.push((file_id, change_kind)); });
}
pub(crate) fn set_file_resolver(&mut self, file_resolver: FileResolverImp) { #[cfg(test)]
self.state.get_mut().file_resolver = file_resolver; mod tests {
} use std::collections::HashMap;
pub fn module2file(&self, m: ModuleId) -> FileId { use im;
m.0 use relative_path::{RelativePath, RelativePathBuf};
} use {
pub fn file2module(&self, file_id: FileId) -> ModuleId { db::{Db},
ModuleId(file_id) imp::FileResolverImp,
} FileId, FileResolver,
pub fn child_module_by_name<'a>( };
&self, use super::*;
parent_mod: ModuleId,
child_mod: &str, #[derive(Debug)]
syntax_provider: &SyntaxProvider, struct FileMap(im::HashMap<FileId, RelativePathBuf>);
) -> Vec<ModuleId> {
self.links(syntax_provider) impl FileResolver for FileMap {
.links fn file_stem(&self, file_id: FileId) -> String {
.iter() self.0[&file_id].file_stem().unwrap().to_string()
.filter(|link| link.owner == parent_mod) }
.filter(|link| link.name() == child_mod) fn resolve(&self, file_id: FileId, rel: &RelativePath) -> Option<FileId> {
.filter_map(|it| it.points_to.first()) let path = self.0[&file_id].join(rel).normalize();
.map(|&it| it) self.0.iter()
.collect() .filter_map(|&(id, ref p)| Some(id).filter(|_| p == &path))
.next()
}
} }
pub fn parent_modules( struct Fixture {
&self, next_file_id: u32,
m: ModuleId, fm: im::HashMap<FileId, RelativePathBuf>,
syntax_provider: &SyntaxProvider, db: Db,
) -> Vec<(ModuleId, SmolStr, SyntaxNode)> {
let mut res = Vec::new();
self.for_each_parent_link(m, syntax_provider, |link| {
res.push(
(link.owner, link.name().clone(), link.syntax.clone())
)
});
res
} }
pub fn parent_module_ids( impl Fixture {
&self, fn new() -> Fixture {
m: ModuleId, Fixture {
syntax_provider: &SyntaxProvider, next_file_id: 1,
) -> Vec<ModuleId> { fm: im::HashMap::new(),
let mut res = Vec::new(); db: Db::new(),
self.for_each_parent_link(m, syntax_provider, |link| res.push(link.owner));
res
}
fn for_each_parent_link(
&self,
m: ModuleId,
syntax_provider: &SyntaxProvider,
f: impl FnMut(&Link)
) {
self.links(syntax_provider)
.links
.iter()
.filter(move |link| link.points_to.iter().any(|&it| it == m))
.for_each(f)
}
pub fn problems(
&self,
file: FileId,
syntax_provider: &SyntaxProvider,
mut cb: impl FnMut(ast::Name, &Problem),
) {
let module = self.file2module(file);
let links = self.links(syntax_provider);
links
.links
.iter()
.filter(|link| link.owner == module)
.filter_map(|link| {
let problem = link.problem.as_ref()?;
Some((link, problem))
})
.for_each(|(link, problem)| cb(link.name_node(), problem))
}
fn links(
&self,
syntax_provider: &SyntaxProvider,
) -> RwLockReadGuard<State> {
{
let guard = self.state.read();
if guard.changes.is_empty() {
return guard;
} }
} }
let mut guard = self.state.write(); fn add_file(&mut self, path: &str, text: &str) -> FileId {
if !guard.changes.is_empty() { assert!(path.starts_with("/"));
guard.apply_changes(syntax_provider); let file_id = FileId(self.next_file_id);
self.next_file_id += 1;
self.fm.insert(file_id, RelativePathBuf::from(&path[1..]));
let mut new_state = self.db.state().clone();
new_state.file_map.insert(file_id, Arc::new(text.to_string()));
new_state.file_resolver = FileResolverImp::new(
Arc::new(FileMap(self.fm.clone()))
);
self.db = self.db.with_changes(new_state, &[file_id], true);
file_id
} }
assert!(guard.changes.is_empty()); fn remove_file(&mut self, file_id: FileId) {
RwLockWriteGuard::downgrade(guard) self.fm.remove(&file_id);
} let mut new_state = self.db.state().clone();
} new_state.file_map.remove(&file_id);
new_state.file_resolver = FileResolverImp::new(
impl State { Arc::new(FileMap(self.fm.clone()))
pub fn apply_changes( );
&mut self, self.db = self.db.with_changes(new_state, &[file_id], true);
syntax_provider: &SyntaxProvider, }
) { fn change_file(&mut self, file_id: FileId, new_text: &str) {
let mut reresolve = false; let mut new_state = self.db.state().clone();
for (file_id, kind) in self.changes.drain(..) { new_state.file_map.insert(file_id, Arc::new(new_text.to_string()));
let mod_id = ModuleId(file_id); self.db = self.db.with_changes(new_state, &[file_id], false);
self.links.retain(|link| link.owner != mod_id); }
match kind { fn check_parent_modules(
ChangeKind::Delete => { &self,
for link in self.links.iter_mut() { file_id: FileId,
link.points_to.retain(|&x| x != mod_id); expected: &[FileId],
} queries: &[(&'static str, u64)]
} ) {
ChangeKind::Insert => { let (tree, events) = self.db.trace_query(|ctx| module_tree(ctx));
let file = syntax_provider(file_id); let actual = tree.parent_modules(file_id)
self.links.extend( .into_iter()
file .map(|link| link.owner(&tree))
.ast() .collect::<Vec<_>>();
.modules() assert_eq!(actual.as_slice(), expected);
.filter_map(|it| Link::new(mod_id, it)) let mut counts = HashMap::new();
); events.into_iter()
reresolve = true; .for_each(|event| *counts.entry(event).or_insert(0) += 1);
} for &(query_id, expected_count) in queries.iter() {
ChangeKind::Update => { let actual_count = *counts.get(&query_id).unwrap_or(&0);
let file = syntax_provider(file_id); assert_eq!(
let resolver = &self.file_resolver; actual_count,
self.links.extend( expected_count,
file "counts for {} differ",
.ast() query_id,
.modules() )
.filter_map(|it| Link::new(mod_id, it))
.map(|mut link| {
link.resolve(resolver);
link
})
);
}
}
}
if reresolve {
for link in self.links.iter_mut() {
link.resolve(&self.file_resolver)
} }
} }
} }
#[test]
fn test_parent_module() {
let mut f = Fixture::new();
let foo = f.add_file("/foo.rs", "");
f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 1)]);
let lib = f.add_file("/lib.rs", "mod foo;");
f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 1)]);
f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 0)]);
f.change_file(lib, "");
f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 1)]);
f.change_file(lib, "mod foo;");
f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 1)]);
f.change_file(lib, "mod bar;");
f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 1)]);
f.change_file(lib, "mod foo;");
f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 1)]);
f.remove_file(lib);
f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 0)]);
}
} }
impl Link {
fn new(owner: ModuleId, module: ast::Module) -> Option<Link> {
if module.name().is_none() {
return None;
}
let link = Link {
owner,
syntax: module.syntax().owned(),
points_to: Vec::new(),
problem: None,
};
Some(link)
}
fn name(&self) -> SmolStr {
self.name_node().text()
}
fn name_node(&self) -> ast::Name {
self.ast().name().unwrap()
}
fn ast(&self) -> ast::Module {
ast::Module::cast(self.syntax.borrowed())
.unwrap()
}
fn resolve(&mut self, file_resolver: &FileResolverImp) {
if !self.ast().has_semi() {
self.problem = None;
self.points_to = Vec::new();
return;
}
let (points_to, problem) = resolve_submodule(self.owner.0, &self.name(), file_resolver);
self.problem = problem;
self.points_to = points_to.into_iter().map(ModuleId).collect();
}
}
pub(crate) fn resolve_submodule(file_id: FileId, name: &SmolStr, file_resolver: &FileResolverImp) -> (Vec<FileId>, Option<Problem>) {
let mod_name = file_resolver.file_stem(file_id);
let is_dir_owner =
mod_name == "mod" || mod_name == "lib" || mod_name == "main";
let file_mod = RelativePathBuf::from(format!("../{}.rs", name));
let dir_mod = RelativePathBuf::from(format!("../{}/mod.rs", name));
let points_to: Vec<FileId>;
let problem: Option<Problem>;
if is_dir_owner {
points_to = [&file_mod, &dir_mod].iter()
.filter_map(|path| file_resolver.resolve(file_id, path))
.collect();
problem = if points_to.is_empty() {
Some(Problem::UnresolvedModule {
candidate: file_mod,
})
} else {
None
}
} else {
points_to = Vec::new();
problem = Some(Problem::NotDirOwner {
move_to: RelativePathBuf::from(format!("../{}/mod.rs", mod_name)),
candidate: file_mod,
});
}
(points_to, problem)
}

View file

@ -1,159 +0,0 @@
use std::sync::Arc;
use {
FileId,
db::{
Query, QueryRegistry, QueryCtx,
file_syntax, file_set
},
descriptors::{ModuleDescriptor, ModuleTreeDescriptor}
};
pub(crate) fn register_queries(reg: &mut QueryRegistry) {
reg.add(MODULE_DESCR, "MODULE_DESCR");
}
pub(crate) fn module_tree(ctx: QueryCtx) -> Arc<ModuleTreeDescriptor> {
ctx.get(MODULE_TREE, ())
}
impl<'a> QueryCtx<'a> {
fn module_descr(&self, file_id: FileId) -> Arc<ModuleDescriptor> {
self.get(MODULE_DESCR, file_id)
}
}
const MODULE_DESCR: Query<FileId, ModuleDescriptor> = Query(30, |ctx, &file_id| {
let file = file_syntax(ctx, file_id);
ModuleDescriptor::new(file.ast())
});
const MODULE_TREE: Query<(), ModuleTreeDescriptor> = Query(31, |ctx, _| {
let file_set = file_set(ctx);
let mut files = Vec::new();
for &file_id in file_set.0.iter() {
let module_descr = ctx.get(MODULE_DESCR, file_id);
files.push((file_id, module_descr));
}
ModuleTreeDescriptor::new(files.iter().map(|(file_id, descr)| (*file_id, &**descr)), &file_set.1)
});
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use im;
use relative_path::{RelativePath, RelativePathBuf};
use {
db::{Query, Db, State},
imp::FileResolverImp,
FileId, FileResolver,
};
use super::*;
#[derive(Debug)]
struct FileMap(im::HashMap<FileId, RelativePathBuf>);
impl FileResolver for FileMap {
fn file_stem(&self, file_id: FileId) -> String {
self.0[&file_id].file_stem().unwrap().to_string()
}
fn resolve(&self, file_id: FileId, rel: &RelativePath) -> Option<FileId> {
let path = self.0[&file_id].join(rel).normalize();
self.0.iter()
.filter_map(|&(id, ref p)| Some(id).filter(|_| p == &path))
.next()
}
}
struct Fixture {
next_file_id: u32,
fm: im::HashMap<FileId, RelativePathBuf>,
db: Db,
}
impl Fixture {
fn new() -> Fixture {
Fixture {
next_file_id: 1,
fm: im::HashMap::new(),
db: Db::new(),
}
}
fn add_file(&mut self, path: &str, text: &str) -> FileId {
assert!(path.starts_with("/"));
let file_id = FileId(self.next_file_id);
self.next_file_id += 1;
self.fm.insert(file_id, RelativePathBuf::from(&path[1..]));
let mut new_state = self.db.state().clone();
new_state.file_map.insert(file_id, Arc::new(text.to_string()));
new_state.file_resolver = FileResolverImp::new(
Arc::new(FileMap(self.fm.clone()))
);
self.db = self.db.with_changes(new_state, &[file_id], true);
file_id
}
fn remove_file(&mut self, file_id: FileId) {
self.fm.remove(&file_id);
let mut new_state = self.db.state().clone();
new_state.file_map.remove(&file_id);
new_state.file_resolver = FileResolverImp::new(
Arc::new(FileMap(self.fm.clone()))
);
self.db = self.db.with_changes(new_state, &[file_id], true);
}
fn change_file(&mut self, file_id: FileId, new_text: &str) {
let mut new_state = self.db.state().clone();
new_state.file_map.insert(file_id, Arc::new(new_text.to_string()));
self.db = self.db.with_changes(new_state, &[file_id], false);
}
fn check_parent_modules(
&self,
file_id: FileId,
expected: &[FileId],
queries: &[(&'static str, u64)]
) {
let (actual, events) = self.db.trace_query(|ctx| {
ctx.get(PARENT_MODULE, file_id)
});
assert_eq!(actual.as_slice(), expected);
let mut counts = HashMap::new();
events.into_iter()
.for_each(|event| *counts.entry(event).or_insert(0) += 1);
for &(query_id, expected_count) in queries.iter() {
let actual_count = *counts.get(&query_id).unwrap_or(&0);
assert_eq!(
actual_count,
expected_count,
"counts for {} differ",
query_id,
)
}
}
}
#[test]
fn test_parent_module() {
let mut f = Fixture::new();
let foo = f.add_file("/foo.rs", "");
f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 1)]);
let lib = f.add_file("/lib.rs", "mod foo;");
f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 1)]);
f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 0)]);
f.change_file(lib, "");
f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 1)]);
f.change_file(lib, "mod foo;");
f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 1)]);
f.change_file(lib, "mod bar;");
f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 1)]);
f.change_file(lib, "mod foo;");
f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 1)]);
f.remove_file(lib);
f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 0)]);
}
}

View file

@ -0,0 +1,28 @@
use std::sync::Arc;
use libsyntax2::File;
use libeditor::LineIndex;
use {
FileId,
db::{Query, QueryCtx, QueryRegistry, file_text},
};
pub(crate) fn register_queries(reg: &mut QueryRegistry) {
reg.add(FILE_SYNTAX, "FILE_SYNTAX");
reg.add(FILE_LINES, "FILE_LINES");
}
pub(crate) fn file_syntax(ctx: QueryCtx, file_id: FileId) -> File {
(&*ctx.get(FILE_SYNTAX, file_id)).clone()
}
pub(crate) fn file_lines(ctx: QueryCtx, file_id: FileId) -> Arc<LineIndex> {
ctx.get(FILE_LINES, file_id)
}
const FILE_SYNTAX: Query<FileId, File> = Query(16, |ctx, file_id: &FileId| {
let text = file_text(ctx, *file_id);
File::parse(&*text)
});
const FILE_LINES: Query<FileId, LineIndex> = Query(17, |ctx, file_id: &FileId| {
let text = file_text(ctx, *file_id);
LineIndex::new(&*text)
});

View file

@ -1,6 +1,5 @@
use std::{ use std::{
collections::HashMap, collections::HashMap,
time::Instant,
sync::Arc, sync::Arc,
panic, panic,
}; };
@ -13,83 +12,74 @@ use libsyntax2::File;
use { use {
FileId, FileId,
imp::FileResolverImp, imp::FileResolverImp,
module_map::{ModuleMap, ChangeKind},
symbol_index::SymbolIndex, symbol_index::SymbolIndex,
descriptors::{ModuleDescriptor, ModuleTreeDescriptor}, descriptors::{ModuleDescriptor, ModuleTreeDescriptor},
db::Db,
}; };
pub(crate) trait SourceRoot { pub(crate) trait SourceRoot {
fn contains(&self, file_id: FileId) -> bool; fn contains(&self, file_id: FileId) -> bool;
fn module_tree(&self) -> Arc<ModuleTreeDescriptor> { fn module_tree(&self) -> Arc<ModuleTreeDescriptor>;
unimplemented!() fn lines(&self, file_id: FileId) -> Arc<LineIndex>;
} fn syntax(&self, file_id: FileId) -> File;
// fn module_map(&self) -> &ModuleMap;
fn lines(&self, file_id: FileId) -> &LineIndex;
fn syntax(&self, file_id: FileId) -> &File;
fn symbols<'a>(&'a self, acc: &mut Vec<&'a SymbolIndex>); fn symbols<'a>(&'a self, acc: &mut Vec<&'a SymbolIndex>);
} }
#[derive(Clone, Default, Debug)] #[derive(Default, Debug)]
pub(crate) struct WritableSourceRoot { pub(crate) struct WritableSourceRoot {
file_map: HashMap<FileId, Arc<(FileData, OnceCell<SymbolIndex>)>>, db: Db,
module_map: ModuleMap,
} }
impl WritableSourceRoot { impl WritableSourceRoot {
pub fn update(&mut self, file_id: FileId, text: Option<String>) { pub fn apply_changes(
let change_kind = if self.file_map.remove(&file_id).is_some() { &self,
if text.is_some() { changes: &mut dyn Iterator<Item=(FileId, Option<String>)>,
ChangeKind::Update file_resolver: Option<FileResolverImp>,
} else { ) -> WritableSourceRoot {
ChangeKind::Delete let resolver_changed = file_resolver.is_some();
} let mut changed_files = Vec::new();
} else { let mut new_state = self.db.state().clone();
ChangeKind::Insert
};
self.module_map.update_file(file_id, change_kind);
self.file_map.remove(&file_id);
if let Some(text) = text {
let file_data = FileData::new(text);
self.file_map.insert(file_id, Arc::new((file_data, Default::default())));
}
}
pub fn set_file_resolver(&mut self, file_resolver: FileResolverImp) {
self.module_map.set_file_resolver(file_resolver)
}
pub fn reindex(&self) {
let now = Instant::now();
self.file_map
.par_iter()
.for_each(|(&file_id, data)| {
symbols(file_id, data);
});
info!("parallel indexing took {:?}", now.elapsed());
} for (file_id, text) in changes {
fn data(&self, file_id: FileId) -> &FileData { changed_files.push(file_id);
match self.file_map.get(&file_id) { match text {
Some(data) => &data.0, Some(text) => {
None => panic!("unknown file: {:?}", file_id), new_state.file_map.insert(file_id, Arc::new(text));
},
None => {
new_state.file_map.remove(&file_id);
}
}
}
if let Some(file_resolver) = file_resolver {
new_state.file_resolver = file_resolver
}
WritableSourceRoot {
db: self.db.with_changes(new_state, &changed_files, resolver_changed)
} }
} }
} }
impl SourceRoot for WritableSourceRoot { impl SourceRoot for WritableSourceRoot {
fn module_tree(&self) -> Arc<ModuleTreeDescriptor> {
self.db.make_query(::module_map::module_tree)
}
fn contains(&self, file_id: FileId) -> bool { fn contains(&self, file_id: FileId) -> bool {
self.file_map.contains_key(&file_id) self.db.state().file_map.contains_key(&file_id)
} }
fn lines(&self, file_id: FileId) -> &LineIndex { fn lines(&self, file_id: FileId) -> Arc<LineIndex> {
self.data(file_id).lines() self.db.make_query(|ctx| ::queries::file_lines(ctx, file_id))
} }
fn syntax(&self, file_id: FileId) -> &File { fn syntax(&self, file_id: FileId) -> File {
self.data(file_id).syntax() self.db.make_query(|ctx| ::queries::file_syntax(ctx, file_id))
} }
fn symbols<'a>(&'a self, acc: &mut Vec<&'a SymbolIndex>) { fn symbols<'a>(&'a self, acc: &mut Vec<&'a SymbolIndex>) {
acc.extend( // acc.extend(
self.file_map // self.file_map
.iter() // .iter()
.map(|(&file_id, data)| symbols(file_id, data)) // .map(|(&file_id, data)| symbols(file_id, data))
) // )
} }
} }
@ -101,7 +91,7 @@ fn symbols(file_id: FileId, (data, symbols): &(FileData, OnceCell<SymbolIndex>))
#[derive(Debug)] #[derive(Debug)]
struct FileData { struct FileData {
text: String, text: String,
lines: OnceCell<LineIndex>, lines: OnceCell<Arc<LineIndex>>,
syntax: OnceCell<File>, syntax: OnceCell<File>,
} }
@ -113,8 +103,8 @@ impl FileData {
lines: OnceCell::new(), lines: OnceCell::new(),
} }
} }
fn lines(&self) -> &LineIndex { fn lines(&self) -> &Arc<LineIndex> {
self.lines.get_or_init(|| LineIndex::new(&self.text)) self.lines.get_or_init(|| Arc::new(LineIndex::new(&self.text)))
} }
fn syntax(&self) -> &File { fn syntax(&self) -> &File {
let text = &self.text; let text = &self.text;
@ -127,10 +117,6 @@ impl FileData {
} }
} }
} }
fn syntax_transient(&self) -> File {
self.syntax.get().map(|s| s.clone())
.unwrap_or_else(|| File::parse(&self.text))
}
} }
#[derive(Debug)] #[derive(Debug)]
@ -184,11 +170,11 @@ impl SourceRoot for ReadonlySourceRoot {
fn contains(&self, file_id: FileId) -> bool { fn contains(&self, file_id: FileId) -> bool {
self.file_map.contains_key(&file_id) self.file_map.contains_key(&file_id)
} }
fn lines(&self, file_id: FileId) -> &LineIndex { fn lines(&self, file_id: FileId) -> Arc<LineIndex> {
self.data(file_id).lines() Arc::clone(self.data(file_id).lines())
} }
fn syntax(&self, file_id: FileId) -> &File { fn syntax(&self, file_id: FileId) -> File {
self.data(file_id).syntax() self.data(file_id).syntax().clone()
} }
fn symbols<'a>(&'a self, acc: &mut Vec<&'a SymbolIndex>) { fn symbols<'a>(&'a self, acc: &mut Vec<&'a SymbolIndex>) {
acc.push(&self.symbol_index) acc.push(&self.symbol_index)