aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax/src/ast/node_ext.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/syntax/src/ast/node_ext.rs')
-rw-r--r--crates/syntax/src/ast/node_ext.rs54
1 files changed, 54 insertions, 0 deletions
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs
index c59a29eab..40dec3c7f 100644
--- a/crates/syntax/src/ast/node_ext.rs
+++ b/crates/syntax/src/ast/node_ext.rs
@@ -3,6 +3,7 @@
3 3
4use std::fmt; 4use std::fmt;
5 5
6use ast::AttrsOwner;
6use itertools::Itertools; 7use itertools::Itertools;
7use parser::SyntaxKind; 8use parser::SyntaxKind;
8 9
@@ -31,6 +32,57 @@ fn text_of_first_token(node: &SyntaxNode) -> &SmolStr {
31 node.green().children().next().and_then(|it| it.into_token()).unwrap().text() 32 node.green().children().next().and_then(|it| it.into_token()).unwrap().text()
32} 33}
33 34
35pub enum Macro {
36 MacroRules(ast::MacroRules),
37 MacroDef(ast::MacroDef),
38}
39
40impl From<ast::MacroRules> for Macro {
41 fn from(it: ast::MacroRules) -> Self {
42 Macro::MacroRules(it)
43 }
44}
45
46impl From<ast::MacroDef> for Macro {
47 fn from(it: ast::MacroDef) -> Self {
48 Macro::MacroDef(it)
49 }
50}
51
52impl AstNode for Macro {
53 fn can_cast(kind: SyntaxKind) -> bool {
54 match kind {
55 SyntaxKind::MACRO_RULES | SyntaxKind::MACRO_DEF => true,
56 _ => false,
57 }
58 }
59 fn cast(syntax: SyntaxNode) -> Option<Self> {
60 let res = match syntax.kind() {
61 SyntaxKind::MACRO_RULES => Macro::MacroRules(ast::MacroRules { syntax }),
62 SyntaxKind::MACRO_DEF => Macro::MacroDef(ast::MacroDef { syntax }),
63 _ => return None,
64 };
65 Some(res)
66 }
67 fn syntax(&self) -> &SyntaxNode {
68 match self {
69 Macro::MacroRules(it) => it.syntax(),
70 Macro::MacroDef(it) => it.syntax(),
71 }
72 }
73}
74
75impl NameOwner for Macro {
76 fn name(&self) -> Option<ast::Name> {
77 match self {
78 Macro::MacroRules(mac) => mac.name(),
79 Macro::MacroDef(mac) => mac.name(),
80 }
81 }
82}
83
84impl AttrsOwner for Macro {}
85
34#[derive(Debug, Clone, PartialEq, Eq)] 86#[derive(Debug, Clone, PartialEq, Eq)]
35pub enum AttrKind { 87pub enum AttrKind {
36 Inner, 88 Inner,
@@ -462,4 +514,6 @@ impl ast::DocCommentsOwner for ast::Const {}
462impl ast::DocCommentsOwner for ast::TypeAlias {} 514impl ast::DocCommentsOwner for ast::TypeAlias {}
463impl ast::DocCommentsOwner for ast::Impl {} 515impl ast::DocCommentsOwner for ast::Impl {}
464impl ast::DocCommentsOwner for ast::MacroRules {} 516impl ast::DocCommentsOwner for ast::MacroRules {}
517impl ast::DocCommentsOwner for ast::MacroDef {}
518impl ast::DocCommentsOwner for ast::Macro {}
465impl ast::DocCommentsOwner for ast::Use {} 519impl ast::DocCommentsOwner for ast::Use {}