From 3746689e9ddea455d10a41d9fc3af33b22a3707d Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Thu, 18 Oct 2018 13:40:12 -0400 Subject: Implement Find All References for local variables --- crates/ra_analysis/src/imp.rs | 32 ++++++++++++++++++++++++++++++++ crates/ra_analysis/src/lib.rs | 3 +++ 2 files changed, 35 insertions(+) (limited to 'crates/ra_analysis/src') diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index f1403cb5d..a67b1717a 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -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::(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::(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 { let root = self.root(file_id); let module_tree = root.module_tree(); diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 2eeacaabe..46cc0722b 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -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) } -- cgit v1.2.3