From 89826a50fc59f271b8b52ae675e145029727aa35 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 10 Oct 2019 17:42:29 +0300 Subject: don't special case path attr --- crates/ra_hir/src/attr.rs | 23 ++++++++++++++++------- crates/ra_hir/src/nameres/collector.rs | 21 ++++++++++++++------- crates/ra_hir/src/nameres/raw.rs | 21 +-------------------- 3 files changed, 31 insertions(+), 34 deletions(-) diff --git a/crates/ra_hir/src/attr.rs b/crates/ra_hir/src/attr.rs index f67e80bfd..a0a74ab8c 100644 --- a/crates/ra_hir/src/attr.rs +++ b/crates/ra_hir/src/attr.rs @@ -64,13 +64,22 @@ impl Attr { } pub(crate) fn as_cfg(&self) -> Option<&Subtree> { - if self.is_simple_atom("cfg") { - match &self.input { - Some(AttrInput::TokenTree(subtree)) => Some(subtree), - _ => None, - } - } else { - None + if !self.is_simple_atom("cfg") { + return None; + } + match &self.input { + Some(AttrInput::TokenTree(subtree)) => Some(subtree), + _ => None, + } + } + + pub(crate) fn as_path(&self) -> Option<&SmolStr> { + if !self.is_simple_atom("path") { + return None; + } + match &self.input { + Some(AttrInput::Literal(it)) => Some(it), + _ => None, } } diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index 5dacdb0d9..54514becc 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -2,7 +2,7 @@ use ra_cfg::CfgOptions; use ra_db::FileId; -use ra_syntax::ast; +use ra_syntax::{ast, SmolStr}; use rustc_hash::FxHashMap; use test_utils::tested_by; @@ -546,7 +546,9 @@ where for item in items { if self.is_cfg_enabled(item.attrs()) { match item.kind { - raw::RawItemKind::Module(m) => self.collect_module(&self.raw_items[m]), + raw::RawItemKind::Module(m) => { + self.collect_module(&self.raw_items[m], item.attrs()) + } raw::RawItemKind::Import(import_id) => self .def_collector .unresolved_imports @@ -558,10 +560,11 @@ where } } - fn collect_module(&mut self, module: &raw::ModuleData) { + fn collect_module(&mut self, module: &raw::ModuleData, attrs: &[Attr]) { + let path_attr = self.path_attr(attrs); match module { // inline module, just recurse - raw::ModuleData::Definition { name, items, ast_id, attr_path, is_macro_use } => { + raw::ModuleData::Definition { name, items, ast_id, is_macro_use } => { let module_id = self.push_child_module(name.clone(), ast_id.with_file_id(self.file_id), None); @@ -570,7 +573,7 @@ where module_id, file_id: self.file_id, raw_items: self.raw_items, - mod_dir: self.mod_dir.descend_into_definition(name, attr_path.as_ref()), + mod_dir: self.mod_dir.descend_into_definition(name, path_attr), } .collect(&*items); if *is_macro_use { @@ -578,13 +581,13 @@ where } } // out of line module, resolve, parse and recurse - raw::ModuleData::Declaration { name, ast_id, attr_path, is_macro_use } => { + raw::ModuleData::Declaration { name, ast_id, is_macro_use } => { let ast_id = ast_id.with_file_id(self.file_id); match self.mod_dir.resolve_submodule( self.def_collector.db, self.file_id, name, - attr_path.as_ref(), + path_attr, ) { Ok((file_id, mod_dir)) => { let module_id = self.push_child_module(name.clone(), ast_id, Some(file_id)); @@ -713,6 +716,10 @@ where fn is_cfg_enabled(&self, attrs: &[Attr]) -> bool { attrs.iter().all(|attr| attr.is_cfg_enabled(&self.def_collector.cfg_options) != Some(false)) } + + fn path_attr<'a>(&self, attrs: &'a [Attr]) -> Option<&'a SmolStr> { + attrs.iter().find_map(|attr| attr.as_path()) + } } fn is_macro_rules(path: &Path) -> bool { diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs index 5f93f920f..469fd8ea7 100644 --- a/crates/ra_hir/src/nameres/raw.rs +++ b/crates/ra_hir/src/nameres/raw.rs @@ -5,7 +5,7 @@ use std::{ops::Index, sync::Arc}; use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; use ra_syntax::{ ast::{self, AttrsOwner, NameOwner}, - AstNode, AstPtr, SmolStr, SourceFile, + AstNode, AstPtr, SourceFile, }; use test_utils::tested_by; @@ -152,14 +152,12 @@ pub(super) enum ModuleData { Declaration { name: Name, ast_id: FileAstId, - attr_path: Option, is_macro_use: bool, }, Definition { name: Name, ast_id: FileAstId, items: Vec, - attr_path: Option, is_macro_use: bool, }, } @@ -295,11 +293,9 @@ impl RawItemsCollector<&DB> { // FIXME: cfg_attr let is_macro_use = module.has_atom_attr("macro_use"); if module.has_semi() { - let attr_path = extract_mod_path_attribute(&module); let item = self.raw_items.modules.alloc(ModuleData::Declaration { name, ast_id, - attr_path, is_macro_use, }); self.push_item(current_module, attrs, RawItemKind::Module(item)); @@ -307,12 +303,10 @@ impl RawItemsCollector<&DB> { } if let Some(item_list) = module.item_list() { - let attr_path = extract_mod_path_attribute(&module); let item = self.raw_items.modules.alloc(ModuleData::Definition { name, ast_id, items: Vec::new(), - attr_path, is_macro_use, }); self.process_module(Some(item), item_list); @@ -423,16 +417,3 @@ impl RawItemsCollector<&DB> { Attr::from_attrs_owner(self.file_id, item, self.db) } } - -fn extract_mod_path_attribute(module: &ast::Module) -> Option { - module.attrs().into_iter().find_map(|attr| { - attr.as_simple_key_value().and_then(|(name, value)| { - let is_path = name == "path"; - if is_path { - Some(value) - } else { - None - } - }) - }) -} -- cgit v1.2.3