From 1a567f5ca28b40c1cd744c9123a59695fab351de Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 5 Dec 2019 16:53:17 +0100 Subject: Reduce copy-paste --- crates/ra_hir/src/from_source.rs | 50 ++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 20 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 58203c721..6fa947759 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -2,8 +2,8 @@ use either::Either; use hir_def::{ - child_from_source::ChildFromSource, nameres::ModuleSource, AstItemDef, EnumVariantId, - LocationCtx, ModuleId, VariantId, + child_from_source::ChildFromSource, nameres::ModuleSource, AstItemDef, EnumVariantId, ImplId, + LocationCtx, ModuleId, TraitId, VariantId, }; use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; use ra_syntax::{ @@ -53,24 +53,18 @@ impl FromSource for Trait { impl FromSource for Function { type Ast = ast::FnDef; fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { - match Container::find(db, src.as_ref().map(|it| it.syntax()))? { - Container::Trait(it) => it.id.child_from_source(db, src), - Container::ImplBlock(it) => it.id.child_from_source(db, src), - Container::Module(it) => it.id.child_from_source(db, src), - } - .map(Function::from) + Container::find(db, src.as_ref().map(|it| it.syntax()))? + .child_from_source(db, src) + .map(Function::from) } } impl FromSource for Const { type Ast = ast::ConstDef; fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { - match Container::find(db, src.as_ref().map(|it| it.syntax()))? { - Container::Trait(it) => it.id.child_from_source(db, src), - Container::ImplBlock(it) => it.id.child_from_source(db, src), - Container::Module(it) => it.id.child_from_source(db, src), - } - .map(Const::from) + Container::find(db, src.as_ref().map(|it| it.syntax()))? + .child_from_source(db, src) + .map(Const::from) } } impl FromSource for Static { @@ -86,12 +80,9 @@ impl FromSource for Static { impl FromSource for TypeAlias { type Ast = ast::TypeAliasDef; fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { - match Container::find(db, src.as_ref().map(|it| it.syntax()))? { - Container::Trait(it) => it.id.child_from_source(db, src), - Container::ImplBlock(it) => it.id.child_from_source(db, src), - Container::Module(it) => it.id.child_from_source(db, src), - } - .map(TypeAlias::from) + Container::find(db, src.as_ref().map(|it| it.syntax()))? + .child_from_source(db, src) + .map(TypeAlias::from) } } @@ -263,3 +254,22 @@ impl Container { Some(Container::Module(c)) } } + +impl ChildFromSource for Container +where + TraitId: ChildFromSource, + ImplId: ChildFromSource, + ModuleId: ChildFromSource, +{ + fn child_from_source( + &self, + db: &impl DefDatabase, + child_source: InFile, + ) -> Option { + match self { + Container::Trait(it) => it.id.child_from_source(db, child_source), + Container::ImplBlock(it) => it.id.child_from_source(db, child_source), + Container::Module(it) => it.id.child_from_source(db, child_source), + } + } +} -- cgit v1.2.3