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

Commit e3d8114

Browse files
committed
Updated TSQuery implementation
1 parent 2387873 commit e3d8114

File tree

4 files changed

+198
-20
lines changed

4 files changed

+198
-20
lines changed
Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,74 @@
11
package com.itsaky.androidide.treesitter;
22

3-
/**
4-
* TSQuery
5-
*/
6-
public class TSQuery {
7-
long pointer;
8-
9-
public TSQuery(TSLanguage language, String source) {
10-
this.pointer = Native.newQuery(language.pointer, source);
11-
}
12-
13-
private static class Native {
14-
public static native long newQuery(long pointer, String source);
15-
}
3+
public class TSQuery implements AutoCloseable {
4+
final long pointer;
5+
6+
private int errorOffset;
7+
private int errorType;
8+
9+
/**
10+
* Create a new query from a string containing one or more S-expression patterns. The query is
11+
* associated with a particular language, and can only be run on syntax nodes parsed with that
12+
* language.
13+
*
14+
* <p>If all of the given patterns are valid, this returns a `TSQuery`. If a pattern is invalid,
15+
* this returns `NULL`, and provides two pieces of information about the problem: 1. The byte
16+
* offset of the error is written to the `error_offset` parameter. 2. The type of error is written
17+
* to the `error_type` parameter.
18+
*/
19+
public TSQuery(TSLanguage language, String source) {
20+
this.pointer = Native.newQuery(this, language.pointer, source);
21+
}
22+
23+
public int getErrorOffset() {
24+
return this.errorOffset;
25+
}
26+
27+
public TSQueryError getErrorType() {
28+
return TSQueryError.valueOf(this.errorType);
29+
}
30+
31+
/**
32+
* Get the number of captures in the query.
33+
*
34+
* @return The count.
35+
*/
36+
public int getCaptureCount() {
37+
return Native.captureCount(this.pointer);
38+
}
39+
40+
/**
41+
* Get the number of patterns in the query.
42+
*
43+
* @return The count.
44+
*/
45+
public int getPatternCount() {
46+
return Native.patternCount(this.pointer);
47+
}
48+
49+
/**
50+
* Get the number of string literals in the query.
51+
*
52+
* @return The count.
53+
*/
54+
public int getStringCount() {
55+
return Native.stringCount(this.pointer);
56+
}
57+
58+
@Override
59+
public void close() throws Exception {
60+
Native.delete(this.pointer);
61+
}
62+
63+
private static class Native {
64+
public static native long newQuery(TSQuery query, long pointer, String source);
65+
66+
public static native void delete(long query);
67+
68+
public static native int captureCount(long query);
69+
70+
public static native int patternCount(long query);
71+
72+
public static native int stringCount(long query);
73+
}
1674
}
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+
* The type of a {@link TSQuery} error.
5+
*
6+
* @author Akash Yadav
7+
*/
8+
public enum TSQueryError {
9+
None(0),
10+
Syntax(1),
11+
NodeType(2),
12+
Field(3),
13+
Capture(4),
14+
Structure(5),
15+
Language(6);
16+
17+
private final int type;
18+
19+
TSQueryError(int type) {
20+
this.type = type;
21+
}
22+
23+
public static TSQueryError valueOf(int type) {
24+
for (TSQueryError value : values()) {
25+
if (value.type == type) {
26+
return value;
27+
}
28+
}
29+
return null;
30+
}
31+
}

lib/com_itsaky_androidide_treesitter_TSQuery_Native.h

Lines changed: 34 additions & 2 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: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,71 @@
11
#include "com_itsaky_androidide_treesitter_TSQuery_Native.h"
22
#include "ts_utils.h"
33

4-
JNIEXPORT jlong JNICALL Java_com_itsaky_androidide_treesitter_TSQuery_00024Native_newQuery(
5-
JNIEnv* env, jclass self, jlong language, jstring source) {
6-
4+
JNIEXPORT jlong JNICALL
5+
Java_com_itsaky_androidide_treesitter_TSQuery_00024Native_newQuery(
6+
JNIEnv* env, jclass self, jobject queryObject, jlong language,
7+
jstring source) {
78
const char* c_source;
89
uint32_t source_length = env->GetStringLength(source);
910
c_source = env->GetStringUTFChars(source, NULL);
1011
uint32_t* error_offset = new uint32_t;
1112
TSQueryError* error_type = new TSQueryError;
12-
TSQuery* query = ts_query_new((TSLanguage*) language, c_source, source_length, error_offset, error_type);
13-
return (jlong) query;
13+
TSQuery* query = ts_query_new((TSLanguage*)language, c_source, source_length,
14+
error_offset, error_type);
15+
fillQuery(env, queryObject, *error_offset, *error_type);
16+
return (jlong)query;
17+
}
18+
19+
JNIEXPORT void JNICALL
20+
Java_com_itsaky_androidide_treesitter_TSQuery_00024Native_delete(JNIEnv* env,
21+
jclass self,
22+
jlong query) {
23+
ts_query_delete((TSQuery*)query);
24+
}
25+
26+
JNIEXPORT jint JNICALL
27+
Java_com_itsaky_androidide_treesitter_TSQuery_00024Native_captureCount(
28+
JNIEnv* env, jclass self, jlong query) {
29+
return ts_query_capture_count((TSQuery*)query);
30+
}
31+
32+
JNIEXPORT jint JNICALL
33+
Java_com_itsaky_androidide_treesitter_TSQuery_00024Native_patternCount(
34+
JNIEnv* env, jclass self, jlong query) {
35+
return ts_query_pattern_count((TSQuery*)query);
36+
}
37+
38+
JNIEXPORT jint JNICALL
39+
Java_com_itsaky_androidide_treesitter_TSQuery_00024Native_stringCount(
40+
JNIEnv* env, jclass self, jlong query) {
41+
return ts_query_string_count((TSQuery*)query);
1442
}
43+
44+
void fillQuery(JNIEnv* env, jobject query, uint32_t error_offset,
45+
TSQueryError error_type) {
46+
jclass klass = env->GetObjectClass(query);
47+
jfieldID offsetField = env->GetFieldID(klass, "errorOffset", "I");
48+
jfieldID errorTypeField = env->GetFieldID(klass, "errorType", "I");
49+
env->SetIntField(query, offsetField, error_offset);
50+
env->SetIntField(query, errorTypeField, getErrorType(error_type));
51+
}
52+
53+
jint getErrorType(TSQueryError error) {
54+
switch (error) {
55+
default:
56+
case TSQueryErrorNone:
57+
return 0;
58+
case TSQueryErrorSyntax:
59+
return 1;
60+
case TSQueryErrorNodeType:
61+
return 2;
62+
case TSQueryErrorField:
63+
return 3;
64+
case TSQueryErrorCapture:
65+
return 4;
66+
case TSQueryErrorStructure:
67+
return 5;
68+
case TSQueryErrorLanguage:
69+
return 6;
70+
}
71+
}

0 commit comments

Comments
 (0)