mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 12:33:33 +00:00
Move attrs from code_module to a separate module
This commit is contained in:
parent
663749beab
commit
575fb9ab6a
3 changed files with 140 additions and 126 deletions
132
crates/hir/src/attrs.rs
Normal file
132
crates/hir/src/attrs.rs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,7 +9,6 @@ use hir_def::{
|
||||||
adt::StructKind,
|
adt::StructKind,
|
||||||
adt::VariantData,
|
adt::VariantData,
|
||||||
builtin_type::BuiltinType,
|
builtin_type::BuiltinType,
|
||||||
docs::Documentation,
|
|
||||||
expr::{BindingAnnotation, Pat, PatId},
|
expr::{BindingAnnotation, Pat, PatId},
|
||||||
import_map,
|
import_map,
|
||||||
lang_item::LangItemTarget,
|
lang_item::LangItemTarget,
|
||||||
|
@ -20,7 +19,7 @@ use hir_def::{
|
||||||
type_ref::{Mutability, TypeRef},
|
type_ref::{Mutability, TypeRef},
|
||||||
AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule,
|
AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule,
|
||||||
ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId,
|
ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId,
|
||||||
TraitId, TypeAliasId, TypeParamId, UnionId, VariantId,
|
TraitId, TypeAliasId, TypeParamId, UnionId,
|
||||||
};
|
};
|
||||||
use hir_expand::{
|
use hir_expand::{
|
||||||
diagnostics::DiagnosticSink,
|
diagnostics::DiagnosticSink,
|
||||||
|
@ -43,9 +42,8 @@ use tt::{Ident, Leaf, Literal, TokenTree};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::{DefDatabase, HirDatabase},
|
db::{DefDatabase, HirDatabase},
|
||||||
doc_links::Resolvable,
|
|
||||||
has_source::HasSource,
|
has_source::HasSource,
|
||||||
HirDisplay, InFile, Name,
|
AttrDef, HirDisplay, InFile, Name,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// hir::Crate describes a single crate. It's the main interface with which
|
/// 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 {
|
pub trait HasVisibility {
|
||||||
fn visibility(&self, db: &dyn HirDatabase) -> Visibility;
|
fn visibility(&self, db: &dyn HirDatabase) -> Visibility;
|
||||||
fn is_visible_from(&self, db: &dyn HirDatabase, module: Module) -> bool {
|
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)
|
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -28,15 +28,16 @@ pub mod diagnostics;
|
||||||
mod from_id;
|
mod from_id;
|
||||||
mod code_model;
|
mod code_model;
|
||||||
mod doc_links;
|
mod doc_links;
|
||||||
|
mod attrs;
|
||||||
mod has_source;
|
mod has_source;
|
||||||
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
|
attrs::{AttrDef, HasAttrs},
|
||||||
code_model::{
|
code_model::{
|
||||||
Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, AttrDef, Callable, CallableKind,
|
Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const,
|
||||||
Const, Crate, CrateDependency, DefWithBody, Enum, EnumVariant, Field, FieldSource,
|
Crate, CrateDependency, DefWithBody, Enum, EnumVariant, Field, FieldSource, Function,
|
||||||
Function, GenericDef, HasAttrs, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef,
|
GenericDef, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static,
|
||||||
ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility,
|
Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility,
|
||||||
},
|
},
|
||||||
doc_links::resolve_doc_link,
|
doc_links::resolve_doc_link,
|
||||||
has_source::HasSource,
|
has_source::HasSource,
|
||||||
|
|
Loading…
Reference in a new issue