Skip to content

Commit

Permalink
feat(node): implement missing methods
Browse files Browse the repository at this point in the history
- getFirstChildForByte
- getFirstNamedChildForByte
  • Loading branch information
ObserverOfTime committed Jan 21, 2025
1 parent 9da6529 commit f7fc596
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/java/io/github/treesitter/jtreesitter/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,24 @@ public Optional<Node> getNamedChild(@Unsigned int index) throws IndexOutOfBounds
return optional(ts_node_named_child(arena, self, index));
}

/**
* Get the node's first child that contains or starts after the given byte offset.
*
* @since 0.25.0
*/
public Optional<Node> getFirstChildForByte(@Unsigned int byte_offset) {
return optional(ts_node_first_child_for_byte(arena, self, byte_offset));
}

/**
* Get the node's first <em>named</em> child that contains or starts after the given byte offset.
*
* @since 0.25.0
*/
public Optional<Node> getFirstNamedChildForByte(@Unsigned int byte_offset) {
return optional(ts_node_first_named_child_for_byte(arena, self, byte_offset));
}

/**
* Get the node's first child with the given field ID, if any.
*
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/io/github/treesitter/jtreesitter/NodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ void getNamedChild() {
assertEquals("class_declaration", child.getGrammarType());
}

@Test
void getFirstChildForByte() {
var child = node.getFirstChildForByte(15).orElseThrow();
assertEquals("line_comment", child.getGrammarType());
}

@Test
void getFirstNamedChildForByte() {
var child = node.getFirstNamedChildForByte(15).orElseThrow();
assertEquals("line_comment", child.getGrammarType());
}

@Test
void getChildByFieldId() {
var child = node.getChild(0).orElseThrow();
Expand Down

0 comments on commit f7fc596

Please sign in to comment.