Scale back to only two traits

This commit is contained in:
Aleksey Kladov 2020-04-09 13:00:09 +02:00
parent 60f4d7bd8c
commit 689661c959
8 changed files with 2020 additions and 228 deletions

View file

@ -64,6 +64,22 @@ pub trait AstToken {
}
}
mod support {
use super::{AstChildren, AstNode, AstToken, SyntaxNode};
pub(super) fn child<N: AstNode>(parent: &SyntaxNode) -> Option<N> {
parent.children().find_map(N::cast)
}
pub(super) fn children<N: AstNode>(parent: &SyntaxNode) -> AstChildren<N> {
AstChildren::new(parent)
}
pub(super) fn token<T: AstToken>(parent: &SyntaxNode) -> Option<T> {
parent.children_with_tokens().filter_map(|it| it.into_token()).find_map(T::cast)
}
}
/// An iterator over `SyntaxNode` children of a particular AST type.
#[derive(Debug, Clone)]
pub struct AstChildren<N> {

View file

@ -5,8 +5,7 @@ use itertools::Itertools;
use crate::{
ast::{
self, child_opt, child_token_opt, children, AstElement, AstNode, AstToken, AttrInput,
NameOwner, SyntaxNode,
self, child_opt, children, support, AstNode, AstToken, AttrInput, NameOwner, SyntaxNode,
},
SmolStr, SyntaxElement,
SyntaxKind::*,
@ -437,7 +436,7 @@ impl ast::TypeBound {
.skip_while(|it| it.kind() != T![const])
.find_map(ast::Question::cast)
} else {
child_token_opt(self)
support::token(&self.syntax)
}
}
}
@ -509,7 +508,7 @@ impl ast::RangePat {
pub fn start(&self) -> Option<ast::Pat> {
self.syntax()
.children_with_tokens()
.take_while(|it| !ast::RangeSeparator::can_cast_element(it.kind()))
.take_while(|it| !ast::RangeSeparator::can_cast(it.kind()))
.filter_map(|it| it.into_node())
.find_map(ast::Pat::cast)
}
@ -517,7 +516,7 @@ impl ast::RangePat {
pub fn end(&self) -> Option<ast::Pat> {
self.syntax()
.children_with_tokens()
.skip_while(|it| !ast::RangeSeparator::can_cast_element(it.kind()))
.skip_while(|it| !ast::RangeSeparator::can_cast(it.kind()))
.filter_map(|it| it.into_node())
.find_map(ast::Pat::cast)
}
@ -525,10 +524,10 @@ impl ast::RangePat {
impl ast::TokenTree {
pub fn left_delimiter(&self) -> Option<ast::LeftDelimiter> {
self.syntax().first_child_or_token().and_then(ast::LeftDelimiter::cast_element)
self.syntax().first_child_or_token()?.into_token().and_then(ast::LeftDelimiter::cast)
}
pub fn right_delimiter(&self) -> Option<ast::RightDelimiter> {
self.syntax().last_child_or_token().and_then(ast::RightDelimiter::cast_element)
self.syntax().last_child_or_token()?.into_token().and_then(ast::RightDelimiter::cast)
}
}

File diff suppressed because it is too large Load diff

View file

@ -4,9 +4,9 @@
use itertools::Itertools;
use crate::ast::{
self, child_elements, child_opt, child_token_opt, child_tokens, children, AstChildElements,
AstChildTokens, AstChildren, AstNode, AstToken,
use crate::{
ast::{self, child_opt, children, support, AstChildren, AstNode, AstToken},
syntax_node::SyntaxElementChildren,
};
pub trait TypeAscriptionOwner: AstNode {
@ -71,7 +71,7 @@ pub trait TypeBoundsOwner: AstNode {
}
fn colon(&self) -> Option<ast::Colon> {
child_token_opt(self)
support::token(self.syntax())
}
}
@ -82,14 +82,11 @@ pub trait AttrsOwner: AstNode {
fn has_atom_attr(&self, atom: &str) -> bool {
self.attrs().filter_map(|x| x.as_simple_atom()).any(|x| x == atom)
}
fn attr_or_comments(&self) -> AstChildElements<ast::AttrOrComment> {
child_elements(self)
}
}
pub trait DocCommentsOwner: AstNode {
fn doc_comments(&self) -> AstChildTokens<ast::Comment> {
child_tokens(self)
fn doc_comments(&self) -> CommentIter {
CommentIter { iter: self.syntax().children_with_tokens() }
}
/// Returns the textual content of a doc comment block as a single string.
@ -134,3 +131,14 @@ pub trait DocCommentsOwner: AstNode {
}
}
}
pub struct CommentIter {
iter: SyntaxElementChildren,
}
impl Iterator for CommentIter {
type Item = ast::Comment;
fn next(&mut self) -> Option<ast::Comment> {
self.iter.by_ref().find_map(|el| el.into_token().and_then(ast::Comment::cast))
}
}