aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2020-06-23 17:41:32 +0100
committerJonas Schievink <[email protected]>2020-06-24 15:53:56 +0100
commitb5fd02d93cdfafeba23f50ca9c414053aaa548ae (patch)
tree651805b64f792322be1daafa84cf25f471683135 /crates/ra_hir_def
parent20ff1cdcfbef92429962569f7e98a169c6a10e50 (diff)
Generate ModItem via macro
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r--crates/ra_hir_def/src/item_tree.rs165
1 files changed, 60 insertions, 105 deletions
diff --git a/crates/ra_hir_def/src/item_tree.rs b/crates/ra_hir_def/src/item_tree.rs
index 6eb0c1b91..5beb11df7 100644
--- a/crates/ra_hir_def/src/item_tree.rs
+++ b/crates/ra_hir_def/src/item_tree.rs
@@ -223,71 +223,75 @@ impl<N: ItemTreeNode> fmt::Debug for FileItemTreeId<N> {
223 223
224pub type ItemTreeId<N> = InFile<FileItemTreeId<N>>; 224pub type ItemTreeId<N> = InFile<FileItemTreeId<N>>;
225 225
226macro_rules! nodes { 226macro_rules! mod_items {
227 ( $($node:ident in $fld:ident),+ $(,)? ) => { $( 227 ( $( $typ:ident in $fld:ident -> $ast:ty ),+ $(,)? ) => {
228 impl ItemTreeNode for $node { 228 #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
229 fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self { 229 pub enum ModItem {
230 &tree.$fld[index] 230 $(
231 $typ(FileItemTreeId<$typ>),
232 )+
233 }
234
235 $(
236 impl From<FileItemTreeId<$typ>> for ModItem {
237 fn from(id: FileItemTreeId<$typ>) -> ModItem {
238 ModItem::$typ(id)
239 }
231 } 240 }
241 )+
242
243 $(
244 impl ItemTreeNode for $typ {
245 fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self {
246 &tree.$fld[index]
247 }
232 248
249 fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>> {
250 if let ModItem::$typ(id) = mod_item {
251 Some(id)
252 } else {
253 None
254 }
255 }
233 256
234 fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>> { 257 fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem {
235 if let ModItem::$node(id) = mod_item { 258 ModItem::$typ(id)
236 Some(id)
237 } else {
238 None
239 } 259 }
240 } 260 }
241 261
242 fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem { 262 impl ItemTreeSource for $typ {
243 ModItem::$node(id) 263 type Source = $ast;
264
265 fn ast_id(&self) -> FileAstId<Self::Source> {
266 self.ast_id
267 }
244 } 268 }
245 } 269
246 )+ }; 270 impl Index<Idx<$typ>> for ItemTree {
247} 271 type Output = $typ;
248 272
249nodes!( 273 fn index(&self, index: Idx<$typ>) -> &Self::Output {
250 Import in imports, 274 &self.$fld[index]
251 ExternCrate in extern_crates, 275 }
252 Function in functions,
253 Struct in structs,
254 Union in unions,
255 Enum in enums,
256 Const in consts,
257 Static in statics,
258 Trait in traits,
259 Impl in impls,
260 TypeAlias in type_aliases,
261 Mod in mods,
262 MacroCall in macro_calls,
263);
264
265macro_rules! source {
266 ( $($node:ident -> $ast:path),+ $(,)? ) => { $(
267 impl ItemTreeSource for $node {
268 type Source = $ast;
269
270 fn ast_id(&self) -> FileAstId<Self::Source> {
271 self.ast_id
272 } 276 }
273 } 277 )+
274 )+ }; 278 };
275} 279}
276 280
277source! { 281mod_items! {
278 Import -> ast::UseItem, 282 Import in imports -> ast::UseItem,
279 ExternCrate -> ast::ExternCrateItem, 283 ExternCrate in extern_crates -> ast::ExternCrateItem,
280 Function -> ast::FnDef, 284 Function in functions -> ast::FnDef,
281 Struct -> ast::StructDef, 285 Struct in structs -> ast::StructDef,
282 Union -> ast::UnionDef, 286 Union in unions -> ast::UnionDef,
283 Enum -> ast::EnumDef, 287 Enum in enums -> ast::EnumDef,
284 Const -> ast::ConstDef, 288 Const in consts -> ast::ConstDef,
285 Static -> ast::StaticDef, 289 Static in statics -> ast::StaticDef,
286 Trait -> ast::TraitDef, 290 Trait in traits -> ast::TraitDef,
287 Impl -> ast::ImplDef, 291 Impl in impls -> ast::ImplDef,
288 TypeAlias -> ast::TypeAliasDef, 292 TypeAlias in type_aliases -> ast::TypeAliasDef,
289 Mod -> ast::Module, 293 Mod in mods -> ast::Module,
290 MacroCall -> ast::MacroCall, 294 MacroCall in macro_calls -> ast::MacroCall,
291} 295}
292 296
293macro_rules! impl_index { 297macro_rules! impl_index {
@@ -304,23 +308,7 @@ macro_rules! impl_index {
304 }; 308 };
305} 309}
306 310
307impl_index!( 311impl_index!(fields: Field, variants: Variant, exprs: Expr);
308 imports: Import,
309 functions: Function,
310 structs: Struct,
311 fields: Field,
312 unions: Union,
313 enums: Enum,
314 variants: Variant,
315 consts: Const,
316 statics: Static,
317 traits: Trait,
318 impls: Impl,
319 type_aliases: TypeAlias,
320 mods: Mod,
321 macro_calls: MacroCall,
322 exprs: Expr,
323);
324 312
325impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree { 313impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree {
326 type Output = N; 314 type Output = N;
@@ -500,23 +488,6 @@ macro_rules! impl_froms {
500 } 488 }
501} 489}
502 490
503#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
504pub enum ModItem {
505 Import(FileItemTreeId<Import>),
506 ExternCrate(FileItemTreeId<ExternCrate>),
507 Function(FileItemTreeId<Function>),
508 Struct(FileItemTreeId<Struct>),
509 Union(FileItemTreeId<Union>),
510 Enum(FileItemTreeId<Enum>),
511 Const(FileItemTreeId<Const>),
512 Static(FileItemTreeId<Static>),
513 Trait(FileItemTreeId<Trait>),
514 Impl(FileItemTreeId<Impl>),
515 TypeAlias(FileItemTreeId<TypeAlias>),
516 Mod(FileItemTreeId<Mod>),
517 MacroCall(FileItemTreeId<MacroCall>),
518}
519
520impl ModItem { 491impl ModItem {
521 pub fn as_assoc_item(&self) -> Option<AssocItem> { 492 pub fn as_assoc_item(&self) -> Option<AssocItem> {
522 match self { 493 match self {
@@ -541,22 +512,6 @@ impl ModItem {
541 } 512 }
542} 513}
543 514
544impl_froms!(ModItem {
545 Import(FileItemTreeId<Import>),
546 ExternCrate(FileItemTreeId<ExternCrate>),
547 Function(FileItemTreeId<Function>),
548 Struct(FileItemTreeId<Struct>),
549 Union(FileItemTreeId<Union>),
550 Enum(FileItemTreeId<Enum>),
551 Const(FileItemTreeId<Const>),
552 Static(FileItemTreeId<Static>),
553 Trait(FileItemTreeId<Trait>),
554 Impl(FileItemTreeId<Impl>),
555 TypeAlias(FileItemTreeId<TypeAlias>),
556 Mod(FileItemTreeId<Mod>),
557 MacroCall(FileItemTreeId<MacroCall>),
558});
559
560#[derive(Debug, Copy, Clone, Eq, PartialEq)] 515#[derive(Debug, Copy, Clone, Eq, PartialEq)]
561pub enum AssocItem { 516pub enum AssocItem {
562 Function(FileItemTreeId<Function>), 517 Function(FileItemTreeId<Function>),