Generate AnyHasDocComments node

This commit is contained in:
Lukas Wirth 2022-01-07 12:37:58 +01:00
parent 2fb6f5e46a
commit 69dbfc7754
6 changed files with 95 additions and 21 deletions

View file

@ -1,10 +1,12 @@
//! Various traits that are implemented by ast nodes.
//!
//! The implementations are usually trivial, and live in generated.rs
use itertools::Either;
use crate::{
ast::{self, support, AstChildren, AstNode, AstToken},
syntax_node::SyntaxElementChildren,
SyntaxToken, T,
SyntaxElement, SyntaxToken, T,
};
pub trait HasName: AstNode {
@ -74,6 +76,9 @@ pub trait HasDocComments: HasAttrs {
fn doc_comments(&self) -> CommentIter {
CommentIter { iter: self.syntax().children_with_tokens() }
}
fn doc_comments_and_attrs(&self) -> AttrCommentIter {
AttrCommentIter { iter: self.syntax().children_with_tokens() }
}
}
impl CommentIter {
@ -105,3 +110,17 @@ impl Iterator for CommentIter {
self.iter.by_ref().find_map(|el| el.into_token().and_then(ast::Comment::cast))
}
}
pub struct AttrCommentIter {
iter: SyntaxElementChildren,
}
impl Iterator for AttrCommentIter {
type Item = Either<ast::Comment, ast::Attr>;
fn next(&mut self) -> Option<Self::Item> {
self.iter.by_ref().find_map(|el| match el {
SyntaxElement::Node(node) => ast::Attr::cast(node).map(Either::Right),
SyntaxElement::Token(tok) => ast::Comment::cast(tok).map(Either::Left),
})
}
}