aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/hir
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/src/hir')
-rw-r--r--crates/ra_analysis/src/hir/function/mod.rs2
-rw-r--r--crates/ra_analysis/src/hir/function/scope.rs29
-rw-r--r--crates/ra_analysis/src/hir/mod.rs14
3 files changed, 23 insertions, 22 deletions
diff --git a/crates/ra_analysis/src/hir/function/mod.rs b/crates/ra_analysis/src/hir/function/mod.rs
index 5de9806e3..8161a604f 100644
--- a/crates/ra_analysis/src/hir/function/mod.rs
+++ b/crates/ra_analysis/src/hir/function/mod.rs
@@ -15,7 +15,7 @@ use crate::{
15 syntax_ptr::SyntaxPtr, FileId, 15 syntax_ptr::SyntaxPtr, FileId,
16}; 16};
17 17
18pub(crate) use self::scope::{resolve_local_name, FnScopes}; 18pub(crate) use self::scope::FnScopes;
19pub(crate) use crate::loc2id::FnId; 19pub(crate) use crate::loc2id::FnId;
20 20
21impl FnId { 21impl FnId {
diff --git a/crates/ra_analysis/src/hir/function/scope.rs b/crates/ra_analysis/src/hir/function/scope.rs
index 5307a0a8e..b8bdebe47 100644
--- a/crates/ra_analysis/src/hir/function/scope.rs
+++ b/crates/ra_analysis/src/hir/function/scope.rs
@@ -57,6 +57,19 @@ impl FnScopes {
57 self.scopes[scope].parent 57 self.scopes[scope].parent
58 }) 58 })
59 } 59 }
60 pub(crate) fn resolve_local_name<'a>(
61 &'a self,
62 name_ref: ast::NameRef,
63 ) -> Option<&'a ScopeEntry> {
64 let mut shadowed = FxHashSet::default();
65 let ret = self
66 .scope_chain(name_ref.syntax())
67 .flat_map(|scope| self.entries(scope).iter())
68 .filter(|entry| shadowed.insert(entry.name()))
69 .filter(|entry| entry.name() == &name_ref.text())
70 .nth(0);
71 ret
72 }
60 fn root_scope(&mut self) -> ScopeId { 73 fn root_scope(&mut self) -> ScopeId {
61 self.scopes.alloc(ScopeData { 74 self.scopes.alloc(ScopeData {
62 parent: None, 75 parent: None,
@@ -249,20 +262,6 @@ fn compute_expr_scopes(expr: ast::Expr, scopes: &mut FnScopes, scope: ScopeId) {
249 } 262 }
250} 263}
251 264
252pub fn resolve_local_name<'a>(
253 name_ref: ast::NameRef,
254 scopes: &'a FnScopes,
255) -> Option<&'a ScopeEntry> {
256 let mut shadowed = FxHashSet::default();
257 let ret = scopes
258 .scope_chain(name_ref.syntax())
259 .flat_map(|scope| scopes.entries(scope).iter())
260 .filter(|entry| shadowed.insert(entry.name()))
261 .filter(|entry| entry.name() == &name_ref.text())
262 .nth(0);
263 ret
264}
265
266#[cfg(test)] 265#[cfg(test)]
267mod tests { 266mod tests {
268 use ra_editor::find_node_at_offset; 267 use ra_editor::find_node_at_offset;
@@ -376,7 +375,7 @@ mod tests {
376 375
377 let scopes = FnScopes::new(fn_def); 376 let scopes = FnScopes::new(fn_def);
378 377
379 let local_name_entry = resolve_local_name(name_ref, &scopes).unwrap(); 378 let local_name_entry = scopes.resolve_local_name(name_ref).unwrap();
380 let local_name = local_name_entry.ptr().resolve(&file); 379 let local_name = local_name_entry.ptr().resolve(&file);
381 let expected_name = 380 let expected_name =
382 find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into()).unwrap(); 381 find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into()).unwrap();
diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs
index 2035c2d23..1d37fae32 100644
--- a/crates/ra_analysis/src/hir/mod.rs
+++ b/crates/ra_analysis/src/hir/mod.rs
@@ -5,11 +5,11 @@
5//! to a particular crate instance. That is, it has cfg flags and features 5//! to a particular crate instance. That is, it has cfg flags and features
6//! applied. So, there relation between syntax and HIR is many-to-one. 6//! applied. So, there relation between syntax and HIR is many-to-one.
7 7
8pub(crate) mod function;
9mod module;
10pub(crate) mod db; 8pub(crate) mod db;
11mod path;
12mod query_definitions; 9mod query_definitions;
10mod function;
11mod module;
12mod path;
13 13
14use ra_syntax::{ 14use ra_syntax::{
15 ast::{self, AstNode}, 15 ast::{self, AstNode},
@@ -18,7 +18,6 @@ use ra_syntax::{
18 18
19use crate::{ 19use crate::{
20 hir::db::HirDatabase, 20 hir::db::HirDatabase,
21 hir::function::{resolve_local_name, FnScopes},
22 loc2id::{DefId, DefLoc}, 21 loc2id::{DefId, DefLoc},
23 syntax_ptr::LocalSyntaxPtr, 22 syntax_ptr::LocalSyntaxPtr,
24 Cancelable, 23 Cancelable,
@@ -27,9 +26,12 @@ use crate::{
27pub(crate) use self::{ 26pub(crate) use self::{
28 path::{Path, PathKind}, 27 path::{Path, PathKind},
29 module::{ModuleDescriptor, ModuleId, Problem, nameres::FileItemId}, 28 module::{ModuleDescriptor, ModuleId, Problem, nameres::FileItemId},
30 function::FunctionDescriptor, 29 function::{FunctionDescriptor, FnScopes},
31}; 30};
32 31
32//TODO: FIXME
33pub use self::function::FnDescriptor;
34
33pub(crate) enum Def { 35pub(crate) enum Def {
34 Module(ModuleDescriptor), 36 Module(ModuleDescriptor),
35 Item, 37 Item,
@@ -82,7 +84,7 @@ impl<'a> DeclarationDescriptor<'a> {
82 .syntax() 84 .syntax()
83 .descendants() 85 .descendants()
84 .filter_map(ast::NameRef::cast) 86 .filter_map(ast::NameRef::cast)
85 .filter(|name_ref| match resolve_local_name(*name_ref, &fn_scopes) { 87 .filter(|name_ref| match fn_scopes.resolve_local_name(*name_ref) {
86 None => false, 88 None => false,
87 Some(entry) => entry.ptr() == name_ptr, 89 Some(entry) => entry.ptr() == name_ptr,
88 }) 90 })