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

Add new method for getting the current relative depth of a TreeCursor #116

Merged
merged 4 commits into from
Jan 30, 2024
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
6 changes: 6 additions & 0 deletions lib/ch_usi_si_seart_treesitter_TreeCursor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ JNIEXPORT void JNICALL Java_ch_usi_si_seart_treesitter_TreeCursor_delete(
__clearPointer(env, thisObject);
}

JNIEXPORT jint JNICALL Java_ch_usi_si_seart_treesitter_TreeCursor_getCurrentDepth(
JNIEnv* env, jobject thisObject) {
TSTreeCursor* treeCursor = (TSTreeCursor*)__getPointer(env, thisObject);
return (jint)ts_tree_cursor_current_depth(treeCursor);
}

JNIEXPORT jobject JNICALL Java_ch_usi_si_seart_treesitter_TreeCursor_getCurrentNode(
JNIEnv* env, jobject thisObject) {
TSTreeCursor* cursor = (TSTreeCursor*)__getPointer(env, thisObject);
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 @@ -54,6 +54,11 @@ public void close() {
cursor.close();
}

@Override
public int getCurrentDepth() {
return cursor.getCurrentDepth();
}

@Override
public String getCurrentFieldName() {
return cursor.getCurrentFieldName();
Expand Down
14 changes: 14 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 @@ -43,6 +43,15 @@ public class TreeCursor extends External implements Cloneable {
@Override
protected native void delete();

/**
* Get the depth of the cursor's current {@link Node} relative
* to the original {@link Node} that the cursor was constructed from.
*
* @return the relative depth of the tree cursor's current node
* @since 1.11.0
*/
public native int getCurrentDepth();

/**
* Get the {@link Node} that the cursor is currently pointing to.
*
Expand Down Expand Up @@ -160,6 +169,11 @@ public void preorderTraversal(@NotNull Consumer<Node> callback) {

static class Stub extends TreeCursor {

@Override
public int getCurrentDepth() {
throw new UnsupportedOperationException();
}

@Override
public Node getCurrentNode() {
throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,18 @@ protected String getFileExtension() {
}

protected void write(Consumer<String> appender) {
int depth = 0;
for (;;) {
TreeCursorNode cursorNode = cursor.getCurrentTreeCursorNode();
if (cursorNode.isNamed()) {
int depth = cursor.getCurrentDepth();
String indent = " ".repeat(depth);
appender.accept(indent);
appender.accept(cursorNode.toString());
appender.accept("\n");
}
if (cursor.gotoFirstChild()) {
depth++;
continue;
} else if (cursor.gotoNextSibling()) {
continue;
}
if (cursor.gotoFirstChild() || cursor.gotoNextSibling()) continue;
do {
if (cursor.gotoParent()) {
depth--;
} else {
return;
}
if (!cursor.gotoParent()) return;
} while (!cursor.gotoNextSibling());
}
}
Expand Down
15 changes: 15 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 @@ -69,6 +69,21 @@ void testWalk() {
Assertions.assertTrue(cursor.gotoFirstChild());
}

@Test
void testGetCurrentDepth() {
Assertions.assertEquals(0, cursor.getCurrentDepth());
cursor.gotoFirstChild();
Assertions.assertEquals(1, cursor.getCurrentDepth());
cursor.gotoFirstChild();
Assertions.assertEquals(2, cursor.getCurrentDepth());
cursor.gotoNextSibling();
Assertions.assertEquals(2, cursor.getCurrentDepth());
cursor.gotoParent();
Assertions.assertEquals(1, cursor.getCurrentDepth());
cursor.gotoParent();
Assertions.assertEquals(0, cursor.getCurrentDepth());
}

@Test
void testGotoFirstChildByteOffset() {
cursor.gotoFirstChild(); // function_definition
Expand Down
Loading