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

Commit 0a3f66a

Browse files
committed
feat: add new APIs for TSTreeCursor
1 parent fffcc60 commit 0a3f66a

File tree

2 files changed

+288
-29
lines changed

2 files changed

+288
-29
lines changed

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

Lines changed: 142 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,65 +17,178 @@
1717

1818
#include "utils/ts_obj_utils.h"
1919

20-
extern "C" JNIEXPORT jlong JNICALL Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_newCursor(
21-
JNIEnv* env, jclass self, jobject node) {
22-
TSTreeCursor* cursor =
23-
new TSTreeCursor(ts_tree_cursor_new(_unmarshalNode(env, node)));
24-
return (jlong)cursor;
20+
extern "C" JNIEXPORT jlong JNICALL
21+
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_newCursor(
22+
JNIEnv *env, jclass self, jobject node) {
23+
auto *cursor =
24+
new TSTreeCursor(ts_tree_cursor_new(_unmarshalNode(env, node)));
25+
return (jlong) cursor;
2526
}
2627

2728
extern "C" JNIEXPORT jobject JNICALL
2829
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_currentTreeCursorNode(
29-
JNIEnv* env, jclass self, jlong cursor) {
30-
TSNode node = ts_tree_cursor_current_node((TSTreeCursor*)cursor);
30+
JNIEnv *env, jclass self, jlong cursor) {
31+
TSNode node = ts_tree_cursor_current_node((TSTreeCursor *) cursor);
3132
return _marshalTreeCursorNode(
32-
env,
33-
(TreeCursorNode) {
34-
ts_node_type(node),
35-
ts_tree_cursor_current_field_name((TSTreeCursor*)cursor),
36-
ts_node_start_byte(node) / 2, ts_node_end_byte(node) / 2
37-
});
33+
env,
34+
(TreeCursorNode) {
35+
ts_node_type(node),
36+
ts_tree_cursor_current_field_name((TSTreeCursor *) cursor),
37+
ts_node_start_byte(node) / 2, ts_node_end_byte(node) / 2
38+
});
3839
}
3940

4041

4142
extern "C" JNIEXPORT jstring JNICALL
4243
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_currentFieldName(
43-
JNIEnv* env, jclass self, jlong cursor) {
44-
const char* name = ts_tree_cursor_current_field_name((TSTreeCursor*)cursor);
44+
JNIEnv *env, jclass self, jlong cursor) {
45+
const char *name = ts_tree_cursor_current_field_name((TSTreeCursor *) cursor);
4546
jstring result = env->NewStringUTF(name);
4647
return result;
4748
}
4849

4950
extern "C" JNIEXPORT jobject JNICALL
50-
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_currentNode(JNIEnv* env,
51+
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_currentNode(
52+
JNIEnv *env,
5153
jclass self,
5254
jlong cursor) {
53-
return _marshalNode(env, ts_tree_cursor_current_node((TSTreeCursor*)cursor));
55+
return _marshalNode(env,
56+
ts_tree_cursor_current_node((TSTreeCursor *) cursor));
5457
}
5558

56-
extern "C" JNIEXPORT void JNICALL Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_delete(
57-
JNIEnv* env, jclass self, jlong cursor) {
58-
ts_tree_cursor_delete((TSTreeCursor*)cursor);
59-
delete (TSTreeCursor*)cursor;
59+
extern "C" JNIEXPORT void JNICALL
60+
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_delete(
61+
JNIEnv *env, jclass self, jlong cursor) {
62+
ts_tree_cursor_delete((TSTreeCursor *) cursor);
63+
delete (TSTreeCursor *) cursor;
6064
}
6165

6266
extern "C" JNIEXPORT jboolean JNICALL
63-
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_gotoFirstChild(JNIEnv* env,
67+
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_gotoFirstChild(
68+
JNIEnv *env,
6469
jclass self,
6570
jlong cursor) {
66-
return (jboolean)ts_tree_cursor_goto_first_child((TSTreeCursor*)cursor);
71+
return (jboolean) ts_tree_cursor_goto_first_child((TSTreeCursor *) cursor);
6772
}
6873

6974
extern "C" JNIEXPORT jboolean JNICALL
70-
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_gotoNextSibling(JNIEnv* env,
75+
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_gotoNextSibling(
76+
JNIEnv *env,
7177
jclass self,
7278
jlong cursor) {
73-
return (jboolean)ts_tree_cursor_goto_next_sibling((TSTreeCursor*)cursor);
79+
return (jboolean) ts_tree_cursor_goto_next_sibling((TSTreeCursor *) cursor);
7480
}
7581

7682
extern "C" JNIEXPORT jboolean JNICALL
77-
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_gotoParent(JNIEnv* env,
78-
jclass self,
79-
jlong cursor) {
80-
return (jboolean)ts_tree_cursor_goto_parent((TSTreeCursor*)cursor);
83+
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_gotoParent(JNIEnv *env,
84+
jclass self,
85+
jlong cursor) {
86+
return (jboolean) ts_tree_cursor_goto_parent((TSTreeCursor *) cursor);
87+
}
88+
89+
extern "C"
90+
JNIEXPORT jshort JNICALL
91+
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_currentFieldId(
92+
JNIEnv *env,
93+
jclass clazz,
94+
jlong pointer) {
95+
96+
return (jshort) ts_tree_cursor_current_field_id((TSTreeCursor *) pointer);
97+
}
98+
99+
extern "C"
100+
JNIEXPORT jlong JNICALL
101+
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_gotoFirstChildForByte(
102+
JNIEnv *env,
103+
jclass clazz,
104+
jlong pointer,
105+
jint byte_index) {
106+
return (jlong) ts_tree_cursor_goto_first_child_for_byte((TSTreeCursor *) pointer,
107+
byte_index);
108+
}
109+
110+
extern "C"
111+
JNIEXPORT jboolean JNICALL
112+
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_gotoFirstChildForPoint(
113+
JNIEnv *env,
114+
jclass clazz,
115+
jlong pointer,
116+
jobject point) {
117+
return (jboolean) ts_tree_cursor_goto_first_child_for_point((TSTreeCursor *) pointer,
118+
_unmarshalPoint(
119+
env,
120+
point));
121+
}
122+
123+
extern "C"
124+
JNIEXPORT jboolean JNICALL
125+
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_gotoLastChild(
126+
JNIEnv *env,
127+
jclass clazz,
128+
jlong pointer) {
129+
return (jboolean) ts_tree_cursor_goto_last_child((TSTreeCursor *) pointer);
130+
}
131+
132+
extern "C"
133+
JNIEXPORT jboolean JNICALL
134+
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_gotoPreviousSibling(
135+
JNIEnv *env,
136+
jclass clazz,
137+
jlong pointer) {
138+
return (jboolean) ts_tree_cursor_goto_previous_sibling((TSTreeCursor *) pointer);
139+
}
140+
141+
extern "C"
142+
JNIEXPORT void JNICALL
143+
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_gotoDescendant(
144+
JNIEnv *env,
145+
jclass clazz,
146+
jlong pointer,
147+
jint descendant_index) {
148+
149+
ts_tree_cursor_goto_descendant((TSTreeCursor *) pointer, descendant_index);
150+
}
151+
152+
extern "C"
153+
JNIEXPORT jint JNICALL
154+
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_currentDescendantIndex(
155+
JNIEnv *env,
156+
jclass clazz,
157+
jlong pointer) {
158+
return (jint) ts_tree_cursor_current_descendant_index((TSTreeCursor *) pointer);
159+
}
160+
161+
extern "C"
162+
JNIEXPORT jint JNICALL
163+
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_depth(JNIEnv *env,
164+
jclass clazz,
165+
jlong pointer) {
166+
return (jint) ts_tree_cursor_current_depth((TSTreeCursor *) pointer);
167+
}
168+
169+
extern "C"
170+
JNIEXPORT void JNICALL
171+
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_reset(JNIEnv *env,
172+
jclass clazz,
173+
jlong pointer,
174+
jobject node) {
175+
ts_tree_cursor_reset((TSTreeCursor *) pointer, _unmarshalNode(env, node));
176+
}
177+
178+
extern "C"
179+
JNIEXPORT void JNICALL
180+
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_resetTo(JNIEnv *env,
181+
jclass clazz,
182+
jlong pointer,
183+
jlong another) {
184+
ts_tree_cursor_reset_to((TSTreeCursor *) pointer, (TSTreeCursor *) another);
185+
}
186+
187+
extern "C"
188+
JNIEXPORT jlong JNICALL
189+
Java_com_itsaky_androidide_treesitter_TSTreeCursor_00024Native_copy(JNIEnv *env,
190+
jclass clazz,
191+
jlong pointer) {
192+
auto *copied = new TSTreeCursor (ts_tree_cursor_copy((TSTreeCursor *) pointer));
193+
return (jlong) copied;
81194
}

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

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.itsaky.androidide.treesitter;
1919

2020
public class TSTreeCursor extends TSNativeObject {
21+
2122
private int context0;
2223
private int context1;
2324
private long id;
@@ -56,6 +57,16 @@ public String getCurrentFieldName() {
5657
return Native.currentFieldName(pointer);
5758
}
5859

60+
/**
61+
* Get the field id of the tree cursor's current node.
62+
* <p>
63+
* This returns zero if the current node doesn't have a field.
64+
*/
65+
public short getCurrentFieldId() {
66+
checkAccess();
67+
return Native.currentFieldId(pointer);
68+
}
69+
5970
/**
6071
* Get the current tree cursor node.
6172
*
@@ -76,6 +87,39 @@ public boolean gotoFirstChild() {
7687
return Native.gotoFirstChild(pointer);
7788
}
7889

90+
/**
91+
* Move the cursor to the first child of its current node that extends beyond the given byte
92+
* offset.
93+
* <p>
94+
* This returns the index of the child node if one was found, and returns -1 if no such child was
95+
* found.
96+
*/
97+
public long gotoFirstChildForByte(int byteIndex) {
98+
checkAccess();
99+
return Native.gotoFirstChildForByte(pointer, byteIndex);
100+
}
101+
102+
/**
103+
* Move the cursor to the first child of its current node that extends beyond the given point.
104+
* <p>
105+
* This returns the index of the child node if one was found, and returns -1 if no such child was
106+
* found.
107+
*/
108+
public boolean gotoFirstChildForPoint(TSPoint point) {
109+
checkAccess();
110+
return Native.gotoFirstChildForPoint(pointer, point);
111+
}
112+
113+
/**
114+
* Move to the last child.
115+
*
116+
* @return <code>true</code> if moved successfully, <code>false</code> otherwise.
117+
*/
118+
public boolean gotoLastChild() {
119+
checkAccess();
120+
return Native.gotoLastChild(pointer);
121+
}
122+
79123
/**
80124
* Moves to the next sibling node.
81125
*
@@ -86,6 +130,16 @@ public boolean gotoNextSibling() {
86130
return Native.gotoNextSibling(pointer);
87131
}
88132

133+
/**
134+
* Move to the previous sibling node.
135+
*
136+
* @return <code>true</code> if moved successfully, <code>false</code> otherwise.
137+
*/
138+
public boolean gotoPreviousSibling() {
139+
checkAccess();
140+
return Native.gotoPreviousSibling(pointer);
141+
}
142+
89143
/**
90144
* Moves to the parent node.
91145
*
@@ -96,14 +150,106 @@ public boolean gotoParent() {
96150
return Native.gotoParent(pointer);
97151
}
98152

153+
/**
154+
* Move the cursor to the node that is the nth descendant of the original node that the cursor was
155+
* constructed with, where zero represents the original node itself.
156+
*/
157+
public void gotoDescendant(int descendantIndex) {
158+
checkAccess();
159+
Native.gotoDescendant(pointer, descendantIndex);
160+
}
161+
162+
/**
163+
* Get the index of the cursor's current node out of all of the
164+
* descendants of the original node that the cursor was constructed with.
165+
*/
166+
public int getCurrentDescendantIndex() {
167+
checkAccess();
168+
return Native.currentDescendantIndex(pointer);
169+
}
170+
171+
/**
172+
* Get the depth of the cursor's current node relative to the original
173+
* node that the cursor was constructed with.
174+
*/
175+
public int getDepth() {
176+
checkAccess();
177+
return Native.depth(pointer);
178+
}
179+
180+
/**
181+
* Re-initialize a tree cursor to start at a different node.
182+
*/
183+
public void reset(TSNode node) {
184+
checkAccess();
185+
Native.reset(pointer, node);
186+
}
187+
188+
/**
189+
* Re-initialize a tree cursor to the same position as another cursor.
190+
* <p>
191+
* Unlike {@link #reset(TSNode)}, this will not lose parent information and allows reusing already
192+
* created cursors.
193+
*/
194+
public void resetTo(TSTreeCursor another) {
195+
checkAccess();
196+
another.checkAccess();
197+
Native.resetTo(pointer, another.pointer);
198+
}
199+
200+
/**
201+
* Create a copy of this cursor.
202+
*
203+
* @return The copied {@link TSTreeCursor} or <code>null</code> if there was an error copying the
204+
* cursor.
205+
*/
206+
public TSTreeCursor copy() {
207+
checkAccess();
208+
final var pointer = Native.copy(this.pointer);
209+
if (pointer == 0) {
210+
return null;
211+
}
212+
return new TSTreeCursor(pointer);
213+
}
214+
99215
private static class Native {
216+
100217
public static native long newCursor(TSNode node);
218+
101219
public static native TSTreeCursorNode currentTreeCursorNode(long cursor);
220+
102221
public static native String currentFieldName(long cursor);
222+
103223
public static native TSNode currentNode(long cursor);
224+
104225
public static native void delete(long cursor);
226+
105227
public static native boolean gotoFirstChild(long cursor);
228+
106229
public static native boolean gotoNextSibling(long cursor);
230+
107231
public static native boolean gotoParent(long cursor);
232+
233+
public static native short currentFieldId(long pointer);
234+
235+
public static native long gotoFirstChildForByte(long pointer, int byteIndex);
236+
237+
public static native boolean gotoFirstChildForPoint(long pointer, TSPoint point);
238+
239+
public static native boolean gotoLastChild(long pointer);
240+
241+
public static native boolean gotoPreviousSibling(long pointer);
242+
243+
public static native void gotoDescendant(long pointer, int descendantIndex);
244+
245+
public static native int currentDescendantIndex(long pointer);
246+
247+
public static native int depth(long pointer);
248+
249+
public static native void reset(long pointer, TSNode node);
250+
251+
public static native void resetTo(long pointer, long another);
252+
253+
public static native long copy(long pointer);
108254
}
109255
}

0 commit comments

Comments
 (0)