aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-12-05 15:53:17 +0000
committerAleksey Kladov <[email protected]>2019-12-05 15:56:13 +0000
commit1a567f5ca28b40c1cd744c9123a59695fab351de (patch)
treede4f1deb1cbf454662371e55f5827521ad62b471
parent0c0ce1ae418a2f3f4fc125bd701cdb327f607002 (diff)
Reduce copy-paste
-rw-r--r--crates/ra_hir/src/from_source.rs50
1 files changed, 30 insertions, 20 deletions
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 @@
2use either::Either; 2use either::Either;
3 3
4use hir_def::{ 4use hir_def::{
5 child_from_source::ChildFromSource, nameres::ModuleSource, AstItemDef, EnumVariantId, 5 child_from_source::ChildFromSource, nameres::ModuleSource, AstItemDef, EnumVariantId, ImplId,
6 LocationCtx, ModuleId, VariantId, 6 LocationCtx, ModuleId, TraitId, VariantId,
7}; 7};
8use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; 8use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind};
9use ra_syntax::{ 9use ra_syntax::{
@@ -53,24 +53,18 @@ impl FromSource for Trait {
53impl FromSource for Function { 53impl FromSource for Function {
54 type Ast = ast::FnDef; 54 type Ast = ast::FnDef;
55 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { 55 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()))? { 56 Container::find(db, src.as_ref().map(|it| it.syntax()))?
57 Container::Trait(it) => it.id.child_from_source(db, src), 57 .child_from_source(db, src)
58 Container::ImplBlock(it) => it.id.child_from_source(db, src), 58 .map(Function::from)
59 Container::Module(it) => it.id.child_from_source(db, src),
60 }
61 .map(Function::from)
62 } 59 }
63} 60}
64 61
65impl FromSource for Const { 62impl FromSource for Const {
66 type Ast = ast::ConstDef; 63 type Ast = ast::ConstDef;
67 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { 64 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()))? { 65 Container::find(db, src.as_ref().map(|it| it.syntax()))?
69 Container::Trait(it) => it.id.child_from_source(db, src), 66 .child_from_source(db, src)
70 Container::ImplBlock(it) => it.id.child_from_source(db, src), 67 .map(Const::from)
71 Container::Module(it) => it.id.child_from_source(db, src),
72 }
73 .map(Const::from)
74 } 68 }
75} 69}
76impl FromSource for Static { 70impl FromSource for Static {
@@ -86,12 +80,9 @@ impl FromSource for Static {
86impl FromSource for TypeAlias { 80impl FromSource for TypeAlias {
87 type Ast = ast::TypeAliasDef; 81 type Ast = ast::TypeAliasDef;
88 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { 82 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()))? { 83 Container::find(db, src.as_ref().map(|it| it.syntax()))?
90 Container::Trait(it) => it.id.child_from_source(db, src), 84 .child_from_source(db, src)
91 Container::ImplBlock(it) => it.id.child_from_source(db, src), 85 .map(TypeAlias::from)
92 Container::Module(it) => it.id.child_from_source(db, src),
93 }
94 .map(TypeAlias::from)
95 } 86 }
96} 87}
97 88
@@ -263,3 +254,22 @@ impl Container {
263 Some(Container::Module(c)) 254 Some(Container::Module(c))
264 } 255 }
265} 256}
257
258impl<CHILD, SOURCE> ChildFromSource<CHILD, SOURCE> for Container
259where
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}