Add ItemScope::visibility_of

This commit is contained in:
Edwin Cheng 2020-03-25 04:45:42 +08:00
parent 7c2cc85806
commit bcfb3700ce
4 changed files with 19 additions and 24 deletions

View file

@ -234,6 +234,10 @@ impl Module {
.collect()
}
pub fn visibility_of(self, db: &dyn HirDatabase, def: &ModuleDef) -> Option<Visibility> {
db.crate_def_map(self.id.krate)[self.id.local_id].scope.visbility_of(def.clone().into())
}
pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) {
let _p = profile("Module::diagnostics");
let crate_def_map = db.crate_def_map(self.id.krate);

View file

@ -54,7 +54,7 @@ pub use crate::{
Adt, AsAssocItem, AssocItem, AssocItemContainer, AttrDef, Const, Crate, CrateDependency,
DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, HasAttrs,
HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct,
StructField, Trait, Type, TypeAlias, TypeParam, Union, VariantDef,
StructField, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility,
},
has_source::HasSource,
semantics::{original_range, PathResolution, Semantics, SemanticsScope},

View file

@ -68,6 +68,12 @@ impl ItemScope {
self.impls.iter().copied()
}
pub fn visbility_of(&self, def: ModuleDefId) -> Option<Visibility> {
self.name_of(ItemInNs::Types(def))
.or_else(|| self.name_of(ItemInNs::Values(def)))
.map(|(_, v)| v)
}
/// Iterate over all module scoped macros
pub(crate) fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a {
self.visible.iter().filter_map(|(name, def)| def.take_macros().map(|macro_| (name, macro_)))

View file

@ -6,12 +6,12 @@
// FIXME: this badly needs rename/rewrite (matklad, 2020-02-06).
use hir::{
Adt, FieldSource, HasSource, ImplDef, Local, MacroDef, Module, ModuleDef, Name, PathResolution,
Semantics, StructField, TypeParam,
HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, Name, PathResolution, Semantics,
StructField, TypeParam, Visibility,
};
use ra_prof::profile;
use ra_syntax::{
ast::{self, AstNode, VisibilityOwner},
ast::{self, AstNode},
match_ast,
};
use test_utils::tested_by;
@ -41,28 +41,13 @@ impl Definition {
}
}
pub fn visibility(&self, db: &RootDatabase) -> Option<ast::Visibility> {
pub fn visibility(&self, db: &RootDatabase) -> Option<Visibility> {
let module = self.module(db);
match self {
Definition::Macro(_) => None,
Definition::StructField(sf) => match sf.source(db).value {
FieldSource::Named(it) => it.visibility(),
FieldSource::Pos(it) => it.visibility(),
},
Definition::ModuleDef(def) => match def {
ModuleDef::Module(it) => it.declaration_source(db)?.value.visibility(),
ModuleDef::Function(it) => it.source(db).value.visibility(),
ModuleDef::Adt(adt) => match adt {
Adt::Struct(it) => it.source(db).value.visibility(),
Adt::Union(it) => it.source(db).value.visibility(),
Adt::Enum(it) => it.source(db).value.visibility(),
},
ModuleDef::Const(it) => it.source(db).value.visibility(),
ModuleDef::Static(it) => it.source(db).value.visibility(),
ModuleDef::Trait(it) => it.source(db).value.visibility(),
ModuleDef::TypeAlias(it) => it.source(db).value.visibility(),
ModuleDef::EnumVariant(_) => None,
ModuleDef::BuiltinType(_) => None,
},
Definition::StructField(sf) => Some(sf.visibility(db)),
Definition::ModuleDef(def) => module?.visibility_of(db, def),
Definition::SelfType(_) => None,
Definition::Local(_) => None,
Definition::TypeParam(_) => None,