Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree class is now Cloneable #19

Merged
merged 3 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/ch_usi_si_seart_treesitter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ jfieldID _parserLanguageField;

jclass _treeClass;
jfieldID _treeLanguageField;
jfieldID _treeSourceField;
jmethodID _treeConstructor;

jclass _dotGraphPrinterClass;
Expand Down Expand Up @@ -169,6 +170,7 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) {

_loadClass(_treeClass, "ch/usi/si/seart/treesitter/Tree")
_loadField(_treeLanguageField, _treeClass, "language", "Lch/usi/si/seart/treesitter/Language;")
_loadField(_treeSourceField, _treeClass, "source", "Ljava/lang/String;")
_loadConstructor(_treeConstructor, _treeClass, "(JLch/usi/si/seart/treesitter/Language;Ljava/lang/String;)V")

_loadClass(_dotGraphPrinterClass, "ch/usi/si/seart/treesitter/printer/DotGraphPrinter")
Expand Down
1 change: 1 addition & 0 deletions lib/ch_usi_si_seart_treesitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ extern jfieldID _parserLanguageField;

extern jclass _treeClass;
extern jfieldID _treeLanguageField;
extern jfieldID _treeSourceField;
extern jmethodID _treeConstructor;

extern jclass _dotGraphPrinterClass;
Expand Down
9 changes: 9 additions & 0 deletions lib/ch_usi_si_seart_treesitter_Tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,12 @@ JNIEXPORT jobject JNICALL Java_ch_usi_si_seart_treesitter_Tree_getRootNode(
_setNodeTreeField(nodeObject, thisObject);
return nodeObject;
}

JNIEXPORT jobject JNICALL Java_ch_usi_si_seart_treesitter_Tree_clone(
JNIEnv* env, jobject thisObject) {
jobject languageObject = env->GetObjectField(thisObject, _treeLanguageField);
jobject sourceObject = env->GetObjectField(thisObject, _treeSourceField);
jlong tree = __getPointer(env, thisObject);
jlong copy = (jlong)ts_tree_copy((const TSTree*)tree);
return env->NewObject(_treeClass, _treeConstructor, copy, languageObject, sourceObject);
}
8 changes: 8 additions & 0 deletions lib/ch_usi_si_seart_treesitter_Tree.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/Tree.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
@Getter
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
public class Tree extends External implements Iterable<Node> {
public class Tree extends External implements Iterable<Node>, Cloneable {

private static final Charset CHARSET = StandardCharsets.UTF_16LE;

Expand Down Expand Up @@ -60,6 +60,15 @@ public class Tree extends External implements Iterable<Node> {
return getRootNode().iterator();
}

/**
* Clone this tree, creating a separate, independent instance.
*
* @return a clone of this instance
* @since 1.6.0
*/
@Override
public native Tree clone();

@Override
@Generated
public String toString() {
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/ch/usi/si/seart/treesitter/TreeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ void testEdit() {
Assertions.assertEquals(new Point(2, 0), end);
}

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

@Test
void testConstructorThrows() {
@Cleanup Tree tree = new Tree(0L, Language.JAVA, "");
Expand Down