aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model/src.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/code_model/src.rs')
-rw-r--r--crates/ra_hir/src/code_model/src.rs112
1 files changed, 44 insertions, 68 deletions
diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs
index fc466c1f0..b7bafe23d 100644
--- a/crates/ra_hir/src/code_model/src.rs
+++ b/crates/ra_hir/src/code_model/src.rs
@@ -1,151 +1,127 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use hir_def::{HasSource as _, Lookup}; 3use hir_def::{HasChildSource, HasSource as _, Lookup, VariantId};
4use hir_expand::either::Either;
4use ra_syntax::ast::{self, AstNode}; 5use ra_syntax::ast::{self, AstNode};
5 6
6use crate::{ 7use crate::{
7 db::{AstDatabase, DefDatabase, HirDatabase}, 8 db::{DefDatabase, HirDatabase},
8 ids::AstItemDef, 9 ids::AstItemDef,
9 Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef, Module, 10 Const, Enum, EnumVariant, FieldSource, Function, HasBody, Import, MacroDef, Module,
10 ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, VariantDef, 11 ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union,
11}; 12};
12 13
13pub use hir_expand::Source; 14pub use hir_expand::Source;
14 15
15pub trait HasSource { 16pub trait HasSource {
16 type Ast; 17 type Ast;
17 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<Self::Ast>; 18 fn source(self, db: &impl DefDatabase) -> Source<Self::Ast>;
18} 19}
19 20
20/// 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:
21/// definition and declaration. 22/// definition and declaration.
22impl Module { 23impl Module {
23 /// 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.
24 pub fn definition_source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ModuleSource> { 25 pub fn definition_source(self, db: &impl DefDatabase) -> Source<ModuleSource> {
25 let def_map = db.crate_def_map(self.id.krate); 26 let def_map = db.crate_def_map(self.id.krate);
26 let decl_id = def_map[self.id.module_id].declaration; 27 let src = def_map[self.id.module_id].definition_source(db);
27 let file_id = def_map[self.id.module_id].definition; 28 src.map(|it| match it {
28 let value = ModuleSource::new(db, file_id, decl_id); 29 Either::A(it) => ModuleSource::SourceFile(it),
29 let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id()); 30 Either::B(it) => ModuleSource::Module(it),
30 Source { file_id, value } 31 })
31 } 32 }
32 33
33 /// 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 {}`.
34 /// `None` for the crate root. 35 /// `None` for the crate root.
35 pub fn declaration_source( 36 pub fn declaration_source(self, db: &impl DefDatabase) -> Option<Source<ast::Module>> {
36 self,
37 db: &(impl DefDatabase + AstDatabase),
38 ) -> Option<Source<ast::Module>> {
39 let def_map = db.crate_def_map(self.id.krate); 37 let def_map = db.crate_def_map(self.id.krate);
40 let decl = def_map[self.id.module_id].declaration?; 38 def_map[self.id.module_id].declaration_source(db)
41 let value = decl.to_node(db);
42 Some(Source { file_id: decl.file_id(), value })
43 } 39 }
44} 40}
45 41
46impl HasSource for StructField { 42impl HasSource for StructField {
47 type Ast = FieldSource; 43 type Ast = FieldSource;
48 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<FieldSource> { 44 fn source(self, db: &impl DefDatabase) -> Source<FieldSource> {
49 let var_data = self.parent.variant_data(db); 45 let var = VariantId::from(self.parent);
50 let fields = var_data.fields().unwrap(); 46 let src = var.child_source(db);
51 let ss; 47 src.map(|it| match it[self.id].clone() {
52 let es; 48 Either::A(it) => FieldSource::Pos(it),
53 let (file_id, struct_kind) = match self.parent { 49 Either::B(it) => FieldSource::Named(it),
54 VariantDef::Struct(s) => { 50 })
55 ss = s.source(db);
56 (ss.file_id, ss.value.kind())
57 }
58 VariantDef::EnumVariant(e) => {
59 es = e.source(db);
60 (es.file_id, es.value.kind())
61 }
62 };
63
64 let field_sources = match struct_kind {
65 ast::StructKind::Tuple(fl) => fl.fields().map(|it| FieldSource::Pos(it)).collect(),
66 ast::StructKind::Record(fl) => fl.fields().map(|it| FieldSource::Named(it)).collect(),
67 ast::StructKind::Unit => Vec::new(),
68 };
69 let value = field_sources
70 .into_iter()
71 .zip(fields.iter())
72 .find(|(_syntax, (id, _))| *id == self.id)
73 .unwrap()
74 .0;
75 Source { file_id, value }
76 } 51 }
77} 52}
78impl HasSource for Struct { 53impl HasSource for Struct {
79 type Ast = ast::StructDef; 54 type Ast = ast::StructDef;
80 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::StructDef> { 55 fn source(self, db: &impl DefDatabase) -> Source<ast::StructDef> {
81 self.id.0.source(db) 56 self.id.0.source(db)
82 } 57 }
83} 58}
84impl HasSource for Union { 59impl HasSource for Union {
85 type Ast = ast::StructDef; 60 type Ast = ast::StructDef;
86 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::StructDef> { 61 fn source(self, db: &impl DefDatabase) -> Source<ast::StructDef> {
87 self.id.0.source(db) 62 self.id.0.source(db)
88 } 63 }
89} 64}
90impl HasSource for Enum { 65impl HasSource for Enum {
91 type Ast = ast::EnumDef; 66 type Ast = ast::EnumDef;
92 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::EnumDef> { 67 fn source(self, db: &impl DefDatabase) -> Source<ast::EnumDef> {
93 self.id.source(db) 68 self.id.source(db)
94 } 69 }
95} 70}
96impl HasSource for EnumVariant { 71impl HasSource for EnumVariant {
97 type Ast = ast::EnumVariant; 72 type Ast = ast::EnumVariant;
98 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::EnumVariant> { 73 fn source(self, db: &impl DefDatabase) -> Source<ast::EnumVariant> {
99 let enum_data = db.enum_data(self.parent.id); 74 self.parent.id.child_source(db).map(|map| map[self.id].clone())
100 let src = self.parent.id.source(db);
101 let value = src
102 .value
103 .variant_list()
104 .into_iter()
105 .flat_map(|it| it.variants())
106 .zip(enum_data.variants.iter())
107 .find(|(_syntax, (id, _))| *id == self.id)
108 .unwrap()
109 .0;
110 Source { file_id: src.file_id, value }
111 } 75 }
112} 76}
113impl HasSource for Function { 77impl HasSource for Function {
114 type Ast = ast::FnDef; 78 type Ast = ast::FnDef;
115 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::FnDef> { 79 fn source(self, db: &impl DefDatabase) -> Source<ast::FnDef> {
116 self.id.lookup(db).source(db) 80 self.id.lookup(db).source(db)
117 } 81 }
118} 82}
119impl HasSource for Const { 83impl HasSource for Const {
120 type Ast = ast::ConstDef; 84 type Ast = ast::ConstDef;
121 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::ConstDef> { 85 fn source(self, db: &impl DefDatabase) -> Source<ast::ConstDef> {
122 self.id.lookup(db).source(db) 86 self.id.lookup(db).source(db)
123 } 87 }
124} 88}
125impl HasSource for Static { 89impl HasSource for Static {
126 type Ast = ast::StaticDef; 90 type Ast = ast::StaticDef;
127 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::StaticDef> { 91 fn source(self, db: &impl DefDatabase) -> Source<ast::StaticDef> {
128 self.id.source(db) 92 self.id.source(db)
129 } 93 }
130} 94}
131impl HasSource for Trait { 95impl HasSource for Trait {
132 type Ast = ast::TraitDef; 96 type Ast = ast::TraitDef;
133 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::TraitDef> { 97 fn source(self, db: &impl DefDatabase) -> Source<ast::TraitDef> {
134 self.id.source(db) 98 self.id.source(db)
135 } 99 }
136} 100}
137impl HasSource for TypeAlias { 101impl HasSource for TypeAlias {
138 type Ast = ast::TypeAliasDef; 102 type Ast = ast::TypeAliasDef;
139 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::TypeAliasDef> { 103 fn source(self, db: &impl DefDatabase) -> Source<ast::TypeAliasDef> {
140 self.id.lookup(db).source(db) 104 self.id.lookup(db).source(db)
141 } 105 }
142} 106}
143impl HasSource for MacroDef { 107impl HasSource for MacroDef {
144 type Ast = ast::MacroCall; 108 type Ast = ast::MacroCall;
145 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::MacroCall> { 109 fn source(self, db: &impl DefDatabase) -> Source<ast::MacroCall> {
146 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) }
147 } 111 }
148} 112}
113impl HasSource for Import {
114 type Ast = Either<ast::UseTree, ast::ExternCrateItem>;
115
116 /// Returns the syntax of the last path segment corresponding to this import
117 fn source(self, db: &impl DefDatabase) -> Source<Self::Ast> {
118 let src = self.parent.definition_source(db);
119 let (_, source_map) = db.raw_items_with_source_map(src.file_id);
120 let root = db.parse_or_expand(src.file_id).unwrap();
121 let ptr = source_map.get(self.id);
122 src.with_value(ptr.map(|it| it.to_node(&root), |it| it.to_node(&root)))
123 }
124}
149 125
150pub trait HasBodySource: HasBody + HasSource 126pub trait HasBodySource: HasBody + HasSource
151where 127where