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.rs66
1 files changed, 63 insertions, 3 deletions
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs
index c59a29eab..c45cb514a 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
@@ -11,6 +12,12 @@ use crate::{
11 SmolStr, SyntaxElement, SyntaxToken, T, 12 SmolStr, SyntaxElement, SyntaxToken, T,
12}; 13};
13 14
15impl ast::Lifetime {
16 pub fn text(&self) -> &SmolStr {
17 text_of_first_token(self.syntax())
18 }
19}
20
14impl ast::Name { 21impl ast::Name {
15 pub fn text(&self) -> &SmolStr { 22 pub fn text(&self) -> &SmolStr {
16 text_of_first_token(self.syntax()) 23 text_of_first_token(self.syntax())
@@ -31,6 +38,57 @@ fn text_of_first_token(node: &SyntaxNode) -> &SmolStr {
31 node.green().children().next().and_then(|it| it.into_token()).unwrap().text() 38 node.green().children().next().and_then(|it| it.into_token()).unwrap().text()
32} 39}
33 40
41pub enum Macro {
42 MacroRules(ast::MacroRules),
43 MacroDef(ast::MacroDef),
44}
45
46impl From<ast::MacroRules> for Macro {
47 fn from(it: ast::MacroRules) -> Self {
48 Macro::MacroRules(it)
49 }
50}
51
52impl From<ast::MacroDef> for Macro {
53 fn from(it: ast::MacroDef) -> Self {
54 Macro::MacroDef(it)
55 }
56}
57
58impl AstNode for Macro {
59 fn can_cast(kind: SyntaxKind) -> bool {
60 match kind {
61 SyntaxKind::MACRO_RULES | SyntaxKind::MACRO_DEF => true,
62 _ => false,
63 }
64 }
65 fn cast(syntax: SyntaxNode) -> Option<Self> {
66 let res = match syntax.kind() {
67 SyntaxKind::MACRO_RULES => Macro::MacroRules(ast::MacroRules { syntax }),
68 SyntaxKind::MACRO_DEF => Macro::MacroDef(ast::MacroDef { syntax }),
69 _ => return None,
70 };
71 Some(res)
72 }
73 fn syntax(&self) -> &SyntaxNode {
74 match self {
75 Macro::MacroRules(it) => it.syntax(),
76 Macro::MacroDef(it) => it.syntax(),
77 }
78 }
79}
80
81impl NameOwner for Macro {
82 fn name(&self) -> Option<ast::Name> {
83 match self {
84 Macro::MacroRules(mac) => mac.name(),
85 Macro::MacroDef(mac) => mac.name(),
86 }
87 }
88}
89
90impl AttrsOwner for Macro {}
91
34#[derive(Debug, Clone, PartialEq, Eq)] 92#[derive(Debug, Clone, PartialEq, Eq)]
35pub enum AttrKind { 93pub enum AttrKind {
36 Inner, 94 Inner,
@@ -341,7 +399,7 @@ pub enum TypeBoundKind {
341 /// for<'a> ... 399 /// for<'a> ...
342 ForType(ast::ForType), 400 ForType(ast::ForType),
343 /// 'a 401 /// 'a
344 Lifetime(SyntaxToken), 402 Lifetime(ast::Lifetime),
345} 403}
346 404
347impl ast::TypeBound { 405impl ast::TypeBound {
@@ -350,7 +408,7 @@ impl ast::TypeBound {
350 TypeBoundKind::PathType(path_type) 408 TypeBoundKind::PathType(path_type)
351 } else if let Some(for_type) = support::children(self.syntax()).next() { 409 } else if let Some(for_type) = support::children(self.syntax()).next() {
352 TypeBoundKind::ForType(for_type) 410 TypeBoundKind::ForType(for_type)
353 } else if let Some(lifetime) = self.lifetime_token() { 411 } else if let Some(lifetime) = self.lifetime() {
354 TypeBoundKind::Lifetime(lifetime) 412 TypeBoundKind::Lifetime(lifetime)
355 } else { 413 } else {
356 unreachable!() 414 unreachable!()
@@ -388,7 +446,7 @@ impl ast::LifetimeParam {
388 .children_with_tokens() 446 .children_with_tokens()
389 .filter_map(|it| it.into_token()) 447 .filter_map(|it| it.into_token())
390 .skip_while(|x| x.kind() != T![:]) 448 .skip_while(|x| x.kind() != T![:])
391 .filter(|it| it.kind() == T![lifetime]) 449 .filter(|it| it.kind() == T![lifetime_ident])
392 } 450 }
393} 451}
394 452
@@ -462,4 +520,6 @@ impl ast::DocCommentsOwner for ast::Const {}
462impl ast::DocCommentsOwner for ast::TypeAlias {} 520impl ast::DocCommentsOwner for ast::TypeAlias {}
463impl ast::DocCommentsOwner for ast::Impl {} 521impl ast::DocCommentsOwner for ast::Impl {}
464impl ast::DocCommentsOwner for ast::MacroRules {} 522impl ast::DocCommentsOwner for ast::MacroRules {}
523impl ast::DocCommentsOwner for ast::MacroDef {}
524impl ast::DocCommentsOwner for ast::Macro {}
465impl ast::DocCommentsOwner for ast::Use {} 525impl ast::DocCommentsOwner for ast::Use {}