Add visibility in code model for fields

This commit is contained in:
Florian Diebold 2019-12-24 21:45:58 +01:00
parent 1ce809d0fa
commit ca15cf422c
2 changed files with 20 additions and 3 deletions

View file

@ -118,7 +118,7 @@ impl_froms!(
BuiltinType BuiltinType
); );
pub use hir_def::attr::Attrs; pub use hir_def::{attr::Attrs, visibility::ResolvedVisibility};
impl Module { impl Module {
pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module { pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module {
@ -255,6 +255,15 @@ impl StructField {
} }
} }
impl HasVisibility for StructField {
fn visibility(&self, db: &impl HirDatabase) -> ResolvedVisibility {
let struct_field_id: hir_def::StructFieldId = (*self).into();
let visibility = db.visibility(struct_field_id.into());
let parent_id: hir_def::VariantId = self.parent.into();
visibility.resolve(db, &parent_id.resolver(db))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Struct { pub struct Struct {
pub(crate) id: StructId, pub(crate) id: StructId,
@ -1041,3 +1050,11 @@ impl<T: Into<AttrDef> + Copy> Docs for T {
db.documentation(def.into()) db.documentation(def.into())
} }
} }
pub trait HasVisibility {
fn visibility(&self, db: &impl HirDatabase) -> ResolvedVisibility;
fn visible_from(&self, db: &impl HirDatabase, module: Module) -> bool {
let vis = self.visibility(db);
vis.visible_from(db, module.id)
}
}

View file

@ -40,8 +40,8 @@ mod from_source;
pub use crate::{ pub use crate::{
code_model::{ code_model::{
Adt, AssocItem, AttrDef, Const, Crate, CrateDependency, DefWithBody, Docs, Enum, Adt, AssocItem, AttrDef, Const, Crate, CrateDependency, DefWithBody, Docs, Enum,
EnumVariant, FieldSource, Function, GenericDef, HasAttrs, ImplBlock, Local, MacroDef, EnumVariant, FieldSource, Function, GenericDef, HasAttrs, HasVisibility, ImplBlock, Local,
Module, ModuleDef, ScopeDef, Static, Struct, StructField, Trait, Type, TypeAlias, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, StructField, Trait, Type, TypeAlias,
TypeParam, Union, VariantDef, TypeParam, Union, VariantDef,
}, },
from_source::FromSource, from_source::FromSource,