1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
//! Attributes & documentation for hir types.
use hir_def::{
attr::Attrs,
docs::Documentation,
resolver::{HasResolver, Resolver},
AdtId, AttrDefId, FunctionId, GenericDefId, ModuleId, StaticId, TraitId, VariantId,
};
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>;
}
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)
}
}
)*};
}
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];
impl Resolvable for ModuleDef {
fn resolver(&self, db: &dyn HirDatabase) -> Option<Resolver> {
Some(match self {
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()),
ModuleDef::EnumVariant(ev) => {
GenericDefId::from(GenericDef::from(ev.clone())).resolver(db.upcast())
}
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()),
// 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 {
fn resolver(&self, db: &dyn HirDatabase) -> Option<Resolver> {
Some(ModuleId::from(self.module(db)).resolver(db.upcast()))
}
fn try_into_module_def(self) -> Option<ModuleDef> {
None
}
}
impl Resolvable for MacroDef {
fn resolver(&self, db: &dyn HirDatabase) -> Option<Resolver> {
Some(ModuleId::from(self.module(db)?).resolver(db.upcast()))
}
fn try_into_module_def(self) -> Option<ModuleDef> {
None
}
}
impl Resolvable for Field {
fn resolver(&self, db: &dyn HirDatabase) -> Option<Resolver> {
Some(VariantId::from(self.parent_def(db)).resolver(db.upcast()))
}
fn try_into_module_def(self) -> Option<ModuleDef> {
None
}
}
impl Resolvable for ImplDef {
fn resolver(&self, db: &dyn HirDatabase) -> Option<Resolver> {
Some(ModuleId::from(self.module(db)).resolver(db.upcast()))
}
fn try_into_module_def(self) -> Option<ModuleDef> {
None
}
}
impl Resolvable for Local {
fn resolver(&self, db: &dyn HirDatabase) -> Option<Resolver> {
Some(ModuleId::from(self.module(db)).resolver(db.upcast()))
}
fn try_into_module_def(self) -> Option<ModuleDef> {
None
}
}
|