Skip to content

Commit 9b398c2

Browse files
committed
fix(lib): backtrack to the last relevant iterator if no child was found
1 parent d60789a commit 9b398c2

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

lib/src/node.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,22 +304,36 @@ static inline TSNode ts_node__first_child_for_byte(
304304
TSNode node = self;
305305
bool did_descend = true;
306306

307+
NodeChildIterator last_iterator;
308+
bool has_last_iterator = false;
309+
307310
while (did_descend) {
308311
did_descend = false;
309312

310313
TSNode child;
311314
NodeChildIterator iterator = ts_node_iterate_children(&node);
315+
loop:
312316
while (ts_node_child_iterator_next(&iterator, &child)) {
313317
if (ts_node_end_byte(child) > goal) {
314318
if (ts_node__is_relevant(child, include_anonymous)) {
315319
return child;
316320
} else if (ts_node_child_count(child) > 0) {
321+
if (iterator.child_index < ts_subtree_child_count(ts_node__subtree(child))) {
322+
last_iterator = iterator;
323+
has_last_iterator = true;
324+
}
317325
did_descend = true;
318326
node = child;
319327
break;
320328
}
321329
}
322330
}
331+
332+
if (!did_descend && has_last_iterator) {
333+
iterator = last_iterator;
334+
has_last_iterator = false;
335+
goto loop;
336+
}
323337
}
324338

325339
return ts_node__null();

0 commit comments

Comments
 (0)