Skip to content

Commit 9103f93

Browse files
feat: implement Query.timeoutMicros
1 parent 268f633 commit 9103f93

File tree

9 files changed

+81
-2
lines changed
  • ktreesitter/src

9 files changed

+81
-2
lines changed

ktreesitter/src/androidInstrumentedTest/kotlin/io/github/treesitter/ktreesitter/QueryTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ class QueryTest :
7373
query.captureCount shouldBe 3U
7474
}
7575

76+
test("timeoutMicros") {
77+
val query = Query(language, source)
78+
query.timeoutMicros shouldBe 0UL
79+
query.timeoutMicros = 10UL
80+
query.timeoutMicros shouldBe 10UL
81+
}
82+
7683
test("matchLimit") {
7784
val query = Query(language, source)
7885
query.matchLimit shouldBe UInt.MAX_VALUE

ktreesitter/src/androidMain/kotlin/io/github/treesitter/ktreesitter/Query.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,24 @@ actual class Query @Throws(QueryError::class) actual constructor(
267267
}
268268
}
269269

270+
/**
271+
* The maximum duration in microseconds that query
272+
* execution should be allowed to take before halting.
273+
*
274+
* Default: `0`
275+
*/
276+
@get:JvmName("getTimeoutMicros")
277+
@set:JvmName("setTimeoutMicros")
278+
actual var timeoutMicros: ULong
279+
@FastNative external get
280+
281+
@FastNative external set
282+
270283
/**
271284
* The maximum number of in-progress matches.
272285
*
286+
* Default: `UInt.MAX_VALUE`
287+
*
273288
* @throws [IllegalArgumentException] If the match limit is set to `0`.
274289
*/
275290
@get:JvmName("getMatchLimit")

ktreesitter/src/commonMain/kotlin/io/github/treesitter/ktreesitter/Query.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,19 @@ expect class Query @Throws(QueryError::class) constructor(language: Language, so
1515
/** The number of captures in the query. */
1616
val captureCount: UInt
1717

18+
/**
19+
* The maximum duration in microseconds that query
20+
* execution should be allowed to take before halting.
21+
*
22+
* Default: `0`
23+
*/
24+
var timeoutMicros: ULong
25+
1826
/**
1927
* The maximum number of in-progress matches.
2028
*
29+
* Default: `UInt.MAX_VALUE`
30+
*
2131
* @throws [IllegalArgumentException] If the match limit is set to `0`.
2232
*/
2333
var matchLimit: UInt

ktreesitter/src/commonTest/kotlin/io/github/treesitter/ktreesitter/QueryTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ class QueryTest :
7070
query.captureCount shouldBe 3U
7171
}
7272

73+
test("timeoutMicros") {
74+
val query = Query(language, source)
75+
query.timeoutMicros shouldBe 0UL
76+
query.timeoutMicros = 10UL
77+
query.timeoutMicros shouldBe 10UL
78+
}
79+
7380
test("matchLimit") {
7481
val query = Query(language, source)
7582
query.matchLimit shouldBe UInt.MAX_VALUE

ktreesitter/src/jni/query.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ jint query_get_capture_count(JNIEnv *env, jobject this) {
121121
return (jint)ts_query_capture_count(self);
122122
}
123123

124+
jlong query_get_timeout_micros(JNIEnv *env, jobject this) {
125+
TSQueryCursor *cursor = GET_POINTER(TSQueryCursor, this, Query_cursor);
126+
return (jlong)ts_query_cursor_timeout_micros(cursor);
127+
}
128+
129+
void query_set_timeout_micros(JNIEnv *env, jobject this, jlong value) {
130+
TSQueryCursor *cursor = GET_POINTER(TSQueryCursor, this, Query_cursor);
131+
ts_query_cursor_set_timeout_micros(cursor, (uint64_t)value);
132+
(*env)->SetLongField(env, this, global_field_cache.Query_timeoutMicros, value);
133+
}
134+
124135
jint query_get_match_limit(JNIEnv *env, jobject this) {
125136
TSQueryCursor *cursor = GET_POINTER(TSQueryCursor, this, Query_cursor);
126137
return (jint)ts_query_cursor_match_limit(cursor);
@@ -338,6 +349,8 @@ const JNINativeMethod Query_methods[] = {
338349
{"delete", "(JJ)V", (void *)&query_delete},
339350
{"getPatternCount", "()I", (void *)&query_get_pattern_count},
340351
{"getCaptureCount", "()I", (void *)&query_get_capture_count},
352+
{"getTimeoutMicros", "()J", (void *)&query_get_timeout_micros},
353+
{"setTimeoutMicros", "(J)V", (void *)&query_set_timeout_micros},
341354
{"getMatchLimit", "()I", (void *)&query_get_match_limit},
342355
{"setMatchLimit", "(I)V", (void *)&query_set_match_limit},
343356
{"setMaxStartDepth", "(I)V", (void *)&query_set_max_start_depth},

ktreesitter/src/jni/utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ typedef struct {
8282
jfieldID Query_maxStartDepth;
8383
jfieldID Query_self;
8484
jfieldID Query_source;
85+
jfieldID Query_timeoutMicros;
8586
jfieldID Range_endByte;
8687
jfieldID Range_endPoint;
8788
jfieldID Range_startByte;

ktreesitter/src/jvmMain/kotlin/io/github/treesitter/ktreesitter/Parser.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ actual class Parser actual constructor() {
118118

119119
override fun toString() = "Parser(language=$language)"
120120

121-
// private external fun nativeParse(oldTree: Tree?, callback: (Int, Point) -> CharSequence?): Tree
122-
123121
/** The type of a log message. */
124122
@Suppress("unused")
125123
actual enum class LogType { LEX, PARSE }

ktreesitter/src/jvmMain/kotlin/io/github/treesitter/ktreesitter/Query.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,23 @@ actual class Query @Throws(QueryError::class) actual constructor(
261261
}
262262
}
263263

264+
/**
265+
* The maximum duration in microseconds that query
266+
* execution should be allowed to take before halting.
267+
*
268+
* Default: `0`
269+
*/
270+
@get:JvmName("getTimeoutMicros")
271+
@set:JvmName("setTimeoutMicros")
272+
actual var timeoutMicros: ULong
273+
external get
274+
external set
275+
264276
/**
265277
* The maximum number of in-progress matches.
266278
*
279+
* Default: `UInt.MAX_VALUE`
280+
*
267281
* @throws [IllegalArgumentException] If the match limit is set to `0`.
268282
*/
269283
@get:JvmName("getMatchLimit")

ktreesitter/src/nativeMain/kotlin/io/github/treesitter/ktreesitter/Query.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,23 @@ actual class Query @Throws(QueryError::class) actual constructor(
348348
@OptIn(ExperimentalNativeApi::class)
349349
private val cursorCleaner = createCleaner(cursor, ::ts_query_cursor_delete)
350350

351+
/**
352+
* The maximum duration in microseconds that query
353+
* execution should be allowed to take before halting.
354+
*
355+
* Default: `0`
356+
*/
357+
actual var timeoutMicros: ULong
358+
get() = ts_query_cursor_timeout_micros(cursor)
359+
set(value) {
360+
ts_query_cursor_set_timeout_micros(cursor, value)
361+
}
362+
351363
/**
352364
* The maximum number of in-progress matches.
353365
*
366+
* Default: `UInt.MAX_VALUE`
367+
*
354368
* @throws [IllegalArgumentException] If the match limit is set to `0`.
355369
*/
356370
actual var matchLimit: UInt

0 commit comments

Comments
 (0)