From 0d5ae89948903bb150b72885c6d9f24dc7a2bb51 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 19 Feb 2020 14:45:49 +0100 Subject: Derive visibility as well --- crates/ra_ide/src/references/classify.rs | 23 ++++---- crates/ra_ide/src/references/search_scope.rs | 2 +- crates/ra_ide_db/src/defs.rs | 83 +++++++++++++++------------- 3 files changed, 55 insertions(+), 53 deletions(-) diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs index 2394e68d1..ca5750521 100644 --- a/crates/ra_ide/src/references/classify.rs +++ b/crates/ra_ide/src/references/classify.rs @@ -22,14 +22,14 @@ pub(crate) fn classify_name_ref( if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { tested_by!(goto_def_for_methods); if let Some(func) = analyzer.resolve_method_call(&method_call) { - return Some(from_module_def(sb.db, func.into())); + return Some(from_module_def(func.into())); } } if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { tested_by!(goto_def_for_fields); if let Some(field) = analyzer.resolve_field(&field_expr) { - return Some(from_struct_field(sb.db, field)); + return Some(from_struct_field(field)); } } @@ -37,50 +37,47 @@ pub(crate) fn classify_name_ref( tested_by!(goto_def_for_record_fields); tested_by!(goto_def_for_field_init_shorthand); if let Some(field_def) = analyzer.resolve_record_field(&record_field) { - return Some(from_struct_field(sb.db, field_def)); + return Some(from_struct_field(field_def)); } } - // FIXME: find correct container and visibility for each case - let visibility = None; - if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { tested_by!(goto_def_for_macros); if let Some(macro_def) = analyzer.resolve_macro_call(sb.db, name_ref.with_value(¯o_call)) { let kind = NameKind::Macro(macro_def); - return Some(NameDefinition { kind, visibility }); + return Some(NameDefinition { kind }); } } let path = name_ref.value.syntax().ancestors().find_map(ast::Path::cast)?; let resolved = analyzer.resolve_path(sb.db, &path)?; let res = match resolved { - PathResolution::Def(def) => from_module_def(sb.db, def), + PathResolution::Def(def) => from_module_def(def), PathResolution::AssocItem(item) => { let def = match item { hir::AssocItem::Function(it) => it.into(), hir::AssocItem::Const(it) => it.into(), hir::AssocItem::TypeAlias(it) => it.into(), }; - from_module_def(sb.db, def) + from_module_def(def) } PathResolution::Local(local) => { let kind = NameKind::Local(local); - NameDefinition { kind, visibility: None } + NameDefinition { kind } } PathResolution::TypeParam(par) => { let kind = NameKind::TypeParam(par); - NameDefinition { kind, visibility } + NameDefinition { kind } } PathResolution::Macro(def) => { let kind = NameKind::Macro(def); - NameDefinition { kind, visibility } + NameDefinition { kind } } PathResolution::SelfType(impl_block) => { let kind = NameKind::SelfType(impl_block); - NameDefinition { kind, visibility } + NameDefinition { kind } } }; Some(res) diff --git a/crates/ra_ide/src/references/search_scope.rs b/crates/ra_ide/src/references/search_scope.rs index f9bbe64a4..e5ac12044 100644 --- a/crates/ra_ide/src/references/search_scope.rs +++ b/crates/ra_ide/src/references/search_scope.rs @@ -43,7 +43,7 @@ impl SearchScope { return SearchScope::new(res); } - let vis = def.visibility.as_ref().map(|v| v.syntax().to_string()).unwrap_or_default(); + let vis = def.visibility(db).as_ref().map(|v| v.syntax().to_string()).unwrap_or_default(); if vis.as_str() == "pub(super)" { if let Some(parent_module) = module.parent(db) { diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs index cc772de51..aec748abf 100644 --- a/crates/ra_ide_db/src/defs.rs +++ b/crates/ra_ide_db/src/defs.rs @@ -6,8 +6,8 @@ // FIXME: this badly needs rename/rewrite (matklad, 2020-02-06). use hir::{ - Adt, HasSource, ImplBlock, InFile, Local, MacroDef, Module, ModuleDef, SourceBinder, - StructField, TypeParam, VariantDef, + Adt, FieldSource, HasSource, ImplBlock, InFile, Local, MacroDef, Module, ModuleDef, + SourceBinder, StructField, TypeParam, }; use ra_prof::profile; use ra_syntax::{ @@ -29,7 +29,6 @@ pub enum NameKind { #[derive(PartialEq, Eq)] pub struct NameDefinition { - pub visibility: Option, /// FIXME: this doesn't really make sense. For example, builtin types don't /// really have a module. pub kind: NameKind, @@ -46,6 +45,34 @@ impl NameDefinition { NameKind::TypeParam(it) => Some(it.module(db)), } } + + pub fn visibility(&self, db: &RootDatabase) -> Option { + match self.kind { + NameKind::Macro(_) => None, + NameKind::StructField(sf) => match sf.source(db).value { + FieldSource::Named(it) => it.visibility(), + FieldSource::Pos(it) => it.visibility(), + }, + NameKind::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, + }, + NameKind::SelfType(_) => None, + NameKind::Local(_) => None, + NameKind::TypeParam(_) => None, + } + } } pub fn classify_name( @@ -61,65 +88,63 @@ pub fn classify_name( let src = name.with_value(it); let local = sb.to_def(src)?; Some(NameDefinition { - visibility: None, kind: NameKind::Local(local), }) }, ast::RecordFieldDef(it) => { let src = name.with_value(it); let field: hir::StructField = sb.to_def(src)?; - Some(from_struct_field(sb.db, field)) + Some(from_struct_field(field)) }, ast::Module(it) => { let def = sb.to_def(name.with_value(it))?; - Some(from_module_def(sb.db, def.into())) + Some(from_module_def(def.into())) }, ast::StructDef(it) => { let src = name.with_value(it); let def: hir::Struct = sb.to_def(src)?; - Some(from_module_def(sb.db, def.into())) + Some(from_module_def(def.into())) }, ast::EnumDef(it) => { let src = name.with_value(it); let def: hir::Enum = sb.to_def(src)?; - Some(from_module_def(sb.db, def.into())) + Some(from_module_def(def.into())) }, ast::TraitDef(it) => { let src = name.with_value(it); let def: hir::Trait = sb.to_def(src)?; - Some(from_module_def(sb.db, def.into())) + Some(from_module_def(def.into())) }, ast::StaticDef(it) => { let src = name.with_value(it); let def: hir::Static = sb.to_def(src)?; - Some(from_module_def(sb.db, def.into())) + Some(from_module_def(def.into())) }, ast::EnumVariant(it) => { let src = name.with_value(it); let def: hir::EnumVariant = sb.to_def(src)?; - Some(from_module_def(sb.db, def.into())) + Some(from_module_def(def.into())) }, ast::FnDef(it) => { let src = name.with_value(it); let def: hir::Function = sb.to_def(src)?; - Some(from_module_def(sb.db, def.into())) + Some(from_module_def(def.into())) }, ast::ConstDef(it) => { let src = name.with_value(it); let def: hir::Const = sb.to_def(src)?; - Some(from_module_def(sb.db, def.into())) + Some(from_module_def(def.into())) }, ast::TypeAliasDef(it) => { let src = name.with_value(it); let def: hir::TypeAlias = sb.to_def(src)?; - Some(from_module_def(sb.db, def.into())) + Some(from_module_def(def.into())) }, ast::MacroCall(it) => { let src = name.with_value(it); let def = sb.to_def(src.clone())?; Some(NameDefinition { - visibility: None, kind: NameKind::Macro(def), }) }, @@ -127,7 +152,6 @@ pub fn classify_name( let src = name.with_value(it); let def = sb.to_def(src)?; Some(NameDefinition { - visibility: None, kind: NameKind::TypeParam(def), }) }, @@ -136,31 +160,12 @@ pub fn classify_name( } } -pub fn from_struct_field(db: &RootDatabase, field: StructField) -> NameDefinition { +pub fn from_struct_field(field: StructField) -> NameDefinition { let kind = NameKind::StructField(field); - let parent = field.parent_def(db); - let visibility = match parent { - VariantDef::Struct(s) => s.source(db).value.visibility(), - VariantDef::Union(e) => e.source(db).value.visibility(), - VariantDef::EnumVariant(e) => e.source(db).value.parent_enum().visibility(), - }; - NameDefinition { kind, visibility } + NameDefinition { kind } } -pub fn from_module_def(db: &RootDatabase, def: ModuleDef) -> NameDefinition { +pub fn from_module_def(def: ModuleDef) -> NameDefinition { let kind = NameKind::ModuleDef(def); - let visibility = match def { - ModuleDef::Module(it) => it.declaration_source(db).and_then(|s| s.value.visibility()), - ModuleDef::EnumVariant(it) => it.source(db).value.parent_enum().visibility(), - ModuleDef::Function(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::Adt(Adt::Struct(it)) => it.source(db).value.visibility(), - ModuleDef::Adt(Adt::Union(it)) => it.source(db).value.visibility(), - ModuleDef::Adt(Adt::Enum(it)) => it.source(db).value.visibility(), - ModuleDef::BuiltinType(..) => None, - }; - NameDefinition { kind, visibility } + NameDefinition { kind } } -- cgit v1.2.3