2020-08-25 05:57:15 -05:00
|
|
|
//! Attributes & documentation for hir types.
|
2020-08-25 05:56:01 -05:00
|
|
|
use hir_def::{
|
|
|
|
attr::Attrs,
|
|
|
|
docs::Documentation,
|
|
|
|
resolver::{HasResolver, Resolver},
|
2020-08-25 07:44:15 -05:00
|
|
|
AdtId, AttrDefId, FunctionId, GenericDefId, ModuleId, StaticId, TraitId, VariantId,
|
2020-08-25 05:56:01 -05:00
|
|
|
};
|
|
|
|
use hir_ty::db::HirDatabase;
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
doc_links::Resolvable, Adt, Const, Enum, EnumVariant, Field, Function, GenericDef, ImplDef,
|
|
|
|
Local, MacroDef, Module, ModuleDef, Static, Struct, Trait, TypeAlias, TypeParam, Union,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub trait HasAttrs {
|
|
|
|
fn attrs(self, db: &dyn HirDatabase) -> Attrs;
|
|
|
|
fn docs(self, db: &dyn HirDatabase) -> Option<Documentation>;
|
|
|
|
}
|
|
|
|
|
2020-08-25 07:44:15 -05:00
|
|
|
macro_rules! impl_has_attrs {
|
|
|
|
($(($def:ident, $def_id:ident),)*) => {$(
|
|
|
|
impl HasAttrs for $def {
|
|
|
|
fn attrs(self, db: &dyn HirDatabase) -> Attrs {
|
|
|
|
let def = AttrDefId::$def_id(self.into());
|
|
|
|
db.attrs(def)
|
|
|
|
}
|
|
|
|
fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> {
|
|
|
|
let def = AttrDefId::$def_id(self.into());
|
|
|
|
db.documentation(def)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*};
|
2020-08-25 05:56:01 -05:00
|
|
|
}
|
|
|
|
|
2020-08-25 07:44:15 -05:00
|
|
|
impl_has_attrs![
|
|
|
|
(Field, FieldId),
|
|
|
|
(EnumVariant, EnumVariantId),
|
|
|
|
(Static, StaticId),
|
|
|
|
(Const, ConstId),
|
|
|
|
(Trait, TraitId),
|
|
|
|
(TypeAlias, TypeAliasId),
|
|
|
|
(MacroDef, MacroDefId),
|
|
|
|
(Function, FunctionId),
|
|
|
|
(Adt, AdtId),
|
|
|
|
(Module, ModuleId),
|
|
|
|
];
|
|
|
|
|
|
|
|
macro_rules! impl_has_attrs_adt {
|
|
|
|
($($adt:ident),*) => {$(
|
|
|
|
impl HasAttrs for $adt {
|
|
|
|
fn attrs(self, db: &dyn HirDatabase) -> Attrs {
|
|
|
|
Adt::$adt(self).attrs(db)
|
|
|
|
}
|
|
|
|
fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> {
|
|
|
|
Adt::$adt(self).docs(db)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*};
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_has_attrs_adt![Struct, Union, Enum];
|
|
|
|
|
2020-08-25 05:56:01 -05:00
|
|
|
impl Resolvable for ModuleDef {
|
2020-08-25 07:57:26 -05:00
|
|
|
fn resolver(&self, db: &dyn HirDatabase) -> Option<Resolver> {
|
2020-08-25 05:56:01 -05:00
|
|
|
Some(match self {
|
2020-08-25 07:57:26 -05:00
|
|
|
ModuleDef::Module(m) => ModuleId::from(m.clone()).resolver(db.upcast()),
|
|
|
|
ModuleDef::Function(f) => FunctionId::from(f.clone()).resolver(db.upcast()),
|
|
|
|
ModuleDef::Adt(adt) => AdtId::from(adt.clone()).resolver(db.upcast()),
|
2020-08-25 05:56:01 -05:00
|
|
|
ModuleDef::EnumVariant(ev) => {
|
2020-08-25 07:57:26 -05:00
|
|
|
GenericDefId::from(GenericDef::from(ev.clone())).resolver(db.upcast())
|
2020-08-25 05:56:01 -05:00
|
|
|
}
|
2020-08-25 07:57:26 -05:00
|
|
|
ModuleDef::Const(c) => {
|
|
|
|
GenericDefId::from(GenericDef::from(c.clone())).resolver(db.upcast())
|
|
|
|
}
|
|
|
|
ModuleDef::Static(s) => StaticId::from(s.clone()).resolver(db.upcast()),
|
|
|
|
ModuleDef::Trait(t) => TraitId::from(t.clone()).resolver(db.upcast()),
|
|
|
|
ModuleDef::TypeAlias(t) => ModuleId::from(t.module(db)).resolver(db.upcast()),
|
2020-08-25 05:56:01 -05:00
|
|
|
// FIXME: This should be a resolver relative to `std/core`
|
|
|
|
ModuleDef::BuiltinType(_t) => None?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn try_into_module_def(self) -> Option<ModuleDef> {
|
|
|
|
Some(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resolvable for TypeParam {
|
2020-08-25 07:57:26 -05:00
|
|
|
fn resolver(&self, db: &dyn HirDatabase) -> Option<Resolver> {
|
|
|
|
Some(ModuleId::from(self.module(db)).resolver(db.upcast()))
|
2020-08-25 05:56:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn try_into_module_def(self) -> Option<ModuleDef> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resolvable for MacroDef {
|
2020-08-25 07:57:26 -05:00
|
|
|
fn resolver(&self, db: &dyn HirDatabase) -> Option<Resolver> {
|
|
|
|
Some(ModuleId::from(self.module(db)?).resolver(db.upcast()))
|
2020-08-25 05:56:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn try_into_module_def(self) -> Option<ModuleDef> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resolvable for Field {
|
2020-08-25 07:57:26 -05:00
|
|
|
fn resolver(&self, db: &dyn HirDatabase) -> Option<Resolver> {
|
|
|
|
Some(VariantId::from(self.parent_def(db)).resolver(db.upcast()))
|
2020-08-25 05:56:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn try_into_module_def(self) -> Option<ModuleDef> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resolvable for ImplDef {
|
2020-08-25 07:57:26 -05:00
|
|
|
fn resolver(&self, db: &dyn HirDatabase) -> Option<Resolver> {
|
|
|
|
Some(ModuleId::from(self.module(db)).resolver(db.upcast()))
|
2020-08-25 05:56:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn try_into_module_def(self) -> Option<ModuleDef> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resolvable for Local {
|
2020-08-25 07:57:26 -05:00
|
|
|
fn resolver(&self, db: &dyn HirDatabase) -> Option<Resolver> {
|
|
|
|
Some(ModuleId::from(self.module(db)).resolver(db.upcast()))
|
2020-08-25 05:56:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn try_into_module_def(self) -> Option<ModuleDef> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|