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

Commit ffce1e3

Browse files
committed
Implemented TSNode.getFieldNameForChild()
1 parent 1c53e59 commit ffce1e3

File tree

8 files changed

+47
-17
lines changed

8 files changed

+47
-17
lines changed

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ public class TSNode {
1515

1616
public TSNode() {}
1717

18+
/**
19+
* Get the child of the given node at the child index.
20+
*
21+
* @param index The index of the child.
22+
* @return The child at the child index.
23+
*/
1824
public TSNode getChild(int index) {
1925
final var count = getChildCount();
2026
if (index < 0 || index >= count) {
@@ -24,6 +30,12 @@ public TSNode getChild(int index) {
2430
return getChildAt(index);
2531
}
2632

33+
/**
34+
* Get the named child of the given node at the given index.
35+
*
36+
* @param index The index of the named child.
37+
* @return The named child node at the given index.
38+
*/
2739
public TSNode getNamedChild(int index) {
2840
final var count = getNamedChildCount();
2941
if (index < 0 || index >= count) {
@@ -33,6 +45,12 @@ public TSNode getNamedChild(int index) {
3345
return getNamedChildAt(index);
3446
}
3547

48+
/**
49+
* Find the child node of the given node by field name.
50+
*
51+
* @param fieldName The field name.
52+
* @return The found node.
53+
*/
3654
public TSNode getChildByFieldName(String fieldName) {
3755
final var bytes = fieldName.getBytes(StandardCharsets.UTF_8);
3856
return getChildByFieldName(bytes, bytes.length);
@@ -106,30 +124,20 @@ public String toString() {
106124
*/
107125
public native TSNode getParent();
108126

109-
/**
110-
* Get the child of the given node at the child index.
111-
*
112-
* @param index The index of the child.
113-
* @return The child at the child index.
114-
*/
115127
private native TSNode getChildAt(int index);
116128

117-
/**
118-
* Get the named child of the given node at the given index.
119-
*
120-
* @param index The index of the named child.
121-
* @return The named child node at the given index.
122-
*/
123129
private native TSNode getNamedChildAt(int index);
124130

131+
private native TSNode getChildByFieldName(byte[] bytes, int length);
132+
125133
/**
126-
* Find the child node of the given node by field name.
134+
* Get the field name for node's child at the given index, where zero represents * the first
135+
* child.
127136
*
128-
* @param bytes The field name of the child.
129-
* @param length The length of `fieldName`.
130-
* @return The found node.
137+
* @param childIndex The index of the child.
138+
* @return The field name for the child or <code>null</code>.
131139
*/
132-
private native TSNode getChildByFieldName(byte[] bytes, int length);
140+
public native String getFieldNameForChild(int childIndex);
133141

134142
/**
135143
* Get the number of children of the node.
416 Bytes
Binary file not shown.
424 Bytes
Binary file not shown.
456 Bytes
Binary file not shown.
448 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
@@ -64,6 +64,8 @@ public void testGetChildren() throws UnsupportedEncodingException {
6464
assertThat(isNull).isFalse();
6565

6666
var function = root.getChild(0);
67+
var fieldNameForChild = root.getFieldNameForChild(0);
68+
assertThat(fieldNameForChild).isNull();
6769
start = function.getStartPoint();
6870
assertThat(0).isEqualTo(start.row);
6971
assertThat(0).isEqualTo(start.column);

lib/com_itsaky_androidide_treesitter_TSNode.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_node.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ 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;
60+
}
61+
5062
JNIEXPORT jstring JNICALL
5163
Java_com_itsaky_androidide_treesitter_TSNode_getNodeString(JNIEnv* env,
5264
jobject self) {

0 commit comments

Comments
 (0)