This commit is contained in:
Aleksey Kladov 2020-04-09 18:20:06 +02:00
parent 56c8581b90
commit a95116fbfa

View file

@ -2,16 +2,14 @@
//! Extensions for various expressions live in a sibling `expr_extensions` module. //! Extensions for various expressions live in a sibling `expr_extensions` module.
use itertools::Itertools; use itertools::Itertools;
use ra_parser::SyntaxKind;
use crate::{ use crate::{
ast::{ ast::{
self, child_opt, children, support, AstNode, AstToken, AttrInput, NameOwner, SyntaxNode, self, child_opt, children, support, AstNode, AstToken, AttrInput, NameOwner, SyntaxNode,
}, },
SmolStr, SyntaxElement, SmolStr, SyntaxElement, SyntaxToken, T,
SyntaxKind::*,
SyntaxToken, T,
}; };
use ra_parser::SyntaxKind;
impl ast::Name { impl ast::Name {
pub fn text(&self) -> &SmolStr { pub fn text(&self) -> &SmolStr {
@ -25,13 +23,11 @@ impl ast::NameRef {
} }
pub fn as_tuple_field(&self) -> Option<usize> { pub fn as_tuple_field(&self) -> Option<usize> {
self.syntax().children_with_tokens().find_map(|c| { if let Some(ast::NameRefToken::IntNumber(token)) = self.name_ref_token() {
if c.kind() == SyntaxKind::INT_NUMBER { token.text().as_str().parse().ok()
c.as_token().and_then(|tok| tok.text().as_str().parse().ok()) } else {
} else { None
None }
}
})
} }
} }
@ -142,10 +138,7 @@ impl ast::Path {
impl ast::Module { impl ast::Module {
pub fn has_semi(&self) -> bool { pub fn has_semi(&self) -> bool {
match self.syntax().last_child_or_token() { self.semi().is_some()
None => false,
Some(node) => node.kind() == T![;],
}
} }
} }
@ -181,7 +174,7 @@ impl ast::ImplDef {
} }
pub fn is_negative(&self) -> bool { pub fn is_negative(&self) -> bool {
self.syntax().children_with_tokens().any(|t| t.kind() == T![!]) self.excl().is_some()
} }
} }
@ -225,14 +218,11 @@ impl ast::EnumVariant {
impl ast::FnDef { impl ast::FnDef {
pub fn semicolon_token(&self) -> Option<SyntaxToken> { pub fn semicolon_token(&self) -> Option<SyntaxToken> {
self.syntax() Some(self.semi()?.syntax().clone())
.last_child_or_token()
.and_then(|it| it.into_token())
.filter(|it| it.kind() == T![;])
} }
pub fn is_async(&self) -> bool { pub fn is_async(&self) -> bool {
self.syntax().children_with_tokens().any(|it| it.kind() == T![async]) self.async_kw().is_some()
} }
} }
@ -245,16 +235,13 @@ impl ast::LetStmt {
} }
pub fn eq_token(&self) -> Option<SyntaxToken> { pub fn eq_token(&self) -> Option<SyntaxToken> {
self.syntax().children_with_tokens().find(|t| t.kind() == EQ).and_then(|it| it.into_token()) Some(self.eq()?.syntax().clone())
} }
} }
impl ast::ExprStmt { impl ast::ExprStmt {
pub fn has_semi(&self) -> bool { pub fn has_semi(&self) -> bool {
match self.syntax().last_child_or_token() { self.semi().is_some()
None => false,
Some(node) => node.kind() == T![;],
}
} }
} }