use either::Either;
use hir_expand::HirFileId;
-use itertools::Itertools;
-use syntax::ast::HasAttrs;
+use syntax::ast::HasDocComments;
use crate::{
db::DefDatabase,
impl ChildBySource for TraitId {
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
let data = db.trait_data(*self);
- for (_name, item) in data.items.iter() {
- match *item {
- AssocItemId::FunctionId(func) => {
- let loc = func.lookup(db);
- if loc.id.file_id() == file_id {
- let src = loc.source(db);
- res[keys::FUNCTION].insert(src, func)
- }
- }
- AssocItemId::ConstId(konst) => {
- let loc = konst.lookup(db);
- if loc.id.file_id() == file_id {
- let src = loc.source(db);
- res[keys::CONST].insert(src, konst)
- }
- }
- AssocItemId::TypeAliasId(ty) => {
- let loc = ty.lookup(db);
- if loc.id.file_id() == file_id {
- let src = loc.source(db);
- res[keys::TYPE_ALIAS].insert(src, ty)
- }
- }
- }
- }
+
+ data.attribute_calls().filter(|(ast_id, _)| ast_id.file_id == file_id).for_each(
+ |(ast_id, call_id)| {
+ res[keys::ATTR_MACRO_CALL].insert(ast_id.to_node(db.upcast()), call_id);
+ },
+ );
+ data.items.iter().for_each(|&(_, item)| {
+ add_assoc_item(db, res, file_id, item);
+ });
}
}
impl ChildBySource for ImplId {
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
let data = db.impl_data(*self);
- for &item in data.items.iter() {
- match item {
- AssocItemId::FunctionId(func) => {
- let loc = func.lookup(db);
- if loc.id.file_id() == file_id {
- let src = loc.source(db);
- res[keys::FUNCTION].insert(src, func)
- }
- }
- AssocItemId::ConstId(konst) => {
- let loc = konst.lookup(db);
- if loc.id.file_id() == file_id {
- let src = loc.source(db);
- res[keys::CONST].insert(src, konst)
- }
- }
- AssocItemId::TypeAliasId(ty) => {
- let loc = ty.lookup(db);
- if loc.id.file_id() == file_id {
- let src = loc.source(db);
- res[keys::TYPE_ALIAS].insert(src, ty)
- }
- }
+ data.attribute_calls().filter(|(ast_id, _)| ast_id.file_id == file_id).for_each(
+ |(ast_id, call_id)| {
+ res[keys::ATTR_MACRO_CALL].insert(ast_id.to_node(db.upcast()), call_id);
+ },
+ );
+ data.items.iter().for_each(|&item| {
+ add_assoc_item(db, res, file_id, item);
+ });
+ }
+}
+
+fn add_assoc_item(db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId, item: AssocItemId) {
+ match item {
+ AssocItemId::FunctionId(func) => {
+ let loc = func.lookup(db);
+ if loc.id.file_id() == file_id {
+ res[keys::FUNCTION].insert(loc.source(db).value, func)
+ }
+ }
+ AssocItemId::ConstId(konst) => {
+ let loc = konst.lookup(db);
+ if loc.id.file_id() == file_id {
+ res[keys::CONST].insert(loc.source(db).value, konst)
+ }
+ }
+ AssocItemId::TypeAliasId(ty) => {
+ let loc = ty.lookup(db);
+ if loc.id.file_id() == file_id {
+ res[keys::TYPE_ALIAS].insert(loc.source(db).value, ty)
}
}
}
impl ChildBySource for ItemScope {
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
- self.declarations().for_each(|item| add_module_def(db, file_id, res, item));
+ self.declarations().for_each(|item| add_module_def(db, res, file_id, item));
+ self.impls().for_each(|imp| add_impl(db, res, file_id, imp));
+ self.unnamed_consts().for_each(|konst| {
+ let loc = konst.lookup(db);
+ if loc.id.file_id() == file_id {
+ res[keys::CONST].insert(loc.source(db).value, konst);
+ }
+ });
self.macros().for_each(|(_, makro)| {
let ast_id = makro.ast_id();
if ast_id.either(|it| it.file_id, |it| it.file_id) == file_id {
let src = match ast_id {
- Either::Left(ast_id) => ast_id.with_value(ast_id.to_node(db.upcast())),
+ Either::Left(ast_id) => ast_id.to_node(db.upcast()),
// FIXME: Do we need to add proc-macros into a PROCMACRO dynmap here?
Either::Right(_fn) => return,
};
res[keys::MACRO].insert(src, makro);
}
});
- self.unnamed_consts().for_each(|konst| {
- let src = konst.lookup(db).source(db);
- res[keys::CONST].insert(src, konst);
- });
- self.impls().for_each(|imp| add_impl(db, file_id, res, imp));
- self.attr_macro_invocs().for_each(|(ast_id, call_id)| {
- let item = ast_id.with_value(ast_id.to_node(db.upcast()));
- res[keys::ATTR_MACRO].insert(item, call_id);
- });
- self.derive_macro_invocs().for_each(|(ast_id, calls)| {
- let item = ast_id.to_node(db.upcast());
- let grouped = calls.iter().copied().into_group_map();
- for (attr_id, calls) in grouped {
- if let Some(attr) = item.attrs().nth(attr_id.ast_index as usize) {
- res[keys::DERIVE_MACRO].insert(ast_id.with_value(attr), calls.into());
- }
- }
- });
+ self.attr_macro_invocs().filter(|(id, _)| id.file_id == file_id).for_each(
+ |(ast_id, call_id)| {
+ res[keys::ATTR_MACRO_CALL].insert(ast_id.to_node(db.upcast()), call_id);
+ },
+ );
+ self.derive_macro_invocs().filter(|(id, _)| id.file_id == file_id).for_each(
+ |(ast_id, calls)| {
+ let adt = ast_id.to_node(db.upcast());
+ calls.for_each(|(attr_id, calls)| {
+ if let Some(Either::Left(attr)) =
+ adt.doc_comments_and_attrs().nth(attr_id.ast_index as usize)
+ {
+ res[keys::DERIVE_MACRO_CALL].insert(attr, (attr_id, calls.into()));
+ }
+ });
+ },
+ );
fn add_module_def(
db: &dyn DefDatabase,
- file_id: HirFileId,
map: &mut DynMap,
+ file_id: HirFileId,
item: ModuleDefId,
) {
- match item {
- ModuleDefId::FunctionId(func) => {
- let loc = func.lookup(db);
+ macro_rules! insert {
+ ($map:ident[$key:path].$insert:ident($id:ident)) => {{
+ let loc = $id.lookup(db);
if loc.id.file_id() == file_id {
- let src = loc.source(db);
- map[keys::FUNCTION].insert(src, func)
+ $map[$key].$insert(loc.source(db).value, $id)
}
- }
- ModuleDefId::ConstId(konst) => {
- let loc = konst.lookup(db);
- if loc.id.file_id() == file_id {
- let src = loc.source(db);
- map[keys::CONST].insert(src, konst)
- }
- }
- ModuleDefId::StaticId(statik) => {
- let loc = statik.lookup(db);
- if loc.id.file_id() == file_id {
- let src = loc.source(db);
- map[keys::STATIC].insert(src, statik)
- }
- }
- ModuleDefId::TypeAliasId(ty) => {
- let loc = ty.lookup(db);
- if loc.id.file_id() == file_id {
- let src = loc.source(db);
- map[keys::TYPE_ALIAS].insert(src, ty)
- }
- }
- ModuleDefId::TraitId(trait_) => {
- let loc = trait_.lookup(db);
- if loc.id.file_id() == file_id {
- let src = loc.source(db);
- map[keys::TRAIT].insert(src, trait_)
- }
- }
+ }};
+ }
+ match item {
+ ModuleDefId::FunctionId(id) => insert!(map[keys::FUNCTION].insert(id)),
+ ModuleDefId::ConstId(id) => insert!(map[keys::CONST].insert(id)),
+ ModuleDefId::StaticId(id) => insert!(map[keys::STATIC].insert(id)),
+ ModuleDefId::TypeAliasId(id) => insert!(map[keys::TYPE_ALIAS].insert(id)),
+ ModuleDefId::TraitId(id) => insert!(map[keys::TRAIT].insert(id)),
ModuleDefId::AdtId(adt) => match adt {
- AdtId::StructId(strukt) => {
- let loc = strukt.lookup(db);
- if loc.id.file_id() == file_id {
- let src = loc.source(db);
- map[keys::STRUCT].insert(src, strukt)
- }
- }
- AdtId::UnionId(union_) => {
- let loc = union_.lookup(db);
- if loc.id.file_id() == file_id {
- let src = loc.source(db);
- map[keys::UNION].insert(src, union_)
- }
- }
- AdtId::EnumId(enum_) => {
- let loc = enum_.lookup(db);
- if loc.id.file_id() == file_id {
- let src = loc.source(db);
- map[keys::ENUM].insert(src, enum_)
- }
- }
+ AdtId::StructId(id) => insert!(map[keys::STRUCT].insert(id)),
+ AdtId::UnionId(id) => insert!(map[keys::UNION].insert(id)),
+ AdtId::EnumId(id) => insert!(map[keys::ENUM].insert(id)),
},
_ => (),
}
}
- fn add_impl(db: &dyn DefDatabase, file_id: HirFileId, map: &mut DynMap, imp: ImplId) {
+ fn add_impl(db: &dyn DefDatabase, map: &mut DynMap, file_id: HirFileId, imp: ImplId) {
let loc = imp.lookup(db);
if loc.id.file_id() == file_id {
- let src = loc.source(db);
- map[keys::IMPL].insert(src, imp)
+ map[keys::IMPL].insert(loc.source(db).value, imp)
}
}
}
for (local_id, source) in arena_map.value.iter() {
let id = FieldId { parent, local_id };
match source.clone() {
- Either::Left(source) => {
- res[keys::TUPLE_FIELD].insert(arena_map.with_value(source), id)
- }
- Either::Right(source) => {
- res[keys::RECORD_FIELD].insert(arena_map.with_value(source), id)
- }
+ Either::Left(source) => res[keys::TUPLE_FIELD].insert(source, id),
+ Either::Right(source) => res[keys::RECORD_FIELD].insert(source, id),
}
}
}
let arena_map = arena_map.as_ref();
for (local_id, source) in arena_map.value.iter() {
let id = EnumVariantId { parent: *self, local_id };
- res[keys::VARIANT].insert(arena_map.with_value(source.clone()), id)
+ res[keys::VARIANT].insert(source.clone(), id)
}
}
}