diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 3fc2eccdd7..e8e3211fcd 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -148,6 +148,26 @@ impl ModuleDef { ModuleDef::BuiltinType(_) => None, } } + + pub fn definition_visibility(&self, db: &dyn HirDatabase) -> Option { + let module = match self { + ModuleDef::Module(it) => it.parent(db)?, + ModuleDef::Function(it) => return Some(it.visibility(db)), + ModuleDef::Adt(it) => it.module(db), + ModuleDef::EnumVariant(it) => { + let parent = it.parent_enum(db); + let module = it.module(db); + return module.visibility_of(db, &ModuleDef::Adt(Adt::Enum(parent))); + } + ModuleDef::Const(it) => return Some(it.visibility(db)), + ModuleDef::Static(it) => it.module(db), + ModuleDef::Trait(it) => it.module(db), + ModuleDef::TypeAlias(it) => return Some(it.visibility(db)), + ModuleDef::BuiltinType(_) => return None, + }; + + module.visibility_of(db, self) + } } pub use hir_def::{ diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs index 555ccf2952..074284b42e 100644 --- a/crates/ra_ide/src/references.rs +++ b/crates/ra_ide/src/references.rs @@ -593,6 +593,31 @@ mod tests { check_result(refs, "i BIND_PAT FileId(1) 36..37 Other", &["FileId(1) 51..52 Other Write"]); } + #[test] + fn test_find_struct_function_refs_outside_module() { + let code = r#" + mod foo { + pub struct Foo; + + impl Foo { + pub fn new<|>() -> Foo { + Foo + } + } + } + + fn main() { + let _f = foo::Foo::new(); + }"#; + + let refs = get_all_refs(code); + check_result( + refs, + "new FN_DEF FileId(1) 87..150 94..97 Other", + &["FileId(1) 227..230 StructLiteral"], + ); + } + fn get_all_refs(text: &str) -> ReferenceSearchResult { let (analysis, position) = single_file_with_position(text); analysis.find_all_refs(position, None).unwrap().unwrap() diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs index f990e3bb97..60c11178ee 100644 --- a/crates/ra_ide_db/src/defs.rs +++ b/crates/ra_ide_db/src/defs.rs @@ -6,7 +6,7 @@ // FIXME: this badly needs rename/rewrite (matklad, 2020-02-06). use hir::{ - Adt, Field, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, Name, PathResolution, + Field, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, Name, PathResolution, Semantics, TypeParam, Visibility, }; use ra_prof::profile; @@ -42,18 +42,10 @@ impl Definition { } pub fn visibility(&self, db: &RootDatabase) -> Option { - let module = self.module(db); - match self { Definition::Macro(_) => None, Definition::Field(sf) => Some(sf.visibility(db)), - Definition::ModuleDef(def) => match def { - ModuleDef::EnumVariant(id) => { - let parent = id.parent_enum(db); - module?.visibility_of(db, &ModuleDef::Adt(Adt::Enum(parent))) - } - _ => module?.visibility_of(db, def), - }, + Definition::ModuleDef(def) => def.definition_visibility(db), Definition::SelfType(_) => None, Definition::Local(_) => None, Definition::TypeParam(_) => None,