mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 21:54:42 +00:00
Merge #7128
7128: Implement HasAttrs for GenericParam r=matklad a=Veykril Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
commit
3bf4cec799
5 changed files with 86 additions and 18 deletions
|
@ -3,15 +3,15 @@ use hir_def::{
|
|||
attr::{Attrs, Documentation},
|
||||
path::ModPath,
|
||||
resolver::HasResolver,
|
||||
AttrDefId, ModuleDefId,
|
||||
AttrDefId, GenericParamId, ModuleDefId,
|
||||
};
|
||||
use hir_expand::hygiene::Hygiene;
|
||||
use hir_ty::db::HirDatabase;
|
||||
use syntax::ast;
|
||||
|
||||
use crate::{
|
||||
Adt, Const, Enum, Field, Function, MacroDef, Module, ModuleDef, Static, Struct, Trait,
|
||||
TypeAlias, Union, Variant,
|
||||
Adt, Const, ConstParam, Enum, Field, Function, GenericParam, LifetimeParam, MacroDef, Module,
|
||||
ModuleDef, Static, Struct, Trait, TypeAlias, TypeParam, Union, Variant,
|
||||
};
|
||||
|
||||
pub trait HasAttrs {
|
||||
|
@ -62,25 +62,27 @@ impl_has_attrs![
|
|||
(Function, FunctionId),
|
||||
(Adt, AdtId),
|
||||
(Module, ModuleId),
|
||||
(GenericParam, GenericParamId),
|
||||
];
|
||||
|
||||
macro_rules! impl_has_attrs_adt {
|
||||
($($adt:ident),*) => {$(
|
||||
impl HasAttrs for $adt {
|
||||
macro_rules! impl_has_attrs_enum {
|
||||
($($variant:ident),* for $enum:ident) => {$(
|
||||
impl HasAttrs for $variant {
|
||||
fn attrs(self, db: &dyn HirDatabase) -> Attrs {
|
||||
Adt::$adt(self).attrs(db)
|
||||
$enum::$variant(self).attrs(db)
|
||||
}
|
||||
fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> {
|
||||
Adt::$adt(self).docs(db)
|
||||
$enum::$variant(self).docs(db)
|
||||
}
|
||||
fn resolve_doc_path(self, db: &dyn HirDatabase, link: &str, ns: Option<Namespace>) -> Option<ModuleDef> {
|
||||
Adt::$adt(self).resolve_doc_path(db, link, ns)
|
||||
$enum::$variant(self).resolve_doc_path(db, link, ns)
|
||||
}
|
||||
}
|
||||
)*};
|
||||
}
|
||||
|
||||
impl_has_attrs_adt![Struct, Union, Enum];
|
||||
impl_has_attrs_enum![Struct, Union, Enum for Adt];
|
||||
impl_has_attrs_enum![TypeParam, ConstParam, LifetimeParam for GenericParam];
|
||||
|
||||
fn resolve_doc_path(
|
||||
db: &dyn HirDatabase,
|
||||
|
@ -99,6 +101,12 @@ fn resolve_doc_path(
|
|||
AttrDefId::TraitId(it) => it.resolver(db.upcast()),
|
||||
AttrDefId::TypeAliasId(it) => it.resolver(db.upcast()),
|
||||
AttrDefId::ImplId(it) => it.resolver(db.upcast()),
|
||||
AttrDefId::GenericParamId(it) => match it {
|
||||
GenericParamId::TypeParamId(it) => it.parent,
|
||||
GenericParamId::LifetimeParamId(it) => it.parent,
|
||||
GenericParamId::ConstParamId(it) => it.parent,
|
||||
}
|
||||
.resolver(db.upcast()),
|
||||
AttrDefId::MacroDefId(_) => return None,
|
||||
};
|
||||
let path = ast::Path::parse(link).ok()?;
|
||||
|
|
|
@ -6,13 +6,13 @@
|
|||
use hir_def::{
|
||||
expr::{LabelId, PatId},
|
||||
item_scope::ItemInNs,
|
||||
AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, GenericDefId, ModuleDefId,
|
||||
VariantId,
|
||||
AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, GenericDefId, GenericParamId,
|
||||
ModuleDefId, VariantId,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
Adt, AssocItem, DefWithBody, Field, GenericDef, Label, Local, MacroDef, ModuleDef, Variant,
|
||||
VariantDef,
|
||||
code_model::GenericParam, Adt, AssocItem, DefWithBody, Field, GenericDef, Label, Local,
|
||||
MacroDef, ModuleDef, Variant, VariantDef,
|
||||
};
|
||||
|
||||
macro_rules! from_id {
|
||||
|
@ -68,6 +68,26 @@ impl From<Adt> for AdtId {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<GenericParamId> for GenericParam {
|
||||
fn from(id: GenericParamId) -> Self {
|
||||
match id {
|
||||
GenericParamId::TypeParamId(it) => GenericParam::TypeParam(it.into()),
|
||||
GenericParamId::LifetimeParamId(it) => GenericParam::LifetimeParam(it.into()),
|
||||
GenericParamId::ConstParamId(it) => GenericParam::ConstParam(it.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<GenericParam> for GenericParamId {
|
||||
fn from(id: GenericParam) -> Self {
|
||||
match id {
|
||||
GenericParam::TypeParam(it) => GenericParamId::TypeParamId(it.id),
|
||||
GenericParam::LifetimeParam(it) => GenericParamId::LifetimeParamId(it.id),
|
||||
GenericParam::ConstParam(it) => GenericParamId::ConstParamId(it.id),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<EnumVariantId> for Variant {
|
||||
fn from(id: EnumVariantId) -> Self {
|
||||
Variant { parent: id.parent.into(), id: id.local_id }
|
||||
|
|
|
@ -35,8 +35,9 @@ pub use crate::{
|
|||
code_model::{
|
||||
Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const,
|
||||
ConstParam, Crate, CrateDependency, DefWithBody, Enum, Field, FieldSource, Function,
|
||||
GenericDef, HasVisibility, Impl, Label, LifetimeParam, Local, MacroDef, Module, ModuleDef,
|
||||
ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, Variant, VariantDef,
|
||||
GenericDef, GenericParam, HasVisibility, Impl, Label, LifetimeParam, Local, MacroDef,
|
||||
Module, ModuleDef, ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union,
|
||||
Variant, VariantDef,
|
||||
},
|
||||
has_source::HasSource,
|
||||
semantics::{PathResolution, Semantics, SemanticsScope},
|
||||
|
|
|
@ -21,7 +21,7 @@ use crate::{
|
|||
nameres::ModuleSource,
|
||||
path::{ModPath, PathKind},
|
||||
src::HasChildSource,
|
||||
AdtId, AttrDefId, Lookup,
|
||||
AdtId, AttrDefId, GenericParamId, Lookup,
|
||||
};
|
||||
|
||||
/// Holds documentation
|
||||
|
@ -235,6 +235,25 @@ impl Attrs {
|
|||
AttrDefId::StaticId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
||||
AttrDefId::FunctionId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
||||
AttrDefId::TypeAliasId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
||||
AttrDefId::GenericParamId(it) => match it {
|
||||
GenericParamId::TypeParamId(it) => {
|
||||
let src = it.parent.child_source(db);
|
||||
RawAttrs::from_attrs_owner(
|
||||
db,
|
||||
src.with_value(
|
||||
src.value[it.local_id].as_ref().either(|it| it as _, |it| it as _),
|
||||
),
|
||||
)
|
||||
}
|
||||
GenericParamId::LifetimeParamId(it) => {
|
||||
let src = it.parent.child_source(db);
|
||||
RawAttrs::from_attrs_owner(db, src.with_value(&src.value[it.local_id]))
|
||||
}
|
||||
GenericParamId::ConstParamId(it) => {
|
||||
let src = it.parent.child_source(db);
|
||||
RawAttrs::from_attrs_owner(db, src.with_value(&src.value[it.local_id]))
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
raw_attrs.filter(db, def.krate(db))
|
||||
|
|
|
@ -261,6 +261,15 @@ pub enum AdtId {
|
|||
}
|
||||
impl_from!(StructId, UnionId, EnumId for AdtId);
|
||||
|
||||
/// A generic param
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum GenericParamId {
|
||||
TypeParamId(TypeParamId),
|
||||
LifetimeParamId(LifetimeParamId),
|
||||
ConstParamId(ConstParamId),
|
||||
}
|
||||
impl_from!(TypeParamId, LifetimeParamId, ConstParamId for GenericParamId);
|
||||
|
||||
/// The defs which can be visible in the module.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum ModuleDefId {
|
||||
|
@ -357,6 +366,7 @@ pub enum AttrDefId {
|
|||
TypeAliasId(TypeAliasId),
|
||||
MacroDefId(MacroDefId),
|
||||
ImplId(ImplId),
|
||||
GenericParamId(GenericParamId),
|
||||
}
|
||||
|
||||
impl_from!(
|
||||
|
@ -370,7 +380,8 @@ impl_from!(
|
|||
TraitId,
|
||||
TypeAliasId,
|
||||
MacroDefId,
|
||||
ImplId
|
||||
ImplId,
|
||||
GenericParamId
|
||||
for AttrDefId
|
||||
);
|
||||
|
||||
|
@ -495,6 +506,15 @@ impl AttrDefId {
|
|||
AttrDefId::TraitId(it) => it.lookup(db).container.module(db).krate,
|
||||
AttrDefId::TypeAliasId(it) => it.lookup(db).module(db).krate,
|
||||
AttrDefId::ImplId(it) => it.lookup(db).container.module(db).krate,
|
||||
AttrDefId::GenericParamId(it) => {
|
||||
match it {
|
||||
GenericParamId::TypeParamId(it) => it.parent,
|
||||
GenericParamId::LifetimeParamId(it) => it.parent,
|
||||
GenericParamId::ConstParamId(it) => it.parent,
|
||||
}
|
||||
.module(db)
|
||||
.krate
|
||||
}
|
||||
// FIXME: `MacroDefId` should store the defining module, then this can implement
|
||||
// `HasModule`
|
||||
AttrDefId::MacroDefId(it) => it.krate,
|
||||
|
|
Loading…
Reference in a new issue