Skip to content

Commit a123502

Browse files
committed
Derive Default for Syntax{Node,Element}Children
1 parent 0c1077e commit a123502

File tree

2 files changed

+32
-61
lines changed

2 files changed

+32
-61
lines changed

src/api.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ impl<L: Language> SyntaxElement<L> {
431431
}
432432
}
433433

434-
#[derive(Debug, Clone)]
434+
#[derive(Debug, Clone, Default)]
435435
pub struct SyntaxNodeChildren<L: Language> {
436436
raw: cursor::SyntaxNodeChildren,
437437
_p: PhantomData<L>,
@@ -450,7 +450,7 @@ impl<L: Language> SyntaxNodeChildren<L> {
450450
}
451451
}
452452

453-
#[derive(Debug, Clone)]
453+
#[derive(Debug, Clone, Default)]
454454
pub struct SyntaxElementChildren<L: Language> {
455455
raw: cursor::SyntaxElementChildren,
456456
_p: PhantomData<L>,

src/cursor.rs

Lines changed: 30 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,47 +1353,32 @@ impl From<SyntaxToken> for SyntaxElement {
13531353

13541354
// region: iterators
13551355

1356-
#[derive(Clone, Debug)]
1356+
#[derive(Clone, Debug, Default)]
13571357
pub struct SyntaxNodeChildren {
1358-
parent: SyntaxNode,
13591358
next: Option<SyntaxNode>,
1360-
next_initialized: bool,
13611359
}
13621360

13631361
impl SyntaxNodeChildren {
13641362
fn new(parent: SyntaxNode) -> SyntaxNodeChildren {
1365-
SyntaxNodeChildren { parent, next: None, next_initialized: false }
1363+
SyntaxNodeChildren { next: parent.first_child() }
13661364
}
13671365

13681366
pub fn by_kind<F: Fn(SyntaxKind) -> bool>(self, matcher: F) -> SyntaxNodeChildrenByKind<F> {
1369-
if !self.next_initialized {
1370-
SyntaxNodeChildrenByKind { next: self.parent.first_child_by_kind(&matcher), matcher }
1371-
} else {
1372-
SyntaxNodeChildrenByKind {
1373-
next: self.next.and_then(|node| {
1374-
if matcher(node.kind()) {
1375-
Some(node)
1376-
} else {
1377-
node.next_sibling_by_kind(&matcher)
1378-
}
1379-
}),
1380-
matcher,
1381-
}
1367+
SyntaxNodeChildrenByKind {
1368+
next: self.next.and_then(|node| {
1369+
if matcher(node.kind()) { Some(node) } else { node.next_sibling_by_kind(&matcher) }
1370+
}),
1371+
matcher,
13821372
}
13831373
}
13841374
}
13851375

13861376
impl Iterator for SyntaxNodeChildren {
13871377
type Item = SyntaxNode;
13881378
fn next(&mut self) -> Option<SyntaxNode> {
1389-
if !self.next_initialized {
1390-
self.next = self.parent.first_child();
1391-
self.next_initialized = true;
1392-
} else {
1393-
self.next = self.next.take().and_then(|next| next.to_next_sibling());
1394-
}
1395-
1396-
self.next.clone()
1379+
let curr = self.next.take()?;
1380+
self.next = curr.next_sibling();
1381+
Some(curr)
13971382
}
13981383
}
13991384

@@ -1406,56 +1391,42 @@ pub struct SyntaxNodeChildrenByKind<F: Fn(SyntaxKind) -> bool> {
14061391
impl<F: Fn(SyntaxKind) -> bool> Iterator for SyntaxNodeChildrenByKind<F> {
14071392
type Item = SyntaxNode;
14081393
fn next(&mut self) -> Option<SyntaxNode> {
1409-
self.next.take().inspect(|next| {
1410-
self.next = next.next_sibling_by_kind(&self.matcher);
1411-
})
1394+
let curr = self.next.take()?;
1395+
self.next = curr.next_sibling_by_kind(&self.matcher);
1396+
Some(curr)
14121397
}
14131398
}
14141399

1415-
#[derive(Clone, Debug)]
1400+
#[derive(Clone, Debug, Default)]
14161401
pub struct SyntaxElementChildren {
1417-
parent: SyntaxNode,
14181402
next: Option<SyntaxElement>,
1419-
next_initialized: bool,
14201403
}
14211404

14221405
impl SyntaxElementChildren {
14231406
fn new(parent: SyntaxNode) -> SyntaxElementChildren {
1424-
SyntaxElementChildren { parent, next: None, next_initialized: false }
1407+
SyntaxElementChildren { next: parent.first_child_or_token() }
14251408
}
14261409

14271410
pub fn by_kind<F: Fn(SyntaxKind) -> bool>(self, matcher: F) -> SyntaxElementChildrenByKind<F> {
1428-
if !self.next_initialized {
1429-
SyntaxElementChildrenByKind {
1430-
next: self.parent.first_child_or_token_by_kind(&matcher),
1431-
matcher,
1432-
}
1433-
} else {
1434-
SyntaxElementChildrenByKind {
1435-
next: self.next.and_then(|node| {
1436-
if matcher(node.kind()) {
1437-
Some(node)
1438-
} else {
1439-
node.next_sibling_or_token_by_kind(&matcher)
1440-
}
1441-
}),
1442-
matcher,
1443-
}
1411+
SyntaxElementChildrenByKind {
1412+
next: self.next.and_then(|node| {
1413+
if matcher(node.kind()) {
1414+
Some(node)
1415+
} else {
1416+
node.next_sibling_or_token_by_kind(&matcher)
1417+
}
1418+
}),
1419+
matcher,
14441420
}
14451421
}
14461422
}
14471423

14481424
impl Iterator for SyntaxElementChildren {
14491425
type Item = SyntaxElement;
14501426
fn next(&mut self) -> Option<SyntaxElement> {
1451-
if !self.next_initialized {
1452-
self.next = self.parent.first_child_or_token();
1453-
self.next_initialized = true;
1454-
} else {
1455-
self.next = self.next.take().and_then(|next| next.to_next_sibling_or_token());
1456-
}
1457-
1458-
self.next.clone()
1427+
let curr = self.next.take()?;
1428+
self.next = curr.next_sibling_or_token();
1429+
Some(curr)
14591430
}
14601431
}
14611432

@@ -1468,9 +1439,9 @@ pub struct SyntaxElementChildrenByKind<F: Fn(SyntaxKind) -> bool> {
14681439
impl<F: Fn(SyntaxKind) -> bool> Iterator for SyntaxElementChildrenByKind<F> {
14691440
type Item = SyntaxElement;
14701441
fn next(&mut self) -> Option<SyntaxElement> {
1471-
self.next.take().inspect(|next| {
1472-
self.next = next.next_sibling_or_token_by_kind(&self.matcher);
1473-
})
1442+
let curr = self.next.take()?;
1443+
self.next = curr.next_sibling_or_token_by_kind(&self.matcher);
1444+
Some(curr)
14741445
}
14751446
}
14761447

0 commit comments

Comments
 (0)