aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-23 13:41:04 +0000
committerAleksey Kladov <[email protected]>2019-11-23 13:41:04 +0000
commit338ff052c2fe658d6fcf88888918b2e372129e21 (patch)
tree6551407c9053a4e521a3f4cda018b8b1c1f9cb00 /crates/ra_hir/src/code_model
parente5bcb69e4f88c7536b1039a881816cb8f1c1f867 (diff)
Simplify HasSource
Diffstat (limited to 'crates/ra_hir/src/code_model')
-rw-r--r--crates/ra_hir/src/code_model/src.rs39
1 files changed, 18 insertions, 21 deletions
diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs
index ceb3daa27..402f821bf 100644
--- a/crates/ra_hir/src/code_model/src.rs
+++ b/crates/ra_hir/src/code_model/src.rs
@@ -5,24 +5,24 @@ use hir_expand::either::Either;
5use ra_syntax::ast::{self, AstNode}; 5use ra_syntax::ast::{self, AstNode};
6 6
7use crate::{ 7use crate::{
8 db::{AstDatabase, DefDatabase, HirDatabase}, 8 db::{DefDatabase, HirDatabase},
9 ids::AstItemDef, 9 ids::AstItemDef,
10 Const, Enum, EnumVariant, FieldSource, Function, HasBody, MacroDef, Module, ModuleSource, 10 Const, Enum, EnumVariant, FieldSource, Function, HasBody, Import, MacroDef, Module,
11 Static, Struct, StructField, Trait, TypeAlias, Union, Import 11 ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union,
12}; 12};
13 13
14pub use hir_expand::Source; 14pub use hir_expand::Source;
15 15
16pub trait HasSource { 16pub trait HasSource {
17 type Ast; 17 type Ast;
18 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<Self::Ast>; 18 fn source(self, db: &impl DefDatabase) -> Source<Self::Ast>;
19} 19}
20 20
21/// NB: Module is !HasSource, because it has two source nodes at the same time: 21/// NB: Module is !HasSource, because it has two source nodes at the same time:
22/// definition and declaration. 22/// definition and declaration.
23impl Module { 23impl Module {
24 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. 24 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
25 pub fn definition_source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ModuleSource> { 25 pub fn definition_source(self, db: &impl DefDatabase) -> Source<ModuleSource> {
26 let def_map = db.crate_def_map(self.id.krate); 26 let def_map = db.crate_def_map(self.id.krate);
27 let src = def_map[self.id.module_id].definition_source(db); 27 let src = def_map[self.id.module_id].definition_source(db);
28 src.map(|it| match it { 28 src.map(|it| match it {
@@ -33,10 +33,7 @@ impl Module {
33 33
34 /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. 34 /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
35 /// `None` for the crate root. 35 /// `None` for the crate root.
36 pub fn declaration_source( 36 pub fn declaration_source(self, db: &impl DefDatabase) -> Option<Source<ast::Module>> {
37 self,
38 db: &(impl DefDatabase + AstDatabase),
39 ) -> Option<Source<ast::Module>> {
40 let def_map = db.crate_def_map(self.id.krate); 37 let def_map = db.crate_def_map(self.id.krate);
41 def_map[self.id.module_id].declaration_source(db) 38 def_map[self.id.module_id].declaration_source(db)
42 } 39 }
@@ -44,7 +41,7 @@ impl Module {
44 41
45impl HasSource for StructField { 42impl HasSource for StructField {
46 type Ast = FieldSource; 43 type Ast = FieldSource;
47 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<FieldSource> { 44 fn source(self, db: &impl DefDatabase) -> Source<FieldSource> {
48 let var = VariantId::from(self.parent); 45 let var = VariantId::from(self.parent);
49 let src = var.child_source(db); 46 let src = var.child_source(db);
50 src.map(|it| match it[self.id].clone() { 47 src.map(|it| match it[self.id].clone() {
@@ -55,61 +52,61 @@ impl HasSource for StructField {
55} 52}
56impl HasSource for Struct { 53impl HasSource for Struct {
57 type Ast = ast::StructDef; 54 type Ast = ast::StructDef;
58 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::StructDef> { 55 fn source(self, db: &impl DefDatabase) -> Source<ast::StructDef> {
59 self.id.0.source(db) 56 self.id.0.source(db)
60 } 57 }
61} 58}
62impl HasSource for Union { 59impl HasSource for Union {
63 type Ast = ast::StructDef; 60 type Ast = ast::StructDef;
64 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::StructDef> { 61 fn source(self, db: &impl DefDatabase) -> Source<ast::StructDef> {
65 self.id.0.source(db) 62 self.id.0.source(db)
66 } 63 }
67} 64}
68impl HasSource for Enum { 65impl HasSource for Enum {
69 type Ast = ast::EnumDef; 66 type Ast = ast::EnumDef;
70 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::EnumDef> { 67 fn source(self, db: &impl DefDatabase) -> Source<ast::EnumDef> {
71 self.id.source(db) 68 self.id.source(db)
72 } 69 }
73} 70}
74impl HasSource for EnumVariant { 71impl HasSource for EnumVariant {
75 type Ast = ast::EnumVariant; 72 type Ast = ast::EnumVariant;
76 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::EnumVariant> { 73 fn source(self, db: &impl DefDatabase) -> Source<ast::EnumVariant> {
77 self.parent.id.child_source(db).map(|map| map[self.id].clone()) 74 self.parent.id.child_source(db).map(|map| map[self.id].clone())
78 } 75 }
79} 76}
80impl HasSource for Function { 77impl HasSource for Function {
81 type Ast = ast::FnDef; 78 type Ast = ast::FnDef;
82 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::FnDef> { 79 fn source(self, db: &impl DefDatabase) -> Source<ast::FnDef> {
83 self.id.lookup(db).source(db) 80 self.id.lookup(db).source(db)
84 } 81 }
85} 82}
86impl HasSource for Const { 83impl HasSource for Const {
87 type Ast = ast::ConstDef; 84 type Ast = ast::ConstDef;
88 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::ConstDef> { 85 fn source(self, db: &impl DefDatabase) -> Source<ast::ConstDef> {
89 self.id.lookup(db).source(db) 86 self.id.lookup(db).source(db)
90 } 87 }
91} 88}
92impl HasSource for Static { 89impl HasSource for Static {
93 type Ast = ast::StaticDef; 90 type Ast = ast::StaticDef;
94 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::StaticDef> { 91 fn source(self, db: &impl DefDatabase) -> Source<ast::StaticDef> {
95 self.id.source(db) 92 self.id.source(db)
96 } 93 }
97} 94}
98impl HasSource for Trait { 95impl HasSource for Trait {
99 type Ast = ast::TraitDef; 96 type Ast = ast::TraitDef;
100 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::TraitDef> { 97 fn source(self, db: &impl DefDatabase) -> Source<ast::TraitDef> {
101 self.id.source(db) 98 self.id.source(db)
102 } 99 }
103} 100}
104impl HasSource for TypeAlias { 101impl HasSource for TypeAlias {
105 type Ast = ast::TypeAliasDef; 102 type Ast = ast::TypeAliasDef;
106 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::TypeAliasDef> { 103 fn source(self, db: &impl DefDatabase) -> Source<ast::TypeAliasDef> {
107 self.id.lookup(db).source(db) 104 self.id.lookup(db).source(db)
108 } 105 }
109} 106}
110impl HasSource for MacroDef { 107impl HasSource for MacroDef {
111 type Ast = ast::MacroCall; 108 type Ast = ast::MacroCall;
112 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::MacroCall> { 109 fn source(self, db: &impl DefDatabase) -> Source<ast::MacroCall> {
113 Source { file_id: self.id.ast_id.file_id(), value: self.id.ast_id.to_node(db) } 110 Source { file_id: self.id.ast_id.file_id(), value: self.id.ast_id.to_node(db) }
114 } 111 }
115} 112}
@@ -117,7 +114,7 @@ impl HasSource for Import {
117 type Ast = Either<ast::UseTree, ast::ExternCrateItem>; 114 type Ast = Either<ast::UseTree, ast::ExternCrateItem>;
118 115
119 /// Returns the syntax of the last path segment corresponding to this import 116 /// Returns the syntax of the last path segment corresponding to this import
120 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<Self::Ast> { 117 fn source(self, db: &impl DefDatabase) -> Source<Self::Ast> {
121 let src = self.parent.definition_source(db); 118 let src = self.parent.definition_source(db);
122 let (_, source_map) = db.raw_items_with_source_map(src.file_id); 119 let (_, source_map) = db.raw_items_with_source_map(src.file_id);
123 src.with_value(source_map.get(&src.value, self.id)) 120 src.with_value(source_map.get(&src.value, self.id))