Skip to content

Commit 1a567f5

Browse files
committed
Reduce copy-paste
1 parent 0c0ce1a commit 1a567f5

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

crates/ra_hir/src/from_source.rs

+30-20
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
use either::Either;
33

44
use hir_def::{
5-
child_from_source::ChildFromSource, nameres::ModuleSource, AstItemDef, EnumVariantId,
6-
LocationCtx, ModuleId, VariantId,
5+
child_from_source::ChildFromSource, nameres::ModuleSource, AstItemDef, EnumVariantId, ImplId,
6+
LocationCtx, ModuleId, TraitId, VariantId,
77
};
88
use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind};
99
use ra_syntax::{
@@ -53,24 +53,18 @@ impl FromSource for Trait {
5353
impl FromSource for Function {
5454
type Ast = ast::FnDef;
5555
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
56-
match Container::find(db, src.as_ref().map(|it| it.syntax()))? {
57-
Container::Trait(it) => it.id.child_from_source(db, src),
58-
Container::ImplBlock(it) => it.id.child_from_source(db, src),
59-
Container::Module(it) => it.id.child_from_source(db, src),
60-
}
61-
.map(Function::from)
56+
Container::find(db, src.as_ref().map(|it| it.syntax()))?
57+
.child_from_source(db, src)
58+
.map(Function::from)
6259
}
6360
}
6461

6562
impl FromSource for Const {
6663
type Ast = ast::ConstDef;
6764
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
68-
match Container::find(db, src.as_ref().map(|it| it.syntax()))? {
69-
Container::Trait(it) => it.id.child_from_source(db, src),
70-
Container::ImplBlock(it) => it.id.child_from_source(db, src),
71-
Container::Module(it) => it.id.child_from_source(db, src),
72-
}
73-
.map(Const::from)
65+
Container::find(db, src.as_ref().map(|it| it.syntax()))?
66+
.child_from_source(db, src)
67+
.map(Const::from)
7468
}
7569
}
7670
impl FromSource for Static {
@@ -86,12 +80,9 @@ impl FromSource for Static {
8680
impl FromSource for TypeAlias {
8781
type Ast = ast::TypeAliasDef;
8882
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
89-
match Container::find(db, src.as_ref().map(|it| it.syntax()))? {
90-
Container::Trait(it) => it.id.child_from_source(db, src),
91-
Container::ImplBlock(it) => it.id.child_from_source(db, src),
92-
Container::Module(it) => it.id.child_from_source(db, src),
93-
}
94-
.map(TypeAlias::from)
83+
Container::find(db, src.as_ref().map(|it| it.syntax()))?
84+
.child_from_source(db, src)
85+
.map(TypeAlias::from)
9586
}
9687
}
9788

@@ -263,3 +254,22 @@ impl Container {
263254
Some(Container::Module(c))
264255
}
265256
}
257+
258+
impl<CHILD, SOURCE> ChildFromSource<CHILD, SOURCE> for Container
259+
where
260+
TraitId: ChildFromSource<CHILD, SOURCE>,
261+
ImplId: ChildFromSource<CHILD, SOURCE>,
262+
ModuleId: ChildFromSource<CHILD, SOURCE>,
263+
{
264+
fn child_from_source(
265+
&self,
266+
db: &impl DefDatabase,
267+
child_source: InFile<SOURCE>,
268+
) -> Option<CHILD> {
269+
match self {
270+
Container::Trait(it) => it.id.child_from_source(db, child_source),
271+
Container::ImplBlock(it) => it.id.child_from_source(db, child_source),
272+
Container::Module(it) => it.id.child_from_source(db, child_source),
273+
}
274+
}
275+
}

0 commit comments

Comments
 (0)