mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Get rid of ItemOrMacro
This commit is contained in:
parent
539e597743
commit
db34abeb85
7 changed files with 21 additions and 41 deletions
|
@ -130,8 +130,8 @@ mod tests {
|
||||||
fn add_explicit_type_works_for_macro_call() {
|
fn add_explicit_type_works_for_macro_call() {
|
||||||
check_assist(
|
check_assist(
|
||||||
add_explicit_type,
|
add_explicit_type,
|
||||||
"macro_rules! v { () => {0u64} } fn f() { let a<|> = v!(); }",
|
r"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<|>: u64 = v!(); }",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -563,7 +563,8 @@ impl ExprCollector<'_> {
|
||||||
ast::ModuleItem::ImplDef(_)
|
ast::ModuleItem::ImplDef(_)
|
||||||
| ast::ModuleItem::UseItem(_)
|
| ast::ModuleItem::UseItem(_)
|
||||||
| ast::ModuleItem::ExternCrateItem(_)
|
| ast::ModuleItem::ExternCrateItem(_)
|
||||||
| ast::ModuleItem::Module(_) => continue,
|
| ast::ModuleItem::Module(_)
|
||||||
|
| ast::ModuleItem::MacroCall(_) => continue,
|
||||||
};
|
};
|
||||||
self.body.item_scope.define_def(def);
|
self.body.item_scope.define_def(def);
|
||||||
if let Some(name) = name {
|
if let Some(name) = name {
|
||||||
|
|
|
@ -209,11 +209,8 @@ impl RawItemsCollector {
|
||||||
current_module: Option<Idx<ModuleData>>,
|
current_module: Option<Idx<ModuleData>>,
|
||||||
body: impl ast::ModuleItemOwner,
|
body: impl ast::ModuleItemOwner,
|
||||||
) {
|
) {
|
||||||
for item_or_macro in body.items_with_macros() {
|
for item in body.items() {
|
||||||
match item_or_macro {
|
self.add_item(current_module, item)
|
||||||
ast::ItemOrMacro::Macro(m) => self.add_macro(current_module, m),
|
|
||||||
ast::ItemOrMacro::Item(item) => self.add_item(current_module, item),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -265,6 +262,10 @@ impl RawItemsCollector {
|
||||||
ast::ModuleItem::StaticDef(it) => {
|
ast::ModuleItem::StaticDef(it) => {
|
||||||
(DefKind::Static(self.source_ast_id_map.ast_id(&it)), it.name())
|
(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 {
|
if let Some(name) = name {
|
||||||
let name = name.as_name();
|
let name = name.as_name();
|
||||||
|
|
|
@ -68,8 +68,6 @@ impl AstIdMap {
|
||||||
bfs(node, |it| {
|
bfs(node, |it| {
|
||||||
if let Some(module_item) = ast::ModuleItem::cast(it.clone()) {
|
if let Some(module_item) = ast::ModuleItem::cast(it.clone()) {
|
||||||
res.alloc(module_item.syntax());
|
res.alloc(module_item.syntax());
|
||||||
} else if let Some(macro_call) = ast::MacroCall::cast(it) {
|
|
||||||
res.alloc(macro_call.syntax());
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
res
|
res
|
||||||
|
|
|
@ -4135,6 +4135,7 @@ pub enum ModuleItem {
|
||||||
ConstDef(ConstDef),
|
ConstDef(ConstDef),
|
||||||
StaticDef(StaticDef),
|
StaticDef(StaticDef),
|
||||||
Module(Module),
|
Module(Module),
|
||||||
|
MacroCall(MacroCall),
|
||||||
}
|
}
|
||||||
impl From<StructDef> for ModuleItem {
|
impl From<StructDef> for ModuleItem {
|
||||||
fn from(node: StructDef) -> ModuleItem {
|
fn from(node: StructDef) -> ModuleItem {
|
||||||
|
@ -4196,6 +4197,11 @@ impl From<Module> for ModuleItem {
|
||||||
ModuleItem::Module(node)
|
ModuleItem::Module(node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl From<MacroCall> for ModuleItem {
|
||||||
|
fn from(node: MacroCall) -> ModuleItem {
|
||||||
|
ModuleItem::MacroCall(node)
|
||||||
|
}
|
||||||
|
}
|
||||||
impl std::fmt::Display for ModuleItem {
|
impl std::fmt::Display for ModuleItem {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
std::fmt::Display::fmt(self.syntax(), f)
|
std::fmt::Display::fmt(self.syntax(), f)
|
||||||
|
@ -4205,7 +4211,7 @@ impl AstNode for ModuleItem {
|
||||||
fn can_cast(kind: SyntaxKind) -> bool {
|
fn can_cast(kind: SyntaxKind) -> bool {
|
||||||
match kind {
|
match kind {
|
||||||
STRUCT_DEF | UNION_DEF | ENUM_DEF | FN_DEF | TRAIT_DEF | TYPE_ALIAS_DEF | IMPL_DEF
|
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,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4223,6 +4229,7 @@ impl AstNode for ModuleItem {
|
||||||
CONST_DEF => ModuleItem::ConstDef(ConstDef { syntax }),
|
CONST_DEF => ModuleItem::ConstDef(ConstDef { syntax }),
|
||||||
STATIC_DEF => ModuleItem::StaticDef(StaticDef { syntax }),
|
STATIC_DEF => ModuleItem::StaticDef(StaticDef { syntax }),
|
||||||
MODULE => ModuleItem::Module(Module { syntax }),
|
MODULE => ModuleItem::Module(Module { syntax }),
|
||||||
|
MACRO_CALL => ModuleItem::MacroCall(MacroCall { syntax }),
|
||||||
_ => return None,
|
_ => return None,
|
||||||
};
|
};
|
||||||
Some(res)
|
Some(res)
|
||||||
|
@ -4241,6 +4248,7 @@ impl AstNode for ModuleItem {
|
||||||
ModuleItem::ConstDef(it) => &it.syntax,
|
ModuleItem::ConstDef(it) => &it.syntax,
|
||||||
ModuleItem::StaticDef(it) => &it.syntax,
|
ModuleItem::StaticDef(it) => &it.syntax,
|
||||||
ModuleItem::Module(it) => &it.syntax,
|
ModuleItem::Module(it) => &it.syntax,
|
||||||
|
ModuleItem::MacroCall(it) => &it.syntax,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,7 @@ use itertools::Itertools;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{self, child_opt, children, AstChildren, AstNode, AstToken},
|
ast::{self, child_opt, children, AstChildren, AstNode, AstToken},
|
||||||
match_ast,
|
syntax_node::SyntaxElementChildren,
|
||||||
syntax_node::{SyntaxElementChildren, SyntaxNodeChildren},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait TypeAscriptionOwner: AstNode {
|
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 {
|
pub trait ModuleItemOwner: AstNode {
|
||||||
fn items(&self) -> AstChildren<ast::ModuleItem> {
|
fn items(&self) -> AstChildren<ast::ModuleItem> {
|
||||||
children(self)
|
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 {
|
pub trait TypeParamsOwner: AstNode {
|
||||||
|
|
|
@ -566,6 +566,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
|
||||||
ConstDef,
|
ConstDef,
|
||||||
StaticDef,
|
StaticDef,
|
||||||
Module,
|
Module,
|
||||||
|
MacroCall,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum ImplItem: AttrsOwner {
|
enum ImplItem: AttrsOwner {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue