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

Commit b909970

Browse files
committed
Implemented more TSParser APIs
1 parent acb91d2 commit b909970

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,36 @@ public TSTree parseString(TSTree oldTree, String source, TSInputEncoding encodin
9797
pointer, oldTree.getPointer(), bytes, bytes.length, encoding.getFlag()));
9898
}
9999

100+
/**
101+
* Instruct the parser to start the next parse from the beginning.
102+
*
103+
* <p>If the parser previously failed because of a timeout or a cancellation, then by default, it
104+
* will resume where it left off on the next call to any of the parsing functions. If you don't
105+
* want to resume, and instead intend to use this parser to parse some other document, you must
106+
* call this function first.
107+
*/
108+
public void reset() {
109+
Native.reset(this.pointer);
110+
}
111+
112+
/**
113+
* Set the maximum duration in microseconds that parsing should be allowed to take before halting.
114+
*
115+
* <p>If parsing takes longer than this, it will halt early, returning <code>null</code>.
116+
*/
117+
public void setTimeout(long microseconds) {
118+
Native.setTimeout(this.pointer, microseconds);
119+
}
120+
121+
/**
122+
* Get the duration in microseconds that parsing is allowed to take.
123+
*
124+
* @return The timeout in microseconds.
125+
*/
126+
public long getTimeout() {
127+
return Native.getTimeout(this.pointer);
128+
}
129+
100130
/** Closes and deletes the current parser. */
101131
@Override
102132
public void close() {
@@ -116,5 +146,11 @@ private static class Native {
116146

117147
public static native long incrementalParseBytes(
118148
long parser, long old_tree, byte[] source, int length, int encoding);
149+
150+
public static native void reset(long parser);
151+
152+
public static native void setTimeout(long parser, long timeout);
153+
154+
public static native long getTimeout(long parser);
119155
}
120156
}

lib/com_itsaky_androidide_treesitter_TSParser_Native.h

Lines changed: 24 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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,23 @@ Java_com_itsaky_androidide_treesitter_TSParser_00024Native_incrementalParseBytes
5151
reinterpret_cast<const char*>(source), length, TSInputEncodingUTF16);
5252
env->ReleaseByteArrayElements(source_bytes, source, JNI_ABORT);
5353
return result;
54+
}
55+
56+
JNIEXPORT void JNICALL
57+
Java_com_itsaky_androidide_treesitter_TSParser_00024Native_reset(JNIEnv* env,
58+
jclass self,
59+
jlong parser) {
60+
ts_parser_reset((TSParser*)parser);
61+
}
62+
63+
JNIEXPORT void JNICALL
64+
Java_com_itsaky_androidide_treesitter_TSParser_00024Native_setTimeout(
65+
JNIEnv* env, jclass self, jlong parser, jlong macros) {
66+
ts_parser_set_timeout_micros((TSParser*)parser, macros);
67+
}
68+
69+
JNIEXPORT jlong JNICALL
70+
Java_com_itsaky_androidide_treesitter_TSParser_00024Native_getTimeout(
71+
JNIEnv* env, jclass self, jlong parser) {
72+
return (jlong) ts_parser_timeout_micros((TSParser*)parser);
5473
}

0 commit comments

Comments
 (0)