Skip to content

Commit 1c5bae4

Browse files
committed
Refactor iterators with cleaner logic
1 parent 249bc5c commit 1c5bae4

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

crates/ark/src/lsp/traits/node.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,7 @@ impl<'tree> NodeExt for Node<'tree> {
221221
fn next_siblings(&self) -> impl Iterator<Item = Node<'tree>> {
222222
let mut cursor = self.walk();
223223

224-
let first = if cursor.goto_next_sibling() {
225-
Some(cursor.node())
226-
} else {
227-
None
228-
};
229-
230-
std::iter::successors(first, move |_| {
224+
std::iter::from_fn(move || {
231225
if cursor.goto_next_sibling() {
232226
Some(cursor.node())
233227
} else {
@@ -238,20 +232,15 @@ impl<'tree> NodeExt for Node<'tree> {
238232

239233
fn children_of(node: Node<'tree>) -> impl Iterator<Item = Node<'tree>> {
240234
let mut cursor = node.walk();
241-
let mut first = true;
235+
let mut done = !cursor.goto_first_child();
242236

243237
std::iter::from_fn(move || {
244-
let advanced = if first {
245-
first = false;
246-
cursor.goto_first_child()
247-
} else {
248-
cursor.goto_next_sibling()
249-
};
250-
251-
if advanced {
252-
Some(cursor.node())
253-
} else {
238+
if done {
254239
None
240+
} else {
241+
let item = Some(cursor.node());
242+
done = !cursor.goto_next_sibling();
243+
item
255244
}
256245
})
257246
}

0 commit comments

Comments
 (0)