From 191a6ba330bd47fc3b9cc05d59b2d456b471eb89 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 19 Jul 2019 19:05:34 +0300 Subject: convenience api --- crates/ra_syntax/src/ast/expr_extensions.rs | 72 ++++++++++++++--------------- crates/ra_syntax/src/ast/extensions.rs | 8 ++-- crates/ra_syntax/src/ast/traits.rs | 2 +- crates/ra_syntax/src/syntax_node.rs | 14 ++++++ 4 files changed, 55 insertions(+), 41 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index ca1773908..139bd3ec0 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs @@ -60,7 +60,7 @@ impl ast::PrefixExpr { } pub fn op_token(&self) -> Option { - self.syntax().first_child_or_token()?.as_token().cloned() + self.syntax().first_child_or_token()?.into_token() } } @@ -132,41 +132,41 @@ pub enum BinOp { impl ast::BinExpr { fn op_details(&self) -> Option<(SyntaxToken, BinOp)> { - self.syntax().children_with_tokens().filter_map(|it| it.as_token().cloned()).find_map(|c| { - match c.kind() { - T![||] => Some((c, BinOp::BooleanOr)), - T![&&] => Some((c, BinOp::BooleanAnd)), - T![==] => Some((c, BinOp::EqualityTest)), - T![!=] => Some((c, BinOp::NegatedEqualityTest)), - T![<=] => Some((c, BinOp::LesserEqualTest)), - T![>=] => Some((c, BinOp::GreaterEqualTest)), - T![<] => Some((c, BinOp::LesserTest)), - T![>] => Some((c, BinOp::GreaterTest)), - T![+] => Some((c, BinOp::Addition)), - T![*] => Some((c, BinOp::Multiplication)), - T![-] => Some((c, BinOp::Subtraction)), - T![/] => Some((c, BinOp::Division)), - T![%] => Some((c, BinOp::Remainder)), - T![<<] => Some((c, BinOp::LeftShift)), - T![>>] => Some((c, BinOp::RightShift)), - T![^] => Some((c, BinOp::BitwiseXor)), - T![|] => Some((c, BinOp::BitwiseOr)), - T![&] => Some((c, BinOp::BitwiseAnd)), - T![..] => Some((c, BinOp::RangeRightOpen)), - T![..=] => Some((c, BinOp::RangeRightClosed)), - T![=] => Some((c, BinOp::Assignment)), - T![+=] => Some((c, BinOp::AddAssign)), - T![/=] => Some((c, BinOp::DivAssign)), - T![*=] => Some((c, BinOp::MulAssign)), - T![%=] => Some((c, BinOp::RemAssign)), - T![>>=] => Some((c, BinOp::ShrAssign)), - T![<<=] => Some((c, BinOp::ShlAssign)), - T![-=] => Some((c, BinOp::SubAssign)), - T![|=] => Some((c, BinOp::BitOrAssign)), - T![&=] => Some((c, BinOp::BitAndAssign)), - T![^=] => Some((c, BinOp::BitXorAssign)), - _ => None, - } + self.syntax().children_with_tokens().filter_map(|it| it.into_token()).find_map(|c| match c + .kind() + { + T![||] => Some((c, BinOp::BooleanOr)), + T![&&] => Some((c, BinOp::BooleanAnd)), + T![==] => Some((c, BinOp::EqualityTest)), + T![!=] => Some((c, BinOp::NegatedEqualityTest)), + T![<=] => Some((c, BinOp::LesserEqualTest)), + T![>=] => Some((c, BinOp::GreaterEqualTest)), + T![<] => Some((c, BinOp::LesserTest)), + T![>] => Some((c, BinOp::GreaterTest)), + T![+] => Some((c, BinOp::Addition)), + T![*] => Some((c, BinOp::Multiplication)), + T![-] => Some((c, BinOp::Subtraction)), + T![/] => Some((c, BinOp::Division)), + T![%] => Some((c, BinOp::Remainder)), + T![<<] => Some((c, BinOp::LeftShift)), + T![>>] => Some((c, BinOp::RightShift)), + T![^] => Some((c, BinOp::BitwiseXor)), + T![|] => Some((c, BinOp::BitwiseOr)), + T![&] => Some((c, BinOp::BitwiseAnd)), + T![..] => Some((c, BinOp::RangeRightOpen)), + T![..=] => Some((c, BinOp::RangeRightClosed)), + T![=] => Some((c, BinOp::Assignment)), + T![+=] => Some((c, BinOp::AddAssign)), + T![/=] => Some((c, BinOp::DivAssign)), + T![*=] => Some((c, BinOp::MulAssign)), + T![%=] => Some((c, BinOp::RemAssign)), + T![>>=] => Some((c, BinOp::ShrAssign)), + T![<<=] => Some((c, BinOp::ShlAssign)), + T![-=] => Some((c, BinOp::SubAssign)), + T![|=] => Some((c, BinOp::BitOrAssign)), + T![&=] => Some((c, BinOp::BitAndAssign)), + T![^=] => Some((c, BinOp::BitXorAssign)), + _ => None, }) } diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index 5420f67ff..753fc42c6 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs @@ -239,7 +239,7 @@ impl ast::FnDef { pub fn semicolon_token(&self) -> Option { self.syntax() .last_child_or_token() - .and_then(|it| it.as_token().cloned()) + .and_then(|it| it.into_token()) .filter(|it| it.kind() == T![;]) } } @@ -332,7 +332,7 @@ impl ast::SelfParam { pub fn self_kw_token(&self) -> SyntaxToken { self.syntax() .children_with_tokens() - .filter_map(|it| it.as_token().cloned()) + .filter_map(|it| it.into_token()) .find(|it| it.kind() == T![self]) .expect("invalid tree: self param must have self") } @@ -361,7 +361,7 @@ impl ast::LifetimeParam { pub fn lifetime_token(&self) -> Option { self.syntax() .children_with_tokens() - .filter_map(|it| it.as_token().cloned()) + .filter_map(|it| it.into_token()) .find(|it| it.kind() == LIFETIME) } } @@ -370,7 +370,7 @@ impl ast::WherePred { pub fn lifetime_token(&self) -> Option { self.syntax() .children_with_tokens() - .filter_map(|it| it.as_token().cloned()) + .filter_map(|it| it.into_token()) .find(|it| it.kind() == LIFETIME) } } diff --git a/crates/ra_syntax/src/ast/traits.rs b/crates/ra_syntax/src/ast/traits.rs index ecbd2d427..6ed1b5213 100644 --- a/crates/ra_syntax/src/ast/traits.rs +++ b/crates/ra_syntax/src/ast/traits.rs @@ -155,7 +155,7 @@ pub struct CommentIter { impl Iterator for CommentIter { type Item = ast::Comment; fn next(&mut self) -> Option { - self.iter.by_ref().find_map(|el| el.as_token().cloned().and_then(ast::Comment::cast)) + self.iter.by_ref().find_map(|el| el.into_token().and_then(ast::Comment::cast)) } } diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs index cf680e66a..98955832b 100644 --- a/crates/ra_syntax/src/syntax_node.rs +++ b/crates/ra_syntax/src/syntax_node.rs @@ -423,6 +423,13 @@ impl SyntaxElement { } } + pub fn into_node(self) -> Option { + match self { + SyntaxElement::Node(node) => Some(node), + SyntaxElement::Token(_) => None, + } + } + pub fn as_token(&self) -> Option<&SyntaxToken> { match self { SyntaxElement::Node(_) => None, @@ -430,6 +437,13 @@ impl SyntaxElement { } } + pub fn into_token(self) -> Option { + match self { + SyntaxElement::Node(_) => None, + SyntaxElement::Token(token) => Some(token), + } + } + pub fn next_sibling_or_token(&self) -> Option { match self { SyntaxElement::Node(it) => it.next_sibling_or_token(), -- cgit v1.2.3