2020-08-25 05:57:15 -05:00
|
|
|
//! Attributes & documentation for hir types.
|
2021-08-02 07:33:09 -05:00
|
|
|
|
|
|
|
use either::Either;
|
2020-08-25 05:56:01 -05:00
|
|
|
use hir_def::{
|
2021-03-19 15:23:57 -05:00
|
|
|
attr::{AttrsWithOwner, Documentation},
|
2021-08-02 07:33:09 -05:00
|
|
|
item_scope::ItemInNs,
|
2020-12-07 11:49:03 -06:00
|
|
|
path::ModPath,
|
2021-01-19 09:43:06 -06:00
|
|
|
per_ns::PerNs,
|
2020-12-07 11:49:03 -06:00
|
|
|
resolver::HasResolver,
|
2021-01-01 17:42:07 -06:00
|
|
|
AttrDefId, GenericParamId, ModuleDefId,
|
2020-08-25 05:56:01 -05:00
|
|
|
};
|
2021-08-02 07:33:09 -05:00
|
|
|
use hir_expand::{hygiene::Hygiene, MacroDefId};
|
2020-08-25 05:56:01 -05:00
|
|
|
use hir_ty::db::HirDatabase;
|
2020-08-26 11:56:41 -05:00
|
|
|
use syntax::ast;
|
2020-08-25 05:56:01 -05:00
|
|
|
|
|
|
|
use crate::{
|
2021-07-23 08:36:43 -05:00
|
|
|
Adt, AssocItem, Const, ConstParam, Enum, Field, Function, GenericParam, Impl, LifetimeParam,
|
|
|
|
MacroDef, Module, ModuleDef, Static, Struct, Trait, TypeAlias, TypeParam, Union, Variant,
|
2020-08-25 05:56:01 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
pub trait HasAttrs {
|
2021-03-19 15:23:57 -05:00
|
|
|
fn attrs(self, db: &dyn HirDatabase) -> AttrsWithOwner;
|
2020-08-25 05:56:01 -05:00
|
|
|
fn docs(self, db: &dyn HirDatabase) -> Option<Documentation>;
|
2020-08-26 11:56:41 -05:00
|
|
|
fn resolve_doc_path(
|
|
|
|
self,
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
link: &str,
|
|
|
|
ns: Option<Namespace>,
|
2021-08-02 07:33:09 -05:00
|
|
|
) -> Option<Either<ModuleDef, MacroDef>>;
|
2020-08-26 11:56:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)]
|
|
|
|
pub enum Namespace {
|
|
|
|
Types,
|
|
|
|
Values,
|
|
|
|
Macros,
|
2020-08-25 05:56:01 -05:00
|
|
|
}
|
|
|
|
|
2020-08-25 07:44:15 -05:00
|
|
|
macro_rules! impl_has_attrs {
|
|
|
|
($(($def:ident, $def_id:ident),)*) => {$(
|
|
|
|
impl HasAttrs for $def {
|
2021-03-19 15:23:57 -05:00
|
|
|
fn attrs(self, db: &dyn HirDatabase) -> AttrsWithOwner {
|
2020-08-25 07:44:15 -05:00
|
|
|
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());
|
2020-12-07 11:06:46 -06:00
|
|
|
db.attrs(def).docs()
|
2020-08-25 07:44:15 -05:00
|
|
|
}
|
2021-08-02 07:33:09 -05:00
|
|
|
fn resolve_doc_path(self, db: &dyn HirDatabase, link: &str, ns: Option<Namespace>) -> Option<Either<ModuleDef, MacroDef>> {
|
2020-08-26 11:56:41 -05:00
|
|
|
let def = AttrDefId::$def_id(self.into());
|
2021-08-02 07:33:09 -05:00
|
|
|
resolve_doc_path(db, def, link, ns).map(|it| it.map_left(ModuleDef::from).map_right(MacroDef::from))
|
2020-08-26 11:56:41 -05:00
|
|
|
}
|
2020-08-25 07:44:15 -05:00
|
|
|
}
|
|
|
|
)*};
|
2020-08-25 05:56:01 -05:00
|
|
|
}
|
|
|
|
|
2020-08-25 07:44:15 -05:00
|
|
|
impl_has_attrs![
|
|
|
|
(Field, FieldId),
|
2020-12-20 01:05:24 -06:00
|
|
|
(Variant, EnumVariantId),
|
2020-08-25 07:44:15 -05:00
|
|
|
(Static, StaticId),
|
|
|
|
(Const, ConstId),
|
|
|
|
(Trait, TraitId),
|
|
|
|
(TypeAlias, TypeAliasId),
|
|
|
|
(MacroDef, MacroDefId),
|
|
|
|
(Function, FunctionId),
|
|
|
|
(Adt, AdtId),
|
|
|
|
(Module, ModuleId),
|
2021-01-01 17:42:07 -06:00
|
|
|
(GenericParam, GenericParamId),
|
2021-03-16 12:57:47 -05:00
|
|
|
(Impl, ImplId),
|
2020-08-25 07:44:15 -05:00
|
|
|
];
|
|
|
|
|
2021-01-02 05:11:46 -06:00
|
|
|
macro_rules! impl_has_attrs_enum {
|
|
|
|
($($variant:ident),* for $enum:ident) => {$(
|
|
|
|
impl HasAttrs for $variant {
|
2021-03-19 15:23:57 -05:00
|
|
|
fn attrs(self, db: &dyn HirDatabase) -> AttrsWithOwner {
|
2021-01-02 05:11:46 -06:00
|
|
|
$enum::$variant(self).attrs(db)
|
2020-08-25 07:44:15 -05:00
|
|
|
}
|
|
|
|
fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> {
|
2021-01-02 05:11:46 -06:00
|
|
|
$enum::$variant(self).docs(db)
|
2020-08-25 07:44:15 -05:00
|
|
|
}
|
2021-08-02 07:33:09 -05:00
|
|
|
fn resolve_doc_path(self, db: &dyn HirDatabase, link: &str, ns: Option<Namespace>) -> Option<Either<ModuleDef, MacroDef>> {
|
2021-01-02 05:11:46 -06:00
|
|
|
$enum::$variant(self).resolve_doc_path(db, link, ns)
|
2020-08-26 11:56:41 -05:00
|
|
|
}
|
2020-08-25 07:44:15 -05:00
|
|
|
}
|
|
|
|
)*};
|
|
|
|
}
|
|
|
|
|
2021-01-02 05:11:46 -06:00
|
|
|
impl_has_attrs_enum![Struct, Union, Enum for Adt];
|
|
|
|
impl_has_attrs_enum![TypeParam, ConstParam, LifetimeParam for GenericParam];
|
2020-08-25 07:44:15 -05:00
|
|
|
|
2021-07-23 08:36:43 -05:00
|
|
|
impl HasAttrs for AssocItem {
|
|
|
|
fn attrs(self, db: &dyn HirDatabase) -> AttrsWithOwner {
|
|
|
|
match self {
|
|
|
|
AssocItem::Function(it) => it.attrs(db),
|
|
|
|
AssocItem::Const(it) => it.attrs(db),
|
|
|
|
AssocItem::TypeAlias(it) => it.attrs(db),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> {
|
|
|
|
match self {
|
|
|
|
AssocItem::Function(it) => it.docs(db),
|
|
|
|
AssocItem::Const(it) => it.docs(db),
|
|
|
|
AssocItem::TypeAlias(it) => it.docs(db),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_doc_path(
|
|
|
|
self,
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
link: &str,
|
|
|
|
ns: Option<Namespace>,
|
2021-08-02 07:33:09 -05:00
|
|
|
) -> Option<Either<ModuleDef, MacroDef>> {
|
2021-07-23 08:36:43 -05:00
|
|
|
match self {
|
|
|
|
AssocItem::Function(it) => it.resolve_doc_path(db, link, ns),
|
|
|
|
AssocItem::Const(it) => it.resolve_doc_path(db, link, ns),
|
|
|
|
AssocItem::TypeAlias(it) => it.resolve_doc_path(db, link, ns),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 11:56:41 -05:00
|
|
|
fn resolve_doc_path(
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
def: AttrDefId,
|
|
|
|
link: &str,
|
|
|
|
ns: Option<Namespace>,
|
2021-08-02 07:33:09 -05:00
|
|
|
) -> Option<Either<ModuleDefId, MacroDefId>> {
|
2020-08-26 11:56:41 -05:00
|
|
|
let resolver = match def {
|
|
|
|
AttrDefId::ModuleId(it) => it.resolver(db.upcast()),
|
|
|
|
AttrDefId::FieldId(it) => it.parent.resolver(db.upcast()),
|
|
|
|
AttrDefId::AdtId(it) => it.resolver(db.upcast()),
|
|
|
|
AttrDefId::FunctionId(it) => it.resolver(db.upcast()),
|
|
|
|
AttrDefId::EnumVariantId(it) => it.parent.resolver(db.upcast()),
|
|
|
|
AttrDefId::StaticId(it) => it.resolver(db.upcast()),
|
|
|
|
AttrDefId::ConstId(it) => it.resolver(db.upcast()),
|
|
|
|
AttrDefId::TraitId(it) => it.resolver(db.upcast()),
|
|
|
|
AttrDefId::TypeAliasId(it) => it.resolver(db.upcast()),
|
|
|
|
AttrDefId::ImplId(it) => it.resolver(db.upcast()),
|
2021-12-07 10:31:26 -06:00
|
|
|
AttrDefId::ExternBlockId(it) => it.resolver(db.upcast()),
|
2021-01-01 17:42:07 -06:00
|
|
|
AttrDefId::GenericParamId(it) => match it {
|
|
|
|
GenericParamId::TypeParamId(it) => it.parent,
|
|
|
|
GenericParamId::LifetimeParamId(it) => it.parent,
|
|
|
|
GenericParamId::ConstParamId(it) => it.parent,
|
|
|
|
}
|
|
|
|
.resolver(db.upcast()),
|
2021-08-02 07:33:09 -05:00
|
|
|
// FIXME
|
2020-08-26 11:56:41 -05:00
|
|
|
AttrDefId::MacroDefId(_) => return None,
|
|
|
|
};
|
|
|
|
let path = ast::Path::parse(link).ok()?;
|
2021-11-17 22:19:58 -06:00
|
|
|
let modpath = ModPath::from_src(db.upcast(), path, &Hygiene::new_unhygienic())?;
|
2020-08-26 11:56:41 -05:00
|
|
|
let resolved = resolver.resolve_module_path_in_items(db.upcast(), &modpath);
|
2021-07-19 13:36:44 -05:00
|
|
|
let resolved = if resolved == PerNs::none() {
|
|
|
|
resolver.resolve_module_path_in_trait_assoc_items(db.upcast(), &modpath)?
|
|
|
|
} else {
|
|
|
|
resolved
|
2020-08-26 11:56:41 -05:00
|
|
|
};
|
2021-07-19 13:36:44 -05:00
|
|
|
match ns {
|
2021-08-02 07:33:09 -05:00
|
|
|
Some(Namespace::Types) => resolved.take_types().map(Either::Left),
|
|
|
|
Some(Namespace::Values) => resolved.take_values().map(Either::Left),
|
|
|
|
Some(Namespace::Macros) => resolved.take_macros().map(Either::Right),
|
|
|
|
None => resolved.iter_items().next().map(|it| match it {
|
|
|
|
ItemInNs::Types(it) => Either::Left(it),
|
|
|
|
ItemInNs::Values(it) => Either::Left(it),
|
|
|
|
ItemInNs::Macros(it) => Either::Right(it),
|
|
|
|
}),
|
2021-07-19 13:36:44 -05:00
|
|
|
}
|
2020-08-25 05:56:01 -05:00
|
|
|
}
|