don't special case path attr

This commit is contained in:
Aleksey Kladov 2019-10-10 17:42:29 +03:00
parent b36b8970cc
commit 89826a50fc
3 changed files with 31 additions and 34 deletions

View File

@ -64,13 +64,22 @@ pub(crate) fn is_simple_atom(&self, name: &str) -> bool {
}
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,
}
}

View File

@ -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 @@ fn collect(&mut self, items: &[raw::RawItem]) {
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 @@ fn collect(&mut self, items: &[raw::RawItem]) {
}
}
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 @@ fn collect_module(&mut self, module: &raw::ModuleData) {
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 @@ fn collect_module(&mut self, module: &raw::ModuleData) {
}
}
// 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 @@ fn import_all_legacy_macros(&mut self, module_id: CrateModuleId) {
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 {

View File

@ -5,7 +5,7 @@
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<ast::Module>,
attr_path: Option<SmolStr>,
is_macro_use: bool,
},
Definition {
name: Name,
ast_id: FileAstId<ast::Module>,
items: Vec<RawItem>,
attr_path: Option<SmolStr>,
is_macro_use: bool,
},
}
@ -295,11 +293,9 @@ fn add_module(&mut self, current_module: Option<Module>, module: ast::Module) {
// 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 @@ fn add_module(&mut self, current_module: Option<Module>, module: ast::Module) {
}
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 @@ fn parse_attrs(&self, item: &impl ast::AttrsOwner) -> Attrs {
Attr::from_attrs_owner(self.file_id, item, self.db)
}
}
fn extract_mod_path_attribute(module: &ast::Module) -> Option<SmolStr> {
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
}
})
})
}