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

Commit 2d9ccf1

Browse files
committed
feat: add state APIs in TSLanguage
1 parent 0a3f66a commit 2d9ccf1

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

.idea/kotlinc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

android-tree-sitter/src/main/cpp/ts_language.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,22 @@ Java_com_itsaky_androidide_treesitter_TSLanguage_00024Native_dlclose(JNIEnv *env
155155
jlong libhandle) {
156156
if (libhandle == 0) return;
157157
dlclose((void *) libhandle);
158+
}
159+
160+
extern "C"
161+
JNIEXPORT jint JNICALL
162+
Java_com_itsaky_androidide_treesitter_TSLanguage_00024Native_stateCount(JNIEnv *env,
163+
jclass clazz,
164+
jlong pointer) {
165+
return (jint) ts_language_state_count((TSLanguage*) pointer);
166+
}
167+
168+
extern "C"
169+
JNIEXPORT jshort JNICALL
170+
Java_com_itsaky_androidide_treesitter_TSLanguage_00024Native_nextState(JNIEnv *env,
171+
jclass clazz,
172+
jlong pointer,
173+
jshort state_id,
174+
jshort symbol) {
175+
return (jshort) ts_language_next_state((TSLanguage*) pointer, state_id, symbol);
158176
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,23 @@ public TSSymbolType getSymbolType(int symbol) {
127127
return TSSymbolType.forId(Native.symType(this.pointer, symbol));
128128
}
129129

130+
/**
131+
* Get the number of valid states in this language.
132+
*/
133+
public int getStateCount() {
134+
checkAccess();
135+
return Native.stateCount(pointer);
136+
}
137+
138+
/**
139+
* Get the next parse state. Combine this with lookahead iterators to generate
140+
* completion suggestions or valid symbols in error nodes.
141+
*/
142+
public short getNextState(short stateId, short symbol) {
143+
checkAccess();
144+
return Native.nextState(pointer, stateId, symbol);
145+
}
146+
130147
public int getLanguageVersion() {
131148
checkAccess();
132149
return Native.langVer(this.pointer);
@@ -246,5 +263,9 @@ private static class Native {
246263
private static native long[] loadLanguage(String sharedLib, String func);
247264

248265
private static native void dlclose(long libhandle);
266+
267+
public static native int stateCount(long pointer);
268+
269+
public static native short nextState(long pointer, short stateId, short symbol);
249270
}
250271
}

0 commit comments

Comments
 (0)