diff options
Diffstat (limited to 'crates/ra_hir/src/nameres/raw.rs')
-rw-r--r-- | crates/ra_hir/src/nameres/raw.rs | 69 |
1 files changed, 52 insertions, 17 deletions
diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs index 35cbe6655..b7416ede6 100644 --- a/crates/ra_hir/src/nameres/raw.rs +++ b/crates/ra_hir/src/nameres/raw.rs | |||
@@ -31,21 +31,43 @@ pub struct RawItems { | |||
31 | 31 | ||
32 | #[derive(Debug, Default, PartialEq, Eq)] | 32 | #[derive(Debug, Default, PartialEq, Eq)] |
33 | pub struct ImportSourceMap { | 33 | pub struct ImportSourceMap { |
34 | map: ArenaMap<ImportId, AstPtr<ast::UseTree>>, | 34 | map: ArenaMap<ImportId, ImportSourcePtr>, |
35 | } | ||
36 | |||
37 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
38 | enum ImportSourcePtr { | ||
39 | UseTree(AstPtr<ast::UseTree>), | ||
40 | ExternCrate(AstPtr<ast::ExternCrateItem>), | ||
41 | } | ||
42 | |||
43 | impl ImportSourcePtr { | ||
44 | fn to_node(self, file: &SourceFile) -> ImportSource { | ||
45 | match self { | ||
46 | ImportSourcePtr::UseTree(ptr) => ImportSource::UseTree(ptr.to_node(file).to_owned()), | ||
47 | ImportSourcePtr::ExternCrate(ptr) => { | ||
48 | ImportSource::ExternCrate(ptr.to_node(file).to_owned()) | ||
49 | } | ||
50 | } | ||
51 | } | ||
52 | } | ||
53 | |||
54 | pub enum ImportSource { | ||
55 | UseTree(TreeArc<ast::UseTree>), | ||
56 | ExternCrate(TreeArc<ast::ExternCrateItem>), | ||
35 | } | 57 | } |
36 | 58 | ||
37 | impl ImportSourceMap { | 59 | impl ImportSourceMap { |
38 | fn insert(&mut self, import: ImportId, use_tree: &ast::UseTree) { | 60 | fn insert(&mut self, import: ImportId, ptr: ImportSourcePtr) { |
39 | self.map.insert(import, AstPtr::new(use_tree)) | 61 | self.map.insert(import, ptr) |
40 | } | 62 | } |
41 | 63 | ||
42 | pub(crate) fn get(&self, source: &ModuleSource, import: ImportId) -> TreeArc<ast::UseTree> { | 64 | pub(crate) fn get(&self, source: &ModuleSource, import: ImportId) -> ImportSource { |
43 | let file = match source { | 65 | let file = match source { |
44 | ModuleSource::SourceFile(file) => &*file, | 66 | ModuleSource::SourceFile(file) => &*file, |
45 | ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), | 67 | ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), |
46 | }; | 68 | }; |
47 | 69 | ||
48 | self.map[import].to_node(file).to_owned() | 70 | self.map[import].to_node(file) |
49 | } | 71 | } |
50 | } | 72 | } |
51 | 73 | ||
@@ -257,15 +279,13 @@ impl RawItemsCollector { | |||
257 | let is_prelude = use_item.has_atom_attr("prelude_import"); | 279 | let is_prelude = use_item.has_atom_attr("prelude_import"); |
258 | 280 | ||
259 | Path::expand_use_item(use_item, |path, use_tree, is_glob, alias| { | 281 | Path::expand_use_item(use_item, |path, use_tree, is_glob, alias| { |
260 | let import = self.raw_items.imports.alloc(ImportData { | 282 | let import_data = |
261 | path, | 283 | ImportData { path, alias, is_glob, is_prelude, is_extern_crate: false }; |
262 | alias, | 284 | self.push_import( |
263 | is_glob, | 285 | current_module, |
264 | is_prelude, | 286 | import_data, |
265 | is_extern_crate: false, | 287 | ImportSourcePtr::UseTree(AstPtr::new(use_tree)), |
266 | }); | 288 | ); |
267 | self.source_map.insert(import, use_tree); | ||
268 | self.push_item(current_module, RawItem::Import(import)) | ||
269 | }) | 289 | }) |
270 | } | 290 | } |
271 | 291 | ||
@@ -277,14 +297,18 @@ impl RawItemsCollector { | |||
277 | if let Some(name_ref) = extern_crate.name_ref() { | 297 | if let Some(name_ref) = extern_crate.name_ref() { |
278 | let path = Path::from_name_ref(name_ref); | 298 | let path = Path::from_name_ref(name_ref); |
279 | let alias = extern_crate.alias().and_then(|a| a.name()).map(AsName::as_name); | 299 | let alias = extern_crate.alias().and_then(|a| a.name()).map(AsName::as_name); |
280 | let import = self.raw_items.imports.alloc(ImportData { | 300 | let import_data = ImportData { |
281 | path, | 301 | path, |
282 | alias, | 302 | alias, |
283 | is_glob: false, | 303 | is_glob: false, |
284 | is_prelude: false, | 304 | is_prelude: false, |
285 | is_extern_crate: true, | 305 | is_extern_crate: true, |
286 | }); | 306 | }; |
287 | self.push_item(current_module, RawItem::Import(import)) | 307 | self.push_import( |
308 | current_module, | ||
309 | import_data, | ||
310 | ImportSourcePtr::ExternCrate(AstPtr::new(extern_crate)), | ||
311 | ); | ||
288 | } | 312 | } |
289 | } | 313 | } |
290 | 314 | ||
@@ -301,6 +325,17 @@ impl RawItemsCollector { | |||
301 | self.push_item(current_module, RawItem::Macro(m)); | 325 | self.push_item(current_module, RawItem::Macro(m)); |
302 | } | 326 | } |
303 | 327 | ||
328 | fn push_import( | ||
329 | &mut self, | ||
330 | current_module: Option<Module>, | ||
331 | data: ImportData, | ||
332 | source: ImportSourcePtr, | ||
333 | ) { | ||
334 | let import = self.raw_items.imports.alloc(data); | ||
335 | self.source_map.insert(import, source); | ||
336 | self.push_item(current_module, RawItem::Import(import)) | ||
337 | } | ||
338 | |||
304 | fn push_item(&mut self, current_module: Option<Module>, item: RawItem) { | 339 | fn push_item(&mut self, current_module: Option<Module>, item: RawItem) { |
305 | match current_module { | 340 | match current_module { |
306 | Some(module) => match &mut self.raw_items.modules[module] { | 341 | Some(module) => match &mut self.raw_items.modules[module] { |