diff options
Diffstat (limited to 'crates/syntax/src/ast/node_ext.rs')
-rw-r--r-- | crates/syntax/src/ast/node_ext.rs | 66 |
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 | ||
4 | use std::fmt; | 4 | use std::fmt; |
5 | 5 | ||
6 | use ast::AttrsOwner; | ||
6 | use itertools::Itertools; | 7 | use itertools::Itertools; |
7 | use parser::SyntaxKind; | 8 | use parser::SyntaxKind; |
8 | 9 | ||
@@ -11,6 +12,12 @@ use crate::{ | |||
11 | SmolStr, SyntaxElement, SyntaxToken, T, | 12 | SmolStr, SyntaxElement, SyntaxToken, T, |
12 | }; | 13 | }; |
13 | 14 | ||
15 | impl ast::Lifetime { | ||
16 | pub fn text(&self) -> &SmolStr { | ||
17 | text_of_first_token(self.syntax()) | ||
18 | } | ||
19 | } | ||
20 | |||
14 | impl ast::Name { | 21 | impl 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 | ||
41 | pub enum Macro { | ||
42 | MacroRules(ast::MacroRules), | ||
43 | MacroDef(ast::MacroDef), | ||
44 | } | ||
45 | |||
46 | impl From<ast::MacroRules> for Macro { | ||
47 | fn from(it: ast::MacroRules) -> Self { | ||
48 | Macro::MacroRules(it) | ||
49 | } | ||
50 | } | ||
51 | |||
52 | impl From<ast::MacroDef> for Macro { | ||
53 | fn from(it: ast::MacroDef) -> Self { | ||
54 | Macro::MacroDef(it) | ||
55 | } | ||
56 | } | ||
57 | |||
58 | impl 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 | |||
81 | impl 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 | |||
90 | impl AttrsOwner for Macro {} | ||
91 | |||
34 | #[derive(Debug, Clone, PartialEq, Eq)] | 92 | #[derive(Debug, Clone, PartialEq, Eq)] |
35 | pub enum AttrKind { | 93 | pub 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 | ||
347 | impl ast::TypeBound { | 405 | impl 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 {} | |||
462 | impl ast::DocCommentsOwner for ast::TypeAlias {} | 520 | impl ast::DocCommentsOwner for ast::TypeAlias {} |
463 | impl ast::DocCommentsOwner for ast::Impl {} | 521 | impl ast::DocCommentsOwner for ast::Impl {} |
464 | impl ast::DocCommentsOwner for ast::MacroRules {} | 522 | impl ast::DocCommentsOwner for ast::MacroRules {} |
523 | impl ast::DocCommentsOwner for ast::MacroDef {} | ||
524 | impl ast::DocCommentsOwner for ast::Macro {} | ||
465 | impl ast::DocCommentsOwner for ast::Use {} | 525 | impl ast::DocCommentsOwner for ast::Use {} |