From 69a524fbef067d9ee265fa93606c3ae743656c2a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 22 Aug 2018 17:01:51 +0300 Subject: dedupe --- crates/libsyntax2/src/ast/generated.rs | 21 ++++---------- crates/libsyntax2/src/ast/generated.rs.tera | 9 ++---- crates/libsyntax2/src/ast/mod.rs | 43 ++++++++++++++--------------- 3 files changed, 28 insertions(+), 45 deletions(-) (limited to 'crates') diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs index d0e07fddc..0f53e8f4a 100644 --- a/crates/libsyntax2/src/ast/generated.rs +++ b/crates/libsyntax2/src/ast/generated.rs @@ -40,10 +40,7 @@ impl<'a> AstNode<'a> for Attr<'a> { impl<'a> Attr<'a> { pub fn value(self) -> Option> { - self.syntax() - .children() - .filter_map(TokenTree::cast) - .next() + super::child_opt(self) } } @@ -125,15 +122,11 @@ impl<'a> AstNode<'a> for File<'a> { impl<'a> File<'a> { pub fn functions(self) -> impl Iterator> + 'a { - self.syntax() - .children() - .filter_map(FnDef::cast) + super::children(self) } pub fn modules(self) -> impl Iterator> + 'a { - self.syntax() - .children() - .filter_map(Module::cast) + super::children(self) } } @@ -250,9 +243,7 @@ impl<'a> ast::NameOwner<'a> for Module<'a> {} impl<'a> ast::AttrsOwner<'a> for Module<'a> {} impl<'a> Module<'a> { pub fn modules(self) -> impl Iterator> + 'a { - self.syntax() - .children() - .filter_map(Module::cast) + super::children(self) } } @@ -507,9 +498,7 @@ impl<'a> ast::TypeParamsOwner<'a> for StructDef<'a> {} impl<'a> ast::AttrsOwner<'a> for StructDef<'a> {} impl<'a> StructDef<'a> { pub fn fields(self) -> impl Iterator> + 'a { - self.syntax() - .children() - .filter_map(NamedField::cast) + super::children(self) } } diff --git a/crates/libsyntax2/src/ast/generated.rs.tera b/crates/libsyntax2/src/ast/generated.rs.tera index 0572cceaa..69f9236b7 100644 --- a/crates/libsyntax2/src/ast/generated.rs.tera +++ b/crates/libsyntax2/src/ast/generated.rs.tera @@ -58,9 +58,7 @@ impl<'a> {{ node }}<'a> { {%- set method_name = m.0 -%} {%- set ChildName = m.1 %} pub fn {{ method_name }}(self) -> impl Iterator> + 'a { - self.syntax() - .children() - .filter_map({{ ChildName }}::cast) + super::children(self) } {% endfor -%} {%- endif -%} @@ -70,10 +68,7 @@ impl<'a> {{ node }}<'a> { {%- set method_name = m.0 -%} {%- set ChildName = m.1 %} pub fn {{ method_name }}(self) -> Option<{{ ChildName }}<'a>> { - self.syntax() - .children() - .filter_map({{ ChildName }}::cast) - .next() + super::child_opt(self) } {% endfor -%} {%- endif -%} diff --git a/crates/libsyntax2/src/ast/mod.rs b/crates/libsyntax2/src/ast/mod.rs index 1784b871e..ba80fc64e 100644 --- a/crates/libsyntax2/src/ast/mod.rs +++ b/crates/libsyntax2/src/ast/mod.rs @@ -9,7 +9,7 @@ use { }; pub use self::generated::*; -pub trait AstNode<'a>: Clone + Copy { +pub trait AstNode<'a>: Clone + Copy + 'a { fn cast(syntax: SyntaxNodeRef<'a>) -> Option where Self: Sized; fn syntax(self) -> SyntaxNodeRef<'a>; @@ -17,34 +17,23 @@ pub trait AstNode<'a>: Clone + Copy { pub trait NameOwner<'a>: AstNode<'a> { fn name(self) -> Option> { - self.syntax() - .children() - .filter_map(Name::cast) - .next() + child_opt(self) } } pub trait TypeParamsOwner<'a>: AstNode<'a> { fn type_param_list(self) -> Option> { - self.syntax() - .children() - .filter_map(TypeParamList::cast) - .next() + child_opt(self) } fn where_clause(self) -> Option> { - self.syntax() - .children() - .filter_map(WhereClause::cast) - .next() + child_opt(self) } } pub trait AttrsOwner<'a>: AstNode<'a> { - fn attrs(&self) -> Box> + 'a> { - let it = self.syntax().children() - .filter_map(Attr::cast); - Box::new(it) + fn attrs(self) -> Box> + 'a> { + Box::new(children(self)) } } @@ -118,22 +107,22 @@ impl<'a> NameRef<'a> { } impl<'a> ImplItem<'a> { - pub fn target_type(&self) -> Option> { + pub fn target_type(self) -> Option> { match self.target() { (Some(t), None) | (_, Some(t)) => Some(t), _ => None, } } - pub fn target_trait(&self) -> Option> { + pub fn target_trait(self) -> Option> { match self.target() { (Some(t), Some(_)) => Some(t), _ => None, } } - fn target(&self) -> (Option>, Option>) { - let mut types = self.syntax().children().filter_map(TypeRef::cast); + fn target(self) -> (Option>, Option>) { + let mut types = children(self); let first = types.next(); let second = types.next(); (first, second) @@ -141,10 +130,20 @@ impl<'a> ImplItem<'a> { } impl<'a> Module<'a> { - pub fn has_semi(&self) -> bool { + pub fn has_semi(self) -> bool { match self.syntax().last_child() { None => false, Some(node) => node.kind() == SEMI, } } } + +fn child_opt<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> Option { + children(parent).next() +} + +fn children<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> impl Iterator + 'a { + parent.syntax() + .children() + .filter_map(C::cast) +} -- cgit v1.2.3