Skip to content

Commit 99cd283

Browse files
committed
query: Fix detection of repeated field names
Fixes tree-sitter#790
1 parent 3497f34 commit 99cd283

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

cli/src/tests/query_test.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,6 +1835,33 @@ fn test_query_matches_with_no_captures() {
18351835
});
18361836
}
18371837

1838+
#[test]
1839+
fn test_query_matches_with_repeated_fields() {
1840+
allocations::record(|| {
1841+
let language = get_language("c");
1842+
let query = Query::new(
1843+
language,
1844+
"(field_declaration declarator: (field_identifier) @field)",
1845+
)
1846+
.unwrap();
1847+
1848+
assert_query_matches(
1849+
language,
1850+
&query,
1851+
"
1852+
struct S {
1853+
int a, b, c;
1854+
}
1855+
",
1856+
&[
1857+
(0, vec![("field", "a")]),
1858+
(0, vec![("field", "b")]),
1859+
(0, vec![("field", "c")]),
1860+
],
1861+
);
1862+
});
1863+
}
1864+
18381865
#[test]
18391866
fn test_query_captures_basic() {
18401867
allocations::record(|| {

lib/src/tree_cursor.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ void ts_tree_cursor_current_status(
330330
}
331331
}
332332

333-
#undef subtree_metadata
333+
#undef subtree_symbol
334334

335335
if (!ts_subtree_extra(*entry->subtree)) {
336336
const TSFieldMapEntry *field_map, *field_map_end;
@@ -345,7 +345,6 @@ void ts_tree_cursor_current_status(
345345
for (const TSFieldMapEntry *i = field_map; i < field_map_end; i++) {
346346
if (!i->inherited && i->child_index == entry->structural_child_index) {
347347
*field_id = i->field_id;
348-
*can_have_later_siblings_with_this_field = false;
349348
break;
350349
}
351350
}
@@ -354,9 +353,14 @@ void ts_tree_cursor_current_status(
354353
// Determine if the current node can have later siblings with the same field name.
355354
if (*field_id) {
356355
for (const TSFieldMapEntry *i = field_map; i < field_map_end; i++) {
357-
if (i->field_id == *field_id && i->child_index > entry->structural_child_index) {
358-
*can_have_later_siblings_with_this_field = true;
359-
break;
356+
if (i->field_id == *field_id) {
357+
if (
358+
i->child_index > entry->structural_child_index ||
359+
(i->child_index == entry->structural_child_index && *has_later_named_siblings)
360+
) {
361+
*can_have_later_siblings_with_this_field = true;
362+
break;
363+
}
360364
}
361365
}
362366
}

0 commit comments

Comments
 (0)