diff options
Diffstat (limited to 'crates/ra_analysis/src/hir')
-rw-r--r-- | crates/ra_analysis/src/hir/function/mod.rs | 28 | ||||
-rw-r--r-- | crates/ra_analysis/src/hir/function/scope.rs | 29 | ||||
-rw-r--r-- | crates/ra_analysis/src/hir/mod.rs | 53 |
3 files changed, 55 insertions, 55 deletions
diff --git a/crates/ra_analysis/src/hir/function/mod.rs b/crates/ra_analysis/src/hir/function/mod.rs index 5e44a88a7..d171b6a8d 100644 --- a/crates/ra_analysis/src/hir/function/mod.rs +++ b/crates/ra_analysis/src/hir/function/mod.rs | |||
@@ -6,8 +6,8 @@ use std::{ | |||
6 | }; | 6 | }; |
7 | 7 | ||
8 | use ra_syntax::{ | 8 | use ra_syntax::{ |
9 | TextRange, TextUnit, SyntaxNodeRef, | ||
9 | ast::{self, AstNode, DocCommentsOwner, NameOwner}, | 10 | ast::{self, AstNode, DocCommentsOwner, NameOwner}, |
10 | TextRange, TextUnit, | ||
11 | }; | 11 | }; |
12 | 12 | ||
13 | use crate::{ | 13 | use crate::{ |
@@ -39,6 +39,32 @@ impl FunctionDescriptor { | |||
39 | FunctionDescriptor { fn_id } | 39 | FunctionDescriptor { fn_id } |
40 | } | 40 | } |
41 | 41 | ||
42 | pub(crate) fn guess_for_name_ref( | ||
43 | db: &impl HirDatabase, | ||
44 | file_id: FileId, | ||
45 | name_ref: ast::NameRef, | ||
46 | ) -> Option<FunctionDescriptor> { | ||
47 | FunctionDescriptor::guess_for_node(db, file_id, name_ref.syntax()) | ||
48 | } | ||
49 | |||
50 | pub(crate) fn guess_for_bind_pat( | ||
51 | db: &impl HirDatabase, | ||
52 | file_id: FileId, | ||
53 | bind_pat: ast::BindPat, | ||
54 | ) -> Option<FunctionDescriptor> { | ||
55 | FunctionDescriptor::guess_for_node(db, file_id, bind_pat.syntax()) | ||
56 | } | ||
57 | |||
58 | fn guess_for_node( | ||
59 | db: &impl HirDatabase, | ||
60 | file_id: FileId, | ||
61 | node: SyntaxNodeRef, | ||
62 | ) -> Option<FunctionDescriptor> { | ||
63 | let fn_def = node.ancestors().find_map(ast::FnDef::cast)?; | ||
64 | let res = FunctionDescriptor::guess_from_source(db, file_id, fn_def); | ||
65 | Some(res) | ||
66 | } | ||
67 | |||
42 | pub(crate) fn scope(&self, db: &impl HirDatabase) -> Arc<FnScopes> { | 68 | pub(crate) fn scope(&self, db: &impl HirDatabase) -> Arc<FnScopes> { |
43 | db.fn_scopes(self.fn_id) | 69 | db.fn_scopes(self.fn_id) |
44 | } | 70 | } |
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 @@ | |||
1 | use rustc_hash::{FxHashMap, FxHashSet}; | 1 | use rustc_hash::{FxHashMap, FxHashSet}; |
2 | 2 | ||
3 | use ra_syntax::{ | 3 | use 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 | ||
9 | use crate::{ | 9 | use 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)] | ||
287 | pub struct ReferenceDescriptor { | ||
288 | pub range: TextRange, | ||
289 | pub name: String, | ||
290 | } | ||
291 | |||
265 | #[cfg(test)] | 292 | #[cfg(test)] |
266 | mod tests { | 293 | mod tests { |
267 | use ra_editor::find_node_at_offset; | 294 | use ra_editor::find_node_at_offset; |
diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs index 5a9086cef..e234173a9 100644 --- a/crates/ra_analysis/src/hir/mod.rs +++ b/crates/ra_analysis/src/hir/mod.rs | |||
@@ -11,15 +11,9 @@ mod function; | |||
11 | mod module; | 11 | mod module; |
12 | mod path; | 12 | mod path; |
13 | 13 | ||
14 | use ra_syntax::{ | ||
15 | ast::{self, AstNode}, | ||
16 | TextRange, | ||
17 | }; | ||
18 | |||
19 | use crate::{ | 14 | use crate::{ |
20 | hir::db::HirDatabase, | 15 | hir::db::HirDatabase, |
21 | loc2id::{DefId, DefLoc}, | 16 | loc2id::{DefId, DefLoc}, |
22 | syntax_ptr::LocalSyntaxPtr, | ||
23 | Cancelable, | 17 | Cancelable, |
24 | }; | 18 | }; |
25 | 19 | ||
@@ -49,50 +43,3 @@ impl DefId { | |||
49 | Ok(res) | 43 | Ok(res) |
50 | } | 44 | } |
51 | } | 45 | } |
52 | |||
53 | #[derive(Debug)] | ||
54 | pub struct ReferenceDescriptor { | ||
55 | pub range: TextRange, | ||
56 | pub name: String, | ||
57 | } | ||
58 | |||
59 | #[derive(Debug)] | ||
60 | pub struct DeclarationDescriptor<'a> { | ||
61 | pat: ast::BindPat<'a>, | ||
62 | pub range: TextRange, | ||
63 | } | ||
64 | |||
65 | impl<'a> DeclarationDescriptor<'a> { | ||
66 | pub fn new(pat: ast::BindPat) -> DeclarationDescriptor { | ||
67 | let range = pat.syntax().range(); | ||
68 | |||
69 | DeclarationDescriptor { pat, range } | ||
70 | } | ||
71 | |||
72 | pub fn find_all_refs(&self) -> Vec<ReferenceDescriptor> { | ||
73 | let name_ptr = LocalSyntaxPtr::new(self.pat.syntax()); | ||
74 | |||
75 | let fn_def = match self.pat.syntax().ancestors().find_map(ast::FnDef::cast) { | ||
76 | Some(def) => def, | ||
77 | None => return Default::default(), | ||
78 | }; | ||
79 | |||
80 | let fn_scopes = FnScopes::new(fn_def); | ||
81 | |||
82 | let refs: Vec<_> = fn_def | ||
83 | .syntax() | ||
84 | .descendants() | ||
85 | .filter_map(ast::NameRef::cast) | ||
86 | .filter(|name_ref| match fn_scopes.resolve_local_name(*name_ref) { | ||
87 | None => false, | ||
88 | Some(entry) => entry.ptr() == name_ptr, | ||
89 | }) | ||
90 | .map(|name_ref| ReferenceDescriptor { | ||
91 | name: name_ref.syntax().text().to_string(), | ||
92 | range: name_ref.syntax().range(), | ||
93 | }) | ||
94 | .collect(); | ||
95 | |||
96 | refs | ||
97 | } | ||
98 | } | ||