From 60f4d7bd8c0ecb9f23557464e824140a2be8f41a Mon Sep 17 00:00:00 2001 From: Luca Barbieri Date: Fri, 3 Apr 2020 21:12:09 +0200 Subject: Provide more complete AST accessors to support usage in rustc --- crates/ra_syntax/src/ast/traits.rs | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'crates/ra_syntax/src/ast/traits.rs') diff --git a/crates/ra_syntax/src/ast/traits.rs b/crates/ra_syntax/src/ast/traits.rs index 576378306..e6f3a4ebb 100644 --- a/crates/ra_syntax/src/ast/traits.rs +++ b/crates/ra_syntax/src/ast/traits.rs @@ -4,9 +4,9 @@ use itertools::Itertools; -use crate::{ - ast::{self, child_opt, children, AstChildren, AstNode, AstToken}, - syntax_node::SyntaxElementChildren, +use crate::ast::{ + self, child_elements, child_opt, child_token_opt, child_tokens, children, AstChildElements, + AstChildTokens, AstChildren, AstNode, AstToken, }; pub trait TypeAscriptionOwner: AstNode { @@ -31,6 +31,10 @@ pub trait LoopBodyOwner: AstNode { fn loop_body(&self) -> Option { child_opt(self) } + + fn label(&self) -> Option { + child_opt(self) + } } pub trait ArgListOwner: AstNode { @@ -65,6 +69,10 @@ pub trait TypeBoundsOwner: AstNode { fn type_bound_list(&self) -> Option { child_opt(self) } + + fn colon(&self) -> Option { + child_token_opt(self) + } } pub trait AttrsOwner: AstNode { @@ -74,11 +82,14 @@ 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 { + child_elements(self) + } } pub trait DocCommentsOwner: AstNode { - fn doc_comments(&self) -> CommentIter { - CommentIter { iter: self.syntax().children_with_tokens() } + fn doc_comments(&self) -> AstChildTokens { + child_tokens(self) } /// Returns the textual content of a doc comment block as a single string. @@ -123,14 +134,3 @@ pub trait DocCommentsOwner: AstNode { } } } - -pub struct CommentIter { - iter: SyntaxElementChildren, -} - -impl Iterator for CommentIter { - type Item = ast::Comment; - fn next(&mut self) -> Option { - self.iter.by_ref().find_map(|el| el.into_token().and_then(ast::Comment::cast)) - } -} -- cgit v1.2.3 From 689661c95968cb438f8bd1f10ce0ee096287741b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 9 Apr 2020 13:00:09 +0200 Subject: Scale back to only two traits --- crates/ra_syntax/src/ast/traits.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'crates/ra_syntax/src/ast/traits.rs') diff --git a/crates/ra_syntax/src/ast/traits.rs b/crates/ra_syntax/src/ast/traits.rs index e6f3a4ebb..870e83804 100644 --- a/crates/ra_syntax/src/ast/traits.rs +++ b/crates/ra_syntax/src/ast/traits.rs @@ -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 { - 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 { - child_elements(self) - } } pub trait DocCommentsOwner: AstNode { - fn doc_comments(&self) -> AstChildTokens { - 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 { + self.iter.by_ref().find_map(|el| el.into_token().and_then(ast::Comment::cast)) + } +} -- cgit v1.2.3