move resolve_local_scope

This commit is contained in:
Aleksey Kladov 2018-11-27 23:40:23 +03:00
parent 93fae08261
commit 67de38ec7d
6 changed files with 27 additions and 27 deletions

View file

@ -12,7 +12,7 @@ use crate::{
completion::CompletionItem, completion::CompletionItem,
hir::{ hir::{
ModuleDescriptor, ModuleDescriptor,
function::FnScopes, FnScopes,
Def, Def,
Path, Path,
}, },

View file

@ -15,7 +15,7 @@ use crate::{
syntax_ptr::SyntaxPtr, FileId, 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; pub(crate) use crate::loc2id::FnId;
impl FnId { impl FnId {

View file

@ -57,6 +57,19 @@ impl FnScopes {
self.scopes[scope].parent 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 { fn root_scope(&mut self) -> ScopeId {
self.scopes.alloc(ScopeData { self.scopes.alloc(ScopeData {
parent: None, 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)] #[cfg(test)]
mod tests { mod tests {
use ra_editor::find_node_at_offset; use ra_editor::find_node_at_offset;
@ -376,7 +375,7 @@ mod tests {
let scopes = FnScopes::new(fn_def); 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 local_name = local_name_entry.ptr().resolve(&file);
let expected_name = let expected_name =
find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into()).unwrap(); find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into()).unwrap();

View file

@ -5,11 +5,11 @@
//! to a particular crate instance. That is, it has cfg flags and features //! 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. //! applied. So, there relation between syntax and HIR is many-to-one.
pub(crate) mod function;
mod module;
pub(crate) mod db; pub(crate) mod db;
mod path;
mod query_definitions; mod query_definitions;
mod function;
mod module;
mod path;
use ra_syntax::{ use ra_syntax::{
ast::{self, AstNode}, ast::{self, AstNode},
@ -18,7 +18,6 @@ use ra_syntax::{
use crate::{ use crate::{
hir::db::HirDatabase, hir::db::HirDatabase,
hir::function::{resolve_local_name, FnScopes},
loc2id::{DefId, DefLoc}, loc2id::{DefId, DefLoc},
syntax_ptr::LocalSyntaxPtr, syntax_ptr::LocalSyntaxPtr,
Cancelable, Cancelable,
@ -27,9 +26,12 @@ use crate::{
pub(crate) use self::{ pub(crate) use self::{
path::{Path, PathKind}, path::{Path, PathKind},
module::{ModuleDescriptor, ModuleId, Problem, nameres::FileItemId}, module::{ModuleDescriptor, ModuleId, Problem, nameres::FileItemId},
function::FunctionDescriptor, function::{FunctionDescriptor, FnScopes},
}; };
//TODO: FIXME
pub use self::function::FnDescriptor;
pub(crate) enum Def { pub(crate) enum Def {
Module(ModuleDescriptor), Module(ModuleDescriptor),
Item, Item,
@ -82,7 +84,7 @@ impl<'a> DeclarationDescriptor<'a> {
.syntax() .syntax()
.descendants() .descendants()
.filter_map(ast::NameRef::cast) .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, None => false,
Some(entry) => entry.ptr() == name_ptr, Some(entry) => entry.ptr() == name_ptr,
}) })

View file

@ -20,8 +20,7 @@ use crate::{
completion::{completions, CompletionItem}, completion::{completions, CompletionItem},
db::{self, FileSyntaxQuery, SyntaxDatabase}, db::{self, FileSyntaxQuery, SyntaxDatabase},
hir::{ hir::{
FunctionDescriptor, ModuleDescriptor, FnDescriptor, FunctionDescriptor, ModuleDescriptor,
function::FnDescriptor,
Problem, Problem,
DeclarationDescriptor, DeclarationDescriptor,
}, },
@ -590,7 +589,7 @@ fn resolve_local_name(
let fn_def = name_ref.syntax().ancestors().find_map(ast::FnDef::cast)?; let fn_def = name_ref.syntax().ancestors().find_map(ast::FnDef::cast)?;
let function = FunctionDescriptor::guess_from_source(db, file_id, fn_def); let function = FunctionDescriptor::guess_from_source(db, file_id, fn_def);
let scopes = function.scope(db); 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)); let syntax = db.resolve_syntax_ptr(scope_entry.ptr().into_global(file_id));
Some((scope_entry.name().clone(), syntax.range())) Some((scope_entry.name().clone(), syntax.range()))
} }

View file

@ -42,7 +42,7 @@ use crate::{
pub use crate::{ pub use crate::{
completion::CompletionItem, completion::CompletionItem,
hir::function::FnDescriptor, hir::FnDescriptor,
input::{CrateGraph, CrateId, FileId, FileResolver}, input::{CrateGraph, CrateId, FileId, FileResolver},
}; };
pub use ra_editor::{ pub use ra_editor::{