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

Commit d387d73

Browse files
committed
Added basic implementation of TSQueryCursor
1 parent e3d8114 commit d387d73

File tree

10 files changed

+110
-1
lines changed

10 files changed

+110
-1
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.itsaky.androidide.treesitter;
2+
3+
/**
4+
* @author Akash Yadav
5+
*/
6+
public class TSQueryCursor implements AutoCloseable {
7+
8+
final long pointer;
9+
10+
public TSQueryCursor() {
11+
this.pointer = Native.newCursor();
12+
}
13+
14+
/** Start running the given query on the given node. */
15+
public void exec(TSQuery query, TSNode node) {
16+
Native.exec(this.pointer, query.pointer, node);
17+
}
18+
19+
@Override
20+
public void close() throws Exception {
21+
Native.delete(this.pointer);
22+
}
23+
24+
private static class Native {
25+
public static native long newCursor();
26+
27+
public static native void delete(long cursor);
28+
29+
public static native void exec(long cursor, long query, TSNode node);
30+
}
31+
}
4.55 KB
Binary file not shown.
3.24 KB
Binary file not shown.
4.33 KB
Binary file not shown.
4.46 KB
Binary file not shown.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.itsaky.androidide.treesitter;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @author Akash Yadav
7+
*/
8+
public class QueryTest extends TestBase {
9+
10+
@Test
11+
public void test() throws Exception {
12+
try (final var parser = new TSParser()) {
13+
parser.setLanguage(TSLanguages.java());
14+
try (final var tree = parser.parseString("public class MyClass { int x = 0; }");
15+
final var query = new TSQuery(tree.getLanguage(), "(class_body)");
16+
final var cursor = new TSQueryCursor()) {
17+
cursor.exec(query, tree.getRootNode());
18+
}
19+
}
20+
}
21+
}

genh.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -eu
44

55
script_dir=$(realpath $(dirname $0))
66

7-
for header in TSLanguages.Native TSLanguage.Native TSParser.Native TSTreeCursor.Native TSTree.Native TSQuery.Native TSNode
7+
for header in TSLanguages.Native TSLanguage.Native TSParser.Native TSTreeCursor.Native TSTree.Native TSQuery.Native TSQueryCursor.Native TSNode
88
do
99
javah -d $script_dir/lib -classpath ${script_dir}/android-tree-sitter/src/main/java com.itsaky.androidide.treesitter.${header}
1010
done

lib/com_itsaky_androidide_treesitter_TSQueryCursor_Native.h

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ts_query.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include "com_itsaky_androidide_treesitter_TSQuery_Native.h"
22
#include "ts_utils.h"
33

4+
void fillQuery(JNIEnv*, jobject, uint32_t, TSQueryError);
5+
jint getErrorType(TSQueryError);
6+
47
JNIEXPORT jlong JNICALL
58
Java_com_itsaky_androidide_treesitter_TSQuery_00024Native_newQuery(
69
JNIEnv* env, jclass self, jobject queryObject, jlong language,

lib/ts_query_cursor.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "ts_utils.h"
2+
#include "com_itsaky_androidide_treesitter_TSQueryCursor_Native.h"
3+
4+
JNIEXPORT jlong JNICALL Java_com_itsaky_androidide_treesitter_TSQueryCursor_00024Native_newCursor
5+
(JNIEnv* env, jclass self) {
6+
return (jlong) ts_query_cursor_new();
7+
}
8+
9+
JNIEXPORT void JNICALL Java_com_itsaky_androidide_treesitter_TSQueryCursor_00024Native_delete
10+
(JNIEnv* env, jclass self, jlong cursor) {
11+
ts_query_cursor_delete((TSQueryCursor*) cursor);
12+
}
13+
14+
JNIEXPORT void JNICALL Java_com_itsaky_androidide_treesitter_TSQueryCursor_00024Native_exec
15+
(JNIEnv* env, jclass self, jlong cursor, jlong query, jobject node) {
16+
ts_query_cursor_exec((TSQueryCursor*) cursor, (TSQuery*) query, _unmarshalNode(env, node));
17+
}

0 commit comments

Comments
 (0)