aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/code_model')
-rw-r--r--crates/ra_hir/src/code_model/src.rs32
1 files changed, 13 insertions, 19 deletions
diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs
index 7484faf04..7d8abb39e 100644
--- a/crates/ra_hir/src/code_model/src.rs
+++ b/crates/ra_hir/src/code_model/src.rs
@@ -11,12 +11,6 @@ pub struct Source<T> {
11 pub ast: T, 11 pub ast: T,
12} 12}
13 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 { 14pub trait HasSource {
21 type Ast; 15 type Ast;
22 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<Self::Ast>; 16 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<Self::Ast>;
@@ -30,9 +24,9 @@ impl Module {
30 let def_map = db.crate_def_map(self.krate); 24 let def_map = db.crate_def_map(self.krate);
31 let decl_id = def_map[self.module_id].declaration; 25 let decl_id = def_map[self.module_id].declaration;
32 let file_id = def_map[self.module_id].definition; 26 let file_id = def_map[self.module_id].definition;
33 let module_source = ModuleSource::new(db, file_id, decl_id); 27 let ast = ModuleSource::new(db, file_id, decl_id);
34 let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id()); 28 let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id());
35 (file_id, module_source).into() 29 Source { file_id, ast }
36 } 30 }
37 31
38 /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. 32 /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
@@ -44,32 +38,32 @@ impl Module {
44 let def_map = db.crate_def_map(self.krate); 38 let def_map = db.crate_def_map(self.krate);
45 let decl = def_map[self.module_id].declaration?; 39 let decl = def_map[self.module_id].declaration?;
46 let ast = decl.to_node(db); 40 let ast = decl.to_node(db);
47 Some((decl.file_id(), ast).into()) 41 Some(Source { file_id: decl.file_id(), ast })
48 } 42 }
49} 43}
50 44
51impl HasSource for StructField { 45impl HasSource for StructField {
52 type Ast = FieldSource; 46 type Ast = FieldSource;
53 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<FieldSource> { 47 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<FieldSource> {
54 self.source_impl(db).into() 48 self.source_impl(db)
55 } 49 }
56} 50}
57impl HasSource for Struct { 51impl HasSource for Struct {
58 type Ast = TreeArc<ast::StructDef>; 52 type Ast = TreeArc<ast::StructDef>;
59 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StructDef>> { 53 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StructDef>> {
60 self.id.source(db).into() 54 self.id.source(db)
61 } 55 }
62} 56}
63impl HasSource for Union { 57impl HasSource for Union {
64 type Ast = TreeArc<ast::StructDef>; 58 type Ast = TreeArc<ast::StructDef>;
65 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StructDef>> { 59 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StructDef>> {
66 self.id.source(db).into() 60 self.id.source(db)
67 } 61 }
68} 62}
69impl HasSource for Enum { 63impl HasSource for Enum {
70 type Ast = TreeArc<ast::EnumDef>; 64 type Ast = TreeArc<ast::EnumDef>;
71 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::EnumDef>> { 65 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::EnumDef>> {
72 self.id.source(db).into() 66 self.id.source(db)
73 } 67 }
74} 68}
75impl HasSource for EnumVariant { 69impl HasSource for EnumVariant {
@@ -82,39 +76,39 @@ impl HasSource for Function {
82 type Ast = TreeArc<ast::FnDef>; 76 type Ast = TreeArc<ast::FnDef>;
83 77
84 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::FnDef>> { 78 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::FnDef>> {
85 self.id.source(db).into() 79 self.id.source(db)
86 } 80 }
87} 81}
88impl HasSource for Const { 82impl HasSource for Const {
89 type Ast = TreeArc<ast::ConstDef>; 83 type Ast = TreeArc<ast::ConstDef>;
90 84
91 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::ConstDef>> { 85 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::ConstDef>> {
92 self.id.source(db).into() 86 self.id.source(db)
93 } 87 }
94} 88}
95impl HasSource for Static { 89impl HasSource for Static {
96 type Ast = TreeArc<ast::StaticDef>; 90 type Ast = TreeArc<ast::StaticDef>;
97 91
98 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StaticDef>> { 92 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StaticDef>> {
99 self.id.source(db).into() 93 self.id.source(db)
100 } 94 }
101} 95}
102impl HasSource for Trait { 96impl HasSource for Trait {
103 type Ast = TreeArc<ast::TraitDef>; 97 type Ast = TreeArc<ast::TraitDef>;
104 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::TraitDef>> { 98 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::TraitDef>> {
105 self.id.source(db).into() 99 self.id.source(db)
106 } 100 }
107} 101}
108impl HasSource for TypeAlias { 102impl HasSource for TypeAlias {
109 type Ast = TreeArc<ast::TypeAliasDef>; 103 type Ast = TreeArc<ast::TypeAliasDef>;
110 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::TypeAliasDef>> { 104 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::TypeAliasDef>> {
111 self.id.source(db).into() 105 self.id.source(db)
112 } 106 }
113} 107}
114impl HasSource for MacroDef { 108impl HasSource for MacroDef {
115 type Ast = TreeArc<ast::MacroCall>; 109 type Ast = TreeArc<ast::MacroCall>;
116 110
117 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::MacroCall>> { 111 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::MacroCall>> {
118 (self.id.0.file_id(), self.id.0.to_node(db)).into() 112 Source { file_id: self.id.0.file_id(), ast: self.id.0.to_node(db) }
119 } 113 }
120} 114}