aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/ast/extensions.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/ast/extensions.rs')
-rw-r--r--crates/ra_syntax/src/ast/extensions.rs48
1 files changed, 25 insertions, 23 deletions
diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs
index 11ec70bc0..1aacb0676 100644
--- a/crates/ra_syntax/src/ast/extensions.rs
+++ b/crates/ra_syntax/src/ast/extensions.rs
@@ -5,7 +5,7 @@ use itertools::Itertools;
5use ra_parser::SyntaxKind; 5use ra_parser::SyntaxKind;
6 6
7use crate::{ 7use crate::{
8 ast::{self, support, AstNode, AstToken, AttrInput, NameOwner, SyntaxNode}, 8 ast::{self, support, AstNode, AttrInput, NameOwner, SyntaxNode},
9 SmolStr, SyntaxElement, SyntaxToken, T, 9 SmolStr, SyntaxElement, SyntaxToken, T,
10}; 10};
11 11
@@ -21,11 +21,7 @@ impl ast::NameRef {
21 } 21 }
22 22
23 pub fn as_tuple_field(&self) -> Option<usize> { 23 pub fn as_tuple_field(&self) -> Option<usize> {
24 if let Some(ast::NameRefToken::IntNumber(token)) = self.name_ref_token_token() { 24 self.text().parse().ok()
25 token.text().as_str().parse().ok()
26 } else {
27 None
28 }
29 } 25 }
30} 26}
31 27
@@ -81,7 +77,7 @@ impl ast::Attr {
81 first_token.and_then(|token| token.next_token()).as_ref().map(SyntaxToken::kind); 77 first_token.and_then(|token| token.next_token()).as_ref().map(SyntaxToken::kind);
82 78
83 match (first_token_kind, second_token_kind) { 79 match (first_token_kind, second_token_kind) {
84 (Some(SyntaxKind::POUND), Some(SyntaxKind::EXCL)) => AttrKind::Inner, 80 (Some(SyntaxKind::POUND), Some(T![!])) => AttrKind::Inner,
85 _ => AttrKind::Outer, 81 _ => AttrKind::Outer,
86 } 82 }
87 } 83 }
@@ -315,7 +311,7 @@ pub enum TypeBoundKind {
315 /// for<'a> ... 311 /// for<'a> ...
316 ForType(ast::ForType), 312 ForType(ast::ForType),
317 /// 'a 313 /// 'a
318 Lifetime(ast::Lifetime), 314 Lifetime(SyntaxToken),
319} 315}
320 316
321impl ast::TypeBound { 317impl ast::TypeBound {
@@ -331,23 +327,23 @@ impl ast::TypeBound {
331 } 327 }
332 } 328 }
333 329
334 pub fn const_question_token(&self) -> Option<ast::Question> { 330 pub fn const_question_token(&self) -> Option<SyntaxToken> {
335 self.syntax() 331 self.syntax()
336 .children_with_tokens() 332 .children_with_tokens()
337 .filter_map(|it| it.into_token()) 333 .filter_map(|it| it.into_token())
338 .take_while(|it| it.kind() != T![const]) 334 .take_while(|it| it.kind() != T![const])
339 .find_map(ast::Question::cast) 335 .find(|it| it.kind() == T![?])
340 } 336 }
341 337
342 pub fn question_token(&self) -> Option<ast::Question> { 338 pub fn question_token(&self) -> Option<SyntaxToken> {
343 if self.const_token().is_some() { 339 if self.const_token().is_some() {
344 self.syntax() 340 self.syntax()
345 .children_with_tokens() 341 .children_with_tokens()
346 .filter_map(|it| it.into_token()) 342 .filter_map(|it| it.into_token())
347 .skip_while(|it| it.kind() != T![const]) 343 .skip_while(|it| it.kind() != T![const])
348 .find_map(ast::Question::cast) 344 .find(|it| it.kind() == T![?])
349 } else { 345 } else {
350 support::token(&self.syntax) 346 support::token(&self.syntax, T![?])
351 } 347 }
352 } 348 }
353} 349}
@@ -388,12 +384,12 @@ impl ast::MacroCall {
388} 384}
389 385
390impl ast::LifetimeParam { 386impl ast::LifetimeParam {
391 pub fn lifetime_bounds(&self) -> impl Iterator<Item = ast::Lifetime> { 387 pub fn lifetime_bounds(&self) -> impl Iterator<Item = SyntaxToken> {
392 self.syntax() 388 self.syntax()
393 .children_with_tokens() 389 .children_with_tokens()
394 .filter_map(|it| it.into_token()) 390 .filter_map(|it| it.into_token())
395 .skip_while(|x| x.kind() != T![:]) 391 .skip_while(|x| x.kind() != T![:])
396 .filter_map(ast::Lifetime::cast) 392 .filter(|it| it.kind() == T![lifetime])
397 } 393 }
398} 394}
399 395
@@ -401,7 +397,7 @@ impl ast::RangePat {
401 pub fn start(&self) -> Option<ast::Pat> { 397 pub fn start(&self) -> Option<ast::Pat> {
402 self.syntax() 398 self.syntax()
403 .children_with_tokens() 399 .children_with_tokens()
404 .take_while(|it| !ast::RangeSeparator::can_cast(it.kind())) 400 .take_while(|it| !(it.kind() == T![..] || it.kind() == T![..=]))
405 .filter_map(|it| it.into_node()) 401 .filter_map(|it| it.into_node())
406 .find_map(ast::Pat::cast) 402 .find_map(ast::Pat::cast)
407 } 403 }
@@ -409,18 +405,24 @@ impl ast::RangePat {
409 pub fn end(&self) -> Option<ast::Pat> { 405 pub fn end(&self) -> Option<ast::Pat> {
410 self.syntax() 406 self.syntax()
411 .children_with_tokens() 407 .children_with_tokens()
412 .skip_while(|it| !ast::RangeSeparator::can_cast(it.kind())) 408 .skip_while(|it| !(it.kind() == T![..] || it.kind() == T![..=]))
413 .filter_map(|it| it.into_node()) 409 .filter_map(|it| it.into_node())
414 .find_map(ast::Pat::cast) 410 .find_map(ast::Pat::cast)
415 } 411 }
416} 412}
417 413
418impl ast::TokenTree { 414impl ast::TokenTree {
419 pub fn left_delimiter(&self) -> Option<ast::LeftDelimiter> { 415 pub fn left_delimiter_token(&self) -> Option<SyntaxToken> {
420 self.syntax().first_child_or_token()?.into_token().and_then(ast::LeftDelimiter::cast) 416 self.syntax().first_child_or_token()?.into_token().filter(|it| match it.kind() {
421 } 417 T!['{'] | T!['('] | T!['['] => true,
422 418 _ => false,
423 pub fn right_delimiter(&self) -> Option<ast::RightDelimiter> { 419 })
424 self.syntax().last_child_or_token()?.into_token().and_then(ast::RightDelimiter::cast) 420 }
421
422 pub fn right_delimiter_token(&self) -> Option<SyntaxToken> {
423 self.syntax().last_child_or_token()?.into_token().filter(|it| match it.kind() {
424 T!['{'] | T!['('] | T!['['] => true,
425 _ => false,
426 })
425 } 427 }
426} 428}