Skip to content

Commit

Permalink
Merge pull request #116 from seart-group/feature/depth
Browse files Browse the repository at this point in the history
  • Loading branch information
dabico authored Jan 30, 2024
2 parents b74cfbc + a2fa8f3 commit 9eee806
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 12 deletions.
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

0 comments on commit 9eee806

Please sign in to comment.