From 67de38ec7d5c235dec209fe859d6bf6b0dbe497a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 27 Nov 2018 23:40:23 +0300 Subject: move resolve_local_scope --- .../src/completion/reference_completion.rs | 2 +- crates/ra_analysis/src/hir/function/mod.rs | 2 +- crates/ra_analysis/src/hir/function/scope.rs | 29 +++++++++++----------- crates/ra_analysis/src/hir/mod.rs | 14 ++++++----- crates/ra_analysis/src/imp.rs | 5 ++-- crates/ra_analysis/src/lib.rs | 2 +- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/crates/ra_analysis/src/completion/reference_completion.rs b/crates/ra_analysis/src/completion/reference_completion.rs index 1bf210685..5bf8c3725 100644 --- a/crates/ra_analysis/src/completion/reference_completion.rs +++ b/crates/ra_analysis/src/completion/reference_completion.rs @@ -12,7 +12,7 @@ use crate::{ completion::CompletionItem, hir::{ ModuleDescriptor, - function::FnScopes, + FnScopes, Def, Path, }, 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::{ syntax_ptr::SyntaxPtr, FileId, }; -pub(crate) use self::scope::{resolve_local_name, FnScopes}; +pub(crate) use self::scope::FnScopes; pub(crate) use crate::loc2id::FnId; impl 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 { self.scopes[scope].parent }) } + pub(crate) fn resolve_local_name<'a>( + &'a self, + name_ref: ast::NameRef, + ) -> Option<&'a ScopeEntry> { + let mut shadowed = FxHashSet::default(); + let ret = self + .scope_chain(name_ref.syntax()) + .flat_map(|scope| self.entries(scope).iter()) + .filter(|entry| shadowed.insert(entry.name())) + .filter(|entry| entry.name() == &name_ref.text()) + .nth(0); + ret + } fn root_scope(&mut self) -> ScopeId { self.scopes.alloc(ScopeData { parent: None, @@ -249,20 +262,6 @@ fn compute_expr_scopes(expr: ast::Expr, scopes: &mut FnScopes, scope: ScopeId) { } } -pub fn resolve_local_name<'a>( - name_ref: ast::NameRef, - scopes: &'a FnScopes, -) -> Option<&'a ScopeEntry> { - let mut shadowed = FxHashSet::default(); - let ret = scopes - .scope_chain(name_ref.syntax()) - .flat_map(|scope| scopes.entries(scope).iter()) - .filter(|entry| shadowed.insert(entry.name())) - .filter(|entry| entry.name() == &name_ref.text()) - .nth(0); - ret -} - #[cfg(test)] mod tests { use ra_editor::find_node_at_offset; @@ -376,7 +375,7 @@ mod tests { let scopes = FnScopes::new(fn_def); - let local_name_entry = resolve_local_name(name_ref, &scopes).unwrap(); + let local_name_entry = scopes.resolve_local_name(name_ref).unwrap(); let local_name = local_name_entry.ptr().resolve(&file); let expected_name = find_node_at_offset::(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 @@ //! to a particular crate instance. That is, it has cfg flags and features //! applied. So, there relation between syntax and HIR is many-to-one. -pub(crate) mod function; -mod module; pub(crate) mod db; -mod path; mod query_definitions; +mod function; +mod module; +mod path; use ra_syntax::{ ast::{self, AstNode}, @@ -18,7 +18,6 @@ use ra_syntax::{ use crate::{ hir::db::HirDatabase, - hir::function::{resolve_local_name, FnScopes}, loc2id::{DefId, DefLoc}, syntax_ptr::LocalSyntaxPtr, Cancelable, @@ -27,9 +26,12 @@ use crate::{ pub(crate) use self::{ path::{Path, PathKind}, module::{ModuleDescriptor, ModuleId, Problem, nameres::FileItemId}, - function::FunctionDescriptor, + function::{FunctionDescriptor, FnScopes}, }; +//TODO: FIXME +pub use self::function::FnDescriptor; + pub(crate) enum Def { Module(ModuleDescriptor), Item, @@ -82,7 +84,7 @@ impl<'a> DeclarationDescriptor<'a> { .syntax() .descendants() .filter_map(ast::NameRef::cast) - .filter(|name_ref| match resolve_local_name(*name_ref, &fn_scopes) { + .filter(|name_ref| match fn_scopes.resolve_local_name(*name_ref) { None => false, Some(entry) => entry.ptr() == name_ptr, }) diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 1b7e16ff4..347d44638 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -20,8 +20,7 @@ use crate::{ completion::{completions, CompletionItem}, db::{self, FileSyntaxQuery, SyntaxDatabase}, hir::{ - FunctionDescriptor, ModuleDescriptor, - function::FnDescriptor, + FnDescriptor, FunctionDescriptor, ModuleDescriptor, Problem, DeclarationDescriptor, }, @@ -590,7 +589,7 @@ fn resolve_local_name( let fn_def = name_ref.syntax().ancestors().find_map(ast::FnDef::cast)?; let function = FunctionDescriptor::guess_from_source(db, file_id, fn_def); let scopes = function.scope(db); - let scope_entry = crate::hir::function::resolve_local_name(name_ref, &scopes)?; + let scope_entry = scopes.resolve_local_name(name_ref)?; let syntax = db.resolve_syntax_ptr(scope_entry.ptr().into_global(file_id)); Some((scope_entry.name().clone(), syntax.range())) } diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 4659eb523..a3088c5ad 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -42,7 +42,7 @@ use crate::{ pub use crate::{ completion::CompletionItem, - hir::function::FnDescriptor, + hir::FnDescriptor, input::{CrateGraph, CrateId, FileId, FileResolver}, }; pub use ra_editor::{ -- cgit v1.2.3