mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Some cleanup and additional tests
This commit is contained in:
parent
d571d26955
commit
c5852f422f
7 changed files with 138 additions and 31 deletions
|
@ -46,7 +46,6 @@ pub(crate) enum Scope {
|
|||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Resolution {
|
||||
// FIXME make these tuple variants
|
||||
/// An item
|
||||
Def(ModuleDef),
|
||||
/// A local binding (only value namespace)
|
||||
|
@ -85,7 +84,7 @@ impl Resolver {
|
|||
|
||||
pub fn all_names(&self) -> FxHashMap<Name, PerNs<Resolution>> {
|
||||
let mut names = FxHashMap::default();
|
||||
for scope in &self.scopes {
|
||||
for scope in self.scopes.iter().rev() {
|
||||
scope.collect_names(&mut |name, res| {
|
||||
let current: &mut PerNs<Resolution> = names.entry(name).or_default();
|
||||
if current.types.is_none() {
|
||||
|
|
|
@ -204,12 +204,13 @@ pub fn macro_symbols(db: &impl HirDatabase, file_id: FileId) -> Vec<(SmolStr, Te
|
|||
}
|
||||
|
||||
pub fn resolver_for_position(db: &impl HirDatabase, position: FilePosition) -> Resolver {
|
||||
let file = db.parse(position.file_id);
|
||||
let file_id = position.file_id;
|
||||
let file = db.parse(file_id);
|
||||
find_leaf_at_offset(file.syntax(), position.offset)
|
||||
.find_map(|node| {
|
||||
node.ancestors().find_map(|node| {
|
||||
if ast::Expr::cast(node).is_some() || ast::Block::cast(node).is_some() {
|
||||
if let Some(func) = function_from_child_node(db, position.file_id, node) {
|
||||
if let Some(func) = function_from_child_node(db, file_id, node) {
|
||||
let scopes = func.scopes(db);
|
||||
let scope = scopes.scope_for_offset(position.offset);
|
||||
Some(expr::resolver_for_scope(func.body(db), db, scope))
|
||||
|
@ -218,9 +219,15 @@ pub fn resolver_for_position(db: &impl HirDatabase, position: FilePosition) -> R
|
|||
None
|
||||
}
|
||||
} else if let Some(module) = ast::Module::cast(node) {
|
||||
Some(module_from_declaration(db, position.file_id, module)?.resolver(db))
|
||||
Some(module_from_declaration(db, file_id, module)?.resolver(db))
|
||||
} else if let Some(_) = ast::SourceFile::cast(node) {
|
||||
Some(module_from_source(db, position.file_id.into(), None)?.resolver(db))
|
||||
Some(module_from_source(db, file_id.into(), None)?.resolver(db))
|
||||
} else if let Some(s) = ast::StructDef::cast(node) {
|
||||
let module = module_from_child_node(db, file_id, s.syntax())?;
|
||||
Some(struct_from_module(db, module, s).resolver(db))
|
||||
} else if let Some(e) = ast::EnumDef::cast(node) {
|
||||
let module = module_from_child_node(db, file_id, e.syntax())?;
|
||||
Some(enum_from_module(db, module, e).resolver(db))
|
||||
} else {
|
||||
// TODO add missing cases
|
||||
None
|
||||
|
@ -246,6 +253,12 @@ pub fn resolver_for_node(db: &impl HirDatabase, file_id: FileId, node: &SyntaxNo
|
|||
Some(module_from_declaration(db, file_id, module)?.resolver(db))
|
||||
} else if let Some(_) = ast::SourceFile::cast(node) {
|
||||
Some(module_from_source(db, file_id.into(), None)?.resolver(db))
|
||||
} else if let Some(s) = ast::StructDef::cast(node) {
|
||||
let module = module_from_child_node(db, file_id, s.syntax())?;
|
||||
Some(struct_from_module(db, module, s).resolver(db))
|
||||
} else if let Some(e) = ast::EnumDef::cast(node) {
|
||||
let module = module_from_child_node(db, file_id, e.syntax())?;
|
||||
Some(enum_from_module(db, module, e).resolver(db))
|
||||
} else {
|
||||
// TODO add missing cases
|
||||
None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue