diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index c5cfd875f2..3f0ebca0df 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -234,6 +234,10 @@ impl Module { .collect() } + pub fn visibility_of(self, db: &dyn HirDatabase, def: &ModuleDef) -> Option { + 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); diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 713d45f481..5af7e5d6d7 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -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}, diff --git a/crates/ra_hir_def/src/item_scope.rs b/crates/ra_hir_def/src/item_scope.rs index 5e943b780a..ef1aaba6f7 100644 --- a/crates/ra_hir_def/src/item_scope.rs +++ b/crates/ra_hir_def/src/item_scope.rs @@ -68,6 +68,12 @@ impl ItemScope { self.impls.iter().copied() } + pub fn visbility_of(&self, def: ModuleDefId) -> Option { + 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 + 'a { self.visible.iter().filter_map(|(name, def)| def.take_macros().map(|macro_| (name, macro_))) diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs index 97961bb6d3..e9934844e3 100644 --- a/crates/ra_ide_db/src/defs.rs +++ b/crates/ra_ide_db/src/defs.rs @@ -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 { + pub fn visibility(&self, db: &RootDatabase) -> Option { + 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,