rust-analyzer/crates/syntax/src/ast/traits.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

137 lines
3.6 KiB
Rust
Raw Normal View History

2019-04-02 10:02:23 +00:00
//! Various traits that are implemented by ast nodes.
//!
//! The implementations are usually trivial, and live in generated.rs
2022-01-07 11:37:58 +00:00
use itertools::Either;
2020-04-09 11:00:09 +00:00
use crate::{
2020-04-09 20:22:58 +00:00
ast::{self, support, AstChildren, AstNode, AstToken},
2020-04-09 11:00:09 +00:00
syntax_node::SyntaxElementChildren,
2022-01-07 11:37:58 +00:00
SyntaxElement, SyntaxToken, T,
2019-04-02 07:03:19 +00:00
};
2021-09-27 10:54:24 +00:00
pub trait HasName: AstNode {
2019-07-18 16:23:05 +00:00
fn name(&self) -> Option<ast::Name> {
2020-04-09 20:22:58 +00:00
support::child(self.syntax())
2019-04-02 07:03:19 +00:00
}
}
2021-09-27 10:54:24 +00:00
pub trait HasVisibility: AstNode {
2019-07-18 16:23:05 +00:00
fn visibility(&self) -> Option<ast::Visibility> {
2020-04-09 20:22:58 +00:00
support::child(self.syntax())
2019-04-02 07:03:19 +00:00
}
}
2021-09-27 10:54:24 +00:00
pub trait HasLoopBody: AstNode {
2019-09-02 18:23:19 +00:00
fn loop_body(&self) -> Option<ast::BlockExpr> {
2020-04-09 20:22:58 +00:00
support::child(self.syntax())
2019-04-02 07:03:19 +00:00
}
fn label(&self) -> Option<ast::Label> {
2020-04-09 20:22:58 +00:00
support::child(self.syntax())
}
2019-04-02 07:03:19 +00:00
}
2021-09-27 10:54:24 +00:00
pub trait HasArgList: AstNode {
2019-07-18 16:23:05 +00:00
fn arg_list(&self) -> Option<ast::ArgList> {
2020-04-09 20:22:58 +00:00
support::child(self.syntax())
2019-04-02 07:03:19 +00:00
}
}
2021-09-27 10:54:24 +00:00
pub trait HasModuleItem: AstNode {
2020-07-29 22:23:03 +00:00
fn items(&self) -> AstChildren<ast::Item> {
2020-04-09 20:22:58 +00:00
support::children(self.syntax())
2019-04-02 07:03:19 +00:00
}
}
2021-09-27 10:54:24 +00:00
pub trait HasGenericParams: AstNode {
fn generic_param_list(&self) -> Option<ast::GenericParamList> {
2020-04-09 20:22:58 +00:00
support::child(self.syntax())
2019-04-02 07:03:19 +00:00
}
2019-07-18 16:23:05 +00:00
fn where_clause(&self) -> Option<ast::WhereClause> {
2020-04-09 20:22:58 +00:00
support::child(self.syntax())
2019-04-02 07:03:19 +00:00
}
}
2021-09-27 10:54:24 +00:00
pub trait HasTypeBounds: AstNode {
2019-07-18 16:23:05 +00:00
fn type_bound_list(&self) -> Option<ast::TypeBoundList> {
2020-04-09 20:22:58 +00:00
support::child(self.syntax())
2019-04-02 07:03:19 +00:00
}
fn colon_token(&self) -> Option<SyntaxToken> {
2020-04-10 13:54:05 +00:00
support::token(self.syntax(), T![:])
}
2019-04-02 07:03:19 +00:00
}
2021-09-27 10:54:24 +00:00
pub trait HasAttrs: AstNode {
2019-04-02 07:03:19 +00:00
fn attrs(&self) -> AstChildren<ast::Attr> {
2020-04-09 20:22:58 +00:00
support::children(self.syntax())
2019-04-02 07:03:19 +00:00
}
fn has_atom_attr(&self, atom: &str) -> bool {
2019-09-29 21:15:03 +00:00
self.attrs().filter_map(|x| x.as_simple_atom()).any(|x| x == atom)
2019-04-02 07:03:19 +00:00
}
}
2021-09-27 10:54:24 +00:00
pub trait HasDocComments: HasAttrs {
fn doc_comments(&self) -> DocCommentIter {
DocCommentIter { iter: self.syntax().children_with_tokens() }
2019-04-02 07:03:19 +00:00
}
fn doc_comments_and_attrs(&self) -> AttrDocCommentIter {
AttrDocCommentIter { iter: self.syntax().children_with_tokens() }
2022-01-07 11:37:58 +00:00
}
}
impl DocCommentIter {
pub fn from_syntax_node(syntax_node: &ast::SyntaxNode) -> DocCommentIter {
DocCommentIter { iter: syntax_node.children_with_tokens() }
}
2021-03-17 13:38:11 +00:00
#[cfg(test)]
pub fn doc_comment_text(self) -> Option<String> {
2021-03-17 13:38:11 +00:00
let docs = itertools::Itertools::join(
&mut self.filter_map(|comment| comment.doc_comment().map(ToOwned::to_owned)),
"\n",
);
2020-12-07 19:38:28 +00:00
if docs.is_empty() {
2019-04-02 07:34:34 +00:00
None
2020-12-07 19:38:28 +00:00
} else {
Some(docs)
2019-04-02 07:03:19 +00:00
}
}
}
2020-04-09 11:00:09 +00:00
pub struct DocCommentIter {
2020-04-09 11:00:09 +00:00
iter: SyntaxElementChildren,
}
impl Iterator for DocCommentIter {
2020-04-09 11:00:09 +00:00
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).filter(ast::Comment::is_doc)
})
2020-04-09 11:00:09 +00:00
}
}
2022-01-07 11:37:58 +00:00
pub struct AttrDocCommentIter {
2022-01-07 11:37:58 +00:00
iter: SyntaxElementChildren,
}
impl AttrDocCommentIter {
pub fn from_syntax_node(syntax_node: &ast::SyntaxNode) -> AttrDocCommentIter {
AttrDocCommentIter { iter: syntax_node.children_with_tokens() }
}
}
impl Iterator for AttrDocCommentIter {
type Item = Either<ast::Attr, ast::Comment>;
2022-01-07 11:37:58 +00:00
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::Left),
SyntaxElement::Token(tok) => {
ast::Comment::cast(tok).filter(ast::Comment::is_doc).map(Either::Right)
}
2022-01-07 11:37:58 +00:00
})
}
}