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

Commit 28c7f1c

Browse files
committed
Added more APIs in TSQuery
1 parent 0acf011 commit 28c7f1c

File tree

9 files changed

+377
-10
lines changed

9 files changed

+377
-10
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,45 @@ public int getStringCount() {
5555
return Native.stringCount(this.pointer);
5656
}
5757

58+
public int getStartByteForPattern(int pattern) {
59+
validatePatternIndex(pattern);
60+
return Native.startByteForPattern(this.pointer, pattern);
61+
}
62+
63+
public TSQueryPredicateStep[] getPredicatesForPattern(int pattern) {
64+
validatePatternIndex(pattern);
65+
return Native.predicatesForPattern(this.pointer, pattern);
66+
}
67+
68+
public boolean isPatternRooted(int pattern) {
69+
validatePatternIndex(pattern);
70+
return Native.patternRooted(this.pointer, pattern);
71+
}
72+
73+
public boolean isPatternGuaranteedAtStep(int offset) {
74+
return Native.patternGuaranteedAtStep(this.pointer, offset);
75+
}
76+
77+
public String getCaptureNameForId(int id) {
78+
return Native.captureNameForId(this.pointer, id);
79+
}
80+
81+
public String getStringValueForId(int id) {
82+
return Native.stringValueForId(this.pointer, id);
83+
}
84+
5885
@Override
5986
public void close() throws Exception {
6087
Native.delete(this.pointer);
6188
}
6289

90+
private void validatePatternIndex(int pattern) {
91+
if (pattern < 0 || pattern >= getPatternCount()) {
92+
throw new IndexOutOfBoundsException(
93+
"pattern count: " + getPatternCount() + ", pattern: " + pattern);
94+
}
95+
}
96+
6397
private static class Native {
6498
public static native long newQuery(TSQuery query, long pointer, String source);
6599

@@ -70,5 +104,17 @@ private static class Native {
70104
public static native int patternCount(long query);
71105

72106
public static native int stringCount(long query);
107+
108+
public static native int startByteForPattern(long query, int pattern);
109+
110+
public static native TSQueryPredicateStep[] predicatesForPattern(long query, int pattern);
111+
112+
public static native boolean patternRooted(long query, int pattern);
113+
114+
public static native boolean patternGuaranteedAtStep(long query, int offset);
115+
116+
public static native String captureNameForId(long query, int id);
117+
118+
public static native String stringValueForId(long query, int id);
73119
}
74120
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.itsaky.androidide.treesitter;
2+
3+
/**
4+
* @author Akash Yadav
5+
*/
6+
public class TSQueryPredicateStep {
7+
8+
private Type cachedType = null;
9+
private int type = -1;
10+
private int valueId = -1;
11+
12+
public Type getType() {
13+
if (cachedType == null) {
14+
cachedType = Type.forId(this.type);
15+
}
16+
return cachedType;
17+
}
18+
19+
public int getValueId() {
20+
return valueId;
21+
}
22+
23+
public enum Type {
24+
Done(0),
25+
Capture(1),
26+
String(2);
27+
28+
private final int id;
29+
30+
Type(int id) {
31+
this.id = id;
32+
}
33+
34+
public static Type forId(int typeId) {
35+
for (Type value : values()) {
36+
if (value.id == typeId) {
37+
return value;
38+
}
39+
}
40+
return null;
41+
}
42+
}
43+
}
3.43 KB
Binary file not shown.
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
; Methods
2+
3+
(method_declaration
4+
name: (identifier) @function.method)
5+
(method_invocation
6+
name: (identifier) @function.method)
7+
(super) @function.builtin
8+
9+
; Annotations
10+
11+
(annotation
12+
name: (identifier) @attribute)
13+
(marker_annotation
14+
name: (identifier) @attribute)
15+
16+
"@" @operator
17+
18+
; Types
19+
20+
(type_identifier) @type
21+
22+
(interface_declaration
23+
name: (identifier) @type)
24+
(class_declaration
25+
name: (identifier) @type)
26+
(enum_declaration
27+
name: (identifier) @type)
28+
29+
((field_access
30+
object: (identifier) @type)
31+
(#match? @type "^[A-Z]"))
32+
((scoped_identifier
33+
scope: (identifier) @type)
34+
(#match? @type "^[A-Z]"))
35+
((method_invocation
36+
object: (identifier) @type)
37+
(#match? @type "^[A-Z]"))
38+
((method_reference
39+
. (identifier) @type)
40+
(#match? @type "^[A-Z]"))
41+
42+
(constructor_declaration
43+
name: (identifier) @type)
44+
45+
[
46+
(boolean_type)
47+
(integral_type)
48+
(floating_point_type)
49+
(floating_point_type)
50+
(void_type)
51+
] @type.builtin
52+
53+
; Variables
54+
55+
((identifier) @constant
56+
(#match? @constant "^_*[A-Z][A-Z\\d_]+$"))
57+
58+
(identifier) @variable
59+
60+
(this) @variable.builtin
61+
62+
; Literals
63+
64+
[
65+
(hex_integer_literal)
66+
(decimal_integer_literal)
67+
(octal_integer_literal)
68+
(decimal_floating_point_literal)
69+
(hex_floating_point_literal)
70+
] @number
71+
72+
[
73+
(character_literal)
74+
(string_literal)
75+
] @string
76+
77+
[
78+
(true)
79+
(false)
80+
(null_literal)
81+
] @constant.builtin
82+
83+
[
84+
(line_comment)
85+
(block_comment)
86+
] @comment
87+
88+
; Keywords
89+
90+
[
91+
"abstract"
92+
"assert"
93+
"break"
94+
"case"
95+
"catch"
96+
"class"
97+
"continue"
98+
"default"
99+
"do"
100+
"else"
101+
"enum"
102+
"exports"
103+
"extends"
104+
"final"
105+
"finally"
106+
"for"
107+
"if"
108+
"implements"
109+
"import"
110+
"instanceof"
111+
"interface"
112+
"module"
113+
"native"
114+
"new"
115+
"non-sealed"
116+
"open"
117+
"opens"
118+
"package"
119+
"private"
120+
"protected"
121+
"provides"
122+
"public"
123+
"requires"
124+
"return"
125+
"sealed"
126+
"static"
127+
"strictfp"
128+
"switch"
129+
"synchronized"
130+
"throw"
131+
"throws"
132+
"to"
133+
"transient"
134+
"transitive"
135+
"try"
136+
"uses"
137+
"volatile"
138+
"while"
139+
"with"
140+
] @keyword

lib/include/com_itsaky_androidide_treesitter_TSQuery_Native.h

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

lib/ts_parser.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ Java_com_itsaky_androidide_treesitter_TSParser_00024Native_getIncludedRanges(
9292
JNIEnv* env, jclass self, jlong parser) {
9393
uint32_t count;
9494
const TSRange* ranges = ts_parser_included_ranges((TSParser*)parser, &count);
95-
std::cout << "Number of included ranges is " << count << std::endl;
9695
jclass klass = env->FindClass("com/itsaky/androidide/treesitter/TSRange");
9796
jobjectArray result = env->NewObjectArray(count, klass, NULL);
9897
for (uint32_t i = 0; i < count; i++) {

lib/ts_query.cc

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,58 @@ Java_com_itsaky_androidide_treesitter_TSQuery_00024Native_stringCount(
4444
return ts_query_string_count((TSQuery*)query);
4545
}
4646

47+
JNIEXPORT jint JNICALL
48+
Java_com_itsaky_androidide_treesitter_TSQuery_00024Native_startByteForPattern(
49+
JNIEnv* env, jclass self, jlong query, jint pattern) {
50+
return (jint)ts_query_start_byte_for_pattern((TSQuery*)query, pattern);
51+
}
52+
53+
JNIEXPORT jobjectArray JNICALL
54+
Java_com_itsaky_androidide_treesitter_TSQuery_00024Native_predicatesForPattern(
55+
JNIEnv* env, jclass self, jlong query, jint pattern) {
56+
uint32_t count;
57+
const TSQueryPredicateStep* predicates =
58+
ts_query_predicates_for_pattern((TSQuery*)query, pattern, &count);
59+
jclass klass =
60+
env->FindClass("com/itsaky/androidide/treesitter/TSQueryPredicateStep");
61+
jobjectArray result = env->NewObjectArray(count, klass, NULL);
62+
for (uint32_t i = 0; i < count; i++) {
63+
const TSQueryPredicateStep* predicate = (predicates + i);
64+
jobject obj = _marshalQueryPredicateStep(env, predicate);
65+
env->SetObjectArrayElement(result, i, obj);
66+
}
67+
return result;
68+
}
69+
70+
JNIEXPORT jboolean JNICALL
71+
Java_com_itsaky_androidide_treesitter_TSQuery_00024Native_patternRooted(
72+
JNIEnv* env, jclass self, jlong query, jint pattern) {
73+
return (jboolean)ts_query_is_pattern_rooted((TSQuery*)query, pattern);
74+
}
75+
76+
JNIEXPORT jboolean JNICALL
77+
Java_com_itsaky_androidide_treesitter_TSQuery_00024Native_patternGuaranteedAtStep(
78+
JNIEnv* env, jclass self, jlong query, jint offset) {
79+
return (jboolean)ts_query_is_pattern_guaranteed_at_step((TSQuery*)query,
80+
offset);
81+
}
82+
83+
JNIEXPORT jstring JNICALL
84+
Java_com_itsaky_androidide_treesitter_TSQuery_00024Native_captureNameForId(
85+
JNIEnv* env, jclass self, jlong query, jint id) {
86+
uint32_t count;
87+
const char* name = ts_query_capture_name_for_id((TSQuery*)query, id, &count);
88+
return (jstring)env->NewStringUTF(name);
89+
}
90+
91+
JNIEXPORT jstring JNICALL
92+
Java_com_itsaky_androidide_treesitter_TSQuery_00024Native_stringValueForId(
93+
JNIEnv* env, jclass self, jlong query, jint id) {
94+
uint32_t count;
95+
const char* str = ts_query_string_value_for_id((TSQuery*)query, id, &count);
96+
return env->NewStringUTF(str);
97+
}
98+
4799
void fillQuery(JNIEnv* env, jobject query, uint32_t error_offset,
48100
TSQueryError error_type) {
49101
jclass klass = env->GetObjectClass(query);

lib/ts_utils.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ jobject _marshalCapture(JNIEnv *env, TSQueryCapture capture);
3838

3939
jobject _marshalTreeCursorNode(JNIEnv* env, TreeCursorNode node);
4040

41-
TSInputEdit _unmarshalInputEdit(JNIEnv* env, jobject inputEdit);
41+
TSInputEdit _unmarshalInputEdit(JNIEnv* env, jobject inputEdit);
42+
43+
jobject _marshalQueryPredicateStep(JNIEnv* env, const TSQueryPredicateStep* predicate);

0 commit comments

Comments
 (0)