Support resolution of #[macro_use] extern crate

This commit is contained in:
uHOOCCOOHu 2019-08-30 01:56:00 +08:00
parent f2a200c1ee
commit f5bea9051b
No known key found for this signature in database
GPG Key ID: CED392DE0C483D00
4 changed files with 30 additions and 2 deletions

View File

@ -295,6 +295,23 @@ where
} }
} }
// `#[macro_use] extern crate` glob import macros
if import.is_extern_crate && import.is_macro_use {
if let Some(ModuleDef::Module(m)) =
def.a().and_then(|item| item.take_types())
{
let item_map = self.db.crate_def_map(m.krate);
let scope = &item_map[m.module_id].scope;
let macros = scope
.macros
.iter()
.map(|(name, res)| (name.clone(), Either::B(*res)))
.collect::<Vec<_>>();
self.update(module_id, Some(import_id), &macros);
}
}
let resolution = match def { let resolution = match def {
Either::A(item) => { Either::A(item) => {
Either::A(Resolution { def: item, import: Some(import_id) }) Either::A(Resolution { def: item, import: Some(import_id) })

View File

@ -154,6 +154,7 @@ pub struct ImportData {
pub(super) is_glob: bool, pub(super) is_glob: bool,
pub(super) is_prelude: bool, pub(super) is_prelude: bool,
pub(super) is_extern_crate: bool, pub(super) is_extern_crate: bool,
pub(super) is_macro_use: bool,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@ -293,8 +294,14 @@ impl RawItemsCollector {
let is_prelude = use_item.has_atom_attr("prelude_import"); let is_prelude = use_item.has_atom_attr("prelude_import");
Path::expand_use_item(&use_item, |path, use_tree, is_glob, alias| { Path::expand_use_item(&use_item, |path, use_tree, is_glob, alias| {
let import_data = let import_data = ImportData {
ImportData { path, alias, is_glob, is_prelude, is_extern_crate: false }; path,
alias,
is_glob,
is_prelude,
is_extern_crate: false,
is_macro_use: false,
};
self.push_import(current_module, import_data, Either::A(AstPtr::new(use_tree))); self.push_import(current_module, import_data, Either::A(AstPtr::new(use_tree)));
}) })
} }
@ -307,12 +314,14 @@ impl RawItemsCollector {
if let Some(name_ref) = extern_crate.name_ref() { if let Some(name_ref) = extern_crate.name_ref() {
let path = Path::from_name_ref(&name_ref); let path = Path::from_name_ref(&name_ref);
let alias = extern_crate.alias().and_then(|a| a.name()).map(|it| it.as_name()); let alias = extern_crate.alias().and_then(|a| a.name()).map(|it| it.as_name());
let is_macro_use = extern_crate.has_atom_attr("macro_use");
let import_data = ImportData { let import_data = ImportData {
path, path,
alias, alias,
is_glob: false, is_glob: false,
is_prelude: false, is_prelude: false,
is_extern_crate: true, is_extern_crate: true,
is_macro_use,
}; };
self.push_import(current_module, import_data, Either::B(AstPtr::new(&extern_crate))); self.push_import(current_module, import_data, Either::B(AstPtr::new(&extern_crate)));
} }

View File

@ -934,6 +934,7 @@ impl AstNode for ExternCrateItem {
&self.syntax &self.syntax
} }
} }
impl ast::AttrsOwner for ExternCrateItem {}
impl ExternCrateItem { impl ExternCrateItem {
pub fn name_ref(&self) -> Option<NameRef> { pub fn name_ref(&self) -> Option<NameRef> {
AstChildren::new(&self.syntax).next() AstChildren::new(&self.syntax).next()

View File

@ -669,6 +669,7 @@ Grammar(
collections: [("use_trees", "UseTree")] collections: [("use_trees", "UseTree")]
), ),
"ExternCrateItem": ( "ExternCrateItem": (
traits: ["AttrsOwner"],
options: ["NameRef", "Alias"], options: ["NameRef", "Alias"],
), ),
"ArgList": ( "ArgList": (