From 762ec9581a4331a2726cc236c565323d0c8cdb07 Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Mon, 11 May 2020 12:25:18 +0300 Subject: [PATCH 1/3] Find references to a function outside module --- crates/ra_ide/src/references.rs | 25 +++++++++++++++++++++++++ crates/ra_ide_db/src/defs.rs | 1 + 2 files changed, 26 insertions(+) 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..c74daff383 100644 --- a/crates/ra_ide_db/src/defs.rs +++ b/crates/ra_ide_db/src/defs.rs @@ -52,6 +52,7 @@ impl Definition { let parent = id.parent_enum(db); module?.visibility_of(db, &ModuleDef::Adt(Adt::Enum(parent))) } + ModuleDef::Function(f) => Some(f.visibility(db)), _ => module?.visibility_of(db, def), }, Definition::SelfType(_) => None, From 753e1e679cf14b4ead73602976d43a8dc89a7052 Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Mon, 11 May 2020 13:52:20 +0300 Subject: [PATCH 2/3] Also for consts and type aliases --- crates/ra_ide_db/src/defs.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs index c74daff383..bddfef2407 100644 --- a/crates/ra_ide_db/src/defs.rs +++ b/crates/ra_ide_db/src/defs.rs @@ -53,6 +53,8 @@ impl Definition { module?.visibility_of(db, &ModuleDef::Adt(Adt::Enum(parent))) } ModuleDef::Function(f) => Some(f.visibility(db)), + ModuleDef::Const(c) => Some(c.visibility(db)), + ModuleDef::TypeAlias(t) => Some(t.visibility(db)), _ => module?.visibility_of(db, def), }, Definition::SelfType(_) => None, From 3d66aa054230ad788162ce49f0d334e900458cac Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Mon, 11 May 2020 14:28:14 +0300 Subject: [PATCH 3/3] New definition_visibility method --- crates/ra_hir/src/code_model.rs | 20 ++++++++++++++++++++ crates/ra_ide_db/src/defs.rs | 15 ++------------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index be18c845c6..cb91bd965e 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_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs index bddfef2407..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,21 +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))) - } - ModuleDef::Function(f) => Some(f.visibility(db)), - ModuleDef::Const(c) => Some(c.visibility(db)), - ModuleDef::TypeAlias(t) => Some(t.visibility(db)), - _ => module?.visibility_of(db, def), - }, + Definition::ModuleDef(def) => def.definition_visibility(db), Definition::SelfType(_) => None, Definition::Local(_) => None, Definition::TypeParam(_) => None,