aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r--crates/ra_syntax/src/ast/extensions.rs39
1 files changed, 13 insertions, 26 deletions
diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs
index 33fe60762..c7df15662 100644
--- a/crates/ra_syntax/src/ast/extensions.rs
+++ b/crates/ra_syntax/src/ast/extensions.rs
@@ -2,16 +2,14 @@
2//! Extensions for various expressions live in a sibling `expr_extensions` module. 2//! Extensions for various expressions live in a sibling `expr_extensions` module.
3 3
4use itertools::Itertools; 4use itertools::Itertools;
5use ra_parser::SyntaxKind;
5 6
6use crate::{ 7use crate::{
7 ast::{ 8 ast::{
8 self, child_opt, children, support, AstNode, AstToken, AttrInput, NameOwner, SyntaxNode, 9 self, child_opt, children, support, AstNode, AstToken, AttrInput, NameOwner, SyntaxNode,
9 }, 10 },
10 SmolStr, SyntaxElement, 11 SmolStr, SyntaxElement, SyntaxToken, T,
11 SyntaxKind::*,
12 SyntaxToken, T,
13}; 12};
14use ra_parser::SyntaxKind;
15 13
16impl ast::Name { 14impl ast::Name {
17 pub fn text(&self) -> &SmolStr { 15 pub fn text(&self) -> &SmolStr {
@@ -25,13 +23,11 @@ impl ast::NameRef {
25 } 23 }
26 24
27 pub fn as_tuple_field(&self) -> Option<usize> { 25 pub fn as_tuple_field(&self) -> Option<usize> {
28 self.syntax().children_with_tokens().find_map(|c| { 26 if let Some(ast::NameRefToken::IntNumber(token)) = self.name_ref_token() {
29 if c.kind() == SyntaxKind::INT_NUMBER { 27 token.text().as_str().parse().ok()
30 c.as_token().and_then(|tok| tok.text().as_str().parse().ok()) 28 } else {
31 } else { 29 None
32 None 30 }
33 }
34 })
35 } 31 }
36} 32}
37 33
@@ -142,10 +138,7 @@ impl ast::Path {
142 138
143impl ast::Module { 139impl ast::Module {
144 pub fn has_semi(&self) -> bool { 140 pub fn has_semi(&self) -> bool {
145 match self.syntax().last_child_or_token() { 141 self.semi().is_some()
146 None => false,
147 Some(node) => node.kind() == T![;],
148 }
149 } 142 }
150} 143}
151 144
@@ -181,7 +174,7 @@ impl ast::ImplDef {
181 } 174 }
182 175
183 pub fn is_negative(&self) -> bool { 176 pub fn is_negative(&self) -> bool {
184 self.syntax().children_with_tokens().any(|t| t.kind() == T![!]) 177 self.excl().is_some()
185 } 178 }
186} 179}
187 180
@@ -225,14 +218,11 @@ impl ast::EnumVariant {
225 218
226impl ast::FnDef { 219impl ast::FnDef {
227 pub fn semicolon_token(&self) -> Option<SyntaxToken> { 220 pub fn semicolon_token(&self) -> Option<SyntaxToken> {
228 self.syntax() 221 Some(self.semi()?.syntax().clone())
229 .last_child_or_token()
230 .and_then(|it| it.into_token())
231 .filter(|it| it.kind() == T![;])
232 } 222 }
233 223
234 pub fn is_async(&self) -> bool { 224 pub fn is_async(&self) -> bool {
235 self.syntax().children_with_tokens().any(|it| it.kind() == T![async]) 225 self.async_kw().is_some()
236 } 226 }
237} 227}
238 228
@@ -245,16 +235,13 @@ impl ast::LetStmt {
245 } 235 }
246 236
247 pub fn eq_token(&self) -> Option<SyntaxToken> { 237 pub fn eq_token(&self) -> Option<SyntaxToken> {
248 self.syntax().children_with_tokens().find(|t| t.kind() == EQ).and_then(|it| it.into_token()) 238 Some(self.eq()?.syntax().clone())
249 } 239 }
250} 240}
251 241
252impl ast::ExprStmt { 242impl ast::ExprStmt {
253 pub fn has_semi(&self) -> bool { 243 pub fn has_semi(&self) -> bool {
254 match self.syntax().last_child_or_token() { 244 self.semi().is_some()
255 None => false,
256 Some(node) => node.kind() == T![;],
257 }
258 } 245 }
259} 246}
260 247