diff options
author | Aleksey Kladov <[email protected]> | 2019-06-11 15:47:24 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-06-11 16:28:51 +0100 |
commit | 0145d06515d990f8b7bf45e261674c265c52d858 (patch) | |
tree | bddaf6d176e9d903503fbf2cd406f60f61dce2df /crates/ra_hir/src | |
parent | 178d8e96b5c810f40e7dd282ab06bb25d1fc8a2a (diff) |
use Source for module, part 1
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 13 | ||||
-rw-r--r-- | crates/ra_hir/src/impl_block.rs | 14 | ||||
-rw-r--r-- | crates/ra_hir/src/lang_item.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 6 |
4 files changed, 16 insertions, 19 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 4c121503e..e920256e5 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -196,16 +196,13 @@ impl Module { | |||
196 | } | 196 | } |
197 | 197 | ||
198 | /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. | 198 | /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. |
199 | pub fn definition_source( | 199 | pub fn definition_source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ModuleSource> { |
200 | self, | ||
201 | db: &(impl DefDatabase + AstDatabase), | ||
202 | ) -> (HirFileId, ModuleSource) { | ||
203 | let def_map = db.crate_def_map(self.krate); | 200 | let def_map = db.crate_def_map(self.krate); |
204 | let decl_id = def_map[self.module_id].declaration; | 201 | let decl_id = def_map[self.module_id].declaration; |
205 | let file_id = def_map[self.module_id].definition; | 202 | let file_id = def_map[self.module_id].definition; |
206 | let module_source = ModuleSource::new(db, file_id, decl_id); | 203 | let module_source = ModuleSource::new(db, file_id, decl_id); |
207 | let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id()); | 204 | let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id()); |
208 | (file_id, module_source) | 205 | (file_id, module_source).into() |
209 | } | 206 | } |
210 | 207 | ||
211 | /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. | 208 | /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. |
@@ -226,9 +223,9 @@ impl Module { | |||
226 | db: &impl HirDatabase, | 223 | db: &impl HirDatabase, |
227 | import: ImportId, | 224 | import: ImportId, |
228 | ) -> Either<TreeArc<ast::UseTree>, TreeArc<ast::ExternCrateItem>> { | 225 | ) -> Either<TreeArc<ast::UseTree>, TreeArc<ast::ExternCrateItem>> { |
229 | let (file_id, source) = self.definition_source(db); | 226 | let src = self.definition_source(db); |
230 | let (_, source_map) = db.raw_items_with_source_map(file_id); | 227 | let (_, source_map) = db.raw_items_with_source_map(src.file_id); |
231 | source_map.get(&source, import) | 228 | source_map.get(&src.ast, import) |
232 | } | 229 | } |
233 | 230 | ||
234 | /// Returns the crate this module is part of. | 231 | /// Returns the crate this module is part of. |
diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index 8194f38d4..646b603d3 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs | |||
@@ -48,8 +48,8 @@ impl HasSource for ImplBlock { | |||
48 | type Ast = TreeArc<ast::ImplBlock>; | 48 | type Ast = TreeArc<ast::ImplBlock>; |
49 | fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::ImplBlock>> { | 49 | fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::ImplBlock>> { |
50 | let source_map = db.impls_in_module_with_source_map(self.module).1; | 50 | let source_map = db.impls_in_module_with_source_map(self.module).1; |
51 | let (file_id, source) = self.module.definition_source(db); | 51 | let src = self.module.definition_source(db); |
52 | (file_id, source_map.get(&source, self.impl_id)).into() | 52 | (src.file_id, source_map.get(&src.ast, self.impl_id)).into() |
53 | } | 53 | } |
54 | } | 54 | } |
55 | 55 | ||
@@ -69,8 +69,8 @@ impl ImplBlock { | |||
69 | /// Returns the syntax of the impl block | 69 | /// Returns the syntax of the impl block |
70 | pub fn source(&self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::ImplBlock>> { | 70 | pub fn source(&self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::ImplBlock>> { |
71 | let source_map = db.impls_in_module_with_source_map(self.module).1; | 71 | let source_map = db.impls_in_module_with_source_map(self.module).1; |
72 | let (file_id, source) = self.module.definition_source(db); | 72 | let src = self.module.definition_source(db); |
73 | (file_id, source_map.get(&source, self.impl_id)).into() | 73 | (src.file_id, source_map.get(&src.ast, self.impl_id)).into() |
74 | } | 74 | } |
75 | 75 | ||
76 | pub fn id(&self) -> ImplId { | 76 | pub fn id(&self) -> ImplId { |
@@ -207,8 +207,8 @@ impl ModuleImplBlocks { | |||
207 | impls_by_def: FxHashMap::default(), | 207 | impls_by_def: FxHashMap::default(), |
208 | }; | 208 | }; |
209 | 209 | ||
210 | let (file_id, module_source) = m.module.definition_source(db); | 210 | let src = m.module.definition_source(db); |
211 | let node = match &module_source { | 211 | let node = match &src.ast { |
212 | ModuleSource::SourceFile(node) => node.syntax(), | 212 | ModuleSource::SourceFile(node) => node.syntax(), |
213 | ModuleSource::Module(node) => { | 213 | ModuleSource::Module(node) => { |
214 | node.item_list().expect("inline module should have item list").syntax() | 214 | node.item_list().expect("inline module should have item list").syntax() |
@@ -216,7 +216,7 @@ impl ModuleImplBlocks { | |||
216 | }; | 216 | }; |
217 | 217 | ||
218 | for impl_block_ast in node.children().filter_map(ast::ImplBlock::cast) { | 218 | for impl_block_ast in node.children().filter_map(ast::ImplBlock::cast) { |
219 | let impl_block = ImplData::from_ast(db, file_id, m.module, impl_block_ast); | 219 | let impl_block = ImplData::from_ast(db, src.file_id, m.module, impl_block_ast); |
220 | let id = m.impls.alloc(impl_block); | 220 | let id = m.impls.alloc(impl_block); |
221 | for &impl_item in &m.impls[id].items { | 221 | for &impl_item in &m.impls[id].items { |
222 | m.impls_by_def.insert(impl_item, id); | 222 | m.impls_by_def.insert(impl_item, id); |
diff --git a/crates/ra_hir/src/lang_item.rs b/crates/ra_hir/src/lang_item.rs index 684fbc068..cdc9182d6 100644 --- a/crates/ra_hir/src/lang_item.rs +++ b/crates/ra_hir/src/lang_item.rs | |||
@@ -84,7 +84,7 @@ impl LangItems { | |||
84 | ) { | 84 | ) { |
85 | // Look for impl targets | 85 | // Look for impl targets |
86 | let (impl_blocks, source_map) = db.impls_in_module_with_source_map(module.clone()); | 86 | let (impl_blocks, source_map) = db.impls_in_module_with_source_map(module.clone()); |
87 | let source = module.definition_source(db).1; | 87 | let source = module.definition_source(db).ast; |
88 | for (impl_id, _) in impl_blocks.impls.iter() { | 88 | for (impl_id, _) in impl_blocks.impls.iter() { |
89 | let impl_block = source_map.get(&source, impl_id); | 89 | let impl_block = source_map.get(&source, impl_id); |
90 | let lang_item_name = impl_block | 90 | let lang_item_name = impl_block |
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 876ebe0e3..4f9e8c5a9 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -97,13 +97,13 @@ pub fn struct_from_module( | |||
97 | module: Module, | 97 | module: Module, |
98 | struct_def: &ast::StructDef, | 98 | struct_def: &ast::StructDef, |
99 | ) -> Struct { | 99 | ) -> Struct { |
100 | let (file_id, _) = module.definition_source(db); | 100 | let file_id = module.definition_source(db).file_id; |
101 | let ctx = LocationCtx::new(db, module, file_id); | 101 | let ctx = LocationCtx::new(db, module, file_id); |
102 | Struct { id: ctx.to_def(struct_def) } | 102 | Struct { id: ctx.to_def(struct_def) } |
103 | } | 103 | } |
104 | 104 | ||
105 | pub fn enum_from_module(db: &impl HirDatabase, module: Module, enum_def: &ast::EnumDef) -> Enum { | 105 | pub fn enum_from_module(db: &impl HirDatabase, module: Module, enum_def: &ast::EnumDef) -> Enum { |
106 | let (file_id, _) = module.definition_source(db); | 106 | let file_id = module.definition_source(db).file_id; |
107 | let ctx = LocationCtx::new(db, module, file_id); | 107 | let ctx = LocationCtx::new(db, module, file_id); |
108 | Enum { id: ctx.to_def(enum_def) } | 108 | Enum { id: ctx.to_def(enum_def) } |
109 | } | 109 | } |
@@ -113,7 +113,7 @@ pub fn trait_from_module( | |||
113 | module: Module, | 113 | module: Module, |
114 | trait_def: &ast::TraitDef, | 114 | trait_def: &ast::TraitDef, |
115 | ) -> Trait { | 115 | ) -> Trait { |
116 | let (file_id, _) = module.definition_source(db); | 116 | let file_id = module.definition_source(db).file_id; |
117 | let ctx = LocationCtx::new(db, module, file_id); | 117 | let ctx = LocationCtx::new(db, module, file_id); |
118 | Trait { id: ctx.to_def(trait_def) } | 118 | Trait { id: ctx.to_def(trait_def) } |
119 | } | 119 | } |