Move attrs from code_module to a separate module

This commit is contained in:
Aleksey Kladov 2020-08-25 12:56:01 +02:00
parent 663749beab
commit 575fb9ab6a
3 changed files with 140 additions and 126 deletions

132
crates/hir/src/attrs.rs Normal file
View file

@ -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<Documentation>;
}
impl<T: Into<AttrDef>> 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<Documentation> {
let def: AttrDef = self.into();
db.documentation(def.into())
}
}
impl Resolvable for ModuleDef {
fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
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<ModuleDef> {
Some(self)
}
}
impl Resolvable for TypeParam {
fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
Some(Into::<ModuleId>::into(self.module(db)).resolver(db))
}
fn try_into_module_def(self) -> Option<ModuleDef> {
None
}
}
impl Resolvable for MacroDef {
fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
Some(Into::<ModuleId>::into(self.module(db)?).resolver(db))
}
fn try_into_module_def(self) -> Option<ModuleDef> {
None
}
}
impl Resolvable for Field {
fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
Some(Into::<VariantId>::into(Into::<VariantDef>::into(self.parent_def(db))).resolver(db))
}
fn try_into_module_def(self) -> Option<ModuleDef> {
None
}
}
impl Resolvable for ImplDef {
fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
Some(Into::<ModuleId>::into(self.module(db)).resolver(db))
}
fn try_into_module_def(self) -> Option<ModuleDef> {
None
}
}
impl Resolvable for Local {
fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
Some(Into::<ModuleId>::into(self.module(db)).resolver(db))
}
fn try_into_module_def(self) -> Option<ModuleDef> {
None
}
}

View file

@ -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<Documentation>;
}
impl<T: Into<AttrDef>> 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<Documentation> {
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<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
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<ModuleDef> {
Some(self)
}
}
impl Resolvable for TypeParam {
fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
Some(Into::<ModuleId>::into(self.module(db)).resolver(db))
}
fn try_into_module_def(self) -> Option<ModuleDef> {
None
}
}
impl Resolvable for MacroDef {
fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
Some(Into::<ModuleId>::into(self.module(db)?).resolver(db))
}
fn try_into_module_def(self) -> Option<ModuleDef> {
None
}
}
impl Resolvable for Field {
fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
Some(Into::<VariantId>::into(Into::<VariantDef>::into(self.parent_def(db))).resolver(db))
}
fn try_into_module_def(self) -> Option<ModuleDef> {
None
}
}
impl Resolvable for ImplDef {
fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
Some(Into::<ModuleId>::into(self.module(db)).resolver(db))
}
fn try_into_module_def(self) -> Option<ModuleDef> {
None
}
}
impl Resolvable for Local {
fn resolver<D: DefDatabase + HirDatabase>(&self, db: &D) -> Option<Resolver> {
Some(Into::<ModuleId>::into(self.module(db)).resolver(db))
}
fn try_into_module_def(self) -> Option<ModuleDef> {
None
}
}

View file

@ -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,