From a95116fbfa11cad4e03b8b31f8d4498f3ddd5d9e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 9 Apr 2020 18:20:06 +0200 Subject: Simplify --- crates/ra_syntax/src/ast/extensions.rs | 39 ++++++++++++---------------------- 1 file changed, 13 insertions(+), 26 deletions(-) (limited to 'crates/ra_syntax/src/ast/extensions.rs') 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 @@ //! Extensions for various expressions live in a sibling `expr_extensions` module. use itertools::Itertools; +use ra_parser::SyntaxKind; use crate::{ ast::{ self, child_opt, children, support, AstNode, AstToken, AttrInput, NameOwner, SyntaxNode, }, - SmolStr, SyntaxElement, - SyntaxKind::*, - SyntaxToken, T, + SmolStr, SyntaxElement, SyntaxToken, T, }; -use ra_parser::SyntaxKind; impl ast::Name { pub fn text(&self) -> &SmolStr { @@ -25,13 +23,11 @@ impl ast::NameRef { } pub fn as_tuple_field(&self) -> Option { - self.syntax().children_with_tokens().find_map(|c| { - if c.kind() == SyntaxKind::INT_NUMBER { - c.as_token().and_then(|tok| tok.text().as_str().parse().ok()) - } else { - None - } - }) + if let Some(ast::NameRefToken::IntNumber(token)) = self.name_ref_token() { + token.text().as_str().parse().ok() + } else { + None + } } } @@ -142,10 +138,7 @@ impl ast::Path { impl ast::Module { pub fn has_semi(&self) -> bool { - match self.syntax().last_child_or_token() { - None => false, - Some(node) => node.kind() == T![;], - } + self.semi().is_some() } } @@ -181,7 +174,7 @@ impl ast::ImplDef { } 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 { pub fn semicolon_token(&self) -> Option { - self.syntax() - .last_child_or_token() - .and_then(|it| it.into_token()) - .filter(|it| it.kind() == T![;]) + Some(self.semi()?.syntax().clone()) } 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 { - self.syntax().children_with_tokens().find(|t| t.kind() == EQ).and_then(|it| it.into_token()) + Some(self.eq()?.syntax().clone()) } } impl ast::ExprStmt { pub fn has_semi(&self) -> bool { - match self.syntax().last_child_or_token() { - None => false, - Some(node) => node.kind() == T![;], - } + self.semi().is_some() } } -- cgit v1.2.3 From e6d22187a67e762bb950de244a6ca15f3a0b0731 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 9 Apr 2020 18:25:36 +0200 Subject: Add _token suffix to token accessors I think this makes is more clear which things are : AstNode and which are : AstToken --- crates/ra_syntax/src/ast/extensions.rs | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) (limited to 'crates/ra_syntax/src/ast/extensions.rs') diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index c7df15662..ff3525c84 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs @@ -23,7 +23,7 @@ impl ast::NameRef { } pub fn as_tuple_field(&self) -> Option { - if let Some(ast::NameRefToken::IntNumber(token)) = self.name_ref_token() { + if let Some(ast::NameRefToken::IntNumber(token)) = self.name_ref_token_token() { token.text().as_str().parse().ok() } else { None @@ -138,7 +138,7 @@ impl ast::Path { impl ast::Module { pub fn has_semi(&self) -> bool { - self.semi().is_some() + self.semi_token().is_some() } } @@ -174,7 +174,7 @@ impl ast::ImplDef { } pub fn is_negative(&self) -> bool { - self.excl().is_some() + self.excl_token().is_some() } } @@ -218,11 +218,11 @@ impl ast::EnumVariant { impl ast::FnDef { pub fn semicolon_token(&self) -> Option { - Some(self.semi()?.syntax().clone()) + Some(self.semi_token()?.syntax().clone()) } pub fn is_async(&self) -> bool { - self.async_kw().is_some() + self.async_kw_token().is_some() } } @@ -233,15 +233,11 @@ impl ast::LetStmt { Some(node) => node.kind() == T![;], } } - - pub fn eq_token(&self) -> Option { - Some(self.eq()?.syntax().clone()) - } } impl ast::ExprStmt { pub fn has_semi(&self) -> bool { - self.semi().is_some() + self.semi_token().is_some() } } @@ -350,7 +346,7 @@ pub enum SelfParamKind { impl ast::SelfParam { pub fn kind(&self) -> SelfParamKind { - if self.amp().is_some() { + if self.amp_token().is_some() { if self.amp_mut_kw().is_some() { SelfParamKind::MutRef } else { @@ -396,7 +392,7 @@ impl ast::TypeBound { TypeBoundKind::PathType(path_type) } else if let Some(for_type) = children(self).next() { TypeBoundKind::ForType(for_type) - } else if let Some(lifetime) = self.lifetime() { + } else if let Some(lifetime) = self.lifetime_token() { TypeBoundKind::Lifetime(lifetime) } else { unreachable!() @@ -416,7 +412,7 @@ impl ast::TypeBound { } pub fn question(&self) -> Option { - if self.const_kw().is_some() { + if self.const_kw_token().is_some() { self.syntax() .children_with_tokens() .filter_map(|it| it.into_token()) -- cgit v1.2.3 From 2bfb65db93e48d8f9e8ecac0b2ea837c081a4db5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 9 Apr 2020 18:40:43 +0200 Subject: Be consistent about token accesors --- crates/ra_syntax/src/ast/extensions.rs | 104 +++------------------------------ 1 file changed, 8 insertions(+), 96 deletions(-) (limited to 'crates/ra_syntax/src/ast/extensions.rs') diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index ff3525c84..b50a89864 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs @@ -136,12 +136,6 @@ impl ast::Path { } } -impl ast::Module { - pub fn has_semi(&self) -> bool { - self.semi_token().is_some() - } -} - impl ast::UseTreeList { pub fn parent_use_tree(&self) -> ast::UseTree { self.syntax() @@ -172,10 +166,6 @@ impl ast::ImplDef { let second = types.next(); (first, second) } - - pub fn is_negative(&self) -> bool { - self.excl_token().is_some() - } } #[derive(Debug, Clone, PartialEq, Eq)] @@ -216,31 +206,6 @@ impl ast::EnumVariant { } } -impl ast::FnDef { - pub fn semicolon_token(&self) -> Option { - Some(self.semi_token()?.syntax().clone()) - } - - pub fn is_async(&self) -> bool { - self.async_kw_token().is_some() - } -} - -impl ast::LetStmt { - pub fn has_semi(&self) -> bool { - match self.syntax().last_child_or_token() { - None => false, - Some(node) => node.kind() == T![;], - } - } -} - -impl ast::ExprStmt { - pub fn has_semi(&self) -> bool { - self.semi_token().is_some() - } -} - #[derive(Debug, Clone, PartialEq, Eq)] pub enum FieldKind { Name(ast::NameRef), @@ -269,25 +234,6 @@ impl ast::FieldExpr { } } -impl ast::RefPat { - pub fn is_mut(&self) -> bool { - self.syntax().children_with_tokens().any(|n| n.kind() == T![mut]) - } -} - -impl ast::BindPat { - pub fn is_mutable(&self) -> bool { - self.syntax().children_with_tokens().any(|n| n.kind() == T![mut]) - } - - pub fn is_ref(&self) -> bool { - self.syntax().children_with_tokens().any(|n| n.kind() == T![ref]) - } - pub fn has_at(&self) -> bool { - self.syntax().children_with_tokens().any(|it| it.kind() == T![@]) - } -} - pub struct SlicePatComponents { pub prefix: Vec, pub slice: Option, @@ -322,18 +268,6 @@ impl ast::SlicePat { } } -impl ast::PointerType { - pub fn is_mut(&self) -> bool { - self.syntax().children_with_tokens().any(|n| n.kind() == T![mut]) - } -} - -impl ast::ReferenceType { - pub fn is_mut(&self) -> bool { - self.syntax().children_with_tokens().any(|n| n.kind() == T![mut]) - } -} - #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] pub enum SelfParamKind { /// self @@ -347,7 +281,7 @@ pub enum SelfParamKind { impl ast::SelfParam { pub fn kind(&self) -> SelfParamKind { if self.amp_token().is_some() { - if self.amp_mut_kw().is_some() { + if self.amp_mut_kw_token().is_some() { SelfParamKind::MutRef } else { SelfParamKind::Ref @@ -358,7 +292,7 @@ impl ast::SelfParam { } /// the "mut" in "mut self", not the one in "&mut self" - pub fn mut_kw(&self) -> Option { + pub fn mut_kw_token(&self) -> Option { self.syntax() .children_with_tokens() .filter_map(|it| it.into_token()) @@ -367,7 +301,7 @@ impl ast::SelfParam { } /// the "mut" in "&mut self", not the one in "mut self" - pub fn amp_mut_kw(&self) -> Option { + pub fn amp_mut_kw_token(&self) -> Option { self.syntax() .children_with_tokens() .filter_map(|it| it.into_token()) @@ -399,11 +333,7 @@ impl ast::TypeBound { } } - pub fn has_question_mark(&self) -> bool { - self.question().is_some() - } - - pub fn const_question(&self) -> Option { + pub fn const_question_token(&self) -> Option { self.syntax() .children_with_tokens() .filter_map(|it| it.into_token()) @@ -411,7 +341,7 @@ impl ast::TypeBound { .find_map(ast::Question::cast) } - pub fn question(&self) -> Option { + pub fn question_token(&self) -> Option { if self.const_kw_token().is_some() { self.syntax() .children_with_tokens() @@ -424,12 +354,6 @@ impl ast::TypeBound { } } -impl ast::TraitDef { - pub fn is_auto(&self) -> bool { - self.syntax().children_with_tokens().any(|t| t.kind() == T![auto]) - } -} - pub enum VisibilityKind { In(ast::Path), PubCrate, @@ -442,28 +366,16 @@ impl ast::Visibility { pub fn kind(&self) -> VisibilityKind { if let Some(path) = children(self).next() { VisibilityKind::In(path) - } else if self.is_pub_crate() { + } else if self.crate_kw_token().is_some() { VisibilityKind::PubCrate - } else if self.is_pub_super() { + } else if self.super_kw_token().is_some() { VisibilityKind::PubSuper - } else if self.is_pub_self() { + } else if self.self_kw_token().is_some() { VisibilityKind::PubSuper } else { VisibilityKind::Pub } } - - fn is_pub_crate(&self) -> bool { - self.syntax().children_with_tokens().any(|it| it.kind() == T![crate]) - } - - fn is_pub_super(&self) -> bool { - self.syntax().children_with_tokens().any(|it| it.kind() == T![super]) - } - - fn is_pub_self(&self) -> bool { - self.syntax().children_with_tokens().any(|it| it.kind() == T![self]) - } } impl ast::MacroCall { -- cgit v1.2.3