]> git.lizzy.rs Git - rust.git/blob - crates/ra_hir_def/src/src.rs
20200d1db49d0b392ba92833bf32011b8d92684a
[rust.git] / crates / ra_hir_def / src / src.rs
1 //! Utilities for mapping between hir IDs and the surface syntax.
2
3 use hir_expand::InFile;
4 use ra_arena::map::ArenaMap;
5 use ra_syntax::ast;
6
7 use crate::{
8     db::DefDatabase, ConstLoc, EnumLoc, FunctionLoc, ImplLoc, StaticLoc, StructLoc, TraitLoc,
9     TypeAliasLoc, UnionLoc,
10 };
11
12 pub trait HasSource {
13     type Value;
14     fn source(&self, db: &impl DefDatabase) -> InFile<Self::Value>;
15 }
16
17 impl HasSource for FunctionLoc {
18     type Value = ast::FnDef;
19
20     fn source(&self, db: &impl DefDatabase) -> InFile<ast::FnDef> {
21         let node = self.ast_id.to_node(db);
22         InFile::new(self.ast_id.file_id, node)
23     }
24 }
25
26 impl HasSource for TypeAliasLoc {
27     type Value = ast::TypeAliasDef;
28
29     fn source(&self, db: &impl DefDatabase) -> InFile<ast::TypeAliasDef> {
30         let node = self.ast_id.to_node(db);
31         InFile::new(self.ast_id.file_id, node)
32     }
33 }
34
35 impl HasSource for ConstLoc {
36     type Value = ast::ConstDef;
37
38     fn source(&self, db: &impl DefDatabase) -> InFile<ast::ConstDef> {
39         let node = self.ast_id.to_node(db);
40         InFile::new(self.ast_id.file_id, node)
41     }
42 }
43
44 impl HasSource for StaticLoc {
45     type Value = ast::StaticDef;
46
47     fn source(&self, db: &impl DefDatabase) -> InFile<ast::StaticDef> {
48         let node = self.ast_id.to_node(db);
49         InFile::new(self.ast_id.file_id, node)
50     }
51 }
52
53 impl HasSource for ImplLoc {
54     type Value = ast::ImplBlock;
55
56     fn source(&self, db: &impl DefDatabase) -> InFile<ast::ImplBlock> {
57         let node = self.ast_id.to_node(db);
58         InFile::new(self.ast_id.file_id, node)
59     }
60 }
61
62 impl HasSource for TraitLoc {
63     type Value = ast::TraitDef;
64
65     fn source(&self, db: &impl DefDatabase) -> InFile<ast::TraitDef> {
66         let node = self.ast_id.to_node(db);
67         InFile::new(self.ast_id.file_id, node)
68     }
69 }
70
71 impl HasSource for StructLoc {
72     type Value = ast::StructDef;
73
74     fn source(&self, db: &impl DefDatabase) -> InFile<ast::StructDef> {
75         let node = self.ast_id.to_node(db);
76         InFile::new(self.ast_id.file_id, node)
77     }
78 }
79
80 impl HasSource for UnionLoc {
81     type Value = ast::UnionDef;
82
83     fn source(&self, db: &impl DefDatabase) -> InFile<ast::UnionDef> {
84         let node = self.ast_id.to_node(db);
85         InFile::new(self.ast_id.file_id, node)
86     }
87 }
88
89 impl HasSource for EnumLoc {
90     type Value = ast::EnumDef;
91
92     fn source(&self, db: &impl DefDatabase) -> InFile<ast::EnumDef> {
93         let node = self.ast_id.to_node(db);
94         InFile::new(self.ast_id.file_id, node)
95     }
96 }
97
98 pub trait HasChildSource {
99     type ChildId;
100     type Value;
101     fn child_source(&self, db: &impl DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>>;
102 }