Skip to content

Commit

Permalink
feat: implement Node.childWithDescendant
Browse files Browse the repository at this point in the history
and deprecate Node.childContainingDescendant
  • Loading branch information
ObserverOfTime committed Oct 23, 2024
1 parent 3909ebc commit 0349739
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,19 @@ class NodeTest : FunSpec({
rootNode.child(0U)!!.fieldNameForNamedChild(2U).shouldBeNull()
}

@Suppress("DEPRECATION")
test("childContainingDescendant()") {
val descendant = rootNode.child(0U)!!.child(0U)!!
val child = rootNode.childContainingDescendant(descendant)
child?.type shouldBe "class_declaration"
}

test("childWithDescendant()") {
val descendant = rootNode.child(0U)!!
val child = rootNode.childWithDescendant(descendant)
child?.type shouldBe "class_declaration"
}

test("descendant()") {
rootNode.descendant(0U, 5U)?.type shouldBe "class"
rootNode.descendant(Point(0U, 10U), Point(0U, 12U))?.type shouldBe "class_body"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,20 @@ actual class Node internal constructor(

/** Get the child of the node that contains the given descendant, if any. */
@FastNative
@Deprecated(
"This method will not return a direct descendant",
ReplaceWith("childWithDescendant(descendant)", "io.github.treesitter.ktreesitter.Node")
)
actual external fun childContainingDescendant(descendant: Node): Node?

/**
* Get the node that contains the given descendant, if any.
*
* @since 0.24.0
*/
@FastNative
actual external fun childWithDescendant(descendant: Node): Node?

/**
* Get the smallest node within this node
* that spans the given byte range, if any.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,19 @@ expect class Node {
fun fieldNameForNamedChild(index: UInt): String?

/** Get the child of the node that contains the given descendant, if any. */
@Deprecated(
"This method will not return a direct descendant",
ReplaceWith("childWithDescendant(descendant)", "io.github.treesitter.ktreesitter.Node")
)
fun childContainingDescendant(descendant: Node): Node?

/**
* Get the node that contains the given descendant, if any.
*
* @since 0.24.0
*/
fun childWithDescendant(descendant: Node): Node?

/**
* Get the smallest node within this node
* that spans the given byte range, if any.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,19 @@ class NodeTest : FunSpec({
rootNode.child(0U)!!.fieldNameForNamedChild(2U).shouldBeNull()
}

@Suppress("DEPRECATION")
test("childContainingDescendant()") {
val descendant = rootNode.child(0U)!!.child(0U)!!
val child = rootNode.childContainingDescendant(descendant)
child?.type shouldBe "class_declaration"
}

test("childWithDescendant()") {
val descendant = rootNode.child(0U)!!
val child = rootNode.childWithDescendant(descendant)
child?.type shouldBe "class_declaration"
}

test("descendant()") {
rootNode.descendant(0U, 5U)?.type shouldBe "class"
rootNode.descendant(Point(0U, 10U), Point(0U, 12U))?.type shouldBe "class_body"
Expand Down
12 changes: 12 additions & 0 deletions ktreesitter/src/jni/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,16 @@ jobject JNICALL node_child_containing_descendant(JNIEnv *env, jobject this, jobj
return marshal_node(env, result, tree);
}

jobject JNICALL node_child_with_descendant(JNIEnv *env, jobject this, jobject descendant) {
TSNode self = unmarshal_node(env, this);
TSNode other = unmarshal_node(env, descendant);
TSNode result = ts_node_child_with_descendant(self, other);
if (ts_node_is_null(result))
return NULL;
jobject tree = GET_FIELD(Object, this, Node_tree);
return marshal_node(env, result, tree);
}

jobject JNICALL node_descendant__bytes(JNIEnv *env, jobject this, jint start, jint end) {
TSNode self = unmarshal_node(env, this);
TSNode result = ts_node_descendant_for_byte_range(self, (uint32_t)start, (uint32_t)end);
Expand Down Expand Up @@ -391,6 +401,8 @@ const JNINativeMethod Node_methods[] = {
{"fieldNameForNamedChild", "(I)Ljava/lang/String;", (void *)&node_field_name_for_named_child},
{"childContainingDescendant", "(L" PACKAGE "Node;)L" PACKAGE "Node;",
(void *)&node_child_containing_descendant},
{"childWithDescendant", "(L" PACKAGE "Node;)L" PACKAGE "Node;",
(void *)&node_child_with_descendant},
{"descendant", "(II)L" PACKAGE "Node;", (void *)&node_descendant__bytes},
{"descendant", "(L" PACKAGE "Point;L" PACKAGE "Point;)L" PACKAGE "Node;",
(void *)&node_descendant__points},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,19 @@ actual class Node internal constructor(
actual external fun fieldNameForNamedChild(index: UInt): String?

/** Get the child of the node that contains the given descendant, if any. */
@Deprecated(
"This method will not return a direct descendant",
ReplaceWith("childWithDescendant(descendant)", "io.github.treesitter.ktreesitter.Node")
)
actual external fun childContainingDescendant(descendant: Node): Node?

/**
* Get the node that contains the given descendant, if any.
*
* @since 0.24.0
*/
actual external fun childWithDescendant(descendant: Node): Node?

/**
* Get the smallest node within this node
* that spans the given byte range, if any.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,21 @@ actual class Node internal constructor(
}

/** Get the child of the node that contains the given descendant, if any. */
@Deprecated(
"This method will not return a direct descendant",
ReplaceWith("childWithDescendant(descendant)", "io.github.treesitter.ktreesitter.Node")
)
actual fun childContainingDescendant(descendant: Node) =
ts_node_child_containing_descendant(self, descendant.self).convert(tree)

/**
* Get the node that contains the given descendant, if any.
*
* @since 0.24.0
*/
actual fun childWithDescendant(descendant: Node) =
ts_node_child_with_descendant(self, descendant.self).convert(tree)

/**
* Get the smallest node within this node
* that spans the given byte range, if any.
Expand Down

0 comments on commit 0349739

Please sign in to comment.