From 575fb9ab6a5f3d0dc60a8fc811ba9833f5f81b4e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 25 Aug 2020 12:56:01 +0200 Subject: Move attrs from code_module to a separate module --- crates/hir/src/attrs.rs | 132 +++++++++++++++++++++++++++++++++++++++++++ crates/hir/src/code_model.rs | 123 +--------------------------------------- crates/hir/src/lib.rs | 11 ++-- 3 files changed, 140 insertions(+), 126 deletions(-) create mode 100644 crates/hir/src/attrs.rs (limited to 'crates/hir') diff --git a/crates/hir/src/attrs.rs b/crates/hir/src/attrs.rs new file mode 100644 index 000000000..2603411eb --- /dev/null +++ b/crates/hir/src/attrs.rs @@ -0,0 +1,132 @@ +use hir_def::{ + attr::Attrs, + db::DefDatabase, + docs::Documentation, + resolver::{HasResolver, Resolver}, + AdtId, FunctionId, GenericDefId, ModuleId, StaticId, TraitId, VariantId, +}; +use hir_ty::db::HirDatabase; +use stdx::impl_from; + +use crate::{ + doc_links::Resolvable, Adt, Const, Enum, EnumVariant, Field, Function, GenericDef, ImplDef, + Local, MacroDef, Module, ModuleDef, Static, Struct, Trait, TypeAlias, TypeParam, Union, + VariantDef, +}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum AttrDef { + Module(Module), + Field(Field), + Adt(Adt), + Function(Function), + EnumVariant(EnumVariant), + Static(Static), + Const(Const), + Trait(Trait), + TypeAlias(TypeAlias), + MacroDef(MacroDef), +} + +impl_from!( + Module, + Field, + Adt(Struct, Enum, Union), + EnumVariant, + Static, + Const, + Function, + Trait, + TypeAlias, + MacroDef + for AttrDef +); + +pub trait HasAttrs { + fn attrs(self, db: &dyn HirDatabase) -> Attrs; + fn docs(self, db: &dyn HirDatabase) -> Option; +} + +impl> HasAttrs for T { + fn attrs(self, db: &dyn HirDatabase) -> Attrs { + let def: AttrDef = self.into(); + db.attrs(def.into()) + } + fn docs(self, db: &dyn HirDatabase) -> Option { + let def: AttrDef = self.into(); + db.documentation(def.into()) + } +} + +impl Resolvable for ModuleDef { + fn resolver(&self, db: &D) -> Option { + Some(match self { + ModuleDef::Module(m) => ModuleId::from(m.clone()).resolver(db), + ModuleDef::Function(f) => FunctionId::from(f.clone()).resolver(db), + ModuleDef::Adt(adt) => AdtId::from(adt.clone()).resolver(db), + ModuleDef::EnumVariant(ev) => { + GenericDefId::from(GenericDef::from(ev.clone())).resolver(db) + } + ModuleDef::Const(c) => GenericDefId::from(GenericDef::from(c.clone())).resolver(db), + ModuleDef::Static(s) => StaticId::from(s.clone()).resolver(db), + ModuleDef::Trait(t) => TraitId::from(t.clone()).resolver(db), + ModuleDef::TypeAlias(t) => ModuleId::from(t.module(db)).resolver(db), + // FIXME: This should be a resolver relative to `std/core` + ModuleDef::BuiltinType(_t) => None?, + }) + } + + fn try_into_module_def(self) -> Option { + Some(self) + } +} + +impl Resolvable for TypeParam { + fn resolver(&self, db: &D) -> Option { + Some(Into::::into(self.module(db)).resolver(db)) + } + + fn try_into_module_def(self) -> Option { + None + } +} + +impl Resolvable for MacroDef { + fn resolver(&self, db: &D) -> Option { + Some(Into::::into(self.module(db)?).resolver(db)) + } + + fn try_into_module_def(self) -> Option { + None + } +} + +impl Resolvable for Field { + fn resolver(&self, db: &D) -> Option { + Some(Into::::into(Into::::into(self.parent_def(db))).resolver(db)) + } + + fn try_into_module_def(self) -> Option { + None + } +} + +impl Resolvable for ImplDef { + fn resolver(&self, db: &D) -> Option { + Some(Into::::into(self.module(db)).resolver(db)) + } + + fn try_into_module_def(self) -> Option { + None + } +} + +impl Resolvable for Local { + fn resolver(&self, db: &D) -> Option { + Some(Into::::into(self.module(db)).resolver(db)) + } + + fn try_into_module_def(self) -> Option { + None + } +} 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::{ adt::StructKind, adt::VariantData, builtin_type::BuiltinType, - docs::Documentation, expr::{BindingAnnotation, Pat, PatId}, import_map, lang_item::LangItemTarget, @@ -20,7 +19,7 @@ use hir_def::{ type_ref::{Mutability, TypeRef}, AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId, - TraitId, TypeAliasId, TypeParamId, UnionId, VariantId, + TraitId, TypeAliasId, TypeParamId, UnionId, }; use hir_expand::{ diagnostics::DiagnosticSink, @@ -43,9 +42,8 @@ use tt::{Ident, Leaf, Literal, TokenTree}; use crate::{ db::{DefDatabase, HirDatabase}, - doc_links::Resolvable, has_source::HasSource, - HirDisplay, InFile, Name, + AttrDef, HirDisplay, InFile, Name, }; /// hir::Crate describes a single crate. It's the main interface with which @@ -1741,50 +1739,6 @@ impl ScopeDef { } } -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub enum AttrDef { - Module(Module), - Field(Field), - Adt(Adt), - Function(Function), - EnumVariant(EnumVariant), - Static(Static), - Const(Const), - Trait(Trait), - TypeAlias(TypeAlias), - MacroDef(MacroDef), -} - -impl_from!( - Module, - Field, - Adt(Struct, Enum, Union), - EnumVariant, - Static, - Const, - Function, - Trait, - TypeAlias, - MacroDef - for AttrDef -); - -pub trait HasAttrs { - fn attrs(self, db: &dyn HirDatabase) -> Attrs; - fn docs(self, db: &dyn HirDatabase) -> Option; -} - -impl> HasAttrs for T { - fn attrs(self, db: &dyn HirDatabase) -> Attrs { - let def: AttrDef = self.into(); - db.attrs(def.into()) - } - fn docs(self, db: &dyn HirDatabase) -> Option { - let def: AttrDef = self.into(); - db.documentation(def.into()) - } -} - pub trait HasVisibility { fn visibility(&self, db: &dyn HirDatabase) -> Visibility; fn is_visible_from(&self, db: &dyn HirDatabase, module: Module) -> bool { @@ -1792,76 +1746,3 @@ pub trait HasVisibility { vis.is_visible_from(db.upcast(), module.id) } } - -impl Resolvable for ModuleDef { - fn resolver(&self, db: &D) -> Option { - Some(match self { - ModuleDef::Module(m) => ModuleId::from(m.clone()).resolver(db), - ModuleDef::Function(f) => FunctionId::from(f.clone()).resolver(db), - ModuleDef::Adt(adt) => AdtId::from(adt.clone()).resolver(db), - ModuleDef::EnumVariant(ev) => { - GenericDefId::from(GenericDef::from(ev.clone())).resolver(db) - } - ModuleDef::Const(c) => GenericDefId::from(GenericDef::from(c.clone())).resolver(db), - ModuleDef::Static(s) => StaticId::from(s.clone()).resolver(db), - ModuleDef::Trait(t) => TraitId::from(t.clone()).resolver(db), - ModuleDef::TypeAlias(t) => ModuleId::from(t.module(db)).resolver(db), - // FIXME: This should be a resolver relative to `std/core` - ModuleDef::BuiltinType(_t) => None?, - }) - } - - fn try_into_module_def(self) -> Option { - Some(self) - } -} - -impl Resolvable for TypeParam { - fn resolver(&self, db: &D) -> Option { - Some(Into::::into(self.module(db)).resolver(db)) - } - - fn try_into_module_def(self) -> Option { - None - } -} - -impl Resolvable for MacroDef { - fn resolver(&self, db: &D) -> Option { - Some(Into::::into(self.module(db)?).resolver(db)) - } - - fn try_into_module_def(self) -> Option { - None - } -} - -impl Resolvable for Field { - fn resolver(&self, db: &D) -> Option { - Some(Into::::into(Into::::into(self.parent_def(db))).resolver(db)) - } - - fn try_into_module_def(self) -> Option { - None - } -} - -impl Resolvable for ImplDef { - fn resolver(&self, db: &D) -> Option { - Some(Into::::into(self.module(db)).resolver(db)) - } - - fn try_into_module_def(self) -> Option { - None - } -} - -impl Resolvable for Local { - fn resolver(&self, db: &D) -> Option { - Some(Into::::into(self.module(db)).resolver(db)) - } - - fn try_into_module_def(self) -> Option { - None - } -} 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; mod from_id; mod code_model; mod doc_links; - +mod attrs; mod has_source; pub use crate::{ + attrs::{AttrDef, HasAttrs}, code_model::{ - Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, AttrDef, Callable, CallableKind, - Const, Crate, CrateDependency, DefWithBody, Enum, EnumVariant, Field, FieldSource, - Function, GenericDef, HasAttrs, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, - ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility, + Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const, + Crate, CrateDependency, DefWithBody, Enum, EnumVariant, Field, FieldSource, Function, + GenericDef, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, + Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility, }, doc_links::resolve_doc_link, has_source::HasSource, -- cgit v1.2.3 From 748788530962a4095762b82c21756ff4589066b3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 25 Aug 2020 12:57:15 +0200 Subject: Simplify --- crates/hir/src/attrs.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'crates/hir') diff --git a/crates/hir/src/attrs.rs b/crates/hir/src/attrs.rs index 2603411eb..953960e14 100644 --- a/crates/hir/src/attrs.rs +++ b/crates/hir/src/attrs.rs @@ -1,3 +1,4 @@ +//! Attributes & documentation for hir types. use hir_def::{ attr::Attrs, db::DefDatabase, @@ -11,7 +12,6 @@ use stdx::impl_from; use crate::{ doc_links::Resolvable, Adt, Const, Enum, EnumVariant, Field, Function, GenericDef, ImplDef, Local, MacroDef, Module, ModuleDef, Static, Struct, Trait, TypeAlias, TypeParam, Union, - VariantDef, }; #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] @@ -83,7 +83,7 @@ impl Resolvable for ModuleDef { impl Resolvable for TypeParam { fn resolver(&self, db: &D) -> Option { - Some(Into::::into(self.module(db)).resolver(db)) + Some(ModuleId::from(self.module(db)).resolver(db)) } fn try_into_module_def(self) -> Option { @@ -93,7 +93,7 @@ impl Resolvable for TypeParam { impl Resolvable for MacroDef { fn resolver(&self, db: &D) -> Option { - Some(Into::::into(self.module(db)?).resolver(db)) + Some(ModuleId::from(self.module(db)?).resolver(db)) } fn try_into_module_def(self) -> Option { @@ -103,7 +103,7 @@ impl Resolvable for MacroDef { impl Resolvable for Field { fn resolver(&self, db: &D) -> Option { - Some(Into::::into(Into::::into(self.parent_def(db))).resolver(db)) + Some(VariantId::from(self.parent_def(db)).resolver(db)) } fn try_into_module_def(self) -> Option { @@ -113,7 +113,7 @@ impl Resolvable for Field { impl Resolvable for ImplDef { fn resolver(&self, db: &D) -> Option { - Some(Into::::into(self.module(db)).resolver(db)) + Some(ModuleId::from(self.module(db)).resolver(db)) } fn try_into_module_def(self) -> Option { @@ -123,7 +123,7 @@ impl Resolvable for ImplDef { impl Resolvable for Local { fn resolver(&self, db: &D) -> Option { - Some(Into::::into(self.module(db)).resolver(db)) + Some(ModuleId::from(self.module(db)).resolver(db)) } fn try_into_module_def(self) -> Option { -- cgit v1.2.3