make file-symbols query cancelable

This commit is contained in:
Aleksey Kladov 2018-10-20 22:29:26 +03:00
parent e74bf6e56e
commit 71cbdddf1c
4 changed files with 48 additions and 40 deletions

View file

@ -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<LineIndex> {
type FileLinesQuery;
}
fn file_symbols(file_id: FileId) -> Arc<SymbolIndex> {
fn file_symbols(file_id: FileId) -> Cancelable<Arc<SymbolIndex>> {
type FileSymbolsQuery;
}
}
@ -112,7 +114,7 @@ fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<LineIndex> {
let text = db.file_text(file_id);
Arc::new(LineIndex::new(&*text))
}
fn file_symbols(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<SymbolIndex> {
fn file_symbols(db: &impl SyntaxDatabase, file_id: FileId) -> Cancelable<Arc<SymbolIndex>> {
let syntax = db.file_syntax(file_id);
Arc::new(SymbolIndex::for_file(file_id, syntax))
Ok(Arc::new(SymbolIndex::for_file(file_id, syntax)))
}

View file

@ -148,14 +148,16 @@ impl AnalysisImpl {
pub fn file_line_index(&self, file_id: FileId) -> Arc<LineIndex> {
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<Vec<(FileId, FileSymbol)>> {
let mut buf = Vec::new();
if query.libs {
self.data.libs.iter().for_each(|it| it.symbols(&mut buf));
} else {
self.data.root.symbols(&mut buf);
for lib in self.data.libs.iter() {
lib.symbols(&mut buf)?;
}
query.search(&buf)
} else {
self.data.root.symbols(&mut buf)?;
}
Ok(query.search(&buf))
}
pub fn parent_module(&self, file_id: FileId) -> Cancelable<Vec<(FileId, FileSymbol)>> {
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::<ast::NameRef>(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::<ast::Name>(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<usize>)> {
) -> Cancelable<Option<(FnDescriptor, Option<usize>)>> {
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<Vec<(FileId, FileSymbol)>> {
let name = name_ref.text();
let mut query = Query::new(name.to_string());
query.exact();

View file

@ -224,7 +224,7 @@ impl Analysis {
ra_editor::folding_ranges(&file)
}
pub fn symbol_search(&self, query: Query) -> Cancelable<Vec<(FileId, FileSymbol)>> {
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<Option<(FnDescriptor, Option<usize>)>> {
Ok(self.imp.resolve_callable(file_id, offset))
self.imp.resolve_callable(file_id, offset)
}
}

View file

@ -22,7 +22,7 @@ pub(crate) trait SourceRoot {
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>>);
fn symbols(&self, acc: &mut Vec<Arc<SymbolIndex>>) -> 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<Arc<SymbolIndex>>) {
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<Arc<SymbolIndex>>) -> 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<Arc<SymbolIndex>>) {
acc.push(Arc::clone(&self.symbol_index))
fn symbols(&self, acc: &mut Vec<Arc<SymbolIndex>>) -> Cancelable<()> {
acc.push(Arc::clone(&self.symbol_index));
Ok(())
}
}