2020-08-25 05:57:15 -05:00
|
|
|
//! Attributes & documentation for hir types.
|
2020-08-25 05:56:01 -05:00
|
|
|
use hir_def::{
|
2020-12-07 11:49:03 -06:00
|
|
|
attr::{Attrs, Documentation},
|
|
|
|
path::ModPath,
|
|
|
|
resolver::HasResolver,
|
|
|
|
AttrDefId, ModuleDefId,
|
2020-08-25 05:56:01 -05:00
|
|
|
};
|
2020-08-26 11:56:41 -05:00
|
|
|
use hir_expand::hygiene::Hygiene;
|
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::{
|
2020-08-26 11:56:41 -05:00
|
|
|
Adt, Const, Enum, EnumVariant, Field, Function, MacroDef, Module, ModuleDef, Static, Struct,
|
|
|
|
Trait, TypeAlias, Union,
|
2020-08-25 05:56:01 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
pub trait HasAttrs {
|
|
|
|
fn attrs(self, db: &dyn HirDatabase) -> Attrs;
|
|
|
|
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>,
|
|
|
|
) -> Option<ModuleDef>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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 {
|
|
|
|
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());
|
2020-12-07 11:06:46 -06:00
|
|
|
db.attrs(def).docs()
|
2020-08-25 07:44:15 -05:00
|
|
|
}
|
2020-08-26 11:56:41 -05:00
|
|
|
fn resolve_doc_path(self, db: &dyn HirDatabase, link: &str, ns: Option<Namespace>) -> Option<ModuleDef> {
|
|
|
|
let def = AttrDefId::$def_id(self.into());
|
|
|
|
resolve_doc_path(db, def, link, ns).map(ModuleDef::from)
|
|
|
|
}
|
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),
|
|
|
|
(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)
|
|
|
|
}
|
2020-08-26 11:56:41 -05:00
|
|
|
fn resolve_doc_path(self, db: &dyn HirDatabase, link: &str, ns: Option<Namespace>) -> Option<ModuleDef> {
|
|
|
|
Adt::$adt(self).resolve_doc_path(db, link, ns)
|
|
|
|
}
|
2020-08-25 07:44:15 -05:00
|
|
|
}
|
|
|
|
)*};
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_has_attrs_adt![Struct, Union, Enum];
|
|
|
|
|
2020-08-26 11:56:41 -05:00
|
|
|
fn resolve_doc_path(
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
def: AttrDefId,
|
|
|
|
link: &str,
|
|
|
|
ns: Option<Namespace>,
|
|
|
|
) -> Option<ModuleDefId> {
|
|
|
|
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()),
|
|
|
|
AttrDefId::MacroDefId(_) => return None,
|
|
|
|
};
|
|
|
|
let path = ast::Path::parse(link).ok()?;
|
|
|
|
let modpath = ModPath::from_src(path, &Hygiene::new_unhygienic()).unwrap();
|
|
|
|
let resolved = resolver.resolve_module_path_in_items(db.upcast(), &modpath);
|
|
|
|
let def = match ns {
|
|
|
|
Some(Namespace::Types) => resolved.take_types()?,
|
|
|
|
Some(Namespace::Values) => resolved.take_values()?,
|
|
|
|
Some(Namespace::Macros) => return None,
|
|
|
|
None => resolved.iter_items().find_map(|it| it.as_module_def_id())?,
|
|
|
|
};
|
|
|
|
Some(def.into())
|
2020-08-25 05:56:01 -05:00
|
|
|
}
|