Skip to content

Commit eb07803

Browse files
bors[bot]matklad
andauthored
Merge #3922
3922: Remove code duplication r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 33df208 + e07d3c9 commit eb07803

File tree

5 files changed

+49
-60
lines changed

5 files changed

+49
-60
lines changed

crates/ra_syntax/src/ast.rs

+17-25
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ pub trait AstNode {
4242
fn syntax(&self) -> &SyntaxNode;
4343
}
4444

45-
#[test]
46-
fn assert_ast_is_object_safe() {
47-
fn _f(_: &dyn AstNode, _: &dyn NameOwner) {}
48-
}
49-
5045
/// Like `AstNode`, but wraps tokens rather than interior nodes.
5146
pub trait AstToken {
5247
fn can_cast(token: SyntaxKind) -> bool
@@ -64,22 +59,6 @@ pub trait AstToken {
6459
}
6560
}
6661

67-
mod support {
68-
use super::{AstChildren, AstNode, AstToken, SyntaxNode};
69-
70-
pub(super) fn child<N: AstNode>(parent: &SyntaxNode) -> Option<N> {
71-
parent.children().find_map(N::cast)
72-
}
73-
74-
pub(super) fn children<N: AstNode>(parent: &SyntaxNode) -> AstChildren<N> {
75-
AstChildren::new(parent)
76-
}
77-
78-
pub(super) fn token<T: AstToken>(parent: &SyntaxNode) -> Option<T> {
79-
parent.children_with_tokens().filter_map(|it| it.into_token()).find_map(T::cast)
80-
}
81-
}
82-
8362
/// An iterator over `SyntaxNode` children of a particular AST type.
8463
#[derive(Debug, Clone)]
8564
pub struct AstChildren<N> {
@@ -100,12 +79,25 @@ impl<N: AstNode> Iterator for AstChildren<N> {
10079
}
10180
}
10281

103-
fn child_opt<P: AstNode + ?Sized, C: AstNode>(parent: &P) -> Option<C> {
104-
children(parent).next()
82+
mod support {
83+
use super::{AstChildren, AstNode, AstToken, SyntaxNode};
84+
85+
pub(super) fn child<N: AstNode>(parent: &SyntaxNode) -> Option<N> {
86+
parent.children().find_map(N::cast)
87+
}
88+
89+
pub(super) fn children<N: AstNode>(parent: &SyntaxNode) -> AstChildren<N> {
90+
AstChildren::new(parent)
91+
}
92+
93+
pub(super) fn token<T: AstToken>(parent: &SyntaxNode) -> Option<T> {
94+
parent.children_with_tokens().filter_map(|it| it.into_token()).find_map(T::cast)
95+
}
10596
}
10697

107-
fn children<P: AstNode + ?Sized, C: AstNode>(parent: &P) -> AstChildren<C> {
108-
AstChildren::new(parent.syntax())
98+
#[test]
99+
fn assert_ast_is_object_safe() {
100+
fn _f(_: &dyn AstNode, _: &dyn NameOwner) {}
109101
}
110102

111103
#[test]

crates/ra_syntax/src/ast/edit.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{iter, ops::RangeInclusive};
66
use arrayvec::ArrayVec;
77

88
use crate::{
9-
algo,
9+
algo::{self, neighbor, SyntaxRewriter},
1010
ast::{
1111
self,
1212
make::{self, tokens},
@@ -16,7 +16,6 @@ use crate::{
1616
SyntaxKind::{ATTR, COMMENT, WHITESPACE},
1717
SyntaxNode, SyntaxToken, T,
1818
};
19-
use algo::{neighbor, SyntaxRewriter};
2019

2120
impl ast::BinExpr {
2221
#[must_use]

crates/ra_syntax/src/ast/expr_extensions.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Various extension methods to ast Expr Nodes, which are hard to code-generate.
22
33
use crate::{
4-
ast::{self, child_opt, children, AstChildren, AstNode},
4+
ast::{self, support, AstChildren, AstNode},
55
SmolStr,
66
SyntaxKind::*,
77
SyntaxToken, T,
@@ -36,15 +36,15 @@ impl ast::IfExpr {
3636
let res = match self.blocks().nth(1) {
3737
Some(block) => ElseBranch::Block(block),
3838
None => {
39-
let elif: ast::IfExpr = child_opt(self)?;
39+
let elif: ast::IfExpr = support::child(self.syntax())?;
4040
ElseBranch::IfExpr(elif)
4141
}
4242
};
4343
Some(res)
4444
}
4545

4646
fn blocks(&self) -> AstChildren<ast::BlockExpr> {
47-
children(self)
47+
support::children(self.syntax())
4848
}
4949
}
5050

@@ -212,15 +212,15 @@ impl ast::BinExpr {
212212
}
213213

214214
pub fn lhs(&self) -> Option<ast::Expr> {
215-
children(self).next()
215+
support::children(self.syntax()).next()
216216
}
217217

218218
pub fn rhs(&self) -> Option<ast::Expr> {
219-
children(self).nth(1)
219+
support::children(self.syntax()).nth(1)
220220
}
221221

222222
pub fn sub_exprs(&self) -> (Option<ast::Expr>, Option<ast::Expr>) {
223-
let mut children = children(self);
223+
let mut children = support::children(self.syntax());
224224
let first = children.next();
225225
let second = children.next();
226226
(first, second)
@@ -275,10 +275,10 @@ impl ast::RangeExpr {
275275

276276
impl ast::IndexExpr {
277277
pub fn base(&self) -> Option<ast::Expr> {
278-
children(self).next()
278+
support::children(self.syntax()).next()
279279
}
280280
pub fn index(&self) -> Option<ast::Expr> {
281-
children(self).nth(1)
281+
support::children(self.syntax()).nth(1)
282282
}
283283
}
284284

@@ -291,11 +291,11 @@ impl ast::ArrayExpr {
291291
pub fn kind(&self) -> ArrayExprKind {
292292
if self.is_repeat() {
293293
ArrayExprKind::Repeat {
294-
initializer: children(self).next(),
295-
repeat: children(self).nth(1),
294+
initializer: support::children(self.syntax()).next(),
295+
repeat: support::children(self.syntax()).nth(1),
296296
}
297297
} else {
298-
ArrayExprKind::ElementList(children(self))
298+
ArrayExprKind::ElementList(support::children(self.syntax()))
299299
}
300300
}
301301

crates/ra_syntax/src/ast/extensions.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ use itertools::Itertools;
55
use ra_parser::SyntaxKind;
66

77
use crate::{
8-
ast::{
9-
self, child_opt, children, support, AstNode, AstToken, AttrInput, NameOwner, SyntaxNode,
10-
},
8+
ast::{self, support, AstNode, AstToken, AttrInput, NameOwner, SyntaxNode},
119
SmolStr, SyntaxElement, SyntaxToken, T,
1210
};
1311

@@ -161,7 +159,7 @@ impl ast::ImplDef {
161159
}
162160

163161
fn target(&self) -> (Option<ast::TypeRef>, Option<ast::TypeRef>) {
164-
let mut types = children(self);
162+
let mut types = support::children(self.syntax());
165163
let first = types.next();
166164
let second = types.next();
167165
(first, second)
@@ -177,9 +175,9 @@ pub enum StructKind {
177175

178176
impl StructKind {
179177
fn from_node<N: AstNode>(node: &N) -> StructKind {
180-
if let Some(nfdl) = child_opt::<_, ast::RecordFieldDefList>(node) {
178+
if let Some(nfdl) = support::child::<ast::RecordFieldDefList>(node.syntax()) {
181179
StructKind::Record(nfdl)
182-
} else if let Some(pfl) = child_opt::<_, ast::TupleFieldDefList>(node) {
180+
} else if let Some(pfl) = support::child::<ast::TupleFieldDefList>(node.syntax()) {
183181
StructKind::Tuple(pfl)
184182
} else {
185183
StructKind::Unit
@@ -322,9 +320,9 @@ pub enum TypeBoundKind {
322320

323321
impl ast::TypeBound {
324322
pub fn kind(&self) -> TypeBoundKind {
325-
if let Some(path_type) = children(self).next() {
323+
if let Some(path_type) = support::children(self.syntax()).next() {
326324
TypeBoundKind::PathType(path_type)
327-
} else if let Some(for_type) = children(self).next() {
325+
} else if let Some(for_type) = support::children(self.syntax()).next() {
328326
TypeBoundKind::ForType(for_type)
329327
} else if let Some(lifetime) = self.lifetime_token() {
330328
TypeBoundKind::Lifetime(lifetime)
@@ -364,7 +362,7 @@ pub enum VisibilityKind {
364362

365363
impl ast::Visibility {
366364
pub fn kind(&self) -> VisibilityKind {
367-
if let Some(path) = children(self).next() {
365+
if let Some(path) = support::children(self.syntax()).next() {
368366
VisibilityKind::In(path)
369367
} else if self.crate_kw_token().is_some() {
370368
VisibilityKind::PubCrate

crates/ra_syntax/src/ast/traits.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -5,69 +5,69 @@
55
use itertools::Itertools;
66

77
use crate::{
8-
ast::{self, child_opt, children, support, AstChildren, AstNode, AstToken},
8+
ast::{self, support, AstChildren, AstNode, AstToken},
99
syntax_node::SyntaxElementChildren,
1010
};
1111

1212
pub trait TypeAscriptionOwner: AstNode {
1313
fn ascribed_type(&self) -> Option<ast::TypeRef> {
14-
child_opt(self)
14+
support::child(self.syntax())
1515
}
1616
}
1717

1818
pub trait NameOwner: AstNode {
1919
fn name(&self) -> Option<ast::Name> {
20-
child_opt(self)
20+
support::child(self.syntax())
2121
}
2222
}
2323

2424
pub trait VisibilityOwner: AstNode {
2525
fn visibility(&self) -> Option<ast::Visibility> {
26-
child_opt(self)
26+
support::child(self.syntax())
2727
}
2828
}
2929

3030
pub trait LoopBodyOwner: AstNode {
3131
fn loop_body(&self) -> Option<ast::BlockExpr> {
32-
child_opt(self)
32+
support::child(self.syntax())
3333
}
3434

3535
fn label(&self) -> Option<ast::Label> {
36-
child_opt(self)
36+
support::child(self.syntax())
3737
}
3838
}
3939

4040
pub trait ArgListOwner: AstNode {
4141
fn arg_list(&self) -> Option<ast::ArgList> {
42-
child_opt(self)
42+
support::child(self.syntax())
4343
}
4444
}
4545

4646
pub trait FnDefOwner: AstNode {
4747
fn functions(&self) -> AstChildren<ast::FnDef> {
48-
children(self)
48+
support::children(self.syntax())
4949
}
5050
}
5151

5252
pub trait ModuleItemOwner: AstNode {
5353
fn items(&self) -> AstChildren<ast::ModuleItem> {
54-
children(self)
54+
support::children(self.syntax())
5555
}
5656
}
5757

5858
pub trait TypeParamsOwner: AstNode {
5959
fn type_param_list(&self) -> Option<ast::TypeParamList> {
60-
child_opt(self)
60+
support::child(self.syntax())
6161
}
6262

6363
fn where_clause(&self) -> Option<ast::WhereClause> {
64-
child_opt(self)
64+
support::child(self.syntax())
6565
}
6666
}
6767

6868
pub trait TypeBoundsOwner: AstNode {
6969
fn type_bound_list(&self) -> Option<ast::TypeBoundList> {
70-
child_opt(self)
70+
support::child(self.syntax())
7171
}
7272

7373
fn colon(&self) -> Option<ast::Colon> {
@@ -77,7 +77,7 @@ pub trait TypeBoundsOwner: AstNode {
7777

7878
pub trait AttrsOwner: AstNode {
7979
fn attrs(&self) -> AstChildren<ast::Attr> {
80-
children(self)
80+
support::children(self.syntax())
8181
}
8282
fn has_atom_attr(&self, atom: &str) -> bool {
8383
self.attrs().filter_map(|x| x.as_simple_atom()).any(|x| x == atom)

0 commit comments

Comments
 (0)