From ca15cf422cb83c9657ab1db8a8165f486e4b7cde Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Tue, 24 Dec 2019 21:45:58 +0100 Subject: Add visibility in code model for fields --- crates/ra_hir/src/code_model.rs | 19 ++++++++++++++++++- crates/ra_hir/src/lib.rs | 4 ++-- 2 files changed, 20 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 76d8f85f1..c5114742b 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -118,7 +118,7 @@ impl_froms!( BuiltinType ); -pub use hir_def::attr::Attrs; +pub use hir_def::{attr::Attrs, visibility::ResolvedVisibility}; impl Module { pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module { @@ -255,6 +255,15 @@ impl StructField { } } +impl HasVisibility for StructField { + fn visibility(&self, db: &impl HirDatabase) -> ResolvedVisibility { + let struct_field_id: hir_def::StructFieldId = (*self).into(); + let visibility = db.visibility(struct_field_id.into()); + let parent_id: hir_def::VariantId = self.parent.into(); + visibility.resolve(db, &parent_id.resolver(db)) + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Struct { pub(crate) id: StructId, @@ -1041,3 +1050,11 @@ impl + Copy> Docs for T { db.documentation(def.into()) } } + +pub trait HasVisibility { + fn visibility(&self, db: &impl HirDatabase) -> ResolvedVisibility; + fn visible_from(&self, db: &impl HirDatabase, module: Module) -> bool { + let vis = self.visibility(db); + vis.visible_from(db, module.id) + } +} diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 0008a8858..3d13978d4 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -40,8 +40,8 @@ mod from_source; pub use crate::{ code_model::{ Adt, AssocItem, AttrDef, Const, Crate, CrateDependency, DefWithBody, Docs, Enum, - EnumVariant, FieldSource, Function, GenericDef, HasAttrs, ImplBlock, Local, MacroDef, - Module, ModuleDef, ScopeDef, Static, Struct, StructField, Trait, Type, TypeAlias, + EnumVariant, FieldSource, Function, GenericDef, HasAttrs, HasVisibility, ImplBlock, Local, + MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, StructField, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, }, from_source::FromSource, -- cgit v1.2.3 From 50ebff257dafe6e820f002241466ff4a98aa1f32 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 26 Dec 2019 16:00:10 +0100 Subject: Rename ResolvedVisibility -> Visibility --- crates/ra_hir/src/code_model.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index c5114742b..fdf316745 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -118,7 +118,7 @@ impl_froms!( BuiltinType ); -pub use hir_def::{attr::Attrs, visibility::ResolvedVisibility}; +pub use hir_def::{attr::Attrs, visibility::Visibility}; impl Module { pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module { @@ -256,7 +256,7 @@ impl StructField { } impl HasVisibility for StructField { - fn visibility(&self, db: &impl HirDatabase) -> ResolvedVisibility { + fn visibility(&self, db: &impl HirDatabase) -> Visibility { let struct_field_id: hir_def::StructFieldId = (*self).into(); let visibility = db.visibility(struct_field_id.into()); let parent_id: hir_def::VariantId = self.parent.into(); @@ -1052,7 +1052,7 @@ impl + Copy> Docs for T { } pub trait HasVisibility { - fn visibility(&self, db: &impl HirDatabase) -> ResolvedVisibility; + fn visibility(&self, db: &impl HirDatabase) -> Visibility; fn visible_from(&self, db: &impl HirDatabase, module: Module) -> bool { let vis = self.visibility(db); vis.visible_from(db, module.id) -- cgit v1.2.3 From 78111620a33c57b58b07ebf044a7d53dc56176ef Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 26 Dec 2019 16:22:15 +0100 Subject: Remove visibility query, instead add struct field visibility to data Methods should be handled the same, and for items the visibility will be in the def map. --- crates/ra_hir/src/code_model.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index fdf316745..9612c86da 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -257,8 +257,8 @@ impl StructField { impl HasVisibility for StructField { fn visibility(&self, db: &impl HirDatabase) -> Visibility { - let struct_field_id: hir_def::StructFieldId = (*self).into(); - let visibility = db.visibility(struct_field_id.into()); + let variant_data = self.parent.variant_data(db); + let visibility = &variant_data.fields()[self.id].visibility; let parent_id: hir_def::VariantId = self.parent.into(); visibility.resolve(db, &parent_id.resolver(db)) } -- cgit v1.2.3 From 9fd2c813ca355c3a1f10f54993c16e81778b867b Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 27 Dec 2019 11:24:31 +0100 Subject: visible_from -> is_visible_from --- crates/ra_hir/src/code_model.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 9612c86da..488f74cfb 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -1053,8 +1053,8 @@ impl + Copy> Docs for T { pub trait HasVisibility { fn visibility(&self, db: &impl HirDatabase) -> Visibility; - fn visible_from(&self, db: &impl HirDatabase, module: Module) -> bool { + fn is_visible_from(&self, db: &impl HirDatabase, module: Module) -> bool { let vis = self.visibility(db); - vis.visible_from(db, module.id) + vis.is_visible_from(db, module.id) } } -- cgit v1.2.3