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

Commit 2511004

Browse files
committed
Added more TSParser API implementations
1 parent d6e9ae6 commit 2511004

File tree

9 files changed

+93
-14
lines changed

9 files changed

+93
-14
lines changed

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

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,6 @@ 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-
112100
/**
113101
* Set the maximum duration in microseconds that parsing should be allowed to take before halting.
114102
*
@@ -127,6 +115,46 @@ public long getTimeout() {
127115
return Native.getTimeout(this.pointer);
128116
}
129117

118+
/**
119+
* Set the ranges of text that the parser should include when parsing.
120+
*
121+
* <p>By default, the parser will always include entire documents. This function allows you to
122+
* parse only a *portion* of a document but still return a syntax tree whose ranges match up with
123+
* the document as a whole. You can also pass multiple disjoint ranges.
124+
*
125+
* <p>The second and third parameters specify the location and length of an array of ranges. The
126+
* parser does *not* take ownership of these ranges; it copies the data, so it doesn't matter how
127+
* these ranges are allocated.
128+
*
129+
* <p>If the ranges parameter is an empty array, then the entire document will be parsed.
130+
* Otherwise, the given ranges must be ordered from earliest to latest in the document, and they
131+
* must not overlap. That is, the following must hold for all `i` < `length - 1`:
132+
* ranges[i].end_byte <= ranges[i + 1].start_byte where `length` is the length of the ranges
133+
* array.
134+
*
135+
* <p>If this requirement is not satisfied, the operation will fail, the ranges will not be
136+
* assigned, and this function will return `false`. On success, this function returns `true`
137+
*/
138+
public boolean setIncludedRanges(TSRange[] ranges) {
139+
return Native.setIncludedRanges(this.pointer, ranges);
140+
}
141+
142+
public TSRange[] getIncludedRanges() {
143+
return Native.getIncludedRanges(this.pointer);
144+
}
145+
146+
/**
147+
* Instruct the parser to start the next parse from the beginning.
148+
*
149+
* <p>If the parser previously failed because of a timeout or a cancellation, then by default, it
150+
* will resume where it left off on the next call to any of the parsing functions. If you don't
151+
* want to resume, and instead intend to use this parser to parse some other document, you must
152+
* call this function first.
153+
*/
154+
public void reset() {
155+
Native.reset(this.pointer);
156+
}
157+
130158
/** Closes and deletes the current parser. */
131159
@Override
132160
public void close() {
@@ -152,5 +180,9 @@ public static native long incrementalParseBytes(
152180
public static native void setTimeout(long parser, long timeout);
153181

154182
public static native long getTimeout(long parser);
183+
184+
public static native boolean setIncludedRanges(long parser, TSRange[] ranges);
185+
186+
public static native TSRange[] getIncludedRanges(long parser);
155187
}
156188
}
845 KB
Binary file not shown.
621 KB
Binary file not shown.
754 KB
Binary file not shown.
797 KB
Binary file not shown.

lib/com_itsaky_androidide_treesitter_TSParser_Native.h

Lines changed: 16 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: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "com_itsaky_androidide_treesitter_TSParser_Native.h"
22
#include "ts_utils.h"
33

4+
#include <iostream>
5+
46
JNIEXPORT jlong JNICALL
57
Java_com_itsaky_androidide_treesitter_TSParser_00024Native_newParser(
68
JNIEnv* env, jclass self) {
@@ -69,5 +71,33 @@ Java_com_itsaky_androidide_treesitter_TSParser_00024Native_setTimeout(
6971
JNIEXPORT jlong JNICALL
7072
Java_com_itsaky_androidide_treesitter_TSParser_00024Native_getTimeout(
7173
JNIEnv* env, jclass self, jlong parser) {
72-
return (jlong) ts_parser_timeout_micros((TSParser*)parser);
74+
return (jlong)ts_parser_timeout_micros((TSParser*)parser);
75+
}
76+
77+
JNIEXPORT jboolean JNICALL
78+
Java_com_itsaky_androidide_treesitter_TSParser_00024Native_setIncludedRanges(
79+
JNIEnv* env, jclass self, jlong parser, jobjectArray ranges) {
80+
int count = env->GetArrayLength(ranges);
81+
TSRange tsRanges[count];
82+
for (int i = 0; i < count; i++) {
83+
tsRanges[i] = _unmarshalRange(env, env->GetObjectArrayElement(ranges, i));
84+
}
85+
86+
const TSRange* r = tsRanges;
87+
return (jboolean) ts_parser_set_included_ranges((TSParser*)parser, r, count);
88+
}
89+
90+
JNIEXPORT jobjectArray JNICALL
91+
Java_com_itsaky_androidide_treesitter_TSParser_00024Native_getIncludedRanges(
92+
JNIEnv* env, jclass self, jlong parser) {
93+
uint32_t count;
94+
const TSRange* ranges = ts_parser_included_ranges((TSParser*)parser, &count);
95+
std::cout << "Number of included ranges is " << count << std::endl;
96+
jclass klass = env->FindClass("com/itsaky/androidide/treesitter/TSRange");
97+
jobjectArray result = env->NewObjectArray(count, klass, NULL);
98+
for (uint32_t i = 0; i < count; i++) {
99+
const TSRange *r = (ranges + i);
100+
env->SetObjectArrayElement(result, i, _marshalRange(env, *r));
101+
}
102+
return result;
73103
}

lib/ts_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct TreeCursorNode {
1818

1919
#define _loadField(VARIABLE, CLASS, NAME, TYPE) \
2020
{ VARIABLE = env->GetFieldID(CLASS, NAME, TYPE); }
21-
21+
2222

2323
void onLoad(JNIEnv* env);
2424

lib/utils.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ static jclass _pointClass;
2424
static jfieldID _pointRowField;
2525
static jfieldID _pointColumnField;
2626

27+
// TSRange
2728
static jclass _rangeClass;
2829
static jfieldID _rangeClassStartByteField;
2930
static jfieldID _rangeClassEndByteField;

0 commit comments

Comments
 (0)