aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/hir/function/scope.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/src/hir/function/scope.rs')
-rw-r--r--crates/ra_analysis/src/hir/function/scope.rs29
1 files changed, 28 insertions, 1 deletions
diff --git a/crates/ra_analysis/src/hir/function/scope.rs b/crates/ra_analysis/src/hir/function/scope.rs
index b8bdebe47..76b2fea68 100644
--- a/crates/ra_analysis/src/hir/function/scope.rs
+++ b/crates/ra_analysis/src/hir/function/scope.rs
@@ -1,9 +1,9 @@
1use rustc_hash::{FxHashMap, FxHashSet}; 1use rustc_hash::{FxHashMap, FxHashSet};
2 2
3use ra_syntax::{ 3use ra_syntax::{
4 AstNode, SmolStr, SyntaxNodeRef, TextRange,
4 algo::generate, 5 algo::generate,
5 ast::{self, ArgListOwner, LoopBodyOwner, NameOwner}, 6 ast::{self, ArgListOwner, LoopBodyOwner, NameOwner},
6 AstNode, SmolStr, SyntaxNodeRef,
7}; 7};
8 8
9use crate::{ 9use crate::{
@@ -70,6 +70,27 @@ impl FnScopes {
70 .nth(0); 70 .nth(0);
71 ret 71 ret
72 } 72 }
73
74 pub fn find_all_refs(&self, pat: ast::BindPat) -> Vec<ReferenceDescriptor> {
75 let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap();
76 let name_ptr = LocalSyntaxPtr::new(pat.syntax());
77 let refs: Vec<_> = fn_def
78 .syntax()
79 .descendants()
80 .filter_map(ast::NameRef::cast)
81 .filter(|name_ref| match self.resolve_local_name(*name_ref) {
82 None => false,
83 Some(entry) => entry.ptr() == name_ptr,
84 })
85 .map(|name_ref| ReferenceDescriptor {
86 name: name_ref.syntax().text().to_string(),
87 range: name_ref.syntax().range(),
88 })
89 .collect();
90
91 refs
92 }
93
73 fn root_scope(&mut self) -> ScopeId { 94 fn root_scope(&mut self) -> ScopeId {
74 self.scopes.alloc(ScopeData { 95 self.scopes.alloc(ScopeData {
75 parent: None, 96 parent: None,
@@ -262,6 +283,12 @@ fn compute_expr_scopes(expr: ast::Expr, scopes: &mut FnScopes, scope: ScopeId) {
262 } 283 }
263} 284}
264 285
286#[derive(Debug)]
287pub struct ReferenceDescriptor {
288 pub range: TextRange,
289 pub name: String,
290}
291
265#[cfg(test)] 292#[cfg(test)]
266mod tests { 293mod tests {
267 use ra_editor::find_node_at_offset; 294 use ra_editor::find_node_at_offset;