2020-08-25 05:57:15 -05:00
|
|
|
//! Attributes & documentation for hir types.
|
2021-08-02 07:33:09 -05:00
|
|
|
|
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,
|
2023-08-06 10:59:35 -05:00
|
|
|
path::{ModPath, Path},
|
|
|
|
resolver::{HasResolver, Resolver, TypeNs},
|
|
|
|
AssocItemId, AttrDefId, GenericParamId, ModuleDefId,
|
2020-08-25 05:56:01 -05:00
|
|
|
};
|
2023-08-06 10:59:35 -05:00
|
|
|
use hir_expand::{hygiene::Hygiene, name::Name};
|
2020-08-25 05:56:01 -05:00
|
|
|
use hir_ty::db::HirDatabase;
|
2021-12-28 06:48:07 -06:00
|
|
|
use syntax::{ast, AstNode};
|
2020-08-25 05:56:01 -05:00
|
|
|
|
|
|
|
use crate::{
|
2023-08-06 10:59:35 -05:00
|
|
|
Adt, AsAssocItem, AssocItem, BuiltinType, Const, ConstParam, Enum, ExternCrateDecl, Field,
|
|
|
|
Function, GenericParam, Impl, LifetimeParam, Macro, Module, ModuleDef, Static, Struct, Trait,
|
|
|
|
TraitAlias, TypeAlias, TypeParam, Union, Variant, VariantDef,
|
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>,
|
2023-08-06 10:59:35 -05:00
|
|
|
) -> Option<DocLinkDef>;
|
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
|
|
|
}
|
|
|
|
|
2023-08-06 10:59:35 -05:00
|
|
|
/// Subset of `ide_db::Definition` that doc links can resolve to.
|
|
|
|
pub enum DocLinkDef {
|
|
|
|
ModuleDef(ModuleDef),
|
|
|
|
Field(Field),
|
|
|
|
SelfType(Trait),
|
|
|
|
}
|
|
|
|
|
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());
|
2023-04-14 05:47:33 -05:00
|
|
|
db.attrs_with_owner(def)
|
2020-08-25 07:44:15 -05:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2023-08-06 10:59:35 -05:00
|
|
|
fn resolve_doc_path(
|
|
|
|
self,
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
link: &str,
|
|
|
|
ns: Option<Namespace>
|
|
|
|
) -> Option<DocLinkDef> {
|
2020-08-26 11:56:41 -05:00
|
|
|
let def = AttrDefId::$def_id(self.into());
|
2023-08-06 10:59:35 -05:00
|
|
|
resolve_doc_path(db, def, link, ns)
|
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),
|
2023-03-03 09:24:07 -06:00
|
|
|
(TraitAlias, TraitAliasId),
|
2020-08-25 07:44:15 -05:00
|
|
|
(TypeAlias, TypeAliasId),
|
2022-03-08 16:51:48 -06:00
|
|
|
(Macro, MacroId),
|
2020-08-25 07:44:15 -05:00
|
|
|
(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
|
|
|
}
|
2023-08-06 10:59:35 -05:00
|
|
|
fn resolve_doc_path(
|
|
|
|
self,
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
link: &str,
|
|
|
|
ns: Option<Namespace>
|
|
|
|
) -> Option<DocLinkDef> {
|
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>,
|
2023-08-06 10:59:35 -05:00
|
|
|
) -> Option<DocLinkDef> {
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 04:52:55 -05:00
|
|
|
impl HasAttrs for ExternCrateDecl {
|
|
|
|
fn attrs(self, db: &dyn HirDatabase) -> AttrsWithOwner {
|
|
|
|
let def = AttrDefId::ExternCrateId(self.into());
|
|
|
|
db.attrs_with_owner(def)
|
|
|
|
}
|
|
|
|
fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> {
|
2023-08-02 05:18:10 -05:00
|
|
|
let crate_docs = self.resolved_crate(db)?.root_module().attrs(db).docs().map(String::from);
|
2023-08-02 04:52:55 -05:00
|
|
|
let def = AttrDefId::ExternCrateId(self.into());
|
|
|
|
let decl_docs = db.attrs(def).docs().map(String::from);
|
|
|
|
match (decl_docs, crate_docs) {
|
|
|
|
(None, None) => None,
|
|
|
|
(Some(decl_docs), None) => Some(decl_docs),
|
|
|
|
(None, Some(crate_docs)) => Some(crate_docs),
|
|
|
|
(Some(mut decl_docs), Some(crate_docs)) => {
|
|
|
|
decl_docs.push('\n');
|
|
|
|
decl_docs.push('\n');
|
|
|
|
decl_docs += &crate_docs;
|
|
|
|
Some(decl_docs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.map(Documentation::new)
|
|
|
|
}
|
|
|
|
fn resolve_doc_path(
|
|
|
|
self,
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
link: &str,
|
|
|
|
ns: Option<Namespace>,
|
2023-08-06 10:59:35 -05:00
|
|
|
) -> Option<DocLinkDef> {
|
2023-08-02 04:52:55 -05:00
|
|
|
let def = AttrDefId::ExternCrateId(self.into());
|
2023-08-06 10:59:35 -05:00
|
|
|
resolve_doc_path(db, def, link, ns)
|
2023-08-02 04:52:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 01:52:13 -05:00
|
|
|
/// Resolves the item `link` points to in the scope of `def`.
|
2020-08-26 11:56:41 -05:00
|
|
|
fn resolve_doc_path(
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
def: AttrDefId,
|
|
|
|
link: &str,
|
|
|
|
ns: Option<Namespace>,
|
2023-08-06 10:59:35 -05:00
|
|
|
) -> Option<DocLinkDef> {
|
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()),
|
2023-03-03 09:24:07 -06:00
|
|
|
AttrDefId::TraitAliasId(it) => it.resolver(db.upcast()),
|
2020-08-26 11:56:41 -05:00
|
|
|
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()),
|
2023-08-02 07:53:45 -05:00
|
|
|
AttrDefId::UseId(it) => it.resolver(db.upcast()),
|
2022-03-08 16:51:48 -06:00
|
|
|
AttrDefId::MacroId(it) => it.resolver(db.upcast()),
|
2023-06-15 05:28:40 -05:00
|
|
|
AttrDefId::ExternCrateId(it) => it.resolver(db.upcast()),
|
2021-01-01 17:42:07 -06:00
|
|
|
AttrDefId::GenericParamId(it) => match it {
|
2021-12-29 07:35:59 -06:00
|
|
|
GenericParamId::TypeParamId(it) => it.parent(),
|
|
|
|
GenericParamId::ConstParamId(it) => it.parent(),
|
2021-01-01 17:42:07 -06:00
|
|
|
GenericParamId::LifetimeParamId(it) => it.parent,
|
|
|
|
}
|
|
|
|
.resolver(db.upcast()),
|
2020-08-26 11:56:41 -05:00
|
|
|
};
|
2021-12-28 06:48:07 -06:00
|
|
|
|
2023-08-06 10:59:35 -05:00
|
|
|
let mut modpath = modpath_from_str(db, link)?;
|
|
|
|
|
|
|
|
let resolved = resolver.resolve_module_path_in_items(db.upcast(), &modpath);
|
|
|
|
if resolved.is_none() {
|
|
|
|
let last_name = modpath.pop_segment()?;
|
|
|
|
resolve_assoc_or_field(db, resolver, modpath, last_name, ns)
|
|
|
|
} else {
|
|
|
|
let def = match ns {
|
|
|
|
Some(Namespace::Types) => resolved.take_types(),
|
|
|
|
Some(Namespace::Values) => resolved.take_values(),
|
|
|
|
Some(Namespace::Macros) => resolved.take_macros().map(ModuleDefId::MacroId),
|
2023-08-17 05:17:41 -05:00
|
|
|
None => resolved.iter_items().next().map(|(it, _)| match it {
|
2023-08-06 10:59:35 -05:00
|
|
|
ItemInNs::Types(it) => it,
|
|
|
|
ItemInNs::Values(it) => it,
|
|
|
|
ItemInNs::Macros(it) => ModuleDefId::MacroId(it),
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
Some(DocLinkDef::ModuleDef(def?.into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_assoc_or_field(
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
resolver: Resolver,
|
|
|
|
path: ModPath,
|
|
|
|
name: Name,
|
|
|
|
ns: Option<Namespace>,
|
|
|
|
) -> Option<DocLinkDef> {
|
|
|
|
let path = Path::from_known_path_with_no_generic(path);
|
|
|
|
// FIXME: This does not handle `Self` on trait definitions, which we should resolve to the
|
|
|
|
// trait itself.
|
|
|
|
let base_def = resolver.resolve_path_in_type_ns_fully(db.upcast(), &path)?;
|
|
|
|
|
|
|
|
let ty = match base_def {
|
|
|
|
TypeNs::SelfType(id) => Impl::from(id).self_ty(db),
|
|
|
|
TypeNs::GenericParam(_) => {
|
|
|
|
// Even if this generic parameter has some trait bounds, rustdoc doesn't
|
|
|
|
// resolve `name` to trait items.
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
TypeNs::AdtId(id) | TypeNs::AdtSelfType(id) => Adt::from(id).ty(db),
|
|
|
|
TypeNs::EnumVariantId(id) => {
|
|
|
|
// Enum variants don't have path candidates.
|
|
|
|
let variant = Variant::from(id);
|
|
|
|
return resolve_field(db, variant.into(), name, ns);
|
|
|
|
}
|
|
|
|
TypeNs::TypeAliasId(id) => {
|
|
|
|
let alias = TypeAlias::from(id);
|
|
|
|
if alias.as_assoc_item(db).is_some() {
|
|
|
|
// We don't normalize associated type aliases, so we have nothing to
|
|
|
|
// resolve `name` to.
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
alias.ty(db)
|
|
|
|
}
|
|
|
|
TypeNs::BuiltinType(id) => BuiltinType::from(id).ty(db),
|
|
|
|
TypeNs::TraitId(id) => {
|
|
|
|
// Doc paths in this context may only resolve to an item of this trait
|
|
|
|
// (i.e. no items of its supertraits), so we need to handle them here
|
|
|
|
// independently of others.
|
|
|
|
return db.trait_data(id).items.iter().find(|it| it.0 == name).map(|(_, assoc_id)| {
|
|
|
|
let def = match *assoc_id {
|
|
|
|
AssocItemId::FunctionId(it) => ModuleDef::Function(it.into()),
|
|
|
|
AssocItemId::ConstId(it) => ModuleDef::Const(it.into()),
|
|
|
|
AssocItemId::TypeAliasId(it) => ModuleDef::TypeAlias(it.into()),
|
|
|
|
};
|
|
|
|
DocLinkDef::ModuleDef(def)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
TypeNs::TraitAliasId(_) => {
|
|
|
|
// XXX: Do these get resolved?
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// FIXME: Resolve associated items here, e.g. `Option::map`. Note that associated items take
|
|
|
|
// precedence over fields.
|
|
|
|
|
|
|
|
let variant_def = match ty.as_adt()? {
|
|
|
|
Adt::Struct(it) => it.into(),
|
|
|
|
Adt::Union(it) => it.into(),
|
|
|
|
Adt::Enum(_) => return None,
|
|
|
|
};
|
|
|
|
resolve_field(db, variant_def, name, ns)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_field(
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
def: VariantDef,
|
|
|
|
name: Name,
|
|
|
|
ns: Option<Namespace>,
|
|
|
|
) -> Option<DocLinkDef> {
|
|
|
|
if let Some(Namespace::Types | Namespace::Macros) = ns {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
def.fields(db).into_iter().find(|f| f.name(db) == name).map(DocLinkDef::Field)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn modpath_from_str(db: &dyn HirDatabase, link: &str) -> Option<ModPath> {
|
|
|
|
// FIXME: this is not how we should get a mod path here.
|
|
|
|
let try_get_modpath = |link: &str| {
|
2022-12-23 12:42:58 -06:00
|
|
|
let ast_path = ast::SourceFile::parse(&format!("type T = {link};"))
|
2021-12-28 06:48:07 -06:00
|
|
|
.syntax_node()
|
|
|
|
.descendants()
|
|
|
|
.find_map(ast::Path::cast)?;
|
2023-05-11 01:55:41 -05:00
|
|
|
if ast_path.syntax().text() != link {
|
2021-12-28 06:48:07 -06:00
|
|
|
return None;
|
|
|
|
}
|
2023-08-06 10:59:35 -05:00
|
|
|
ModPath::from_src(db.upcast(), ast_path, &Hygiene::new_unhygienic())
|
2021-12-28 06:48:07 -06:00
|
|
|
};
|
|
|
|
|
2023-08-06 10:59:35 -05:00
|
|
|
let full = try_get_modpath(link);
|
|
|
|
if full.is_some() {
|
|
|
|
return full;
|
2021-07-19 13:36:44 -05:00
|
|
|
}
|
2023-08-06 10:59:35 -05:00
|
|
|
|
|
|
|
// Tuple field names cannot be a part of `ModPath` usually, but rustdoc can
|
|
|
|
// resolve doc paths like `TupleStruct::0`.
|
|
|
|
// FIXME: Find a better way to handle these.
|
|
|
|
let (base, maybe_tuple_field) = link.rsplit_once("::")?;
|
|
|
|
let tuple_field = Name::new_tuple_field(maybe_tuple_field.parse().ok()?);
|
|
|
|
let mut modpath = try_get_modpath(base)?;
|
|
|
|
modpath.push_segment(tuple_field);
|
|
|
|
Some(modpath)
|
2020-08-25 05:56:01 -05:00
|
|
|
}
|