Implement Find All References for local variables

This commit is contained in:
Jeremy A. Kolb 2018-10-18 13:40:12 -04:00
parent 2a704035f4
commit 3746689e9d
8 changed files with 96 additions and 3 deletions

View file

@ -257,6 +257,38 @@ impl AnalysisImpl {
vec![]
}
pub fn find_all_refs(&self, file_id: FileId, offset: TextUnit, _token: &JobToken) -> Vec<(FileId, TextRange)> {
let root = self.root(file_id);
let file = root.syntax(file_id);
let syntax = file.syntax();
let mut ret = vec![];
// Find the symbol we are looking for
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) {
// We are only handing local references for now
if let Some(resolved) = resolve_local_name(&file, offset, name_ref) {
ret.push((file_id, resolved.1));
if let Some(fn_def) = find_node_at_offset::<ast::FnDef>(syntax, offset) {
let refs : Vec<_> = fn_def.syntax().descendants()
.filter_map(ast::NameRef::cast)
.filter(|n: &ast::NameRef| resolve_local_name(&file, n.syntax().range().start(), *n) == Some(resolved.clone()))
.collect();
for r in refs {
ret.push((file_id, r.syntax().range()));
}
}
}
}
ret
}
pub fn diagnostics(&self, file_id: FileId) -> Vec<Diagnostic> {
let root = self.root(file_id);
let module_tree = root.module_tree();

View file

@ -217,6 +217,9 @@ impl Analysis {
self.imp
.approximately_resolve_symbol(file_id, offset, token)
}
pub fn find_all_refs(&self, file_id: FileId, offset: TextUnit, token: &JobToken) -> Vec<(FileId, TextRange)> {
self.imp.find_all_refs(file_id, offset, token)
}
pub fn parent_module(&self, file_id: FileId) -> Vec<(FileId, FileSymbol)> {
self.imp.parent_module(file_id)
}