From 09bc7ca74dc920a01db32b2a29f70eb9f10c9853 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 26 Feb 2020 13:22:46 +0100 Subject: Reduce visibility --- crates/ra_hir/src/source_binder.rs | 84 +++++++------------------------------- 1 file changed, 14 insertions(+), 70 deletions(-) (limited to 'crates/ra_hir/src/source_binder.rs') diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 0b8a641f9..4353e25ac 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -6,7 +6,7 @@ use hir_def::{ dyn_map::DynMap, keys::{self, Key}, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId, - StaticId, StructFieldId, StructId, TraitId, TypeAliasId, UnionId, VariantId, + StaticId, StructFieldId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, VariantId, }; use hir_expand::{name::AsName, AstId, InFile, MacroDefId, MacroDefKind}; use ra_db::FileId; @@ -17,9 +17,9 @@ use ra_syntax::{ }; use rustc_hash::FxHashMap; -use crate::{db::HirDatabase, Local, Module, TypeParam}; +use crate::{db::HirDatabase, Module}; -pub struct SourceBinder { +pub(crate) struct SourceBinder { child_by_source_cache: FxHashMap, } @@ -38,7 +38,11 @@ impl SourceBinder { Some(Module { id: ModuleId { krate, local_id } }) } - fn to_id(&mut self, db: &impl HirDatabase, src: InFile) -> Option { + pub(crate) fn to_id( + &mut self, + db: &impl HirDatabase, + src: InFile, + ) -> Option { T::to_id(db, self, src) } @@ -118,42 +122,6 @@ pub(crate) trait ToId: Sized { ) -> Option; } -pub trait ToDef: Sized + AstNode + 'static { - type Def; - fn to_def( - db: &DB, - sb: &mut SourceBinder, - src: InFile, - ) -> Option; -} - -macro_rules! to_def_impls { - ($(($def:path, $ast:path)),* ,) => {$( - impl ToDef for $ast { - type Def = $def; - fn to_def(db: &DB, sb: &mut SourceBinder, src: InFile) - -> Option - { sb.to_id(db, src).map(Into::into) } - } - )*} -} - -to_def_impls![ - (crate::Module, ast::Module), - (crate::Struct, ast::StructDef), - (crate::Enum, ast::EnumDef), - (crate::Union, ast::UnionDef), - (crate::Trait, ast::TraitDef), - (crate::ImplBlock, ast::ImplBlock), - (crate::TypeAlias, ast::TypeAliasDef), - (crate::Const, ast::ConstDef), - (crate::Static, ast::StaticDef), - (crate::Function, ast::FnDef), - (crate::StructField, ast::RecordFieldDef), - (crate::EnumVariant, ast::EnumVariant), - (crate::MacroDef, ast::MacroCall), // this one is dubious, not all calls are macros -]; - #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] pub(crate) enum ChildContainer { DefWithBodyId(DefWithBodyId), @@ -245,37 +213,14 @@ impl ToId for ast::MacroCall { } } -impl ToDef for ast::BindPat { - type Def = Local; +impl ToId for ast::TypeParam { + type ID = TypeParamId; - fn to_def(db: &DB, sb: &mut SourceBinder, src: InFile) -> Option { - let file_id = src.file_id; - let parent: DefWithBodyId = src.value.syntax().ancestors().find_map(|it| { - let res = match_ast! { - match it { - ast::ConstDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() }, - ast::StaticDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() }, - ast::FnDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() }, - _ => return None, - } - }; - Some(res) - })?; - let (_body, source_map) = db.body_with_source_map(parent); - let src = src.map(ast::Pat::from); - let pat_id = source_map.node_pat(src.as_ref())?; - Some(Local { parent: parent.into(), pat_id }) - } -} - -impl ToDef for ast::TypeParam { - type Def = TypeParam; - - fn to_def( + fn to_id( db: &DB, sb: &mut SourceBinder, - src: InFile, - ) -> Option { + src: InFile, + ) -> Option { let file_id = src.file_id; let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| { let res = match_ast! { @@ -291,8 +236,7 @@ impl ToDef for ast::TypeParam { }; Some(res) })?; - let &id = sb.child_by_source(db, parent.into())[keys::TYPE_PARAM].get(&src)?; - Some(TypeParam { id }) + sb.child_by_source(db, parent.into())[keys::TYPE_PARAM].get(&src).copied() } } -- cgit v1.2.3