mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
move resolve_local_scope
This commit is contained in:
parent
93fae08261
commit
67de38ec7d
6 changed files with 27 additions and 27 deletions
|
@ -12,7 +12,7 @@ use crate::{
|
|||
completion::CompletionItem,
|
||||
hir::{
|
||||
ModuleDescriptor,
|
||||
function::FnScopes,
|
||||
FnScopes,
|
||||
Def,
|
||||
Path,
|
||||
},
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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::<ast::Name>(file.syntax(), expected_offset.into()).unwrap();
|
||||
|
|
|
@ -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,
|
||||
})
|
||||
|
|
|
@ -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()))
|
||||
}
|
||||
|
|
|
@ -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::{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue