simplify work

This commit is contained in:
Jake Heinz 2021-11-29 07:17:18 +00:00
parent 6e89fb6f73
commit 176f4da77a
2 changed files with 25 additions and 29 deletions

View file

@ -103,6 +103,7 @@ pub use {
hir_def::{ hir_def::{
adt::StructKind, adt::StructKind,
attr::{Attr, Attrs, AttrsWithOwner, Documentation}, attr::{Attr, Attrs, AttrsWithOwner, Documentation},
data::TraitData,
find_path::PrefixKind, find_path::PrefixKind,
import_map, import_map,
item_scope::ItemScope, item_scope::ItemScope,

View file

@ -436,11 +436,11 @@ fn source_file_to_file_symbols(_source_file: &SourceFile, _file_id: FileId) -> V
// todo: delete this. // todo: delete this.
vec![] vec![]
} }
enum SymbolCollectorWork {
Module { module_id: ModuleId, parent: Option<DefWithBodyId> }, /// Represents an outstanding module that the symbol collector must collect symbols from.
Body { body_id: DefWithBodyId }, struct SymbolCollectorWork {
Impl { impl_id: ImplId }, module_id: ModuleId,
Trait { trait_id: TraitId }, parent: Option<DefWithBodyId>,
} }
struct SymbolCollector<'a> { struct SymbolCollector<'a> {
@ -458,7 +458,9 @@ impl<'a> SymbolCollector<'a> {
db, db,
symbols: Default::default(), symbols: Default::default(),
container_name_stack: Default::default(), container_name_stack: Default::default(),
work: vec![SymbolCollectorWork::Module { module_id, parent: None }], // The initial work is the root module we're collecting, additional work will
// be populated as we traverse the module's definitions.
work: vec![SymbolCollectorWork { module_id, parent: None }],
}; };
while let Some(work) = symbol_collector.work.pop() { while let Some(work) = symbol_collector.work.pop() {
@ -471,18 +473,8 @@ impl<'a> SymbolCollector<'a> {
fn do_work(&mut self, work: SymbolCollectorWork) { fn do_work(&mut self, work: SymbolCollectorWork) {
self.db.unwind_if_cancelled(); self.db.unwind_if_cancelled();
match work { let parent_name = work.parent.and_then(|id| self.def_with_body_id_name(id));
SymbolCollectorWork::Module { module_id, parent } => { self.with_container_name(parent_name, |s| s.collect_from_module(work.module_id));
let parent_name = parent.and_then(|id| self.def_with_body_id_name(id));
self.with_container_name(parent_name, |s| s.collect_from_module(module_id));
}
SymbolCollectorWork::Trait { trait_id } => {
let trait_name = self.db.trait_data(trait_id).name.as_text();
self.with_container_name(trait_name, |s| s.collect_from_trait(trait_id));
}
SymbolCollectorWork::Body { body_id } => self.collect_from_body(body_id),
SymbolCollectorWork::Impl { impl_id } => self.collect_from_impl(impl_id),
}
} }
fn collect_from_module(&mut self, module_id: ModuleId) { fn collect_from_module(&mut self, module_id: ModuleId) {
@ -495,7 +487,7 @@ impl<'a> SymbolCollector<'a> {
ModuleDefId::ModuleId(id) => self.push_module(id), ModuleDefId::ModuleId(id) => self.push_module(id),
ModuleDefId::FunctionId(id) => { ModuleDefId::FunctionId(id) => {
self.push_decl_assoc(id, FileSymbolKind::Function); self.push_decl_assoc(id, FileSymbolKind::Function);
self.work.push(SymbolCollectorWork::Body { body_id: id.into() }); self.collect_from_body(id);
} }
ModuleDefId::AdtId(AdtId::StructId(id)) => { ModuleDefId::AdtId(AdtId::StructId(id)) => {
self.push_decl(id, FileSymbolKind::Struct) self.push_decl(id, FileSymbolKind::Struct)
@ -504,15 +496,15 @@ impl<'a> SymbolCollector<'a> {
ModuleDefId::AdtId(AdtId::UnionId(id)) => self.push_decl(id, FileSymbolKind::Union), ModuleDefId::AdtId(AdtId::UnionId(id)) => self.push_decl(id, FileSymbolKind::Union),
ModuleDefId::ConstId(id) => { ModuleDefId::ConstId(id) => {
self.push_decl_assoc(id, FileSymbolKind::Const); self.push_decl_assoc(id, FileSymbolKind::Const);
self.work.push(SymbolCollectorWork::Body { body_id: id.into() }) self.collect_from_body(id);
} }
ModuleDefId::StaticId(id) => { ModuleDefId::StaticId(id) => {
self.push_decl(id, FileSymbolKind::Static); self.push_decl(id, FileSymbolKind::Static);
self.work.push(SymbolCollectorWork::Body { body_id: id.into() }) self.collect_from_body(id);
} }
ModuleDefId::TraitId(id) => { ModuleDefId::TraitId(id) => {
self.push_decl(id, FileSymbolKind::Trait); self.push_decl(id, FileSymbolKind::Trait);
self.work.push(SymbolCollectorWork::Trait { trait_id: id }) self.collect_from_trait(id);
} }
ModuleDefId::TypeAliasId(id) => { ModuleDefId::TypeAliasId(id) => {
self.push_decl_assoc(id, FileSymbolKind::TypeAlias); self.push_decl_assoc(id, FileSymbolKind::TypeAlias);
@ -524,11 +516,11 @@ impl<'a> SymbolCollector<'a> {
} }
for impl_id in scope.impls() { for impl_id in scope.impls() {
self.work.push(SymbolCollectorWork::Impl { impl_id }); self.collect_from_impl(impl_id);
} }
for const_id in scope.unnamed_consts() { for const_id in scope.unnamed_consts() {
self.work.push(SymbolCollectorWork::Body { body_id: const_id.into() }) self.collect_from_body(const_id);
} }
for macro_def_id in scope.macro_declarations() { for macro_def_id in scope.macro_declarations() {
@ -536,13 +528,14 @@ impl<'a> SymbolCollector<'a> {
} }
} }
fn collect_from_body(&mut self, body_id: DefWithBodyId) { fn collect_from_body(&mut self, body_id: impl Into<DefWithBodyId>) {
let body_id = body_id.into();
let body = self.db.body(body_id); let body = self.db.body(body_id);
// Descend into the blocks and enqueue collection of all modules within. // Descend into the blocks and enqueue collection of all modules within.
for (_, def_map) in body.blocks(self.db.upcast()) { for (_, def_map) in body.blocks(self.db.upcast()) {
for (id, _) in def_map.modules() { for (id, _) in def_map.modules() {
self.work.push(SymbolCollectorWork::Module { self.work.push(SymbolCollectorWork {
module_id: def_map.module_id(id), module_id: def_map.module_id(id),
parent: Some(body_id), parent: Some(body_id),
}); });
@ -559,9 +552,11 @@ impl<'a> SymbolCollector<'a> {
fn collect_from_trait(&mut self, trait_id: TraitId) { fn collect_from_trait(&mut self, trait_id: TraitId) {
let trait_data = self.db.trait_data(trait_id); let trait_data = self.db.trait_data(trait_id);
self.with_container_name(trait_data.name.as_text(), |s| {
for &(_, assoc_item_id) in &trait_data.items { for &(_, assoc_item_id) in &trait_data.items {
self.push_assoc_item(assoc_item_id); s.push_assoc_item(assoc_item_id);
} }
});
} }
fn with_container_name(&mut self, container_name: Option<SmolStr>, f: impl FnOnce(&mut Self)) { fn with_container_name(&mut self, container_name: Option<SmolStr>, f: impl FnOnce(&mut Self)) {