mark module queries as cacelable

This commit is contained in:
Aleksey Kladov 2018-10-20 22:15:03 +03:00
parent 9fb41716de
commit e74bf6e56e
4 changed files with 31 additions and 29 deletions

View file

@ -159,7 +159,7 @@ impl AnalysisImpl {
}
pub fn parent_module(&self, file_id: FileId) -> Cancelable<Vec<(FileId, FileSymbol)>> {
let root = self.root(file_id);
let module_tree = root.module_tree();
let module_tree = root.module_tree()?;
let res = module_tree
.parent_modules(file_id)
.iter()
@ -177,8 +177,8 @@ impl AnalysisImpl {
.collect();
Ok(res)
}
pub fn crate_for(&self, file_id: FileId) -> Vec<CrateId> {
let module_tree = self.root(file_id).module_tree();
pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> {
let module_tree = self.root(file_id).module_tree()?;
let crate_graph = &self.data.crate_graph;
let mut res = Vec::new();
let mut work = VecDeque::new();
@ -196,7 +196,7 @@ impl AnalysisImpl {
.filter(|&id| visited.insert(id));
work.extend(parents);
}
res
Ok(res)
}
pub fn crate_root(&self, crate_id: CrateId) -> FileId {
self.data.crate_graph.crate_roots[&crate_id]
@ -205,9 +205,9 @@ impl AnalysisImpl {
&self,
file_id: FileId,
offset: TextUnit,
) -> Vec<(FileId, FileSymbol)> {
) -> Cancelable<Vec<(FileId, FileSymbol)>> {
let root = self.root(file_id);
let module_tree = root.module_tree();
let module_tree = root.module_tree()?;
let file = root.syntax(file_id);
let syntax = file.syntax();
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) {
@ -223,10 +223,10 @@ impl AnalysisImpl {
},
));
return vec;
return Ok(vec);
} else {
// If that fails try the index based approach.
return self.index_resolve(name_ref);
return Ok(self.index_resolve(name_ref));
}
}
if let Some(name) = find_node_at_offset::<ast::Name>(syntax, offset) {
@ -250,11 +250,11 @@ impl AnalysisImpl {
})
.collect();
return res;
return Ok(res);
}
}
}
vec![]
Ok(vec![])
}
pub fn find_all_refs(&self, file_id: FileId, offset: TextUnit) -> Vec<(FileId, TextRange)> {
@ -289,9 +289,9 @@ impl AnalysisImpl {
ret
}
pub fn diagnostics(&self, file_id: FileId) -> Vec<Diagnostic> {
pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
let root = self.root(file_id);
let module_tree = root.module_tree();
let module_tree = root.module_tree()?;
let syntax = root.syntax(file_id);
let mut res = ra_editor::diagnostics(&syntax)
@ -346,7 +346,7 @@ impl AnalysisImpl {
};
res.push(diag)
}
res
Ok(res)
}
pub fn assists(&self, file_id: FileId, range: TextRange) -> Vec<SourceChange> {

View file

@ -37,7 +37,7 @@ pub use ra_editor::{
RunnableKind, StructureNode,
};
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Cancel;
pub type Cancelable<T> = Result<T, Cancel>;
@ -231,8 +231,8 @@ impl Analysis {
file_id: FileId,
offset: TextUnit
) -> Cancelable<Vec<(FileId, FileSymbol)>> {
Ok(self.imp
.approximately_resolve_symbol(file_id, offset))
self.imp
.approximately_resolve_symbol(file_id, offset)
}
pub fn find_all_refs(&self, file_id: FileId, offset: TextUnit, ) -> Cancelable<Vec<(FileId, TextRange)>> {
Ok(self.imp.find_all_refs(file_id, offset))
@ -241,7 +241,7 @@ impl Analysis {
self.imp.parent_module(file_id)
}
pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> {
Ok(self.imp.crate_for(file_id))
self.imp.crate_for(file_id)
}
pub fn crate_root(&self, crate_id: CrateId) -> Cancelable<FileId> {
Ok(self.imp.crate_root(crate_id))
@ -262,7 +262,7 @@ impl Analysis {
Ok(self.imp.assists(file_id, range))
}
pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
Ok(self.imp.diagnostics(file_id))
self.imp.diagnostics(file_id)
}
pub fn resolve_callable(
&self,

View file

@ -1,4 +1,5 @@
use crate::{
Cancelable,
db::SyntaxDatabase,
descriptors::{ModuleDescriptor, ModuleTreeDescriptor},
FileId,
@ -8,30 +9,30 @@ use std::sync::Arc;
salsa::query_group! {
pub(crate) trait ModulesDatabase: SyntaxDatabase {
fn module_tree() -> Arc<ModuleTreeDescriptor> {
fn module_tree() -> Cancelable<Arc<ModuleTreeDescriptor>> {
type ModuleTreeQuery;
}
fn module_descriptor(file_id: FileId) -> Arc<ModuleDescriptor> {
fn module_descriptor(file_id: FileId) -> Cancelable<Arc<ModuleDescriptor>> {
type ModuleDescriptorQuery;
}
}
}
fn module_descriptor(db: &impl ModulesDatabase, file_id: FileId) -> Arc<ModuleDescriptor> {
fn module_descriptor(db: &impl ModulesDatabase, file_id: FileId) -> Cancelable<Arc<ModuleDescriptor>> {
let file = db.file_syntax(file_id);
Arc::new(ModuleDescriptor::new(file.ast()))
Ok(Arc::new(ModuleDescriptor::new(file.ast())))
}
fn module_tree(db: &impl ModulesDatabase) -> Arc<ModuleTreeDescriptor> {
fn module_tree(db: &impl ModulesDatabase) -> Cancelable<Arc<ModuleTreeDescriptor>> {
let file_set = db.file_set();
let mut files = Vec::new();
for &file_id in file_set.files.iter() {
let module_descr = db.module_descriptor(file_id);
let module_descr = db.module_descriptor(file_id)?;
files.push((file_id, module_descr));
}
let res = ModuleTreeDescriptor::new(
files.iter().map(|(file_id, descr)| (*file_id, &**descr)),
&file_set.resolver,
);
Arc::new(res)
Ok(Arc::new(res))
}

View file

@ -8,6 +8,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
use salsa::Database;
use crate::{
Cancelable,
db::{self, FilesDatabase, SyntaxDatabase},
descriptors::{ModuleDescriptor, ModuleTreeDescriptor},
imp::FileResolverImp,
@ -18,7 +19,7 @@ use crate::{
pub(crate) trait SourceRoot {
fn contains(&self, file_id: FileId) -> bool;
fn module_tree(&self) -> Arc<ModuleTreeDescriptor>;
fn module_tree(&self) -> Cancelable<Arc<ModuleTreeDescriptor>>;
fn lines(&self, file_id: FileId) -> Arc<LineIndex>;
fn syntax(&self, file_id: FileId) -> File;
fn symbols(&self, acc: &mut Vec<Arc<SymbolIndex>>);
@ -64,7 +65,7 @@ impl WritableSourceRoot {
}
impl SourceRoot for WritableSourceRoot {
fn module_tree(&self) -> Arc<ModuleTreeDescriptor> {
fn module_tree(&self) -> Cancelable<Arc<ModuleTreeDescriptor>> {
self.db.module_tree()
}
fn contains(&self, file_id: FileId) -> bool {
@ -167,8 +168,8 @@ impl ReadonlySourceRoot {
}
impl SourceRoot for ReadonlySourceRoot {
fn module_tree(&self) -> Arc<ModuleTreeDescriptor> {
Arc::clone(&self.module_tree)
fn module_tree(&self) -> Cancelable<Arc<ModuleTreeDescriptor>> {
Ok(Arc::clone(&self.module_tree))
}
fn contains(&self, file_id: FileId) -> bool {
self.file_map.contains_key(&file_id)