Skip to content

Commit f078e97

Browse files
committed
feat(Query) : added method to find matches with custom allocator
1 parent 956a0a4 commit f078e97

File tree

1 file changed

+26
-7
lines changed
  • src/main/java/io/github/treesitter/jtreesitter

1 file changed

+26
-7
lines changed

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.github.treesitter.jtreesitter.internal.*;
66
import java.lang.foreign.Arena;
77
import java.lang.foreign.MemorySegment;
8+
import java.lang.foreign.SegmentAllocator;
89
import java.util.*;
910
import java.util.function.BiPredicate;
1011
import java.util.function.Consumer;
@@ -478,16 +479,19 @@ public Map<String, Optional<String>> getPatternAssertions(@Unsigned int index, b
478479
}
479480

480481
/**
481-
* Iterate over all the matches in the order that they were found.
482+
* Iterate over all the matches in the order that they were found. The lifetime of the native memory of the returned
483+
* matches is bound to the lifetime of this query object.
482484
*
483485
* @param node The node that the query will run on.
484486
*/
485487
public Stream<QueryMatch> findMatches(Node node) {
486-
return findMatches(node, null);
488+
return findMatches(node, null, arena);
487489
}
488490

491+
489492
/**
490-
* Iterate over all the matches in the order that they were found.
493+
* Iterate over all the matches in the order that they were found. The lifetime of the native memory of the returned
494+
* matches is bound to the lifetime of this query object.
491495
*
492496
* <h4 id="findMatches-example">Predicate Example</h4>
493497
* <p>
@@ -503,11 +507,24 @@ public Stream<QueryMatch> findMatches(Node node) {
503507
* @param node The node that the query will run on.
504508
* @param predicate A function that handles custom predicates.
505509
*/
506-
public Stream<QueryMatch> findMatches(Node node, @Nullable BiPredicate<QueryPredicate, QueryMatch> predicate) {
510+
public Stream<QueryMatch> findMatches(Node node, @Nullable BiPredicate<QueryPredicate, QueryMatch> predicate){
511+
return findMatches(node, predicate, arena);
512+
}
513+
514+
515+
/**
516+
* Like {@link #findMatches(Node, BiPredicate)} but the native memory of the returned matches is created using the
517+
* given allocator.
518+
*
519+
* @param node The node that the query will run on.
520+
* @param predicate A function that handles custom predicates.
521+
* @param allocator The allocator that is used to allocate the native memory of the returned matches.
522+
*/
523+
public Stream<QueryMatch> findMatches(Node node, @Nullable BiPredicate<QueryPredicate, QueryMatch> predicate, SegmentAllocator allocator) {
507524
try (var alloc = Arena.ofConfined()) {
508525
ts_query_cursor_exec(cursor, query, node.copy(alloc));
509526
}
510-
return StreamSupport.stream(new MatchesIterator(node.getTree(), predicate), false);
527+
return StreamSupport.stream(new MatchesIterator(node.getTree(), predicate, allocator), false);
511528
}
512529

513530
@Override
@@ -537,11 +554,13 @@ private void checkIndex(@Unsigned int index) throws IndexOutOfBoundsException {
537554
private final class MatchesIterator extends Spliterators.AbstractSpliterator<QueryMatch> {
538555
private final @Nullable BiPredicate<QueryPredicate, QueryMatch> predicate;
539556
private final Tree tree;
557+
private final SegmentAllocator allocator;
540558

541-
public MatchesIterator(Tree tree, @Nullable BiPredicate<QueryPredicate, QueryMatch> predicate) {
559+
public MatchesIterator(Tree tree, @Nullable BiPredicate<QueryPredicate, QueryMatch> predicate, SegmentAllocator allocator) {
542560
super(Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.NONNULL);
543561
this.predicate = predicate;
544562
this.tree = tree;
563+
this.allocator = allocator;
545564
}
546565

547566
@Override
@@ -555,7 +574,7 @@ public boolean tryAdvance(Consumer<? super QueryMatch> action) {
555574
for (int i = 0; i < count; ++i) {
556575
var capture = TSQueryCapture.asSlice(matchCaptures, i);
557576
var name = captureNames.get(TSQueryCapture.index(capture));
558-
var node = TSNode.allocate(arena).copyFrom(TSQueryCapture.node(capture));
577+
var node = TSNode.allocate(allocator).copyFrom(TSQueryCapture.node(capture));
559578
captureList.add(new QueryCapture(name, new Node(node, tree)));
560579
}
561580
var patternIndex = TSQueryMatch.pattern_index(match);

0 commit comments

Comments
 (0)