Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 87f7142

Browse files
committed
Added implementation for the TSQueryCursor APIs
1 parent d387d73 commit 87f7142

File tree

13 files changed

+348
-14
lines changed

13 files changed

+348
-14
lines changed

android-tree-sitter/build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ for (def arch : archs) {
7373
}
7474

7575
def buildFor(Task task, String arch, String classifier, String ndkDir, String javaHome) {
76-
task.inputs.dir "${rootProject.projectDir.path}/lib"
77-
task.outputs.file "${rootProject.projectDir.path}/output/${classifier}/libts.so"
76+
def outputFile = new File("${rootProject.projectDir.path}/output/${classifier}/libts.so")
77+
def inputs = rootProject.fileTree("lib").files
78+
task.inputs.files(inputs)
79+
task.outputs.file outputFile
7880

7981
task.doLast {
8082
exec {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.itsaky.androidide.treesitter;
2+
3+
/**
4+
* @author Akash Yadav
5+
*/
6+
public class TSQueryCapture {
7+
private TSNode node;
8+
private int index;
9+
10+
public TSNode getNode() {
11+
return node;
12+
}
13+
14+
public int getIndex() {
15+
return index;
16+
}
17+
}

android-tree-sitter/src/main/java/com/itsaky/androidide/treesitter/TSQueryCursor.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,61 @@ public void exec(TSQuery query, TSNode node) {
1616
Native.exec(this.pointer, query.pointer, node);
1717
}
1818

19+
/**
20+
* Whether the maximum number of in-progress matches allowed by this query cursor has been
21+
* exceeded or not.
22+
*
23+
* <p>Query cursors have an optional maximum capacity for storing lists of in-progress captures.
24+
* If this capacity is exceeded, then the earliest-starting match will silently be dropped to make
25+
* room for further matches. This maximum capacity is optional — by default, query cursors allow
26+
* any number of pending matches, dynamically allocating new space for them as needed as the query
27+
* is executed.
28+
*/
29+
public boolean didExceedMatchLimit() {
30+
return Native.exceededMatchLimit(this.pointer);
31+
}
32+
33+
/**
34+
* Get the maximum number of in-progress matches allowed by this query * cursor.
35+
*
36+
* @return The match limit.
37+
* @see #didExceedMatchLimit()
38+
*/
39+
public int getMatchLimit() {
40+
return Native.matchLimit(this.pointer);
41+
}
42+
43+
/**
44+
* Set the maximum number of in-progress matches allowed by this query * cursor.
45+
*
46+
* @param newLimit The new match limit.
47+
* @see #didExceedMatchLimit()
48+
*/
49+
public void setMatchLimit(int newLimit) {
50+
Native.matchLimit(this.pointer, newLimit);
51+
}
52+
53+
public void setByteRange(int start, int end) {
54+
Native.setByteRange(this.pointer, start, end);
55+
}
56+
57+
public void setPointRange(TSPoint start, TSPoint end) {
58+
Native.setPointRange(this.pointer, start, end);
59+
}
60+
61+
public TSQueryMatch nextMatch() {
62+
TSQueryMatch match = new TSQueryMatch();
63+
if (Native.nextMatch(this.pointer, match)) {
64+
return match;
65+
}
66+
67+
return null;
68+
}
69+
70+
public void removeMatch(int id) {
71+
Native.removeMatch(this.pointer, id);
72+
}
73+
1974
@Override
2075
public void close() throws Exception {
2176
Native.delete(this.pointer);
@@ -27,5 +82,19 @@ private static class Native {
2782
public static native void delete(long cursor);
2883

2984
public static native void exec(long cursor, long query, TSNode node);
85+
86+
public static native boolean exceededMatchLimit(long cursor);
87+
88+
public static native void matchLimit(long cursor, int newLimit);
89+
90+
public static native int matchLimit(long cursor);
91+
92+
public static native void setByteRange(long cursor, int start, int end);
93+
94+
public static native void setPointRange(long cursor, TSPoint start, TSPoint end);
95+
96+
public static native boolean nextMatch(long cursor, TSQueryMatch match);
97+
98+
public static native void removeMatch(long cursor, int id);
3099
}
31100
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.itsaky.androidide.treesitter;
2+
3+
/**
4+
* @author Akash Yadav
5+
*/
6+
public class TSQueryMatch {
7+
private int id;
8+
private int patternIndex;
9+
private TSQueryCapture[] captures;
10+
11+
public int getId() {
12+
return id;
13+
}
14+
15+
public int getPatternIndex() {
16+
return patternIndex;
17+
}
18+
19+
public TSQueryCapture[] getCaptures() {
20+
return captures;
21+
}
22+
23+
public TSQueryCapture getCapture(int index) {
24+
return captures[index];
25+
}
26+
}
846 KB
Binary file not shown.
622 KB
Binary file not shown.
755 KB
Binary file not shown.
798 KB
Binary file not shown.
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.itsaky.androidide.treesitter;
22

3+
import static com.google.common.truth.Truth.assertThat;
4+
35
import org.junit.Test;
46

57
/**
@@ -8,14 +10,27 @@
810
public class QueryTest extends TestBase {
911

1012
@Test
11-
public void test() throws Exception {
13+
public void implementationTest() throws Exception {
1214
try (final var parser = new TSParser()) {
1315
parser.setLanguage(TSLanguages.java());
1416
try (final var tree = parser.parseString("public class MyClass { int x = 0; }");
1517
final var query = new TSQuery(tree.getLanguage(), "(class_body)");
1618
final var cursor = new TSQueryCursor()) {
1719
cursor.exec(query, tree.getRootNode());
20+
TSQueryMatch match;
21+
while((match = cursor.nextMatch()) != null) {
22+
assertThat(match.getCaptures()).isNotEmpty();
23+
}
1824
}
1925
}
2026
}
27+
28+
@Test
29+
public void testError() throws Exception {
30+
try (TSQuery query = new TSQuery(TSLanguages.java(), "(class_declaration")) {
31+
assertThat(query.pointer).isEqualTo(0);
32+
assertThat(query.getErrorOffset()).isEqualTo("(class_declaration".length());
33+
assertThat(query.getErrorType()).isEqualTo(TSQueryError.Syntax);
34+
}
35+
}
2136
}

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ plugins {
66

77
task clean {
88
delete "output"
9+
delete "build"
910
}

0 commit comments

Comments
 (0)