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

Commit 2387873

Browse files
committed
Added more API implementations for TSNode
1 parent ffce1e3 commit 2387873

File tree

9 files changed

+283
-17
lines changed

9 files changed

+283
-17
lines changed

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,87 @@ public String toString() {
139139
*/
140140
public native String getFieldNameForChild(int childIndex);
141141

142+
/**
143+
* Get the child for the given field id.
144+
*
145+
* @param fieldId The field id.
146+
* @return The child node. Maybe <code>null</code>.
147+
*/
148+
public native TSNode getChildByFieldId(int fieldId);
149+
150+
/**
151+
* Get the next sibling node of this node.
152+
*
153+
* @return The next sibling node.
154+
*/
155+
public native TSNode getNextSibling();
156+
157+
/**
158+
* Get the previous sibling node of this node.
159+
*
160+
* @return The previous sibling node.
161+
*/
162+
public native TSNode getPreviousSibling();
163+
164+
/**
165+
* Get the next named sibling node of this node.
166+
*
167+
* @return The next named sibling node.
168+
*/
169+
public native TSNode getNextNamedSibling();
170+
171+
/**
172+
* Get the previous named sibling node of this node.
173+
*
174+
* @return The previous named sibling node.
175+
*/
176+
public native TSNode getPreviousNamedSibling();
177+
178+
/**
179+
* Get the node's first child that extends beyond the given byte offset.
180+
*
181+
* @param byteOffset The byte offsest.
182+
* @return The first child beyond the byte offset.
183+
*/
184+
public native TSNode getFirstChildForByte(int byteOffset);
185+
186+
/**
187+
* Get the node's first named child that extends beyond the given byte offset.
188+
*
189+
* @param byteOffset The byte offsest.
190+
* @return The first named child beyond the byte offset.
191+
*/
192+
public native TSNode getFirstNamedChildForByte(int byteOffset);
193+
194+
/**
195+
* Get the smallest node within this node that spans the given range of bytes or (row, column)
196+
* positions.
197+
*/
198+
public native TSNode getDescendantForByteRange(int start, int end);
199+
200+
/**
201+
* @see #getDescendantForByteRange(int, int)
202+
*/
203+
public native TSNode getDescendantForPointRange(TSPoint start, TSPoint end);
204+
205+
/**
206+
* Get the smallest node within this node that spans the given range of bytes or (row, column)
207+
* positions.
208+
*/
209+
public native TSNode getNamedDescendantForByteRange(int start, int end);
210+
211+
/**
212+
* @see #getNamedDescendantForByteRange(int, int)
213+
*/
214+
public native TSNode getNamedDescendantForPointRange(TSPoint start, TSPoint end);
215+
216+
/**
217+
* Check if this node and the other node are identical.
218+
*
219+
* @param another The node to check.
220+
*/
221+
public native boolean isEqualTo(TSNode another);
222+
142223
/**
143224
* Get the number of children of the node.
144225
*
4.5 KB
Binary file not shown.
5.96 KB
Binary file not shown.
5.16 KB
Binary file not shown.
5.3 KB
Binary file not shown.

android-tree-sitter/src/test/java/com/itsaky/androidide/treesitter/NodeTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public void testGetChildren() throws UnsupportedEncodingException {
2020
var root = tree.getRootNode();
2121
var symbol = root.getSymbol();
2222
assertThat(tree.getLanguage().getSymbolName(symbol)).isEqualTo("module");
23+
assertThat(root.getFieldNameForChild(0)).isNull();
2324

2425
var start = root.getStartPoint();
2526
assertThat(0).isEqualTo(start.row);
@@ -64,11 +65,11 @@ public void testGetChildren() throws UnsupportedEncodingException {
6465
assertThat(isNull).isFalse();
6566

6667
var function = root.getChild(0);
67-
var fieldNameForChild = root.getFieldNameForChild(0);
68-
assertThat(fieldNameForChild).isNull();
6968
start = function.getStartPoint();
7069
assertThat(0).isEqualTo(start.row);
7170
assertThat(0).isEqualTo(start.column);
71+
assertThat(function.isEqualTo(function)).isTrue();
72+
assertThat(function.getFieldNameForChild(1)).isEqualTo("name");
7273

7374
end = function.getEndPoint();
7475
assertThat(2).isEqualTo(end.row);

android-tree-sitter/src/test/java/com/itsaky/androidide/treesitter/TreeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void testTreeCopy() throws UnsupportedEncodingException {
1818
try (final var tree =
1919
parser.parseString(
2020
"class Main { void main() {} }", TSInputEncoding.TSInputEncodingUTF8)) {
21-
assertThat(tree.getLanguage()).isEqualTo(TSLanguages.java());
21+
assertThat(tree.getLanguage().pointer).isEqualTo(TSLanguages.java().pointer);
2222
assertThat(tree.copy().getRootNode().getNodeString())
2323
.isEqualTo(tree.getRootNode().getNodeString());
2424
}

lib/com_itsaky_androidide_treesitter_TSNode.h

Lines changed: 96 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ts_node.cc

Lines changed: 102 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,104 @@ Java_com_itsaky_androidide_treesitter_TSNode_getChildByFieldName(
4747
return found;
4848
}
4949

50-
JNIEXPORT jstring JNICALL Java_com_itsaky_androidide_treesitter_TSNode_getFieldNameForChild
51-
(JNIEnv* env, jobject self, jint index) {
52-
const char* fieldName = ts_node_field_name_for_child(_unmarshalNode(env, self), index);
53-
if (fieldName == NULL) {
54-
return NULL;
55-
}
56-
57-
jstring result = env->NewStringUTF(fieldName);
58-
free((char*)fieldName);
59-
return result;
50+
JNIEXPORT jstring JNICALL
51+
Java_com_itsaky_androidide_treesitter_TSNode_getFieldNameForChild(JNIEnv* env,
52+
jobject self,
53+
jint index) {
54+
const char* fieldName =
55+
ts_node_field_name_for_child(_unmarshalNode(env, self), index);
56+
if (fieldName == NULL) {
57+
return NULL;
58+
}
59+
60+
jstring result = env->NewStringUTF(fieldName);
61+
return result;
62+
}
63+
64+
JNIEXPORT jobject JNICALL
65+
Java_com_itsaky_androidide_treesitter_TSNode_getChildByFieldId(JNIEnv* env,
66+
jobject self,
67+
jint fieldId) {
68+
return _marshalNode(
69+
env, ts_node_child_by_field_id(_unmarshalNode(env, self), fieldId));
70+
}
71+
72+
JNIEXPORT jobject JNICALL
73+
Java_com_itsaky_androidide_treesitter_TSNode_getNextSibling(JNIEnv* env,
74+
jobject self) {
75+
return _marshalNode(env, ts_node_next_sibling(_unmarshalNode(env, self)));
76+
}
77+
78+
JNIEXPORT jobject JNICALL
79+
Java_com_itsaky_androidide_treesitter_TSNode_getPreviousSibling(JNIEnv* env,
80+
jobject self) {
81+
return _marshalNode(env, ts_node_prev_sibling(_unmarshalNode(env, self)));
82+
}
83+
84+
JNIEXPORT jobject JNICALL
85+
Java_com_itsaky_androidide_treesitter_TSNode_getNextNamedSibling(JNIEnv* env,
86+
jobject self) {
87+
return _marshalNode(env,
88+
ts_node_next_named_sibling(_unmarshalNode(env, self)));
89+
}
90+
91+
JNIEXPORT jobject JNICALL
92+
Java_com_itsaky_androidide_treesitter_TSNode_getPreviousNamedSibling(
93+
JNIEnv* env, jobject self) {
94+
return _marshalNode(env,
95+
ts_node_prev_named_sibling(_unmarshalNode(env, self)));
96+
}
97+
98+
JNIEXPORT jobject JNICALL
99+
Java_com_itsaky_androidide_treesitter_TSNode_getFirstChildForByte(JNIEnv* env,
100+
jobject self,
101+
jint offset) {
102+
return _marshalNode(
103+
env, ts_node_first_child_for_byte(_unmarshalNode(env, self), offset));
104+
}
105+
106+
JNIEXPORT jobject JNICALL
107+
Java_com_itsaky_androidide_treesitter_TSNode_getFirstNamedChildForByte(
108+
JNIEnv* env, jobject self, jint offset) {
109+
return _marshalNode(env, ts_node_first_named_child_for_byte(
110+
_unmarshalNode(env, self), offset));
111+
}
112+
113+
JNIEXPORT jobject JNICALL
114+
Java_com_itsaky_androidide_treesitter_TSNode_getDescendantForByteRange(
115+
JNIEnv* env, jobject self, jint start, jint end) {
116+
return _marshalNode(env, ts_node_descendant_for_byte_range(
117+
_unmarshalNode(env, self), start, end));
118+
}
119+
120+
JNIEXPORT jobject JNICALL
121+
Java_com_itsaky_androidide_treesitter_TSNode_getNamedDescendantForByteRange(
122+
JNIEnv* env, jobject self, jint start, jint end) {
123+
return _marshalNode(env, ts_node_named_descendant_for_byte_range(
124+
_unmarshalNode(env, self), start, end));
125+
}
126+
127+
JNIEXPORT jobject JNICALL
128+
Java_com_itsaky_androidide_treesitter_TSNode_getDescendantForPointRange(
129+
JNIEnv* env, jobject self, jobject start, jobject end) {
130+
TSPoint sPoint = _unmarshalPoint(env, start);
131+
TSPoint ePoint = _unmarshalPoint(env, end);
132+
return _marshalNode(env, ts_node_descendant_for_point_range(
133+
_unmarshalNode(env, self), sPoint, ePoint));
134+
}
135+
136+
JNIEXPORT jobject JNICALL
137+
Java_com_itsaky_androidide_treesitter_TSNode_getNamedDescendantForPointRange(
138+
JNIEnv* env, jobject self, jobject start, jobject end) {
139+
TSPoint sPoint = _unmarshalPoint(env, start);
140+
TSPoint ePoint = _unmarshalPoint(env, end);
141+
return _marshalNode(env, ts_node_named_descendant_for_point_range(
142+
_unmarshalNode(env, self), sPoint, ePoint));
143+
}
144+
145+
JNIEXPORT jboolean JNICALL Java_com_itsaky_androidide_treesitter_TSNode_isEqualTo
146+
(JNIEnv* env, jobject self, jobject other) {
147+
return (jboolean) ts_node_eq(_unmarshalNode(env, self), _unmarshalNode(env, other));
60148
}
61149

62150
JNIEXPORT jstring JNICALL
@@ -98,10 +186,10 @@ JNIEXPORT jstring JNICALL Java_com_itsaky_androidide_treesitter_TSNode_getType(
98186
return result;
99187
}
100188

101-
JNIEXPORT jint JNICALL Java_com_itsaky_androidide_treesitter_TSNode_getSymbol
102-
(JNIEnv* env, jobject self) {
103-
return (jint) ts_node_symbol(_unmarshalNode(env, self));
104-
}
189+
JNIEXPORT jint JNICALL Java_com_itsaky_androidide_treesitter_TSNode_getSymbol(
190+
JNIEnv* env, jobject self) {
191+
return (jint)ts_node_symbol(_unmarshalNode(env, self));
192+
}
105193

106194
JNIEXPORT jboolean JNICALL
107195
Java_com_itsaky_androidide_treesitter_TSNode_isNull(JNIEnv* env, jobject self) {

0 commit comments

Comments
 (0)