@@ -15,6 +15,7 @@ public final class TreeCursor implements AutoCloseable, Cloneable {
1515 private final MemorySegment self ;
1616 private final Arena arena ;
1717 private final Tree tree ;
18+ private @ Nullable Node node ;
1819
1920 TreeCursor (Node node , Tree tree ) {
2021 arena = Arena .ofShared ();
@@ -33,6 +34,7 @@ private TreeCursor(TreeCursor cursor) {
3334 arena = Arena .ofShared ();
3435 self = ts_tree_cursor_copy (arena , cursor .self );
3536 tree = cursor .tree .clone ();
37+ node = cursor .node ;
3638 }
3739
3840 /**
@@ -41,9 +43,14 @@ private TreeCursor(TreeCursor cursor) {
4143 * @return the current node
4244 */
4345 public Node getCurrentNode () {
44- return getCurrentNode (arena );
46+ if (this .node == null ) {
47+ var node = ts_tree_cursor_current_node (arena , self );
48+ this .node = new Node (node , tree );
49+ }
50+ return this .node ;
4551 }
4652
53+
4754 /**
4855 * Get the current node of the cursor. Its native memory will be managed by the given allocator.
4956 * @param allocator the allocator to use for managing the native memory of the node
@@ -97,7 +104,9 @@ public Node getCurrentNode(SegmentAllocator allocator) {
97104 * {@code false} if there were no children.
98105 */
99106 public boolean gotoFirstChild () {
100- return ts_tree_cursor_goto_first_child (self );
107+ var result = ts_tree_cursor_goto_first_child (self );
108+ if (result ) node = null ;
109+ return result ;
101110 }
102111
103112 /**
@@ -107,7 +116,9 @@ public boolean gotoFirstChild() {
107116 * {@code false} if there were no children.
108117 */
109118 public boolean gotoLastChild () {
110- return ts_tree_cursor_goto_last_child (self );
119+ var result = ts_tree_cursor_goto_last_child (self );
120+ if (result ) node = null ;
121+ return result ;
111122 }
112123
113124 /**
@@ -117,7 +128,9 @@ public boolean gotoLastChild() {
117128 * {@code false} if there was no parent node.
118129 */
119130 public boolean gotoParent () {
120- return ts_tree_cursor_goto_parent (self );
131+ var result = ts_tree_cursor_goto_parent (self );
132+ if (result ) node = null ;
133+ return result ;
121134 }
122135
123136 /**
@@ -127,7 +140,9 @@ public boolean gotoParent() {
127140 * {@code false} if there was no next sibling node.
128141 */
129142 public boolean gotoNextSibling () {
130- return ts_tree_cursor_goto_next_sibling (self );
143+ var result = ts_tree_cursor_goto_next_sibling (self );
144+ if (result ) node = null ;
145+ return result ;
131146 }
132147
133148 /**
@@ -137,7 +152,9 @@ public boolean gotoNextSibling() {
137152 * {@code false} if there was no previous sibling node.
138153 */
139154 public boolean gotoPreviousSibling () {
140- return ts_tree_cursor_goto_previous_sibling (self );
155+ var result = ts_tree_cursor_goto_previous_sibling (self );
156+ if (result ) node = null ;
157+ return result ;
141158 }
142159
143160 /**
@@ -148,7 +165,7 @@ public boolean gotoPreviousSibling() {
148165 */
149166 public void gotoDescendant (@ Unsigned int index ) {
150167 ts_tree_cursor_goto_descendant (self , index );
151-
168+ node = null ;
152169 }
153170
154171 /**
@@ -160,6 +177,7 @@ public void gotoDescendant(@Unsigned int index) {
160177 public @ Unsigned OptionalInt gotoFirstChildForByte (@ Unsigned int offset ) {
161178 var index = ts_tree_cursor_goto_first_child_for_byte (self , offset );
162179 if (index == -1L ) return OptionalInt .empty ();
180+ node = null ;
163181 return OptionalInt .of ((int ) index );
164182 }
165183
@@ -174,6 +192,7 @@ public void gotoDescendant(@Unsigned int index) {
174192 var goal = point .into (arena );
175193 var index = ts_tree_cursor_goto_first_child_for_point (self , goal );
176194 if (index == -1L ) return OptionalInt .empty ();
195+ node = null ;
177196 return OptionalInt .of ((int ) index );
178197 }
179198 }
@@ -182,12 +201,15 @@ public void gotoDescendant(@Unsigned int index) {
182201 public void reset (Node node ) {
183202 try (var arena = Arena .ofConfined ()) {
184203 ts_tree_cursor_reset (self , node .copy (arena ));
204+ } finally {
205+ this .node = null ;
185206 }
186207 }
187208
188209 /** Reset the cursor to start at the same position as another cursor. */
189210 public void reset (TreeCursor cursor ) {
190211 ts_tree_cursor_reset_to (self , cursor .self );
212+ this .node = null ;
191213 }
192214
193215 /** Create a shallow copy of the tree cursor. */
0 commit comments