From e07d3c94de4694f38aa87316018c0d4cf28be941 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 9 Apr 2020 22:22:58 +0200 Subject: Remove code duplication --- crates/ra_syntax/src/ast.rs | 42 ++++++++++++----------------- crates/ra_syntax/src/ast/edit.rs | 3 +-- crates/ra_syntax/src/ast/expr_extensions.rs | 22 +++++++-------- crates/ra_syntax/src/ast/extensions.rs | 16 +++++------ crates/ra_syntax/src/ast/traits.rs | 26 +++++++++--------- 5 files changed, 49 insertions(+), 60 deletions(-) diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 15a8279f3..cb701f7f6 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -42,11 +42,6 @@ pub trait AstNode { fn syntax(&self) -> &SyntaxNode; } -#[test] -fn assert_ast_is_object_safe() { - fn _f(_: &dyn AstNode, _: &dyn NameOwner) {} -} - /// Like `AstNode`, but wraps tokens rather than interior nodes. pub trait AstToken { fn can_cast(token: SyntaxKind) -> bool @@ -64,22 +59,6 @@ pub trait AstToken { } } -mod support { - use super::{AstChildren, AstNode, AstToken, SyntaxNode}; - - pub(super) fn child(parent: &SyntaxNode) -> Option { - parent.children().find_map(N::cast) - } - - pub(super) fn children(parent: &SyntaxNode) -> AstChildren { - AstChildren::new(parent) - } - - pub(super) fn token(parent: &SyntaxNode) -> Option { - parent.children_with_tokens().filter_map(|it| it.into_token()).find_map(T::cast) - } -} - /// An iterator over `SyntaxNode` children of a particular AST type. #[derive(Debug, Clone)] pub struct AstChildren { @@ -100,12 +79,25 @@ impl Iterator for AstChildren { } } -fn child_opt(parent: &P) -> Option { - children(parent).next() +mod support { + use super::{AstChildren, AstNode, AstToken, SyntaxNode}; + + pub(super) fn child(parent: &SyntaxNode) -> Option { + parent.children().find_map(N::cast) + } + + pub(super) fn children(parent: &SyntaxNode) -> AstChildren { + AstChildren::new(parent) + } + + pub(super) fn token(parent: &SyntaxNode) -> Option { + parent.children_with_tokens().filter_map(|it| it.into_token()).find_map(T::cast) + } } -fn children(parent: &P) -> AstChildren { - AstChildren::new(parent.syntax()) +#[test] +fn assert_ast_is_object_safe() { + fn _f(_: &dyn AstNode, _: &dyn NameOwner) {} } #[test] diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index 069c6ee82..3d428fab3 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs @@ -6,7 +6,7 @@ use std::{iter, ops::RangeInclusive}; use arrayvec::ArrayVec; use crate::{ - algo, + algo::{self, neighbor, SyntaxRewriter}, ast::{ self, make::{self, tokens}, @@ -16,7 +16,6 @@ use crate::{ SyntaxKind::{ATTR, COMMENT, WHITESPACE}, SyntaxNode, SyntaxToken, T, }; -use algo::{neighbor, SyntaxRewriter}; impl ast::BinExpr { #[must_use] diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index 40c8fca3b..003ee00b3 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs @@ -1,7 +1,7 @@ //! Various extension methods to ast Expr Nodes, which are hard to code-generate. use crate::{ - ast::{self, child_opt, children, AstChildren, AstNode}, + ast::{self, support, AstChildren, AstNode}, SmolStr, SyntaxKind::*, SyntaxToken, T, @@ -36,7 +36,7 @@ impl ast::IfExpr { let res = match self.blocks().nth(1) { Some(block) => ElseBranch::Block(block), None => { - let elif: ast::IfExpr = child_opt(self)?; + let elif: ast::IfExpr = support::child(self.syntax())?; ElseBranch::IfExpr(elif) } }; @@ -44,7 +44,7 @@ impl ast::IfExpr { } fn blocks(&self) -> AstChildren { - children(self) + support::children(self.syntax()) } } @@ -212,15 +212,15 @@ impl ast::BinExpr { } pub fn lhs(&self) -> Option { - children(self).next() + support::children(self.syntax()).next() } pub fn rhs(&self) -> Option { - children(self).nth(1) + support::children(self.syntax()).nth(1) } pub fn sub_exprs(&self) -> (Option, Option) { - let mut children = children(self); + let mut children = support::children(self.syntax()); let first = children.next(); let second = children.next(); (first, second) @@ -275,10 +275,10 @@ impl ast::RangeExpr { impl ast::IndexExpr { pub fn base(&self) -> Option { - children(self).next() + support::children(self.syntax()).next() } pub fn index(&self) -> Option { - children(self).nth(1) + support::children(self.syntax()).nth(1) } } @@ -291,11 +291,11 @@ impl ast::ArrayExpr { pub fn kind(&self) -> ArrayExprKind { if self.is_repeat() { ArrayExprKind::Repeat { - initializer: children(self).next(), - repeat: children(self).nth(1), + initializer: support::children(self.syntax()).next(), + repeat: support::children(self.syntax()).nth(1), } } else { - ArrayExprKind::ElementList(children(self)) + ArrayExprKind::ElementList(support::children(self.syntax())) } } diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index b50a89864..fc252e79c 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs @@ -5,9 +5,7 @@ use itertools::Itertools; use ra_parser::SyntaxKind; use crate::{ - ast::{ - self, child_opt, children, support, AstNode, AstToken, AttrInput, NameOwner, SyntaxNode, - }, + ast::{self, support, AstNode, AstToken, AttrInput, NameOwner, SyntaxNode}, SmolStr, SyntaxElement, SyntaxToken, T, }; @@ -161,7 +159,7 @@ impl ast::ImplDef { } fn target(&self) -> (Option, Option) { - let mut types = children(self); + let mut types = support::children(self.syntax()); let first = types.next(); let second = types.next(); (first, second) @@ -177,9 +175,9 @@ pub enum StructKind { impl StructKind { fn from_node(node: &N) -> StructKind { - if let Some(nfdl) = child_opt::<_, ast::RecordFieldDefList>(node) { + if let Some(nfdl) = support::child::(node.syntax()) { StructKind::Record(nfdl) - } else if let Some(pfl) = child_opt::<_, ast::TupleFieldDefList>(node) { + } else if let Some(pfl) = support::child::(node.syntax()) { StructKind::Tuple(pfl) } else { StructKind::Unit @@ -322,9 +320,9 @@ pub enum TypeBoundKind { impl ast::TypeBound { pub fn kind(&self) -> TypeBoundKind { - if let Some(path_type) = children(self).next() { + if let Some(path_type) = support::children(self.syntax()).next() { TypeBoundKind::PathType(path_type) - } else if let Some(for_type) = children(self).next() { + } else if let Some(for_type) = support::children(self.syntax()).next() { TypeBoundKind::ForType(for_type) } else if let Some(lifetime) = self.lifetime_token() { TypeBoundKind::Lifetime(lifetime) @@ -364,7 +362,7 @@ pub enum VisibilityKind { impl ast::Visibility { pub fn kind(&self) -> VisibilityKind { - if let Some(path) = children(self).next() { + if let Some(path) = support::children(self.syntax()).next() { VisibilityKind::In(path) } else if self.crate_kw_token().is_some() { VisibilityKind::PubCrate diff --git a/crates/ra_syntax/src/ast/traits.rs b/crates/ra_syntax/src/ast/traits.rs index 870e83804..f6c786e44 100644 --- a/crates/ra_syntax/src/ast/traits.rs +++ b/crates/ra_syntax/src/ast/traits.rs @@ -5,69 +5,69 @@ use itertools::Itertools; use crate::{ - ast::{self, child_opt, children, support, AstChildren, AstNode, AstToken}, + ast::{self, support, AstChildren, AstNode, AstToken}, syntax_node::SyntaxElementChildren, }; pub trait TypeAscriptionOwner: AstNode { fn ascribed_type(&self) -> Option { - child_opt(self) + support::child(self.syntax()) } } pub trait NameOwner: AstNode { fn name(&self) -> Option { - child_opt(self) + support::child(self.syntax()) } } pub trait VisibilityOwner: AstNode { fn visibility(&self) -> Option { - child_opt(self) + support::child(self.syntax()) } } pub trait LoopBodyOwner: AstNode { fn loop_body(&self) -> Option { - child_opt(self) + support::child(self.syntax()) } fn label(&self) -> Option { - child_opt(self) + support::child(self.syntax()) } } pub trait ArgListOwner: AstNode { fn arg_list(&self) -> Option { - child_opt(self) + support::child(self.syntax()) } } pub trait FnDefOwner: AstNode { fn functions(&self) -> AstChildren { - children(self) + support::children(self.syntax()) } } pub trait ModuleItemOwner: AstNode { fn items(&self) -> AstChildren { - children(self) + support::children(self.syntax()) } } pub trait TypeParamsOwner: AstNode { fn type_param_list(&self) -> Option { - child_opt(self) + support::child(self.syntax()) } fn where_clause(&self) -> Option { - child_opt(self) + support::child(self.syntax()) } } pub trait TypeBoundsOwner: AstNode { fn type_bound_list(&self) -> Option { - child_opt(self) + support::child(self.syntax()) } fn colon(&self) -> Option { @@ -77,7 +77,7 @@ pub trait TypeBoundsOwner: AstNode { pub trait AttrsOwner: AstNode { fn attrs(&self) -> AstChildren { - children(self) + support::children(self.syntax()) } fn has_atom_attr(&self, atom: &str) -> bool { self.attrs().filter_map(|x| x.as_simple_atom()).any(|x| x == atom) -- cgit v1.2.3