Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 63f85f0

Browse files
committed
Moved native methods for TSTreeCursor to TSTreeCursor.Native class
1 parent ee09141 commit 63f85f0

File tree

7 files changed

+155
-162
lines changed

7 files changed

+155
-162
lines changed

android-tree-sitter/src/main/java/com/itsaky/androidide/treesitter/TSNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public TSNode getChildByFieldName(String fieldName) {
3939
}
4040

4141
public TSTreeCursor walk() {
42-
return new TSTreeCursor(TreeSitter.treeCursorNew(this));
42+
return new TSTreeCursor(this);
4343
}
4444

4545
public TSNode findNodeWithType(final String type, final boolean namedOnly) {
Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,88 @@
11
package com.itsaky.androidide.treesitter;
22

33
public class TSTreeCursor implements AutoCloseable {
4-
private long pointer;
4+
private final long pointer;
55
private int context0;
66
private int context1;
77
private long id;
88
private long tree;
99

10+
public TSTreeCursor(TSNode node) {
11+
this(Native.newCursor(node));
12+
}
13+
1014
TSTreeCursor(long pointer) {
1115
this.pointer = pointer;
1216
}
1317

18+
/** Close and delete this tree cursor. */
1419
@Override
1520
public void close() {
16-
TreeSitter.treeCursorDelete(pointer);
21+
Native.delete(pointer);
1722
}
1823

24+
/**
25+
* Get the current node of this tree cursor.
26+
*
27+
* @return The current {@link TSNode}.
28+
*/
1929
public TSNode getCurrentNode() {
20-
return TreeSitter.treeCursorCurrentNode(pointer);
30+
return Native.currentNode(pointer);
2131
}
2232

33+
/**
34+
* Get the current field name.
35+
*
36+
* @return The field name.
37+
*/
2338
public String getCurrentFieldName() {
24-
return TreeSitter.treeCursorCurrentFieldName(pointer);
39+
return Native.currentFieldName(pointer);
2540
}
2641

42+
/**
43+
* Get the current tree cursor node.
44+
*
45+
* @return The current tree cursor node.
46+
*/
2747
public TSTreeCursorNode getCurrentTreeCursorNode() {
28-
return TreeSitter.treeCursorCurrentTreeCursorNode(pointer);
48+
return Native.currentTreeCursorNode(pointer);
2949
}
3050

51+
/**
52+
* Moves to the next child.
53+
*
54+
* @return <code>true</code> if moved successfully, <code>false</code> otherwise.
55+
*/
3156
public boolean gotoFirstChild() {
32-
return TreeSitter.treeCursorGotoFirstChild(pointer);
57+
return Native.gotoFirstChild(pointer);
3358
}
3459

60+
/**
61+
* Moves to the next sibling node.
62+
*
63+
* @return <code>true</code> if moved successfully, <code>false</code> otherwise.
64+
*/
3565
public boolean gotoNextSibling() {
36-
return TreeSitter.treeCursorGotoNextSibling(pointer);
66+
return Native.gotoNextSibling(pointer);
3767
}
3868

69+
/**
70+
* Moves to the parent node.
71+
*
72+
* @return <code>true</code> if moved successfully, <code>false</code> otherwise.
73+
*/
3974
public boolean gotoParent() {
40-
return TreeSitter.treeCursorGotoParent(pointer);
75+
return Native.gotoParent(pointer);
76+
}
77+
78+
private static class Native {
79+
public static native long newCursor(TSNode node);
80+
public static native TSTreeCursorNode currentTreeCursorNode(long cursor);
81+
public static native String currentFieldName(long cursor);
82+
public static native TSNode currentNode(long cursor);
83+
public static native void delete(long cursor);
84+
public static native boolean gotoFirstChild(long cursor);
85+
public static native boolean gotoNextSibling(long cursor);
86+
public static native boolean gotoParent(long cursor);
4187
}
4288
}

android-tree-sitter/src/main/java/com/itsaky/androidide/treesitter/TreeSitter.java

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,68 +10,6 @@ public class TreeSitter {
1010
// ---------- Section: TSTreeCursor ----------
1111
// -------------------------------------------
1212

13-
/**
14-
* Create a new {@link TSTreeCursor} for the given node.
15-
*
16-
* @param node The node.
17-
* @return The pointer to the new {@link TSTreeCursor}.
18-
*/
19-
public static native long treeCursorNew(TSNode node);
20-
21-
/**
22-
* Get the current {@link TSTreeCursorNode}.
23-
*
24-
* @param cursor The pointer to the {@link TSTreeCursor}.
25-
* @return The {@link TSTreeCursorNode}.
26-
*/
27-
public static native TSTreeCursorNode treeCursorCurrentTreeCursorNode(long cursor);
28-
29-
/**
30-
* Get the field name of the tree cursor's current node.
31-
*
32-
* <p>This returns <code>null</code> if the current node doesn't have a field.
33-
*/
34-
public static native String treeCursorCurrentFieldName(long cursor);
35-
36-
/**
37-
* Get the current node of the given {@link TSTreeCursor}.
38-
*
39-
* @param cursor The pointer to the {@link TSTreeCursor}.
40-
* @return The current node of the cursor.
41-
*/
42-
public static native TSNode treeCursorCurrentNode(long cursor);
43-
44-
/**
45-
* Delete the given {@link TSTreeCursor}.
46-
*
47-
* @param cursor The pointer to the cursor.
48-
*/
49-
public static native void treeCursorDelete(long cursor);
50-
51-
/**
52-
* Instruct the given cursor to go to its first child.
53-
*
54-
* @param cursor The pointer to the cursor.
55-
* @return <code>true</code> if moved successfully, <code>false</code> otherwise.
56-
*/
57-
public static native boolean treeCursorGotoFirstChild(long cursor);
58-
59-
/**
60-
* Instruct the given cursor to go to the next sibling node.
61-
*
62-
* @param cursor The pointer to the cursor.
63-
* @return <code>true</code> if moved successfully, <code>false</code> otherwise.
64-
*/
65-
public static native boolean treeCursorGotoNextSibling(long cursor);
66-
67-
/**
68-
* Instruct the given cursor to go to the parent node of current node.
69-
*
70-
* @param cursor The pointer to the cursor.
71-
* @return <code>true</code> if moved successfully, <code>false</code> otherwise.
72-
*/
73-
public static native boolean treeCursorGotoParent(long cursor);
74-
7513
// -------------------------------------------
7614
// ---------- Section: TSTree ----------------
7715
// -------------------------------------------

genh.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -eu
44

55
script_dir=$(realpath $(dirname $0))
66

7-
for header in TreeSitter TSLanguages.Native TSLanguage.Native TSParser.Native TSNode
7+
for header in TreeSitter TSLanguages.Native TSLanguage.Native TSParser.Native TSTreeCursor.Native TSNode
88
do
99
javah -d $script_dir/lib -classpath ${script_dir}/android-tree-sitter/src/main/java com.itsaky.androidide.treesitter.${header}
1010
done

lib/com_itsaky_androidide_treesitter_TSTreeCursor_Native.h

Lines changed: 77 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/com_itsaky_androidide_treesitter_TreeSitter.h

Lines changed: 0 additions & 64 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)