mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 22:24:14 +00:00
Generate ModItem via macro
This commit is contained in:
parent
20ff1cdcfb
commit
b5fd02d93c
1 changed files with 61 additions and 106 deletions
|
@ -223,71 +223,75 @@ impl<N: ItemTreeNode> fmt::Debug for FileItemTreeId<N> {
|
||||||
|
|
||||||
pub type ItemTreeId<N> = InFile<FileItemTreeId<N>>;
|
pub type ItemTreeId<N> = InFile<FileItemTreeId<N>>;
|
||||||
|
|
||||||
macro_rules! nodes {
|
macro_rules! mod_items {
|
||||||
( $($node:ident in $fld:ident),+ $(,)? ) => { $(
|
( $( $typ:ident in $fld:ident -> $ast:ty ),+ $(,)? ) => {
|
||||||
impl ItemTreeNode for $node {
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
||||||
fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self {
|
pub enum ModItem {
|
||||||
&tree.$fld[index]
|
$(
|
||||||
|
$typ(FileItemTreeId<$typ>),
|
||||||
|
)+
|
||||||
|
}
|
||||||
|
|
||||||
|
$(
|
||||||
|
impl From<FileItemTreeId<$typ>> for ModItem {
|
||||||
|
fn from(id: FileItemTreeId<$typ>) -> ModItem {
|
||||||
|
ModItem::$typ(id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
)+
|
||||||
|
|
||||||
|
$(
|
||||||
|
impl ItemTreeNode for $typ {
|
||||||
|
fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self {
|
||||||
|
&tree.$fld[index]
|
||||||
|
}
|
||||||
|
|
||||||
fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>> {
|
fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>> {
|
||||||
if let ModItem::$node(id) = mod_item {
|
if let ModItem::$typ(id) = mod_item {
|
||||||
Some(id)
|
Some(id)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem {
|
||||||
|
ModItem::$typ(id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem {
|
impl ItemTreeSource for $typ {
|
||||||
ModItem::$node(id)
|
type Source = $ast;
|
||||||
|
|
||||||
|
fn ast_id(&self) -> FileAstId<Self::Source> {
|
||||||
|
self.ast_id
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
)+ };
|
impl Index<Idx<$typ>> for ItemTree {
|
||||||
|
type Output = $typ;
|
||||||
|
|
||||||
|
fn index(&self, index: Idx<$typ>) -> &Self::Output {
|
||||||
|
&self.$fld[index]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)+
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
nodes!(
|
mod_items! {
|
||||||
Import in imports,
|
Import in imports -> ast::UseItem,
|
||||||
ExternCrate in extern_crates,
|
ExternCrate in extern_crates -> ast::ExternCrateItem,
|
||||||
Function in functions,
|
Function in functions -> ast::FnDef,
|
||||||
Struct in structs,
|
Struct in structs -> ast::StructDef,
|
||||||
Union in unions,
|
Union in unions -> ast::UnionDef,
|
||||||
Enum in enums,
|
Enum in enums -> ast::EnumDef,
|
||||||
Const in consts,
|
Const in consts -> ast::ConstDef,
|
||||||
Static in statics,
|
Static in statics -> ast::StaticDef,
|
||||||
Trait in traits,
|
Trait in traits -> ast::TraitDef,
|
||||||
Impl in impls,
|
Impl in impls -> ast::ImplDef,
|
||||||
TypeAlias in type_aliases,
|
TypeAlias in type_aliases -> ast::TypeAliasDef,
|
||||||
Mod in mods,
|
Mod in mods -> ast::Module,
|
||||||
MacroCall in macro_calls,
|
MacroCall in macro_calls -> ast::MacroCall,
|
||||||
);
|
|
||||||
|
|
||||||
macro_rules! source {
|
|
||||||
( $($node:ident -> $ast:path),+ $(,)? ) => { $(
|
|
||||||
impl ItemTreeSource for $node {
|
|
||||||
type Source = $ast;
|
|
||||||
|
|
||||||
fn ast_id(&self) -> FileAstId<Self::Source> {
|
|
||||||
self.ast_id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)+ };
|
|
||||||
}
|
|
||||||
|
|
||||||
source! {
|
|
||||||
Import -> ast::UseItem,
|
|
||||||
ExternCrate -> ast::ExternCrateItem,
|
|
||||||
Function -> ast::FnDef,
|
|
||||||
Struct -> ast::StructDef,
|
|
||||||
Union -> ast::UnionDef,
|
|
||||||
Enum -> ast::EnumDef,
|
|
||||||
Const -> ast::ConstDef,
|
|
||||||
Static -> ast::StaticDef,
|
|
||||||
Trait -> ast::TraitDef,
|
|
||||||
Impl -> ast::ImplDef,
|
|
||||||
TypeAlias -> ast::TypeAliasDef,
|
|
||||||
Mod -> ast::Module,
|
|
||||||
MacroCall -> ast::MacroCall,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! impl_index {
|
macro_rules! impl_index {
|
||||||
|
@ -304,23 +308,7 @@ macro_rules! impl_index {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_index!(
|
impl_index!(fields: Field, variants: Variant, exprs: Expr);
|
||||||
imports: Import,
|
|
||||||
functions: Function,
|
|
||||||
structs: Struct,
|
|
||||||
fields: Field,
|
|
||||||
unions: Union,
|
|
||||||
enums: Enum,
|
|
||||||
variants: Variant,
|
|
||||||
consts: Const,
|
|
||||||
statics: Static,
|
|
||||||
traits: Trait,
|
|
||||||
impls: Impl,
|
|
||||||
type_aliases: TypeAlias,
|
|
||||||
mods: Mod,
|
|
||||||
macro_calls: MacroCall,
|
|
||||||
exprs: Expr,
|
|
||||||
);
|
|
||||||
|
|
||||||
impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree {
|
impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree {
|
||||||
type Output = N;
|
type Output = N;
|
||||||
|
@ -500,23 +488,6 @@ macro_rules! impl_froms {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
|
||||||
pub enum ModItem {
|
|
||||||
Import(FileItemTreeId<Import>),
|
|
||||||
ExternCrate(FileItemTreeId<ExternCrate>),
|
|
||||||
Function(FileItemTreeId<Function>),
|
|
||||||
Struct(FileItemTreeId<Struct>),
|
|
||||||
Union(FileItemTreeId<Union>),
|
|
||||||
Enum(FileItemTreeId<Enum>),
|
|
||||||
Const(FileItemTreeId<Const>),
|
|
||||||
Static(FileItemTreeId<Static>),
|
|
||||||
Trait(FileItemTreeId<Trait>),
|
|
||||||
Impl(FileItemTreeId<Impl>),
|
|
||||||
TypeAlias(FileItemTreeId<TypeAlias>),
|
|
||||||
Mod(FileItemTreeId<Mod>),
|
|
||||||
MacroCall(FileItemTreeId<MacroCall>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ModItem {
|
impl ModItem {
|
||||||
pub fn as_assoc_item(&self) -> Option<AssocItem> {
|
pub fn as_assoc_item(&self) -> Option<AssocItem> {
|
||||||
match self {
|
match self {
|
||||||
|
@ -541,22 +512,6 @@ impl ModItem {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_froms!(ModItem {
|
|
||||||
Import(FileItemTreeId<Import>),
|
|
||||||
ExternCrate(FileItemTreeId<ExternCrate>),
|
|
||||||
Function(FileItemTreeId<Function>),
|
|
||||||
Struct(FileItemTreeId<Struct>),
|
|
||||||
Union(FileItemTreeId<Union>),
|
|
||||||
Enum(FileItemTreeId<Enum>),
|
|
||||||
Const(FileItemTreeId<Const>),
|
|
||||||
Static(FileItemTreeId<Static>),
|
|
||||||
Trait(FileItemTreeId<Trait>),
|
|
||||||
Impl(FileItemTreeId<Impl>),
|
|
||||||
TypeAlias(FileItemTreeId<TypeAlias>),
|
|
||||||
Mod(FileItemTreeId<Mod>),
|
|
||||||
MacroCall(FileItemTreeId<MacroCall>),
|
|
||||||
});
|
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
pub enum AssocItem {
|
pub enum AssocItem {
|
||||||
Function(FileItemTreeId<Function>),
|
Function(FileItemTreeId<Function>),
|
||||||
|
|
Loading…
Reference in a new issue