diff options
Diffstat (limited to 'crates/ra_hir/src/impl_block.rs')
-rw-r--r-- | crates/ra_hir/src/impl_block.rs | 57 |
1 files changed, 51 insertions, 6 deletions
diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index 222e47349..5fc26324a 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs | |||
@@ -1,8 +1,10 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | use rustc_hash::FxHashMap; | 2 | use rustc_hash::FxHashMap; |
3 | 3 | ||
4 | use ra_arena::{Arena, RawId, impl_arena_id}; | 4 | use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; |
5 | use ra_syntax::ast::{self, AstNode}; | 5 | use ra_syntax::{ |
6 | AstPtr, SourceFile, TreeArc, | ||
7 | ast::{self, AstNode}}; | ||
6 | 8 | ||
7 | use crate::{ | 9 | use crate::{ |
8 | Const, Type, | 10 | Const, Type, |
@@ -14,6 +16,26 @@ use crate::{ | |||
14 | 16 | ||
15 | use crate::code_model_api::{Module, ModuleSource}; | 17 | use crate::code_model_api::{Module, ModuleSource}; |
16 | 18 | ||
19 | #[derive(Debug, Default, PartialEq, Eq)] | ||
20 | pub struct ImplSourceMap { | ||
21 | map: ArenaMap<ImplId, AstPtr<ast::ImplBlock>>, | ||
22 | } | ||
23 | |||
24 | impl ImplSourceMap { | ||
25 | fn insert(&mut self, impl_id: ImplId, impl_block: &ast::ImplBlock) { | ||
26 | self.map.insert(impl_id, AstPtr::new(impl_block)) | ||
27 | } | ||
28 | |||
29 | pub fn get(&self, source: &ModuleSource, impl_id: ImplId) -> TreeArc<ast::ImplBlock> { | ||
30 | let file = match source { | ||
31 | ModuleSource::SourceFile(file) => &*file, | ||
32 | ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), | ||
33 | }; | ||
34 | |||
35 | self.map[impl_id].to_node(file).to_owned() | ||
36 | } | ||
37 | } | ||
38 | |||
17 | #[derive(Debug, Clone, PartialEq, Eq)] | 39 | #[derive(Debug, Clone, PartialEq, Eq)] |
18 | pub struct ImplBlock { | 40 | pub struct ImplBlock { |
19 | module_impl_blocks: Arc<ModuleImplBlocks>, | 41 | module_impl_blocks: Arc<ModuleImplBlocks>, |
@@ -39,6 +61,10 @@ impl ImplBlock { | |||
39 | } | 61 | } |
40 | } | 62 | } |
41 | 63 | ||
64 | pub fn id(&self) -> ImplId { | ||
65 | self.impl_id | ||
66 | } | ||
67 | |||
42 | fn impl_data(&self) -> &ImplData { | 68 | fn impl_data(&self) -> &ImplData { |
43 | &self.module_impl_blocks.impls[self.impl_id] | 69 | &self.module_impl_blocks.impls[self.impl_id] |
44 | } | 70 | } |
@@ -148,7 +174,7 @@ impl ModuleImplBlocks { | |||
148 | } | 174 | } |
149 | } | 175 | } |
150 | 176 | ||
151 | fn collect(&mut self, db: &impl HirDatabase, module: Module) { | 177 | fn collect(&mut self, db: &impl HirDatabase, module: Module, source_map: &mut ImplSourceMap) { |
152 | let (file_id, module_source) = module.definition_source(db); | 178 | let (file_id, module_source) = module.definition_source(db); |
153 | let file_id: HirFileId = file_id.into(); | 179 | let file_id: HirFileId = file_id.into(); |
154 | let node = match &module_source { | 180 | let node = match &module_source { |
@@ -165,12 +191,31 @@ impl ModuleImplBlocks { | |||
165 | for &impl_item in &self.impls[id].items { | 191 | for &impl_item in &self.impls[id].items { |
166 | self.impls_by_def.insert(impl_item, id); | 192 | self.impls_by_def.insert(impl_item, id); |
167 | } | 193 | } |
194 | |||
195 | source_map.insert(id, impl_block_ast); | ||
168 | } | 196 | } |
169 | } | 197 | } |
170 | } | 198 | } |
171 | 199 | ||
172 | pub(crate) fn impls_in_module(db: &impl HirDatabase, module: Module) -> Arc<ModuleImplBlocks> { | 200 | pub(crate) fn impls_in_module_with_source_map_query( |
201 | db: &impl HirDatabase, | ||
202 | module: Module, | ||
203 | ) -> (Arc<ModuleImplBlocks>, Arc<ImplSourceMap>) { | ||
204 | let mut source_map = ImplSourceMap::default(); | ||
205 | |||
173 | let mut result = ModuleImplBlocks::new(); | 206 | let mut result = ModuleImplBlocks::new(); |
174 | result.collect(db, module); | 207 | result.collect(db, module, &mut source_map); |
175 | Arc::new(result) | 208 | |
209 | (Arc::new(result), Arc::new(source_map)) | ||
210 | } | ||
211 | |||
212 | pub(crate) fn impls_in_module(db: &impl HirDatabase, module: Module) -> Arc<ModuleImplBlocks> { | ||
213 | db.impls_in_module_with_source_map(module).0 | ||
214 | } | ||
215 | |||
216 | pub(crate) fn impls_in_module_source_map_query( | ||
217 | db: &impl HirDatabase, | ||
218 | module: Module, | ||
219 | ) -> Arc<ImplSourceMap> { | ||
220 | db.impls_in_module_with_source_map(module).1 | ||
176 | } | 221 | } |