Skip to content

Commit

Permalink
Merge pull request #13 from seart-group/enhancement/cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
dabico committed Sep 21, 2023
2 parents 30f39b2 + 8fa884d commit 9034d83
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
16 changes: 16 additions & 0 deletions lib/ch_usi_si_seart_treesitter_TreeCursor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,19 @@ JNIEXPORT jboolean JNICALL Java_ch_usi_si_seart_treesitter_TreeCursor_gotoParent
env->SetIntField(thisObject, _treeCursorContext1Field, cursor->context[1]);
return result ? JNI_TRUE : JNI_FALSE;
}

JNIEXPORT jobject JNICALL Java_ch_usi_si_seart_treesitter_TreeCursor_clone(
JNIEnv* env, jobject thisObject) {
jobject treeObject = env->GetObjectField(thisObject, _treeCursorTreeField);
const TSTreeCursor* cursor = (const TSTreeCursor*)__getPointer(env, thisObject);
TSTreeCursor copy = ts_tree_cursor_copy(cursor);
return env->NewObject(
_treeCursorClass,
_treeCursorConstructor,
new TSTreeCursor(copy),
copy.context[0],
copy.context[1],
copy.id,
treeObject
);
}
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.

11 changes: 10 additions & 1 deletion src/main/java/ch/usi/si/seart/treesitter/TreeCursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @author Ozren Dabić
*/
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
public class TreeCursor extends External {
public class TreeCursor extends External implements Cloneable {

int context0;
int context1;
Expand Down Expand Up @@ -120,4 +120,13 @@ public void preorderTraversal(@NotNull Consumer<Node> callback) {
public String toString() {
return String.format("TreeCursor(id: %d, tree: %d)", id, tree.pointer);
}

/**
* Clone this cursor, creating a separate, independent instance.
*
* @return a clone of this instance
* @since 1.5.1
*/
@Override
public native TreeCursor clone();
}
7 changes: 7 additions & 0 deletions src/test/java/ch/usi/si/seart/treesitter/TreeCursorTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.usi.si.seart.treesitter;

import lombok.Cleanup;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -76,6 +77,12 @@ void testPreorderTraversal() {
Assertions.assertEquals(17, count.get());
}

@Test
void testClone() {
@Cleanup TreeCursor copy = cursor.clone();
Assertions.assertNotEquals(cursor, copy);
}

@Test
@SuppressWarnings("resource")
void testWalkThrows() {
Expand Down

0 comments on commit 9034d83

Please sign in to comment.