Skip to content

Commit f501c6a

Browse files
committed
Refactor symbol index
1 parent 9c0c13e commit f501c6a

13 files changed

+422
-684
lines changed

crates/hir/src/lib.rs

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use hir_def::{
5151
per_ns::PerNs,
5252
resolver::{HasResolver, Resolver},
5353
src::HasSource as _,
54-
AdtId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, DefWithBodyId, EnumId,
54+
AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, DefWithBodyId, EnumId,
5555
EnumVariantId, FunctionId, GenericDefId, HasModule, ImplId, ItemContainerId, LifetimeParamId,
5656
LocalEnumVariantId, LocalFieldId, Lookup, MacroExpander, MacroId, ModuleId, StaticId, StructId,
5757
TraitAliasId, TraitId, TypeAliasId, TypeOrConstParamId, TypeParamId, UnionId,
@@ -118,11 +118,9 @@ pub use {
118118
path::{ModPath, PathKind},
119119
type_ref::{Mutability, TypeRef},
120120
visibility::Visibility,
121-
// FIXME: This is here since it is input of a method in `HirWrite`
122-
// and things outside of hir need to implement that trait. We probably
123-
// should move whole `hir_ty::display` to this crate so we will become
124-
// able to use `ModuleDef` or `Definition` instead of `ModuleDefId`.
125-
ModuleDefId,
121+
// FIXME: This is here since some queries take it as input that are used
122+
// outside of hir.
123+
{AdtId, ModuleDefId},
126124
},
127125
hir_expand::{
128126
attrs::Attr,
@@ -4408,3 +4406,90 @@ impl HasCrate for Module {
44084406
Module::krate(*self)
44094407
}
44104408
}
4409+
4410+
pub trait HasContainer {
4411+
fn container(&self, db: &dyn HirDatabase) -> ItemContainer;
4412+
}
4413+
4414+
impl HasContainer for Module {
4415+
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
4416+
// FIXME: handle block expressions as modules (their parent is in a different DefMap)
4417+
let def_map = self.id.def_map(db.upcast());
4418+
match def_map[self.id.local_id].parent {
4419+
Some(parent_id) => ItemContainer::Module(Module { id: def_map.module_id(parent_id) }),
4420+
None => ItemContainer::Crate(def_map.krate()),
4421+
}
4422+
}
4423+
}
4424+
4425+
impl HasContainer for Function {
4426+
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
4427+
container_id_to_hir(self.id.lookup(db.upcast()).container)
4428+
}
4429+
}
4430+
4431+
impl HasContainer for Struct {
4432+
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
4433+
ItemContainer::Module(Module { id: self.id.lookup(db.upcast()).container })
4434+
}
4435+
}
4436+
4437+
impl HasContainer for Union {
4438+
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
4439+
ItemContainer::Module(Module { id: self.id.lookup(db.upcast()).container })
4440+
}
4441+
}
4442+
4443+
impl HasContainer for Enum {
4444+
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
4445+
ItemContainer::Module(Module { id: self.id.lookup(db.upcast()).container })
4446+
}
4447+
}
4448+
4449+
impl HasContainer for TypeAlias {
4450+
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
4451+
container_id_to_hir(self.id.lookup(db.upcast()).container)
4452+
}
4453+
}
4454+
4455+
impl HasContainer for Const {
4456+
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
4457+
container_id_to_hir(self.id.lookup(db.upcast()).container)
4458+
}
4459+
}
4460+
4461+
impl HasContainer for Static {
4462+
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
4463+
container_id_to_hir(self.id.lookup(db.upcast()).container)
4464+
}
4465+
}
4466+
4467+
impl HasContainer for Trait {
4468+
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
4469+
ItemContainer::Module(Module { id: self.id.lookup(db.upcast()).container })
4470+
}
4471+
}
4472+
4473+
impl HasContainer for TraitAlias {
4474+
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
4475+
ItemContainer::Module(Module { id: self.id.lookup(db.upcast()).container })
4476+
}
4477+
}
4478+
4479+
fn container_id_to_hir(c: ItemContainerId) -> ItemContainer {
4480+
match c {
4481+
ItemContainerId::ExternBlockId(_id) => ItemContainer::ExternBlock(),
4482+
ItemContainerId::ModuleId(id) => ItemContainer::Module(Module { id }),
4483+
ItemContainerId::ImplId(id) => ItemContainer::Impl(Impl { id }),
4484+
ItemContainerId::TraitId(id) => ItemContainer::Trait(Trait { id }),
4485+
}
4486+
}
4487+
4488+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4489+
pub enum ItemContainer {
4490+
Trait(Trait),
4491+
Impl(Impl),
4492+
Module(Module),
4493+
ExternBlock(),
4494+
Crate(CrateId),
4495+
}

0 commit comments

Comments
 (0)