Skip to content

Commit

Permalink
feat(query): implement endByteForPattern
Browse files Browse the repository at this point in the history
  • Loading branch information
ObserverOfTime committed Jul 29, 2024
1 parent e2e2eed commit 4e702c2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions scripts/jextract.sh
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ jextract \
--include-function ts_query_delete \
--include-function ts_query_disable_capture \
--include-function ts_query_disable_pattern \
--include-function ts_query_end_byte_for_pattern \
--include-function ts_query_is_pattern_guaranteed_at_step \
--include-function ts_query_is_pattern_non_local \
--include-function ts_query_is_pattern_rooted \
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/io/github/treesitter/jtreesitter/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public final class Query implements AutoCloseable {
}

try (var alloc = Arena.ofConfined()) {
for (int i = 0, steps = 0; i < patternCount; ++i) {
for (int i = 0, steps; i < patternCount; ++i) {
var count = alloc.allocate(C_INT);
var tokens = ts_query_predicates_for_pattern(query, i, count);
if ((steps = count.get(C_INT, 0)) == 0) continue;
Expand Down Expand Up @@ -369,6 +369,17 @@ public void disableCapture(String name) throws NoSuchElementException {
return ts_query_start_byte_for_pattern(query, index);
}

/**
* Get the byte offset where the given pattern ends in the query's source.
*
* @throws IndexOutOfBoundsException If the index exceeds the
* {@linkplain #getPatternCount pattern count}.
*/
public @Unsigned int endByteForPattern(@Unsigned int index) throws IndexOutOfBoundsException {
checkIndex(index);
return ts_query_end_byte_for_pattern(query, index);
}

/**
* Check if the pattern with the given index has a single root node.
*
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/io/github/treesitter/jtreesitter/QueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ void startByteForPattern() {
});
}

@Test
void endByteForPattern() {
assertQuery(query -> {
assertEquals(26, query.endByteForPattern(0));
assertThrows(IndexOutOfBoundsException.class, () -> query.endByteForPattern(2));
});
}

@Test
void isPatternRooted() {
assertQuery(query -> {
Expand Down

0 comments on commit 4e702c2

Please sign in to comment.