diff options
Diffstat (limited to 'crates/ra_hir_def/src/nameres')
-rw-r--r-- | crates/ra_hir_def/src/nameres/raw.rs | 62 |
1 files changed, 12 insertions, 50 deletions
diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index ecb4d7c03..73e57f1e5 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs | |||
@@ -7,23 +7,21 @@ | |||
7 | 7 | ||
8 | use std::{ops::Index, sync::Arc}; | 8 | use std::{ops::Index, sync::Arc}; |
9 | 9 | ||
10 | use either::Either; | ||
11 | use hir_expand::{ | 10 | use hir_expand::{ |
12 | ast_id_map::AstIdMap, | 11 | ast_id_map::AstIdMap, |
13 | db::AstDatabase, | 12 | db::AstDatabase, |
14 | hygiene::Hygiene, | 13 | hygiene::Hygiene, |
15 | name::{AsName, Name}, | 14 | name::{AsName, Name}, |
16 | }; | 15 | }; |
17 | use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; | 16 | use ra_arena::{impl_arena_id, Arena, RawId}; |
18 | use ra_syntax::{ | 17 | use ra_syntax::{ |
19 | ast::{self, AttrsOwner, NameOwner}, | 18 | ast::{self, AttrsOwner, NameOwner}, |
20 | AstNode, AstPtr, | 19 | AstNode, |
21 | }; | 20 | }; |
22 | use test_utils::tested_by; | 21 | use test_utils::tested_by; |
23 | 22 | ||
24 | use crate::{ | 23 | use crate::{ |
25 | attr::Attrs, db::DefDatabase, path::ModPath, trace::Trace, FileAstId, HirFileId, InFile, | 24 | attr::Attrs, db::DefDatabase, path::ModPath, FileAstId, HirFileId, InFile, LocalImportId, |
26 | LocalImportId, | ||
27 | }; | 25 | }; |
28 | 26 | ||
29 | /// `RawItems` is a set of top-level items in a file (except for impls). | 27 | /// `RawItems` is a set of top-level items in a file (except for impls). |
@@ -41,35 +39,14 @@ pub struct RawItems { | |||
41 | items: Vec<RawItem>, | 39 | items: Vec<RawItem>, |
42 | } | 40 | } |
43 | 41 | ||
44 | #[derive(Debug, Default, PartialEq, Eq)] | ||
45 | pub struct ImportSourceMap { | ||
46 | map: ArenaMap<LocalImportId, ImportSourcePtr>, | ||
47 | } | ||
48 | |||
49 | type ImportSourcePtr = Either<AstPtr<ast::UseTree>, AstPtr<ast::ExternCrateItem>>; | ||
50 | |||
51 | impl ImportSourceMap { | ||
52 | pub fn get(&self, import: LocalImportId) -> ImportSourcePtr { | ||
53 | self.map[import].clone() | ||
54 | } | ||
55 | } | ||
56 | |||
57 | impl RawItems { | 42 | impl RawItems { |
58 | pub(crate) fn raw_items_query( | 43 | pub(crate) fn raw_items_query( |
59 | db: &(impl DefDatabase + AstDatabase), | 44 | db: &(impl DefDatabase + AstDatabase), |
60 | file_id: HirFileId, | 45 | file_id: HirFileId, |
61 | ) -> Arc<RawItems> { | 46 | ) -> Arc<RawItems> { |
62 | db.raw_items_with_source_map(file_id).0 | ||
63 | } | ||
64 | |||
65 | pub(crate) fn raw_items_with_source_map_query( | ||
66 | db: &(impl DefDatabase + AstDatabase), | ||
67 | file_id: HirFileId, | ||
68 | ) -> (Arc<RawItems>, Arc<ImportSourceMap>) { | ||
69 | let mut collector = RawItemsCollector { | 47 | let mut collector = RawItemsCollector { |
70 | raw_items: RawItems::default(), | 48 | raw_items: RawItems::default(), |
71 | source_ast_id_map: db.ast_id_map(file_id), | 49 | source_ast_id_map: db.ast_id_map(file_id), |
72 | imports: Trace::new(), | ||
73 | file_id, | 50 | file_id, |
74 | hygiene: Hygiene::new(db, file_id), | 51 | hygiene: Hygiene::new(db, file_id), |
75 | }; | 52 | }; |
@@ -80,11 +57,8 @@ impl RawItems { | |||
80 | collector.process_module(None, item_list); | 57 | collector.process_module(None, item_list); |
81 | } | 58 | } |
82 | } | 59 | } |
83 | let mut raw_items = collector.raw_items; | 60 | let raw_items = collector.raw_items; |
84 | let (arena, map) = collector.imports.into_arena_and_map(); | 61 | Arc::new(raw_items) |
85 | raw_items.imports = arena; | ||
86 | let source_map = ImportSourceMap { map }; | ||
87 | (Arc::new(raw_items), Arc::new(source_map)) | ||
88 | } | 62 | } |
89 | 63 | ||
90 | pub(super) fn items(&self) -> &[RawItem] { | 64 | pub(super) fn items(&self) -> &[RawItem] { |
@@ -223,7 +197,6 @@ pub(super) struct ImplData { | |||
223 | 197 | ||
224 | struct RawItemsCollector { | 198 | struct RawItemsCollector { |
225 | raw_items: RawItems, | 199 | raw_items: RawItems, |
226 | imports: Trace<LocalImportId, ImportData, ImportSourcePtr>, | ||
227 | source_ast_id_map: Arc<AstIdMap>, | 200 | source_ast_id_map: Arc<AstIdMap>, |
228 | file_id: HirFileId, | 201 | file_id: HirFileId, |
229 | hygiene: Hygiene, | 202 | hygiene: Hygiene, |
@@ -330,7 +303,7 @@ impl RawItemsCollector { | |||
330 | ModPath::expand_use_item( | 303 | ModPath::expand_use_item( |
331 | InFile { value: use_item, file_id: self.file_id }, | 304 | InFile { value: use_item, file_id: self.file_id }, |
332 | &self.hygiene, | 305 | &self.hygiene, |
333 | |path, use_tree, is_glob, alias| { | 306 | |path, _use_tree, is_glob, alias| { |
334 | let import_data = ImportData { | 307 | let import_data = ImportData { |
335 | path, | 308 | path, |
336 | alias, | 309 | alias, |
@@ -339,11 +312,11 @@ impl RawItemsCollector { | |||
339 | is_extern_crate: false, | 312 | is_extern_crate: false, |
340 | is_macro_use: false, | 313 | is_macro_use: false, |
341 | }; | 314 | }; |
342 | buf.push((import_data, Either::Left(AstPtr::new(use_tree)))); | 315 | buf.push(import_data); |
343 | }, | 316 | }, |
344 | ); | 317 | ); |
345 | for (import_data, ptr) in buf { | 318 | for import_data in buf { |
346 | self.push_import(current_module, attrs.clone(), import_data, ptr); | 319 | self.push_import(current_module, attrs.clone(), import_data); |
347 | } | 320 | } |
348 | } | 321 | } |
349 | 322 | ||
@@ -366,12 +339,7 @@ impl RawItemsCollector { | |||
366 | is_extern_crate: true, | 339 | is_extern_crate: true, |
367 | is_macro_use, | 340 | is_macro_use, |
368 | }; | 341 | }; |
369 | self.push_import( | 342 | self.push_import(current_module, attrs, import_data); |
370 | current_module, | ||
371 | attrs, | ||
372 | import_data, | ||
373 | Either::Right(AstPtr::new(&extern_crate)), | ||
374 | ); | ||
375 | } | 343 | } |
376 | } | 344 | } |
377 | 345 | ||
@@ -402,14 +370,8 @@ impl RawItemsCollector { | |||
402 | self.push_item(current_module, attrs, RawItemKind::Impl(imp)) | 370 | self.push_item(current_module, attrs, RawItemKind::Impl(imp)) |
403 | } | 371 | } |
404 | 372 | ||
405 | fn push_import( | 373 | fn push_import(&mut self, current_module: Option<Module>, attrs: Attrs, data: ImportData) { |
406 | &mut self, | 374 | let import = self.raw_items.imports.alloc(data); |
407 | current_module: Option<Module>, | ||
408 | attrs: Attrs, | ||
409 | data: ImportData, | ||
410 | source: ImportSourcePtr, | ||
411 | ) { | ||
412 | let import = self.imports.alloc(|| source, || data); | ||
413 | self.push_item(current_module, attrs, RawItemKind::Import(import)) | 375 | self.push_item(current_module, attrs, RawItemKind::Import(import)) |
414 | } | 376 | } |
415 | 377 | ||