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

View file

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

View file

@ -1,4 +1,5 @@
use crate::{ use crate::{
Cancelable,
db::SyntaxDatabase, db::SyntaxDatabase,
descriptors::{ModuleDescriptor, ModuleTreeDescriptor}, descriptors::{ModuleDescriptor, ModuleTreeDescriptor},
FileId, FileId,
@ -8,30 +9,30 @@ use std::sync::Arc;
salsa::query_group! { salsa::query_group! {
pub(crate) trait ModulesDatabase: SyntaxDatabase { pub(crate) trait ModulesDatabase: SyntaxDatabase {
fn module_tree() -> Arc<ModuleTreeDescriptor> { fn module_tree() -> Cancelable<Arc<ModuleTreeDescriptor>> {
type ModuleTreeQuery; type ModuleTreeQuery;
} }
fn module_descriptor(file_id: FileId) -> Arc<ModuleDescriptor> { fn module_descriptor(file_id: FileId) -> Cancelable<Arc<ModuleDescriptor>> {
type ModuleDescriptorQuery; 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); 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 file_set = db.file_set();
let mut files = Vec::new(); let mut files = Vec::new();
for &file_id in file_set.files.iter() { 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)); files.push((file_id, module_descr));
} }
let res = ModuleTreeDescriptor::new( let res = ModuleTreeDescriptor::new(
files.iter().map(|(file_id, descr)| (*file_id, &**descr)), files.iter().map(|(file_id, descr)| (*file_id, &**descr)),
&file_set.resolver, &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 salsa::Database;
use crate::{ use crate::{
Cancelable,
db::{self, FilesDatabase, SyntaxDatabase}, db::{self, FilesDatabase, SyntaxDatabase},
descriptors::{ModuleDescriptor, ModuleTreeDescriptor}, descriptors::{ModuleDescriptor, ModuleTreeDescriptor},
imp::FileResolverImp, imp::FileResolverImp,
@ -18,7 +19,7 @@ use crate::{
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) -> Cancelable<Arc<ModuleTreeDescriptor>>;
fn lines(&self, file_id: FileId) -> Arc<LineIndex>; fn lines(&self, file_id: FileId) -> Arc<LineIndex>;
fn syntax(&self, file_id: FileId) -> File; fn syntax(&self, file_id: FileId) -> File;
fn symbols(&self, acc: &mut Vec<Arc<SymbolIndex>>); fn symbols(&self, acc: &mut Vec<Arc<SymbolIndex>>);
@ -64,7 +65,7 @@ impl WritableSourceRoot {
} }
impl SourceRoot for WritableSourceRoot { impl SourceRoot for WritableSourceRoot {
fn module_tree(&self) -> Arc<ModuleTreeDescriptor> { fn module_tree(&self) -> Cancelable<Arc<ModuleTreeDescriptor>> {
self.db.module_tree() self.db.module_tree()
} }
fn contains(&self, file_id: FileId) -> bool { fn contains(&self, file_id: FileId) -> bool {
@ -167,8 +168,8 @@ impl ReadonlySourceRoot {
} }
impl SourceRoot for ReadonlySourceRoot { impl SourceRoot for ReadonlySourceRoot {
fn module_tree(&self) -> Arc<ModuleTreeDescriptor> { fn module_tree(&self) -> Cancelable<Arc<ModuleTreeDescriptor>> {
Arc::clone(&self.module_tree) Ok(Arc::clone(&self.module_tree))
} }
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)