aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-08-25 13:44:15 +0100
committerAleksey Kladov <[email protected]>2020-08-25 13:44:15 +0100
commit66e5ced894e430117d4d040e13721313237936ab (patch)
tree7db11f5c8abbcef1f6c847bd47dcae02a2d96583 /crates
parent8a845eda7bfd24aa4d85f1656bd053db05446a3e (diff)
Remove AttrDef
Diffstat (limited to 'crates')
-rw-r--r--crates/hir/src/attrs.rs81
-rw-r--r--crates/hir/src/code_model.rs10
-rw-r--r--crates/hir/src/from_id.rs25
-rw-r--r--crates/hir/src/lib.rs2
4 files changed, 52 insertions, 66 deletions
diff --git a/crates/hir/src/attrs.rs b/crates/hir/src/attrs.rs
index 953960e14..36027d183 100644
--- a/crates/hir/src/attrs.rs
+++ b/crates/hir/src/attrs.rs
@@ -4,60 +4,63 @@ use hir_def::{
4 db::DefDatabase, 4 db::DefDatabase,
5 docs::Documentation, 5 docs::Documentation,
6 resolver::{HasResolver, Resolver}, 6 resolver::{HasResolver, Resolver},
7 AdtId, FunctionId, GenericDefId, ModuleId, StaticId, TraitId, VariantId, 7 AdtId, AttrDefId, FunctionId, GenericDefId, ModuleId, StaticId, TraitId, VariantId,
8}; 8};
9use hir_ty::db::HirDatabase; 9use hir_ty::db::HirDatabase;
10use stdx::impl_from;
11 10
12use crate::{ 11use crate::{
13 doc_links::Resolvable, Adt, Const, Enum, EnumVariant, Field, Function, GenericDef, ImplDef, 12 doc_links::Resolvable, Adt, Const, Enum, EnumVariant, Field, Function, GenericDef, ImplDef,
14 Local, MacroDef, Module, ModuleDef, Static, Struct, Trait, TypeAlias, TypeParam, Union, 13 Local, MacroDef, Module, ModuleDef, Static, Struct, Trait, TypeAlias, TypeParam, Union,
15}; 14};
16 15
17#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
18pub enum AttrDef {
19 Module(Module),
20 Field(Field),
21 Adt(Adt),
22 Function(Function),
23 EnumVariant(EnumVariant),
24 Static(Static),
25 Const(Const),
26 Trait(Trait),
27 TypeAlias(TypeAlias),
28 MacroDef(MacroDef),
29}
30
31impl_from!(
32 Module,
33 Field,
34 Adt(Struct, Enum, Union),
35 EnumVariant,
36 Static,
37 Const,
38 Function,
39 Trait,
40 TypeAlias,
41 MacroDef
42 for AttrDef
43);
44
45pub trait HasAttrs { 16pub trait HasAttrs {
46 fn attrs(self, db: &dyn HirDatabase) -> Attrs; 17 fn attrs(self, db: &dyn HirDatabase) -> Attrs;
47 fn docs(self, db: &dyn HirDatabase) -> Option<Documentation>; 18 fn docs(self, db: &dyn HirDatabase) -> Option<Documentation>;
48} 19}
49 20
50impl<T: Into<AttrDef>> HasAttrs for T { 21macro_rules! impl_has_attrs {
51 fn attrs(self, db: &dyn HirDatabase) -> Attrs { 22 ($(($def:ident, $def_id:ident),)*) => {$(
52 let def: AttrDef = self.into(); 23 impl HasAttrs for $def {
53 db.attrs(def.into()) 24 fn attrs(self, db: &dyn HirDatabase) -> Attrs {
54 } 25 let def = AttrDefId::$def_id(self.into());
55 fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> { 26 db.attrs(def)
56 let def: AttrDef = self.into(); 27 }
57 db.documentation(def.into()) 28 fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> {
58 } 29 let def = AttrDefId::$def_id(self.into());
30 db.documentation(def)
31 }
32 }
33 )*};
34}
35
36impl_has_attrs![
37 (Field, FieldId),
38 (EnumVariant, EnumVariantId),
39 (Static, StaticId),
40 (Const, ConstId),
41 (Trait, TraitId),
42 (TypeAlias, TypeAliasId),
43 (MacroDef, MacroDefId),
44 (Function, FunctionId),
45 (Adt, AdtId),
46 (Module, ModuleId),
47];
48
49macro_rules! impl_has_attrs_adt {
50 ($($adt:ident),*) => {$(
51 impl HasAttrs for $adt {
52 fn attrs(self, db: &dyn HirDatabase) -> Attrs {
53 Adt::$adt(self).attrs(db)
54 }
55 fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> {
56 Adt::$adt(self).docs(db)
57 }
58 }
59 )*};
59} 60}
60 61
62impl_has_attrs_adt![Struct, Union, Enum];
63
61impl Resolvable for ModuleDef { 64impl Resolvable for ModuleDef {
62 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> { 65 fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
63 Some(match self { 66 Some(match self {
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs
index 59d8b3073..c2ee20dbb 100644
--- a/crates/hir/src/code_model.rs
+++ b/crates/hir/src/code_model.rs
@@ -17,9 +17,9 @@ use hir_def::{
17 resolver::{HasResolver, Resolver}, 17 resolver::{HasResolver, Resolver},
18 src::HasSource as _, 18 src::HasSource as _,
19 type_ref::{Mutability, TypeRef}, 19 type_ref::{Mutability, TypeRef},
20 AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, 20 AdtId, AssocContainerId, AttrDefId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId,
21 ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId, 21 HasModule, ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId,
22 TraitId, TypeAliasId, TypeParamId, UnionId, 22 StructId, TraitId, TypeAliasId, TypeParamId, UnionId,
23}; 23};
24use hir_expand::{ 24use hir_expand::{
25 diagnostics::DiagnosticSink, 25 diagnostics::DiagnosticSink,
@@ -43,7 +43,7 @@ use tt::{Ident, Leaf, Literal, TokenTree};
43use crate::{ 43use crate::{
44 db::{DefDatabase, HirDatabase}, 44 db::{DefDatabase, HirDatabase},
45 has_source::HasSource, 45 has_source::HasSource,
46 AttrDef, HirDisplay, InFile, Name, 46 HirDisplay, InFile, Name,
47}; 47};
48 48
49/// hir::Crate describes a single crate. It's the main interface with which 49/// hir::Crate describes a single crate. It's the main interface with which
@@ -126,7 +126,7 @@ impl Crate {
126 /// Try to get the root URL of the documentation of a crate. 126 /// Try to get the root URL of the documentation of a crate.
127 pub fn get_html_root_url(self: &Crate, db: &dyn HirDatabase) -> Option<String> { 127 pub fn get_html_root_url(self: &Crate, db: &dyn HirDatabase) -> Option<String> {
128 // Look for #![doc(html_root_url = "...")] 128 // Look for #![doc(html_root_url = "...")]
129 let attrs = db.attrs(AttrDef::from(self.root_module(db)).into()); 129 let attrs = db.attrs(AttrDefId::ModuleId(self.root_module(db).into()));
130 let doc_attr_q = attrs.by_key("doc"); 130 let doc_attr_q = attrs.by_key("doc");
131 131
132 if !doc_attr_q.exists() { 132 if !doc_attr_q.exists() {
diff --git a/crates/hir/src/from_id.rs b/crates/hir/src/from_id.rs
index a53ac1e08..033f53ac2 100644
--- a/crates/hir/src/from_id.rs
+++ b/crates/hir/src/from_id.rs
@@ -4,13 +4,13 @@
4//! are splitting the hir. 4//! are splitting the hir.
5 5
6use hir_def::{ 6use hir_def::{
7 expr::PatId, AdtId, AssocItemId, AttrDefId, DefWithBodyId, EnumVariantId, FieldId, 7 expr::PatId, AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, GenericDefId,
8 GenericDefId, ModuleDefId, VariantId, 8 ModuleDefId, VariantId,
9}; 9};
10 10
11use crate::{ 11use crate::{
12 code_model::ItemInNs, Adt, AssocItem, AttrDef, DefWithBody, EnumVariant, Field, GenericDef, 12 code_model::ItemInNs, Adt, AssocItem, DefWithBody, EnumVariant, Field, GenericDef, Local,
13 Local, MacroDef, ModuleDef, VariantDef, 13 MacroDef, ModuleDef, VariantDef,
14}; 14};
15 15
16macro_rules! from_id { 16macro_rules! from_id {
@@ -196,23 +196,6 @@ impl From<FieldId> for Field {
196 } 196 }
197} 197}
198 198
199impl From<AttrDef> for AttrDefId {
200 fn from(def: AttrDef) -> Self {
201 match def {
202 AttrDef::Module(it) => AttrDefId::ModuleId(it.id),
203 AttrDef::Field(it) => AttrDefId::FieldId(it.into()),
204 AttrDef::Adt(it) => AttrDefId::AdtId(it.into()),
205 AttrDef::Function(it) => AttrDefId::FunctionId(it.id),
206 AttrDef::EnumVariant(it) => AttrDefId::EnumVariantId(it.into()),
207 AttrDef::Static(it) => AttrDefId::StaticId(it.id),
208 AttrDef::Const(it) => AttrDefId::ConstId(it.id),
209 AttrDef::Trait(it) => AttrDefId::TraitId(it.id),
210 AttrDef::TypeAlias(it) => AttrDefId::TypeAliasId(it.id),
211 AttrDef::MacroDef(it) => AttrDefId::MacroDefId(it.id),
212 }
213 }
214}
215
216impl From<AssocItem> for GenericDefId { 199impl From<AssocItem> for GenericDefId {
217 fn from(item: AssocItem) -> Self { 200 fn from(item: AssocItem) -> Self {
218 match item { 201 match item {
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index d1c198bff..03915ea1b 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -32,7 +32,7 @@ mod attrs;
32mod has_source; 32mod has_source;
33 33
34pub use crate::{ 34pub use crate::{
35 attrs::{AttrDef, HasAttrs}, 35 attrs::HasAttrs,
36 code_model::{ 36 code_model::{
37 Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const, 37 Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const,
38 Crate, CrateDependency, DefWithBody, Enum, EnumVariant, Field, FieldSource, Function, 38 Crate, CrateDependency, DefWithBody, Enum, EnumVariant, Field, FieldSource, Function,