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

Commit c1ad204

Browse files
committed
Added support for 'ts_node_child_by_field_name'
1 parent 34f7a86 commit c1ad204

File tree

9 files changed

+38
-0
lines changed

9 files changed

+38
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.itsaky.androidide.treesitter;
22

3+
import java.nio.charset.StandardCharsets;
4+
35
public class TSNode {
46
private int context0;
57
private int context1;
@@ -32,6 +34,11 @@ public TSNode getNamedChild(int index) {
3234
return TreeSitter.nodeNamedChild(this, index);
3335
}
3436

37+
public TSNode getChildByFieldName(String fieldName) {
38+
final var bytes = fieldName.getBytes(StandardCharsets.UTF_8);
39+
return TreeSitter.getChildByFieldName(this, bytes, bytes.length);
40+
}
41+
3542
public int getChildCount() {
3643
return TreeSitter.nodeChildCount(this);
3744
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ public class TreeSitter {
4747
*/
4848
public static native TSNode nodeNamedChild(TSNode node, int child);
4949

50+
/**
51+
* Find the child node of the given node by field name.
52+
*
53+
* @param node The node.
54+
* @param fieldName The field name of the child.
55+
* @param fieldNameLength The length of `fieldName`.
56+
* @return The found node.
57+
*/
58+
public static native TSNode getChildByFieldName(
59+
TSNode node, byte[] fieldName, int fieldNameLength);
60+
5061
/**
5162
* Get the end byte of the given node.
5263
*
@@ -139,6 +150,7 @@ public class TreeSitter {
139150
* @param node The node to check.
140151
*/
141152
public static native boolean nodeHasError(TSNode node);
153+
142154
// -------------------------------------------
143155
// ---------- Section: TSParser --------------
144156
// -------------------------------------------
456 Bytes
Binary file not shown.
508 Bytes
Binary file not shown.
540 Bytes
Binary file not shown.
504 Bytes
Binary file not shown.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public void testGetChildren() throws UnsupportedEncodingException {
8080
isNull = function.isNull();
8181
assertFalse(isNull);
8282

83+
var body = function.getChildByFieldName("body");
84+
8385
var parent = function.getParent();
8486
type = parent.getType();
8587
assertEquals("module", type);

lib/com_itsaky_androidide_treesitter_TreeSitter.h

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

lib/ts.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@ JNIEXPORT jobject JNICALL Java_com_itsaky_androidide_treesitter_TreeSitter_nodeN
248248
env, ts_node_named_child(_unmarshalNode(env, node), (uint32_t)child));
249249
}
250250

251+
JNIEXPORT jobject JNICALL Java_com_itsaky_androidide_treesitter_TreeSitter_getChildByFieldName(
252+
JNIEnv* env, jclass self, jobject node, jbyteArray name, jint length) {
253+
jbyte* nameStr = env->GetByteArrayElements(name, NULL);
254+
jobject found = _marshalNode(
255+
env, ts_node_child_by_field_name(_unmarshalNode(env, node), reinterpret_cast<const char*>(nameStr), (uint32_t) length));
256+
env->ReleaseByteArrayElements(name, nameStr, JNI_ABORT);
257+
return found;
258+
}
259+
251260
JNIEXPORT jstring JNICALL Java_com_itsaky_androidide_treesitter_TreeSitter_nodeString(
252261
JNIEnv* env, jclass self, jobject node) {
253262
char* nodeString = ts_node_string(_unmarshalNode(env, node));

0 commit comments

Comments
 (0)