Skip to content

Commit

Permalink
Merge pull request #60 from seart-group/enhancement/goto
Browse files Browse the repository at this point in the history
  • Loading branch information
dabico authored Nov 8, 2023
2 parents c9da64b + eb8acac commit bf72fdd
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/ch_usi_si_seart_treesitter_TreeCursor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,30 @@ JNIEXPORT jboolean JNICALL Java_ch_usi_si_seart_treesitter_TreeCursor_gotoParent
return result ? JNI_TRUE : JNI_FALSE;
}

JNIEXPORT jboolean JNICALL Java_ch_usi_si_seart_treesitter_TreeCursor_gotoNode(
JNIEnv* env, jobject thisObject, jobject nodeObject) {
if (nodeObject == NULL) {
__throwNPE(env, "Node must not be null!");
return JNI_FALSE;
}
TSNode target = __unmarshalNode(env, nodeObject);
if (ts_node_is_null(target)) {
__throwIAE(env, "Node must be part of the tree!");
return JNI_FALSE;
}
TSTreeCursor* cursor = (TSTreeCursor*)__getPointer(env, thisObject);
TSNode source = ts_tree_cursor_current_node(cursor);
if (ts_node_eq(source, target)) return JNI_FALSE;
if (source.tree != target.tree) {
__throwIAE(env, "Node must be part of the tree!");
return JNI_FALSE;
}
ts_tree_cursor_reset(cursor, target);
env->SetIntField(thisObject, _treeCursorContext0Field, cursor->context[0]);
env->SetIntField(thisObject, _treeCursorContext1Field, cursor->context[1]);
return JNI_TRUE;
}

JNIEXPORT jobject JNICALL Java_ch_usi_si_seart_treesitter_TreeCursor_clone(
JNIEnv* env, jobject thisObject) {
jobject treeObject = env->GetObjectField(thisObject, _treeCursorTreeField);
Expand Down
8 changes: 8 additions & 0 deletions lib/ch_usi_si_seart_treesitter_TreeCursor.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public boolean gotoParent() {
return cursor.gotoParent();
}

@Override
public boolean gotoNode(@NotNull Node node) {
return cursor.gotoNode(node);
}

@Override
public void preorderTraversal(@NotNull Consumer<Node> callback) {
cursor.preorderTraversal(callback);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/ch/usi/si/seart/treesitter/TreeCursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ protected TreeCursor() {
*/
public native boolean gotoParent();

/**
* Move the cursor to an arbitrary node.
*
* @param node target node to move the cursor to.
* @return true if the cursor successfully moved,
* and false if it was already at the specified node
* @throws NullPointerException if {@code node} is null
* @throws IllegalArgumentException if {@code node} is not present in the tree
* @since 1.9.0
*/
public native boolean gotoNode(@NotNull Node node);

/**
* Iteratively traverse over the parse tree,
* applying a callback to the nodes before they are visited.
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/ch/usi/si/seart/treesitter/TreeCursorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ void testGotoFirstChildPositionOffsetThrows() {

}

@Test
void testGotoNode() {
Node root = tree.getRootNode();
Assertions.assertFalse(cursor.gotoNode(root));
Node identifier = root.getChild(0).getChildByFieldName("name");
Assertions.assertTrue(cursor.gotoNode(identifier));
Assertions.assertEquals(identifier, cursor.getCurrentNode());
Assertions.assertFalse(cursor.gotoNode(identifier));
Assertions.assertTrue(cursor.gotoNode(root));
Assertions.assertEquals(root, cursor.getCurrentNode());
Node clone = tree.clone().getRootNode();
Assertions.assertThrows(NullPointerException.class, () -> cursor.gotoNode(null));
Assertions.assertThrows(IllegalArgumentException.class, () -> cursor.gotoNode(empty));
Assertions.assertThrows(IllegalArgumentException.class, () -> cursor.gotoNode(clone));
}

@Test
void testPreorderTraversal() {
AtomicInteger count = new AtomicInteger();
Expand Down

0 comments on commit bf72fdd

Please sign in to comment.