Get rid of ItemOrMacro

This commit is contained in:
Aleksey Kladov 2020-03-26 16:10:01 +01:00
parent 539e597743
commit db34abeb85
7 changed files with 21 additions and 41 deletions

View File

@ -130,8 +130,8 @@ mod tests {
fn add_explicit_type_works_for_macro_call() {
check_assist(
add_explicit_type,
"macro_rules! v { () => {0u64} } fn f() { let a<|> = v!(); }",
"macro_rules! v { () => {0u64} } fn f() { let a<|>: u64 = v!(); }",
r"macro_rules! v { () => {0u64} } fn f() { let a<|> = v!(); }",
r"macro_rules! v { () => {0u64} } fn f() { let a<|>: u64 = v!(); }",
);
}

View File

@ -563,7 +563,8 @@ impl ExprCollector<'_> {
ast::ModuleItem::ImplDef(_)
| ast::ModuleItem::UseItem(_)
| ast::ModuleItem::ExternCrateItem(_)
| ast::ModuleItem::Module(_) => continue,
| ast::ModuleItem::Module(_)
| ast::ModuleItem::MacroCall(_) => continue,
};
self.body.item_scope.define_def(def);
if let Some(name) = name {

View File

@ -209,11 +209,8 @@ impl RawItemsCollector {
current_module: Option<Idx<ModuleData>>,
body: impl ast::ModuleItemOwner,
) {
for item_or_macro in body.items_with_macros() {
match item_or_macro {
ast::ItemOrMacro::Macro(m) => self.add_macro(current_module, m),
ast::ItemOrMacro::Item(item) => self.add_item(current_module, item),
}
for item in body.items() {
self.add_item(current_module, item)
}
}
@ -265,6 +262,10 @@ impl RawItemsCollector {
ast::ModuleItem::StaticDef(it) => {
(DefKind::Static(self.source_ast_id_map.ast_id(&it)), it.name())
}
ast::ModuleItem::MacroCall(it) => {
self.add_macro(current_module, it);
return;
}
};
if let Some(name) = name {
let name = name.as_name();

View File

@ -68,8 +68,6 @@ impl AstIdMap {
bfs(node, |it| {
if let Some(module_item) = ast::ModuleItem::cast(it.clone()) {
res.alloc(module_item.syntax());
} else if let Some(macro_call) = ast::MacroCall::cast(it) {
res.alloc(macro_call.syntax());
}
});
res

View File

@ -4135,6 +4135,7 @@ pub enum ModuleItem {
ConstDef(ConstDef),
StaticDef(StaticDef),
Module(Module),
MacroCall(MacroCall),
}
impl From<StructDef> for ModuleItem {
fn from(node: StructDef) -> ModuleItem {
@ -4196,6 +4197,11 @@ impl From<Module> for ModuleItem {
ModuleItem::Module(node)
}
}
impl From<MacroCall> for ModuleItem {
fn from(node: MacroCall) -> ModuleItem {
ModuleItem::MacroCall(node)
}
}
impl std::fmt::Display for ModuleItem {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
@ -4205,7 +4211,7 @@ impl AstNode for ModuleItem {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
STRUCT_DEF | UNION_DEF | ENUM_DEF | FN_DEF | TRAIT_DEF | TYPE_ALIAS_DEF | IMPL_DEF
| USE_ITEM | EXTERN_CRATE_ITEM | CONST_DEF | STATIC_DEF | MODULE => true,
| USE_ITEM | EXTERN_CRATE_ITEM | CONST_DEF | STATIC_DEF | MODULE | MACRO_CALL => true,
_ => false,
}
}
@ -4223,6 +4229,7 @@ impl AstNode for ModuleItem {
CONST_DEF => ModuleItem::ConstDef(ConstDef { syntax }),
STATIC_DEF => ModuleItem::StaticDef(StaticDef { syntax }),
MODULE => ModuleItem::Module(Module { syntax }),
MACRO_CALL => ModuleItem::MacroCall(MacroCall { syntax }),
_ => return None,
};
Some(res)
@ -4241,6 +4248,7 @@ impl AstNode for ModuleItem {
ModuleItem::ConstDef(it) => &it.syntax,
ModuleItem::StaticDef(it) => &it.syntax,
ModuleItem::Module(it) => &it.syntax,
ModuleItem::MacroCall(it) => &it.syntax,
}
}
}

View File

@ -6,8 +6,7 @@ use itertools::Itertools;
use crate::{
ast::{self, child_opt, children, AstChildren, AstNode, AstToken},
match_ast,
syntax_node::{SyntaxElementChildren, SyntaxNodeChildren},
syntax_node::SyntaxElementChildren,
};
pub trait TypeAscriptionOwner: AstNode {
@ -46,38 +45,10 @@ pub trait FnDefOwner: AstNode {
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ItemOrMacro {
Item(ast::ModuleItem),
Macro(ast::MacroCall),
}
pub trait ModuleItemOwner: AstNode {
fn items(&self) -> AstChildren<ast::ModuleItem> {
children(self)
}
fn items_with_macros(&self) -> ItemOrMacroIter {
ItemOrMacroIter(self.syntax().children())
}
}
#[derive(Debug)]
pub struct ItemOrMacroIter(SyntaxNodeChildren);
impl Iterator for ItemOrMacroIter {
type Item = ItemOrMacro;
fn next(&mut self) -> Option<ItemOrMacro> {
loop {
let n = self.0.next()?;
match_ast! {
match n {
ast::ModuleItem(it) => { return Some(ItemOrMacro::Item(it)) },
ast::MacroCall(it) => { return Some(ItemOrMacro::Macro(it)) },
_ => {},
}
}
}
}
}
pub trait TypeParamsOwner: AstNode {

View File

@ -566,6 +566,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
ConstDef,
StaticDef,
Module,
MacroCall,
}
enum ImplItem: AttrsOwner {