diff options
-rw-r--r-- | crates/hir/src/attrs.rs | 132 | ||||
-rw-r--r-- | crates/hir/src/code_model.rs | 123 | ||||
-rw-r--r-- | crates/hir/src/lib.rs | 11 |
3 files changed, 140 insertions, 126 deletions
diff --git a/crates/hir/src/attrs.rs b/crates/hir/src/attrs.rs new file mode 100644 index 000000000..953960e14 --- /dev/null +++ b/crates/hir/src/attrs.rs | |||
@@ -0,0 +1,132 @@ | |||
1 | //! Attributes & documentation for hir types. | ||
2 | use hir_def::{ | ||
3 | attr::Attrs, | ||
4 | db::DefDatabase, | ||
5 | docs::Documentation, | ||
6 | resolver::{HasResolver, Resolver}, | ||
7 | AdtId, FunctionId, GenericDefId, ModuleId, StaticId, TraitId, VariantId, | ||
8 | }; | ||
9 | use hir_ty::db::HirDatabase; | ||
10 | use stdx::impl_from; | ||
11 | |||
12 | use crate::{ | ||
13 | doc_links::Resolvable, Adt, Const, Enum, EnumVariant, Field, Function, GenericDef, ImplDef, | ||
14 | Local, MacroDef, Module, ModuleDef, Static, Struct, Trait, TypeAlias, TypeParam, Union, | ||
15 | }; | ||
16 | |||
17 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
18 | pub 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 | |||
31 | impl_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 | |||
45 | pub trait HasAttrs { | ||
46 | fn attrs(self, db: &dyn HirDatabase) -> Attrs; | ||
47 | fn docs(self, db: &dyn HirDatabase) -> Option<Documentation>; | ||
48 | } | ||
49 | |||
50 | impl<T: Into<AttrDef>> HasAttrs for T { | ||
51 | fn attrs(self, db: &dyn HirDatabase) -> Attrs { | ||
52 | let def: AttrDef = self.into(); | ||
53 | db.attrs(def.into()) | ||
54 | } | ||
55 | fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> { | ||
56 | let def: AttrDef = self.into(); | ||
57 | db.documentation(def.into()) | ||
58 | } | ||
59 | } | ||
60 | |||
61 | impl Resolvable for ModuleDef { | ||
62 | fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> { | ||
63 | Some(match self { | ||
64 | ModuleDef::Module(m) => ModuleId::from(m.clone()).resolver(db), | ||
65 | ModuleDef::Function(f) => FunctionId::from(f.clone()).resolver(db), | ||
66 | ModuleDef::Adt(adt) => AdtId::from(adt.clone()).resolver(db), | ||
67 | ModuleDef::EnumVariant(ev) => { | ||
68 | GenericDefId::from(GenericDef::from(ev.clone())).resolver(db) | ||
69 | } | ||
70 | ModuleDef::Const(c) => GenericDefId::from(GenericDef::from(c.clone())).resolver(db), | ||
71 | ModuleDef::Static(s) => StaticId::from(s.clone()).resolver(db), | ||
72 | ModuleDef::Trait(t) => TraitId::from(t.clone()).resolver(db), | ||
73 | ModuleDef::TypeAlias(t) => ModuleId::from(t.module(db)).resolver(db), | ||
74 | // FIXME: This should be a resolver relative to `std/core` | ||
75 | ModuleDef::BuiltinType(_t) => None?, | ||
76 | }) | ||
77 | } | ||
78 | |||
79 | fn try_into_module_def(self) -> Option<ModuleDef> { | ||
80 | Some(self) | ||
81 | } | ||
82 | } | ||
83 | |||
84 | impl Resolvable for TypeParam { | ||
85 | fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> { | ||
86 | Some(ModuleId::from(self.module(db)).resolver(db)) | ||
87 | } | ||
88 | |||
89 | fn try_into_module_def(self) -> Option<ModuleDef> { | ||
90 | None | ||
91 | } | ||
92 | } | ||
93 | |||
94 | impl Resolvable for MacroDef { | ||
95 | fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> { | ||
96 | Some(ModuleId::from(self.module(db)?).resolver(db)) | ||
97 | } | ||
98 | |||
99 | fn try_into_module_def(self) -> Option<ModuleDef> { | ||
100 | None | ||
101 | } | ||
102 | } | ||
103 | |||
104 | impl Resolvable for Field { | ||
105 | fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> { | ||
106 | Some(VariantId::from(self.parent_def(db)).resolver(db)) | ||
107 | } | ||
108 | |||
109 | fn try_into_module_def(self) -> Option<ModuleDef> { | ||
110 | None | ||
111 | } | ||
112 | } | ||
113 | |||
114 | impl Resolvable for ImplDef { | ||
115 | fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> { | ||
116 | Some(ModuleId::from(self.module(db)).resolver(db)) | ||
117 | } | ||
118 | |||
119 | fn try_into_module_def(self) -> Option<ModuleDef> { | ||
120 | None | ||
121 | } | ||
122 | } | ||
123 | |||
124 | impl Resolvable for Local { | ||
125 | fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> { | ||
126 | Some(ModuleId::from(self.module(db)).resolver(db)) | ||
127 | } | ||
128 | |||
129 | fn try_into_module_def(self) -> Option<ModuleDef> { | ||
130 | None | ||
131 | } | ||
132 | } | ||
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index e553fc7dc..59d8b3073 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs | |||
@@ -9,7 +9,6 @@ use hir_def::{ | |||
9 | adt::StructKind, | 9 | adt::StructKind, |
10 | adt::VariantData, | 10 | adt::VariantData, |
11 | builtin_type::BuiltinType, | 11 | builtin_type::BuiltinType, |
12 | docs::Documentation, | ||
13 | expr::{BindingAnnotation, Pat, PatId}, | 12 | expr::{BindingAnnotation, Pat, PatId}, |
14 | import_map, | 13 | import_map, |
15 | lang_item::LangItemTarget, | 14 | lang_item::LangItemTarget, |
@@ -20,7 +19,7 @@ use hir_def::{ | |||
20 | type_ref::{Mutability, TypeRef}, | 19 | type_ref::{Mutability, TypeRef}, |
21 | AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, | 20 | AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, |
22 | ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId, | 21 | ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId, |
23 | TraitId, TypeAliasId, TypeParamId, UnionId, VariantId, | 22 | TraitId, TypeAliasId, TypeParamId, UnionId, |
24 | }; | 23 | }; |
25 | use hir_expand::{ | 24 | use hir_expand::{ |
26 | diagnostics::DiagnosticSink, | 25 | diagnostics::DiagnosticSink, |
@@ -43,9 +42,8 @@ use tt::{Ident, Leaf, Literal, TokenTree}; | |||
43 | 42 | ||
44 | use crate::{ | 43 | use crate::{ |
45 | db::{DefDatabase, HirDatabase}, | 44 | db::{DefDatabase, HirDatabase}, |
46 | doc_links::Resolvable, | ||
47 | has_source::HasSource, | 45 | has_source::HasSource, |
48 | HirDisplay, InFile, Name, | 46 | AttrDef, HirDisplay, InFile, Name, |
49 | }; | 47 | }; |
50 | 48 | ||
51 | /// 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 |
@@ -1741,50 +1739,6 @@ impl ScopeDef { | |||
1741 | } | 1739 | } |
1742 | } | 1740 | } |
1743 | 1741 | ||
1744 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
1745 | pub enum AttrDef { | ||
1746 | Module(Module), | ||
1747 | Field(Field), | ||
1748 | Adt(Adt), | ||
1749 | Function(Function), | ||
1750 | EnumVariant(EnumVariant), | ||
1751 | Static(Static), | ||
1752 | Const(Const), | ||
1753 | Trait(Trait), | ||
1754 | TypeAlias(TypeAlias), | ||
1755 | MacroDef(MacroDef), | ||
1756 | } | ||
1757 | |||
1758 | impl_from!( | ||
1759 | Module, | ||
1760 | Field, | ||
1761 | Adt(Struct, Enum, Union), | ||
1762 | EnumVariant, | ||
1763 | Static, | ||
1764 | Const, | ||
1765 | Function, | ||
1766 | Trait, | ||
1767 | TypeAlias, | ||
1768 | MacroDef | ||
1769 | for AttrDef | ||
1770 | ); | ||
1771 | |||
1772 | pub trait HasAttrs { | ||
1773 | fn attrs(self, db: &dyn HirDatabase) -> Attrs; | ||
1774 | fn docs(self, db: &dyn HirDatabase) -> Option<Documentation>; | ||
1775 | } | ||
1776 | |||
1777 | impl<T: Into<AttrDef>> HasAttrs for T { | ||
1778 | fn attrs(self, db: &dyn HirDatabase) -> Attrs { | ||
1779 | let def: AttrDef = self.into(); | ||
1780 | db.attrs(def.into()) | ||
1781 | } | ||
1782 | fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> { | ||
1783 | let def: AttrDef = self.into(); | ||
1784 | db.documentation(def.into()) | ||
1785 | } | ||
1786 | } | ||
1787 | |||
1788 | pub trait HasVisibility { | 1742 | pub trait HasVisibility { |
1789 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility; | 1743 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility; |
1790 | fn is_visible_from(&self, db: &dyn HirDatabase, module: Module) -> bool { | 1744 | fn is_visible_from(&self, db: &dyn HirDatabase, module: Module) -> bool { |
@@ -1792,76 +1746,3 @@ pub trait HasVisibility { | |||
1792 | vis.is_visible_from(db.upcast(), module.id) | 1746 | vis.is_visible_from(db.upcast(), module.id) |
1793 | } | 1747 | } |
1794 | } | 1748 | } |
1795 | |||
1796 | impl Resolvable for ModuleDef { | ||
1797 | fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> { | ||
1798 | Some(match self { | ||
1799 | ModuleDef::Module(m) => ModuleId::from(m.clone()).resolver(db), | ||
1800 | ModuleDef::Function(f) => FunctionId::from(f.clone()).resolver(db), | ||
1801 | ModuleDef::Adt(adt) => AdtId::from(adt.clone()).resolver(db), | ||
1802 | ModuleDef::EnumVariant(ev) => { | ||
1803 | GenericDefId::from(GenericDef::from(ev.clone())).resolver(db) | ||
1804 | } | ||
1805 | ModuleDef::Const(c) => GenericDefId::from(GenericDef::from(c.clone())).resolver(db), | ||
1806 | ModuleDef::Static(s) => StaticId::from(s.clone()).resolver(db), | ||
1807 | ModuleDef::Trait(t) => TraitId::from(t.clone()).resolver(db), | ||
1808 | ModuleDef::TypeAlias(t) => ModuleId::from(t.module(db)).resolver(db), | ||
1809 | // FIXME: This should be a resolver relative to `std/core` | ||
1810 | ModuleDef::BuiltinType(_t) => None?, | ||
1811 | }) | ||
1812 | } | ||
1813 | |||
1814 | fn try_into_module_def(self) -> Option<ModuleDef> { | ||
1815 | Some(self) | ||
1816 | } | ||
1817 | } | ||
1818 | |||
1819 | impl Resolvable for TypeParam { | ||
1820 | fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> { | ||
1821 | Some(Into::<ModuleId>::into(self.module(db)).resolver(db)) | ||
1822 | } | ||
1823 | |||
1824 | fn try_into_module_def(self) -> Option<ModuleDef> { | ||
1825 | None | ||
1826 | } | ||
1827 | } | ||
1828 | |||
1829 | impl Resolvable for MacroDef { | ||
1830 | fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> { | ||
1831 | Some(Into::<ModuleId>::into(self.module(db)?).resolver(db)) | ||
1832 | } | ||
1833 | |||
1834 | fn try_into_module_def(self) -> Option<ModuleDef> { | ||
1835 | None | ||
1836 | } | ||
1837 | } | ||
1838 | |||
1839 | impl Resolvable for Field { | ||
1840 | fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> { | ||
1841 | Some(Into::<VariantId>::into(Into::<VariantDef>::into(self.parent_def(db))).resolver(db)) | ||
1842 | } | ||
1843 | |||
1844 | fn try_into_module_def(self) -> Option<ModuleDef> { | ||
1845 | None | ||
1846 | } | ||
1847 | } | ||
1848 | |||
1849 | impl Resolvable for ImplDef { | ||
1850 | fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> { | ||
1851 | Some(Into::<ModuleId>::into(self.module(db)).resolver(db)) | ||
1852 | } | ||
1853 | |||
1854 | fn try_into_module_def(self) -> Option<ModuleDef> { | ||
1855 | None | ||
1856 | } | ||
1857 | } | ||
1858 | |||
1859 | impl Resolvable for Local { | ||
1860 | fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> { | ||
1861 | Some(Into::<ModuleId>::into(self.module(db)).resolver(db)) | ||
1862 | } | ||
1863 | |||
1864 | fn try_into_module_def(self) -> Option<ModuleDef> { | ||
1865 | None | ||
1866 | } | ||
1867 | } | ||
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 2326e3fbb..d1c198bff 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs | |||
@@ -28,15 +28,16 @@ pub mod diagnostics; | |||
28 | mod from_id; | 28 | mod from_id; |
29 | mod code_model; | 29 | mod code_model; |
30 | mod doc_links; | 30 | mod doc_links; |
31 | 31 | mod attrs; | |
32 | mod has_source; | 32 | mod has_source; |
33 | 33 | ||
34 | pub use crate::{ | 34 | pub use crate::{ |
35 | attrs::{AttrDef, HasAttrs}, | ||
35 | code_model::{ | 36 | code_model::{ |
36 | Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, AttrDef, Callable, CallableKind, | 37 | Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const, |
37 | Const, Crate, CrateDependency, DefWithBody, Enum, EnumVariant, Field, FieldSource, | 38 | Crate, CrateDependency, DefWithBody, Enum, EnumVariant, Field, FieldSource, Function, |
38 | Function, GenericDef, HasAttrs, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, | 39 | GenericDef, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, |
39 | ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility, | 40 | Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility, |
40 | }, | 41 | }, |
41 | doc_links::resolve_doc_link, | 42 | doc_links::resolve_doc_link, |
42 | has_source::HasSource, | 43 | has_source::HasSource, |