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, diff --git a/crates/ra_ide_db/src/search.rs b/crates/ra_ide_db/src/search.rs index 1174546959..05a0eed30a 100644 --- a/crates/ra_ide_db/src/search.rs +++ b/crates/ra_ide_db/src/search.rs @@ -6,7 +6,7 @@ use std::mem; -use hir::{DefWithBody, HasSource, ModuleSource, Semantics}; +use hir::{DefWithBody, HasSource, Module, ModuleSource, Semantics, Visibility}; use once_cell::unsync::Lazy; use ra_db::{FileId, FileRange, SourceDatabaseExt}; use ra_prof::profile; @@ -123,51 +123,47 @@ impl Definition { return SearchScope::new(res); } - let vis = self.visibility(db).as_ref().map(|v| v.syntax().to_string()).unwrap_or_default(); + let vis = self.visibility(db); - if vis.as_str() == "pub(super)" { - if let Some(parent_module) = module.parent(db) { - let mut res = FxHashMap::default(); - let parent_src = parent_module.definition_source(db); - let file_id = parent_src.file_id.original_file(db); + // FIXME: + // The following logic are wrong that it does not search + // for submodules within other files recursively. - match parent_src.value { - ModuleSource::Module(m) => { - let range = Some(m.syntax().text_range()); - res.insert(file_id, range); - } - ModuleSource::SourceFile(_) => { - res.insert(file_id, None); - res.extend(parent_module.children(db).map(|m| { - let src = m.definition_source(db); - (src.file_id.original_file(db), None) - })); - } + if let Some(Visibility::Module(module)) = vis.and_then(|it| it.into()) { + let module: Module = module.into(); + let mut res = FxHashMap::default(); + let src = module.definition_source(db); + let file_id = src.file_id.original_file(db); + + match src.value { + ModuleSource::Module(m) => { + let range = Some(m.syntax().text_range()); + res.insert(file_id, range); + } + ModuleSource::SourceFile(_) => { + res.insert(file_id, None); + res.extend(module.children(db).map(|m| { + let src = m.definition_source(db); + (src.file_id.original_file(db), None) + })); } - return SearchScope::new(res); } + return SearchScope::new(res); } - if vis.as_str() != "" { + if let Some(Visibility::Public) = vis { let source_root_id = db.file_source_root(file_id); let source_root = db.source_root(source_root_id); let mut res = source_root.walk().map(|id| (id, None)).collect::>(); - // FIXME: add "pub(in path)" - - if vis.as_str() == "pub(crate)" { - return SearchScope::new(res); - } - if vis.as_str() == "pub" { - let krate = module.krate(); - for rev_dep in krate.reverse_dependencies(db) { - let root_file = rev_dep.root_file(db); - let source_root_id = db.file_source_root(root_file); - let source_root = db.source_root(source_root_id); - res.extend(source_root.walk().map(|id| (id, None))); - } - return SearchScope::new(res); + let krate = module.krate(); + for rev_dep in krate.reverse_dependencies(db) { + let root_file = rev_dep.root_file(db); + let source_root_id = db.file_source_root(root_file); + let source_root = db.source_root(source_root_id); + res.extend(source_root.walk().map(|id| (id, None))); } + return SearchScope::new(res); } let mut res = FxHashMap::default();