From 71cbdddf1c6b6c34c05aea01b92bb8a105b32c71 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 20 Oct 2018 22:29:26 +0300 Subject: make file-symbols query cancelable --- crates/ra_analysis/src/db.rs | 24 +++++++++++++----------- crates/ra_analysis/src/imp.rs | 39 +++++++++++++++++++++++---------------- crates/ra_analysis/src/lib.rs | 4 ++-- crates/ra_analysis/src/roots.rs | 21 ++++++++++----------- 4 files changed, 48 insertions(+), 40 deletions(-) (limited to 'crates/ra_analysis/src') diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index cce959669..d621b3b22 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs @@ -1,17 +1,19 @@ -use crate::{ - module_map::{ModuleDescriptorQuery, ModuleTreeQuery, ModulesDatabase}, - symbol_index::SymbolIndex, - FileId, FileResolverImp, +use std::{ + fmt, + hash::{Hash, Hasher}, + sync::Arc, }; + use ra_editor::LineIndex; use ra_syntax::File; use rustc_hash::FxHashSet; use salsa; -use std::{ - fmt, - hash::{Hash, Hasher}, - sync::Arc, +use crate::{ + Cancelable, + module_map::{ModuleDescriptorQuery, ModuleTreeQuery, ModulesDatabase}, + symbol_index::SymbolIndex, + FileId, FileResolverImp, }; #[derive(Default)] @@ -98,7 +100,7 @@ salsa::query_group! { fn file_lines(file_id: FileId) -> Arc { type FileLinesQuery; } - fn file_symbols(file_id: FileId) -> Arc { + fn file_symbols(file_id: FileId) -> Cancelable> { type FileSymbolsQuery; } } @@ -112,7 +114,7 @@ fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc { let text = db.file_text(file_id); Arc::new(LineIndex::new(&*text)) } -fn file_symbols(db: &impl SyntaxDatabase, file_id: FileId) -> Arc { +fn file_symbols(db: &impl SyntaxDatabase, file_id: FileId) -> Cancelable> { let syntax = db.file_syntax(file_id); - Arc::new(SymbolIndex::for_file(file_id, syntax)) + Ok(Arc::new(SymbolIndex::for_file(file_id, syntax))) } diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index df5aacb2d..32e9bb6d7 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -148,14 +148,16 @@ impl AnalysisImpl { pub fn file_line_index(&self, file_id: FileId) -> Arc { self.root(file_id).lines(file_id) } - pub fn world_symbols(&self, query: Query) -> Vec<(FileId, FileSymbol)> { + pub fn world_symbols(&self, query: Query) -> Cancelable> { let mut buf = Vec::new(); if query.libs { - self.data.libs.iter().for_each(|it| it.symbols(&mut buf)); + for lib in self.data.libs.iter() { + lib.symbols(&mut buf)?; + } } else { - self.data.root.symbols(&mut buf); + self.data.root.symbols(&mut buf)?; } - query.search(&buf) + Ok(query.search(&buf)) } pub fn parent_module(&self, file_id: FileId) -> Cancelable> { let root = self.root(file_id); @@ -212,7 +214,7 @@ impl AnalysisImpl { let syntax = file.syntax(); if let Some(name_ref) = find_node_at_offset::(syntax, offset) { // First try to resolve the symbol locally - if let Some((name, range)) = resolve_local_name(&file, offset, name_ref) { + return if let Some((name, range)) = resolve_local_name(&file, offset, name_ref) { let mut vec = vec![]; vec.push(( file_id, @@ -222,12 +224,11 @@ impl AnalysisImpl { kind: NAME, }, )); - - return Ok(vec); + Ok(vec) } else { // If that fails try the index based approach. - return Ok(self.index_resolve(name_ref)); - } + self.index_resolve(name_ref) + }; } if let Some(name) = find_node_at_offset::(syntax, offset) { if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) { @@ -379,17 +380,23 @@ impl AnalysisImpl { &self, file_id: FileId, offset: TextUnit, - ) -> Option<(FnDescriptor, Option)> { + ) -> Cancelable)>> { let root = self.root(file_id); let file = root.syntax(file_id); let syntax = file.syntax(); // Find the calling expression and it's NameRef - let calling_node = FnCallNode::with_node(syntax, offset)?; - let name_ref = calling_node.name_ref()?; + let calling_node = match FnCallNode::with_node(syntax, offset) { + Some(node) => node, + None => return Ok(None), + }; + let name_ref = match calling_node.name_ref() { + Some(name) => name, + None => return Ok(None), + }; // Resolve the function's NameRef (NOTE: this isn't entirely accurate). - let file_symbols = self.index_resolve(name_ref); + let file_symbols = self.index_resolve(name_ref)?; for (_, fs) in file_symbols { if fs.kind == FN_DEF { if let Some(fn_def) = find_node_at_offset(syntax, fs.node_range.start()) { @@ -431,16 +438,16 @@ impl AnalysisImpl { } } - return Some((descriptor, current_parameter)); + return Ok(Some((descriptor, current_parameter))); } } } } - None + Ok(None) } - fn index_resolve(&self, name_ref: ast::NameRef) -> Vec<(FileId, FileSymbol)> { + fn index_resolve(&self, name_ref: ast::NameRef) -> Cancelable> { let name = name_ref.text(); let mut query = Query::new(name.to_string()); query.exact(); diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 6ce32894a..189dbd9c2 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -224,7 +224,7 @@ impl Analysis { ra_editor::folding_ranges(&file) } pub fn symbol_search(&self, query: Query) -> Cancelable> { - Ok(self.imp.world_symbols(query)) + self.imp.world_symbols(query) } pub fn approximately_resolve_symbol( &self, @@ -269,7 +269,7 @@ impl Analysis { file_id: FileId, offset: TextUnit, ) -> Cancelable)>> { - Ok(self.imp.resolve_callable(file_id, offset)) + self.imp.resolve_callable(file_id, offset) } } diff --git a/crates/ra_analysis/src/roots.rs b/crates/ra_analysis/src/roots.rs index e950a75e2..123c4acfa 100644 --- a/crates/ra_analysis/src/roots.rs +++ b/crates/ra_analysis/src/roots.rs @@ -22,7 +22,7 @@ pub(crate) trait SourceRoot { fn module_tree(&self) -> Cancelable>; fn lines(&self, file_id: FileId) -> Arc; fn syntax(&self, file_id: FileId) -> File; - fn symbols(&self, acc: &mut Vec>); + fn symbols(&self, acc: &mut Vec>) -> Cancelable<()>; } #[derive(Default, Debug, Clone)] @@ -77,14 +77,12 @@ impl SourceRoot for WritableSourceRoot { fn syntax(&self, file_id: FileId) -> File { self.db.file_syntax(file_id) } - fn symbols<'a>(&'a self, acc: &mut Vec>) { - let db = &self.db; - let symbols = db.file_set(); - let symbols = symbols - .files - .iter() - .map(|&file_id| db.file_symbols(file_id)); - acc.extend(symbols); + fn symbols<'a>(&'a self, acc: &mut Vec>) -> Cancelable<()> { + for &file_id in self.db.file_set().files.iter() { + let symbols = self.db.file_symbols(file_id)?; + acc.push(symbols) + } + Ok(()) } } @@ -180,7 +178,8 @@ impl SourceRoot for ReadonlySourceRoot { fn syntax(&self, file_id: FileId) -> File { self.data(file_id).syntax().clone() } - fn symbols(&self, acc: &mut Vec>) { - acc.push(Arc::clone(&self.symbol_index)) + fn symbols(&self, acc: &mut Vec>) -> Cancelable<()> { + acc.push(Arc::clone(&self.symbol_index)); + Ok(()) } } -- cgit v1.2.3