diff options
author | Aleksey Kladov <[email protected]> | 2018-11-27 22:11:29 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-11-27 22:11:29 +0000 |
commit | 16f67ee384d5b49358de167069535af727b87dba (patch) | |
tree | 0394f293061b4fc6a2d5a0a4a55025ce1b655f12 /crates/ra_analysis/src/hir/function | |
parent | 7207eef716c0970df1b3523f8f4bb685518f8f73 (diff) |
move resolve_local to Scopes
Diffstat (limited to 'crates/ra_analysis/src/hir/function')
-rw-r--r-- | crates/ra_analysis/src/hir/function/mod.rs | 28 | ||||
-rw-r--r-- | crates/ra_analysis/src/hir/function/scope.rs | 29 |
2 files changed, 55 insertions, 2 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; |