aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/ast.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/ast.rs')
-rw-r--r--crates/ra_syntax/src/ast.rs20
1 files changed, 18 insertions, 2 deletions
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs
index ab0f44dd2..a42eec91a 100644
--- a/crates/ra_syntax/src/ast.rs
+++ b/crates/ra_syntax/src/ast.rs
@@ -21,7 +21,7 @@ pub use self::{
21 AttrKind, FieldKind, PathSegmentKind, SelfParamKind, SlicePatComponents, StructKind, 21 AttrKind, FieldKind, PathSegmentKind, SelfParamKind, SlicePatComponents, StructKind,
22 TypeBoundKind, VisibilityKind, 22 TypeBoundKind, VisibilityKind,
23 }, 23 },
24 generated::*, 24 generated::{nodes::*, tokens::*},
25 tokens::*, 25 tokens::*,
26 traits::*, 26 traits::*,
27}; 27};
@@ -64,6 +64,22 @@ pub trait AstToken {
64 } 64 }
65} 65}
66 66
67mod support {
68 use super::{AstChildren, AstNode, AstToken, SyntaxNode};
69
70 pub(super) fn child<N: AstNode>(parent: &SyntaxNode) -> Option<N> {
71 parent.children().find_map(N::cast)
72 }
73
74 pub(super) fn children<N: AstNode>(parent: &SyntaxNode) -> AstChildren<N> {
75 AstChildren::new(parent)
76 }
77
78 pub(super) fn token<T: AstToken>(parent: &SyntaxNode) -> Option<T> {
79 parent.children_with_tokens().filter_map(|it| it.into_token()).find_map(T::cast)
80 }
81}
82
67/// An iterator over `SyntaxNode` children of a particular AST type. 83/// An iterator over `SyntaxNode` children of a particular AST type.
68#[derive(Debug, Clone)] 84#[derive(Debug, Clone)]
69pub struct AstChildren<N> { 85pub struct AstChildren<N> {
@@ -271,7 +287,7 @@ where
271 let pred = predicates.next().unwrap(); 287 let pred = predicates.next().unwrap();
272 let mut bounds = pred.type_bound_list().unwrap().bounds(); 288 let mut bounds = pred.type_bound_list().unwrap().bounds();
273 289
274 assert_eq!("'a", pred.lifetime_token().unwrap().text()); 290 assert_eq!("'a", pred.lifetime().unwrap().text());
275 291
276 assert_bound("'b", bounds.next()); 292 assert_bound("'b", bounds.next());
277 assert_bound("'c", bounds.next()); 293 assert_bound("'c", bounds.next());