From 0bc7d285189caaffc13e4d6856baf895f72ed80c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 30 Oct 2019 18:41:50 +0300 Subject: refactor $crate handling Introduce proper hygiene module, which should grow quite a bit eventually. --- crates/ra_hir_def/src/nameres/raw.rs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 86b4fef96..636364628 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -13,6 +13,7 @@ use crate::{ attr::Attr, db::DefDatabase2, either::Either, + hygiene::Hygiene, name::{AsName, Name}, path::Path, FileAstId, HirFileId, ModuleSource, Source, @@ -78,7 +79,7 @@ impl RawItems { source_ast_id_map: db.ast_id_map(file_id), source_map: ImportSourceMap::default(), file_id, - db, + hygiene: Hygiene::new(db, file_id), }; if let Some(node) = db.parse_or_expand(file_id) { if let Some(source_file) = ast::SourceFile::cast(node.clone()) { @@ -204,15 +205,15 @@ pub struct MacroData { pub export: bool, } -struct RawItemsCollector { +struct RawItemsCollector { raw_items: RawItems, source_ast_id_map: Arc, source_map: ImportSourceMap, file_id: HirFileId, - db: DB, + hygiene: Hygiene, } -impl RawItemsCollector<&DB> { +impl RawItemsCollector { fn process_module(&mut self, current_module: Option, body: impl ast::ModuleItemOwner) { for item_or_macro in body.items_with_macros() { match item_or_macro { @@ -309,9 +310,10 @@ impl RawItemsCollector<&DB> { let is_prelude = use_item.has_atom_attr("prelude_import"); let attrs = self.parse_attrs(&use_item); + let mut buf = Vec::new(); Path::expand_use_item( Source { ast: use_item, file_id: self.file_id }, - self.db, + &self.hygiene, |path, use_tree, is_glob, alias| { let import_data = ImportData { path, @@ -321,14 +323,12 @@ impl RawItemsCollector<&DB> { is_extern_crate: false, is_macro_use: false, }; - self.push_import( - current_module, - attrs.clone(), - import_data, - Either::A(AstPtr::new(use_tree)), - ); + buf.push((import_data, Either::A(AstPtr::new(use_tree)))); }, - ) + ); + for (import_data, ptr) in buf { + self.push_import(current_module, attrs.clone(), import_data, ptr); + } } fn add_extern_crate_item( @@ -361,10 +361,7 @@ impl RawItemsCollector<&DB> { fn add_macro(&mut self, current_module: Option, m: ast::MacroCall) { let attrs = self.parse_attrs(&m); - let path = match m - .path() - .and_then(|path| Path::from_src(Source { ast: path, file_id: self.file_id }, self.db)) - { + let path = match m.path().and_then(|path| Path::from_src(path, &self.hygiene)) { Some(it) => it, _ => return, }; @@ -402,6 +399,6 @@ impl RawItemsCollector<&DB> { } fn parse_attrs(&self, item: &impl ast::AttrsOwner) -> Attrs { - Attr::from_attrs_owner(self.file_id, item, self.db) + Attr::from_attrs_owner(item, &self.hygiene) } } -- cgit v1.2.3