diff options
author | Aleksey Kladov <[email protected]> | 2019-12-05 15:53:17 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-12-05 15:56:13 +0000 |
commit | 1a567f5ca28b40c1cd744c9123a59695fab351de (patch) | |
tree | de4f1deb1cbf454662371e55f5827521ad62b471 /crates/ra_hir | |
parent | 0c0ce1ae418a2f3f4fc125bd701cdb327f607002 (diff) |
Reduce copy-paste
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/from_source.rs | 50 |
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 @@ | |||
2 | use either::Either; | 2 | use either::Either; |
3 | 3 | ||
4 | use hir_def::{ | 4 | use 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 | }; |
8 | use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; | 8 | use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; |
9 | use ra_syntax::{ | 9 | use ra_syntax::{ |
@@ -53,24 +53,18 @@ impl FromSource for Trait { | |||
53 | impl FromSource for Function { | 53 | impl 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 | ||
65 | impl FromSource for Const { | 62 | impl 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 | } |
76 | impl FromSource for Static { | 70 | impl FromSource for Static { |
@@ -86,12 +80,9 @@ impl FromSource for Static { | |||
86 | impl FromSource for TypeAlias { | 80 | impl 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 | |||
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 | } | ||