Skip to content

Commit 5c560d3

Browse files
feat(node): implement getChildWithDescendant
and deprecate `getChildContainingDescendant`
1 parent fb84489 commit 5c560d3

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

scripts/jextract.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ $env:package = 'io.github.treesitter.jtreesitter.internal'
3939
--include-function ts_node_child_by_field_id `
4040
--include-function ts_node_child_by_field_name `
4141
--include-function ts_node_child_containing_descendant `
42+
--include-function ts_node_child_with_descendant `
4243
--include-function ts_node_child_count `
4344
--include-function ts_node_descendant_count `
4445
--include-function ts_node_descendant_for_byte_range `

scripts/jextract.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ exec jextract \
3939
--include-function ts_node_child_by_field_id \
4040
--include-function ts_node_child_by_field_name \
4141
--include-function ts_node_child_containing_descendant \
42+
--include-function ts_node_child_with_descendant \
4243
--include-function ts_node_child_count \
4344
--include-function ts_node_descendant_count \
4445
--include-function ts_node_descendant_for_byte_range \

src/main/java/io/github/treesitter/jtreesitter/Node.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,27 @@ public Optional<Node> getNamedDescendant(Point start, Point end) {
386386
return optional(ts_node_named_descendant_for_point_range(arena, self, startPoint, endPoint));
387387
}
388388

389-
/** Get the child of the node that contains the given descendant, if any. */
389+
/**
390+
* Get the child of the node that contains the given descendant, if any.
391+
*
392+
* @apiNote This method will not return the descendant if it is a direct child of this node.
393+
* @deprecated Use {@link #getChildWithDescendant} instead.
394+
*/
395+
@Deprecated(forRemoval = true)
396+
@SuppressWarnings("DeprecatedIsStillUsed")
390397
public Optional<Node> getChildContainingDescendant(Node descendant) {
391398
return optional(ts_node_child_containing_descendant(arena, self, descendant.self));
392399
}
393400

401+
/**
402+
* Get the node that contains the given descendant, if any.
403+
*
404+
* @since 0.24.0
405+
*/
406+
public Optional<Node> getChildWithDescendant(Node descendant) {
407+
return optional(ts_node_child_with_descendant(arena, self, descendant.self));
408+
}
409+
394410
/** Get the source code of the node, if available. */
395411
public @Nullable String getText() {
396412
var text = tree.getText();

src/test/java/io/github/treesitter/jtreesitter/NodeTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,18 +251,26 @@ void getNamedDescendantBytes() {
251251
@DisplayName("getNamedDescendant(points)")
252252
void getNamedDescendantPoints() {
253253
Point startPoint = new Point(0, 6), endPoint = new Point(0, 9);
254-
var descendant = node.getDescendant(startPoint, endPoint).orElseThrow();
254+
var descendant = node.getNamedDescendant(startPoint, endPoint).orElseThrow();
255255
assertEquals("identifier", descendant.getGrammarType());
256256
}
257257

258258
@Test
259+
@SuppressWarnings("removal")
259260
void getChildContainingDescendant() {
260261
var descendant = node.getChild(0).orElseThrow();
261262
descendant = descendant.getChild(0).orElseThrow();
262263
var child = node.getChildContainingDescendant(descendant);
263264
assertEquals("class_declaration", child.orElseThrow().getType());
264265
}
265266

267+
@Test
268+
void getChildWithDescendant() {
269+
var descendant = node.getChild(0).orElseThrow();
270+
var child = node.getChildWithDescendant(descendant);
271+
assertEquals("class_declaration", child.orElseThrow().getType());
272+
}
273+
266274
@Test
267275
void getText() {
268276
var child = node.getDescendant(6, 9).orElseThrow();

0 commit comments

Comments
 (0)