Skip to content

Commit 9e5e830

Browse files
Marcono1234ObserverOfTime
authored andcommitted
fix(querypredicate): fix eq? causing exception for quantified captures
For quantified captures the `predicate` may be called multiple times, so it must not reuse the already exhausted `Stream`.
1 parent 4ccc4bc commit 9e5e830

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ boolean test(QueryMatch match) {
7777

7878
private boolean testCapture(QueryMatch match) {
7979
var findNodes1 = match.findNodes(capture).stream();
80-
var findNodes2 = match.findNodes(value).stream();
80+
var findNodes2 = match.findNodes(value);
8181
Predicate<Node> predicate =
82-
n1 -> findNodes2.anyMatch(n2 -> Objects.equals(n1.getText(), n2.getText()) == isPositive);
82+
n1 -> findNodes2.stream().anyMatch(n2 -> Objects.equals(n1.getText(), n2.getText()) == isPositive);
8383
return isAny ? findNodes1.anyMatch(predicate) : findNodes1.allMatch(predicate);
8484
}
8585

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,21 @@ void findMatches() {
175175
assertEquals(Short.MAX_VALUE + 1, matches.getFirst().captures().size());
176176
});
177177
}
178+
179+
// Verify that `eq?` predicate works with quantified captures
180+
try (var tree = parser.parse("/* 1 */ /* 1 */ /* 1 */").orElseThrow()) {
181+
var source = """
182+
(program
183+
. (block_comment) @b (block_comment)+ @a
184+
(#eq? @a @b)
185+
)
186+
""";
187+
assertCursor(source, cursor -> {
188+
var matches = cursor.findMatches(tree.getRootNode()).toList();
189+
assertEquals(1, matches.size());
190+
assertEquals(
191+
"/* 1 */", matches.getFirst().captures().getFirst().node().getText());
192+
});
193+
}
178194
}
179195
}

0 commit comments

Comments
 (0)