Skip to content

Commit

Permalink
Auto merge of #14715 - Veykril:symbol-index, r=Veykril
Browse files Browse the repository at this point in the history
Refactor symbol index

Closes #14677

instead of eagerly fetching the source data in symbol index we do it lazily now, this shouldn't make it much more expensive as we had to parse the source most of the time anyways even after fetching.
  • Loading branch information
bors committed May 2, 2023
2 parents 86b14c2 + f501c6a commit 94ac1cd
Show file tree
Hide file tree
Showing 13 changed files with 422 additions and 684 deletions.
97 changes: 91 additions & 6 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use hir_def::{
per_ns::PerNs,
resolver::{HasResolver, Resolver},
src::HasSource as _,
AdtId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, DefWithBodyId, EnumId,
AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, DefWithBodyId, EnumId,
EnumVariantId, FunctionId, GenericDefId, HasModule, ImplId, ItemContainerId, LifetimeParamId,
LocalEnumVariantId, LocalFieldId, Lookup, MacroExpander, MacroId, ModuleId, StaticId, StructId,
TraitAliasId, TraitId, TypeAliasId, TypeOrConstParamId, TypeParamId, UnionId,
Expand Down Expand Up @@ -119,11 +119,9 @@ pub use {
path::{ModPath, PathKind},
type_ref::{Mutability, TypeRef},
visibility::Visibility,
// FIXME: This is here since it is input of a method in `HirWrite`
// and things outside of hir need to implement that trait. We probably
// should move whole `hir_ty::display` to this crate so we will become
// able to use `ModuleDef` or `Definition` instead of `ModuleDefId`.
ModuleDefId,
// FIXME: This is here since some queries take it as input that are used
// outside of hir.
{AdtId, ModuleDefId},
},
hir_expand::{
attrs::Attr,
Expand Down Expand Up @@ -4429,3 +4427,90 @@ impl HasCrate for Module {
Module::krate(*self)
}
}

pub trait HasContainer {
fn container(&self, db: &dyn HirDatabase) -> ItemContainer;
}

impl HasContainer for Module {
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
// FIXME: handle block expressions as modules (their parent is in a different DefMap)
let def_map = self.id.def_map(db.upcast());
match def_map[self.id.local_id].parent {
Some(parent_id) => ItemContainer::Module(Module { id: def_map.module_id(parent_id) }),
None => ItemContainer::Crate(def_map.krate()),
}
}
}

impl HasContainer for Function {
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
container_id_to_hir(self.id.lookup(db.upcast()).container)
}
}

impl HasContainer for Struct {
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
ItemContainer::Module(Module { id: self.id.lookup(db.upcast()).container })
}
}

impl HasContainer for Union {
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
ItemContainer::Module(Module { id: self.id.lookup(db.upcast()).container })
}
}

impl HasContainer for Enum {
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
ItemContainer::Module(Module { id: self.id.lookup(db.upcast()).container })
}
}

impl HasContainer for TypeAlias {
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
container_id_to_hir(self.id.lookup(db.upcast()).container)
}
}

impl HasContainer for Const {
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
container_id_to_hir(self.id.lookup(db.upcast()).container)
}
}

impl HasContainer for Static {
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
container_id_to_hir(self.id.lookup(db.upcast()).container)
}
}

impl HasContainer for Trait {
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
ItemContainer::Module(Module { id: self.id.lookup(db.upcast()).container })
}
}

impl HasContainer for TraitAlias {
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
ItemContainer::Module(Module { id: self.id.lookup(db.upcast()).container })
}
}

fn container_id_to_hir(c: ItemContainerId) -> ItemContainer {
match c {
ItemContainerId::ExternBlockId(_id) => ItemContainer::ExternBlock(),
ItemContainerId::ModuleId(id) => ItemContainer::Module(Module { id }),
ItemContainerId::ImplId(id) => ItemContainer::Impl(Impl { id }),
ItemContainerId::TraitId(id) => ItemContainer::Trait(Trait { id }),
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ItemContainer {
Trait(Trait),
Impl(Impl),
Module(Module),
ExternBlock(),
Crate(CrateId),
}
Loading

0 comments on commit 94ac1cd

Please sign in to comment.