aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-06-11 16:00:08 +0100
committerAleksey Kladov <[email protected]>2019-06-11 16:28:51 +0100
commit0dcaded439ae4bd4670bc6a2fbf739cd4fce60af (patch)
tree9f97e6be462d8c3b38da74af095f4160adeda51c /crates/ra_hir/src/code_model
parentc4512fadb1b332b13bb41b0aa8a28aa964664842 (diff)
move source to a seaparate file
Diffstat (limited to 'crates/ra_hir/src/code_model')
-rw-r--r--crates/ra_hir/src/code_model/src.rs120
1 files changed, 120 insertions, 0 deletions
diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs
new file mode 100644
index 000000000..7484faf04
--- /dev/null
+++ b/crates/ra_hir/src/code_model/src.rs
@@ -0,0 +1,120 @@
1use ra_syntax::{TreeArc, ast};
2
3use crate::{
4 HirFileId, DefDatabase, AstDatabase, Module, ModuleSource,
5 StructField, Struct, Enum, Union, EnumVariant, Function, Static, Trait, Const, TypeAlias,
6 FieldSource, MacroDef, ids::AstItemDef,
7};
8
9pub struct Source<T> {
10 pub file_id: HirFileId,
11 pub ast: T,
12}
13
14impl<T> From<(HirFileId, T)> for Source<T> {
15 fn from((file_id, ast): (HirFileId, T)) -> Self {
16 Source { file_id, ast }
17 }
18}
19
20pub trait HasSource {
21 type Ast;
22 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<Self::Ast>;
23}
24
25/// NB: Module is !HasSource, becase it has two source nodes at the same time:
26/// definition and declaration.
27impl Module {
28 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
29 pub fn definition_source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ModuleSource> {
30 let def_map = db.crate_def_map(self.krate);
31 let decl_id = def_map[self.module_id].declaration;
32 let file_id = def_map[self.module_id].definition;
33 let module_source = ModuleSource::new(db, file_id, decl_id);
34 let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id());
35 (file_id, module_source).into()
36 }
37
38 /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
39 /// `None` for the crate root.
40 pub fn declaration_source(
41 self,
42 db: &(impl DefDatabase + AstDatabase),
43 ) -> Option<Source<TreeArc<ast::Module>>> {
44 let def_map = db.crate_def_map(self.krate);
45 let decl = def_map[self.module_id].declaration?;
46 let ast = decl.to_node(db);
47 Some((decl.file_id(), ast).into())
48 }
49}
50
51impl HasSource for StructField {
52 type Ast = FieldSource;
53 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<FieldSource> {
54 self.source_impl(db).into()
55 }
56}
57impl HasSource for Struct {
58 type Ast = TreeArc<ast::StructDef>;
59 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StructDef>> {
60 self.id.source(db).into()
61 }
62}
63impl HasSource for Union {
64 type Ast = TreeArc<ast::StructDef>;
65 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StructDef>> {
66 self.id.source(db).into()
67 }
68}
69impl HasSource for Enum {
70 type Ast = TreeArc<ast::EnumDef>;
71 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::EnumDef>> {
72 self.id.source(db).into()
73 }
74}
75impl HasSource for EnumVariant {
76 type Ast = TreeArc<ast::EnumVariant>;
77 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::EnumVariant>> {
78 self.source_impl(db)
79 }
80}
81impl HasSource for Function {
82 type Ast = TreeArc<ast::FnDef>;
83
84 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::FnDef>> {
85 self.id.source(db).into()
86 }
87}
88impl HasSource for Const {
89 type Ast = TreeArc<ast::ConstDef>;
90
91 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::ConstDef>> {
92 self.id.source(db).into()
93 }
94}
95impl HasSource for Static {
96 type Ast = TreeArc<ast::StaticDef>;
97
98 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StaticDef>> {
99 self.id.source(db).into()
100 }
101}
102impl HasSource for Trait {
103 type Ast = TreeArc<ast::TraitDef>;
104 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::TraitDef>> {
105 self.id.source(db).into()
106 }
107}
108impl HasSource for TypeAlias {
109 type Ast = TreeArc<ast::TypeAliasDef>;
110 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::TypeAliasDef>> {
111 self.id.source(db).into()
112 }
113}
114impl HasSource for MacroDef {
115 type Ast = TreeArc<ast::MacroCall>;
116
117 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::MacroCall>> {
118 (self.id.0.file_id(), self.id.0.to_node(db)).into()
119 }
120}