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.rs41
1 files changed, 22 insertions, 19 deletions
diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs
index bf3ee0834..2cf210349 100644
--- a/crates/ra_hir/src/code_model/src.rs
+++ b/crates/ra_hir/src/code_model/src.rs
@@ -1,6 +1,9 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use hir_def::{AstItemDef, HasChildSource, HasSource as _, Lookup, VariantId}; 3use hir_def::{
4 src::{HasChildSource, HasSource as _},
5 AstItemDef, Lookup, VariantId,
6};
4use hir_expand::either::Either; 7use hir_expand::either::Either;
5use ra_syntax::ast; 8use ra_syntax::ast;
6 9
@@ -9,18 +12,18 @@ use crate::{
9 Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, 12 Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union,
10}; 13};
11 14
12pub use hir_expand::Source; 15pub use hir_expand::InFile;
13 16
14pub trait HasSource { 17pub trait HasSource {
15 type Ast; 18 type Ast;
16 fn source(self, db: &impl DefDatabase) -> Source<Self::Ast>; 19 fn source(self, db: &impl DefDatabase) -> InFile<Self::Ast>;
17} 20}
18 21
19/// NB: Module is !HasSource, because it has two source nodes at the same time: 22/// NB: Module is !HasSource, because it has two source nodes at the same time:
20/// definition and declaration. 23/// definition and declaration.
21impl Module { 24impl Module {
22 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. 25 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
23 pub fn definition_source(self, db: &impl DefDatabase) -> Source<ModuleSource> { 26 pub fn definition_source(self, db: &impl DefDatabase) -> InFile<ModuleSource> {
24 let def_map = db.crate_def_map(self.id.krate); 27 let def_map = db.crate_def_map(self.id.krate);
25 let src = def_map[self.id.local_id].definition_source(db); 28 let src = def_map[self.id.local_id].definition_source(db);
26 src.map(|it| match it { 29 src.map(|it| match it {
@@ -31,7 +34,7 @@ impl Module {
31 34
32 /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. 35 /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
33 /// `None` for the crate root. 36 /// `None` for the crate root.
34 pub fn declaration_source(self, db: &impl DefDatabase) -> Option<Source<ast::Module>> { 37 pub fn declaration_source(self, db: &impl DefDatabase) -> Option<InFile<ast::Module>> {
35 let def_map = db.crate_def_map(self.id.krate); 38 let def_map = db.crate_def_map(self.id.krate);
36 def_map[self.id.local_id].declaration_source(db) 39 def_map[self.id.local_id].declaration_source(db)
37 } 40 }
@@ -39,7 +42,7 @@ impl Module {
39 42
40impl HasSource for StructField { 43impl HasSource for StructField {
41 type Ast = FieldSource; 44 type Ast = FieldSource;
42 fn source(self, db: &impl DefDatabase) -> Source<FieldSource> { 45 fn source(self, db: &impl DefDatabase) -> InFile<FieldSource> {
43 let var = VariantId::from(self.parent); 46 let var = VariantId::from(self.parent);
44 let src = var.child_source(db); 47 let src = var.child_source(db);
45 src.map(|it| match it[self.id].clone() { 48 src.map(|it| match it[self.id].clone() {
@@ -50,67 +53,67 @@ impl HasSource for StructField {
50} 53}
51impl HasSource for Struct { 54impl HasSource for Struct {
52 type Ast = ast::StructDef; 55 type Ast = ast::StructDef;
53 fn source(self, db: &impl DefDatabase) -> Source<ast::StructDef> { 56 fn source(self, db: &impl DefDatabase) -> InFile<ast::StructDef> {
54 self.id.source(db) 57 self.id.source(db)
55 } 58 }
56} 59}
57impl HasSource for Union { 60impl HasSource for Union {
58 type Ast = ast::UnionDef; 61 type Ast = ast::UnionDef;
59 fn source(self, db: &impl DefDatabase) -> Source<ast::UnionDef> { 62 fn source(self, db: &impl DefDatabase) -> InFile<ast::UnionDef> {
60 self.id.source(db) 63 self.id.source(db)
61 } 64 }
62} 65}
63impl HasSource for Enum { 66impl HasSource for Enum {
64 type Ast = ast::EnumDef; 67 type Ast = ast::EnumDef;
65 fn source(self, db: &impl DefDatabase) -> Source<ast::EnumDef> { 68 fn source(self, db: &impl DefDatabase) -> InFile<ast::EnumDef> {
66 self.id.source(db) 69 self.id.source(db)
67 } 70 }
68} 71}
69impl HasSource for EnumVariant { 72impl HasSource for EnumVariant {
70 type Ast = ast::EnumVariant; 73 type Ast = ast::EnumVariant;
71 fn source(self, db: &impl DefDatabase) -> Source<ast::EnumVariant> { 74 fn source(self, db: &impl DefDatabase) -> InFile<ast::EnumVariant> {
72 self.parent.id.child_source(db).map(|map| map[self.id].clone()) 75 self.parent.id.child_source(db).map(|map| map[self.id].clone())
73 } 76 }
74} 77}
75impl HasSource for Function { 78impl HasSource for Function {
76 type Ast = ast::FnDef; 79 type Ast = ast::FnDef;
77 fn source(self, db: &impl DefDatabase) -> Source<ast::FnDef> { 80 fn source(self, db: &impl DefDatabase) -> InFile<ast::FnDef> {
78 self.id.lookup(db).source(db) 81 self.id.lookup(db).source(db)
79 } 82 }
80} 83}
81impl HasSource for Const { 84impl HasSource for Const {
82 type Ast = ast::ConstDef; 85 type Ast = ast::ConstDef;
83 fn source(self, db: &impl DefDatabase) -> Source<ast::ConstDef> { 86 fn source(self, db: &impl DefDatabase) -> InFile<ast::ConstDef> {
84 self.id.lookup(db).source(db) 87 self.id.lookup(db).source(db)
85 } 88 }
86} 89}
87impl HasSource for Static { 90impl HasSource for Static {
88 type Ast = ast::StaticDef; 91 type Ast = ast::StaticDef;
89 fn source(self, db: &impl DefDatabase) -> Source<ast::StaticDef> { 92 fn source(self, db: &impl DefDatabase) -> InFile<ast::StaticDef> {
90 self.id.lookup(db).source(db) 93 self.id.lookup(db).source(db)
91 } 94 }
92} 95}
93impl HasSource for Trait { 96impl HasSource for Trait {
94 type Ast = ast::TraitDef; 97 type Ast = ast::TraitDef;
95 fn source(self, db: &impl DefDatabase) -> Source<ast::TraitDef> { 98 fn source(self, db: &impl DefDatabase) -> InFile<ast::TraitDef> {
96 self.id.source(db) 99 self.id.source(db)
97 } 100 }
98} 101}
99impl HasSource for TypeAlias { 102impl HasSource for TypeAlias {
100 type Ast = ast::TypeAliasDef; 103 type Ast = ast::TypeAliasDef;
101 fn source(self, db: &impl DefDatabase) -> Source<ast::TypeAliasDef> { 104 fn source(self, db: &impl DefDatabase) -> InFile<ast::TypeAliasDef> {
102 self.id.lookup(db).source(db) 105 self.id.lookup(db).source(db)
103 } 106 }
104} 107}
105impl HasSource for MacroDef { 108impl HasSource for MacroDef {
106 type Ast = ast::MacroCall; 109 type Ast = ast::MacroCall;
107 fn source(self, db: &impl DefDatabase) -> Source<ast::MacroCall> { 110 fn source(self, db: &impl DefDatabase) -> InFile<ast::MacroCall> {
108 Source { file_id: self.id.ast_id.file_id(), value: self.id.ast_id.to_node(db) } 111 InFile { file_id: self.id.ast_id.file_id, value: self.id.ast_id.to_node(db) }
109 } 112 }
110} 113}
111impl HasSource for ImplBlock { 114impl HasSource for ImplBlock {
112 type Ast = ast::ImplBlock; 115 type Ast = ast::ImplBlock;
113 fn source(self, db: &impl DefDatabase) -> Source<ast::ImplBlock> { 116 fn source(self, db: &impl DefDatabase) -> InFile<ast::ImplBlock> {
114 self.id.source(db) 117 self.id.source(db)
115 } 118 }
116} 119}
@@ -118,7 +121,7 @@ impl HasSource for Import {
118 type Ast = Either<ast::UseTree, ast::ExternCrateItem>; 121 type Ast = Either<ast::UseTree, ast::ExternCrateItem>;
119 122
120 /// Returns the syntax of the last path segment corresponding to this import 123 /// Returns the syntax of the last path segment corresponding to this import
121 fn source(self, db: &impl DefDatabase) -> Source<Self::Ast> { 124 fn source(self, db: &impl DefDatabase) -> InFile<Self::Ast> {
122 let src = self.parent.definition_source(db); 125 let src = self.parent.definition_source(db);
123 let (_, source_map) = db.raw_items_with_source_map(src.file_id); 126 let (_, source_map) = db.raw_items_with_source_map(src.file_id);
124 let root = db.parse_or_expand(src.file_id).unwrap(); 127 let root = db.parse_or_expand(src.file_id).unwrap();