Skip to content

Commit 07a8052

Browse files
committed
fix(query): fix multiple predicates being parsed incorrectly
1 parent a550908 commit 07a8052

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/main/java/io/github/treesitter/jtreesitter/Query.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ private void handlePredicates(String source, MemorySegment query, @Unsigned int
149149
if ((steps = count.get(C_INT, 0)) == 0) continue;
150150
int offset = ts_query_start_byte_for_pattern(query, i);
151151
long row = source.chars().limit(offset).filter(c -> c == '\n').count();
152-
for (long j = 0, nargs = 0; j < steps; ++j) {
152+
for (long j = 0; j < steps; ++j) {
153+
long nargs = 0;
153154
for (; ; ++nargs) {
154155
var t = TSQueryPredicateStep.asSlice(tokens, nargs);
155156
if (TSQueryPredicateStep.type(t) == TSQueryPredicateStepTypeDone()) break;

src/test/java/io/github/treesitter/jtreesitter/QueryCursorTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,5 +194,18 @@ void findMatches() {
194194
matches.getFirst().captures().getFirst().node().getText());
195195
});
196196
}
197+
198+
// Verify that multiple predicates with different number of args are handled correctly
199+
try (var tree = parser.parse("int a, b;").orElseThrow()) {
200+
var source = """
201+
((_) @a (_) @b (#any-of? @a "a" "x") (#eq? @b "b"))
202+
""";
203+
assertCursor(source, cursor -> {
204+
var matches = cursor.findMatches(tree.getRootNode()).toList();
205+
assertEquals(1, matches.size());
206+
assertEquals("a", matches.getFirst().findNodes("a").getFirst().getText());
207+
assertEquals("b", matches.getFirst().findNodes("b").getFirst().getText());
208+
});
209+
}
197210
}
198211
}

0 commit comments

Comments
 (0)