Package ch.usi.si.seart.treesitter
Class OffsetTreeCursor
- java.lang.Object
-
- ch.usi.si.seart.treesitter.TreeCursor
-
- ch.usi.si.seart.treesitter.OffsetTreeCursor
-
- All Implemented Interfaces:
AutoCloseable,Cloneable
public class OffsetTreeCursor extends TreeCursor
Specialization ofTreeCursorthat applies a positional offset to visited nodes. Retrieving nodes by eithergetCurrentNode()orgetCurrentTreeCursorNode()applies the offset to a copy of the original node, meaning that the tree is not mutated during traversal.So what exactly is the purpose of this cursor? Imagine a scenario in which we need to print out the node positions of a source code file that begins with
nblank lines. Rather than modifying the original file, we can just use this cursor with a positional offset ofnew Position(-n, 0). Likewise, when printing node positions of an indented function, thenspaces of indentation can be negated with a positional offset ofnew Position(0, -n). Row and column offsets can be combined with various positive and negative arrangements.- Since:
- 1.2.0
- Author:
- Ozren Dabić
-
-
Constructor Summary
Constructors Constructor Description OffsetTreeCursor(@NotNull Node node, @NotNull Point offset)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TreeCursorclone()Clone this cursor, creating a separate, independent instance.voidclose()Delete the external resource, freeing all the memory that it used.booleanequals(Object obj)StringgetCurrentFieldName()Get the field name of theNodethat the cursor is currently pointing to.NodegetCurrentNode()Get theNodethat the cursor is currently pointing to.TreeCursorNodegetCurrentTreeCursorNode()Get theTreeCursorNoderepresentation of theNodethat the cursor is currently pointing to.booleangotoFirstChild()Move the cursor to the first child of its current node.booleangotoFirstChild(int offset)Move the cursor to the first child of its current node that extends beyond the given byte offset.booleangotoFirstChild(@NotNull Point point)Move the cursor to the first child of its current node that extends beyond the given row-column offset.booleangotoNextSibling()Move the cursor to the next sibling of its current node.booleangotoNode(@NotNull Node node)Move the cursor to an arbitrary node.booleangotoParent()Move the cursor to the parent of its current node.inthashCode()booleanisNull()Checks whether the memory address associated with this external resource isnullptr.voidpreorderTraversal(@NotNull Consumer<Node> callback)Iteratively traverse over the parse tree, applying a callback to the nodes before they are visited.StringtoString()
-
-
-
Method Detail
-
close
public void close()
Delete the external resource, freeing all the memory that it used.- Specified by:
closein interfaceAutoCloseable
-
getCurrentFieldName
public String getCurrentFieldName()
Description copied from class:TreeCursorGet the field name of theNodethat the cursor is currently pointing to.- Returns:
- the field name of the tree cursor's current node,
nullif the current node doesn't have a field
-
gotoFirstChild
public boolean gotoFirstChild()
Description copied from class:TreeCursorMove the cursor to the first child of its current node.- Returns:
- true if the cursor successfully moved, and false if there were no children
-
gotoFirstChild
public boolean gotoFirstChild(int offset)
Description copied from class:TreeCursorMove the cursor to the first child of its current node that extends beyond the given byte offset.- Parameters:
offset- the starting byte of the child- Returns:
- true if the cursor successfully moved, and false if no such child was found
-
gotoFirstChild
public boolean gotoFirstChild(@NotNull @NotNull Point point)Description copied from class:TreeCursorMove the cursor to the first child of its current node that extends beyond the given row-column offset.- Parameters:
point- the starting row-column position of the child- Returns:
- true if the cursor successfully moved, and false if no such child was found
-
gotoNextSibling
public boolean gotoNextSibling()
Description copied from class:TreeCursorMove the cursor to the next sibling of its current node.- Returns:
- true if the cursor successfully moved, and false if there was no next sibling node
-
gotoParent
public boolean gotoParent()
Description copied from class:TreeCursorMove the cursor to the parent of its current node.- Returns:
- true if the cursor successfully moved, and false if there was no parent node (the cursor was already on the root node)
-
gotoNode
public boolean gotoNode(@NotNull @NotNull Node node)Description copied from class:TreeCursorMove the cursor to an arbitrary node.- Overrides:
gotoNodein classTreeCursor- Parameters:
node- target node to move the cursor to.- Returns:
- true if the cursor successfully moved, and false if it was already at the specified node
-
preorderTraversal
public void preorderTraversal(@NotNull @NotNull Consumer<Node> callback)Description copied from class:TreeCursorIteratively traverse over the parse tree, applying a callback to the nodes before they are visited.- Parameters:
callback- The callback consumer which will execute upon visiting a node
-
getCurrentNode
public Node getCurrentNode()
Description copied from class:TreeCursorGet theNodethat the cursor is currently pointing to.- Returns:
- the tree cursor's current node
-
clone
public TreeCursor clone()
Description copied from class:TreeCursorClone this cursor, creating a separate, independent instance.- Returns:
- a clone of this instance
-
getCurrentTreeCursorNode
public TreeCursorNode getCurrentTreeCursorNode()
Description copied from class:TreeCursorGet theTreeCursorNoderepresentation of theNodethat the cursor is currently pointing to.- Returns:
- the tree cursor's current node
-
isNull
public final boolean isNull()
Checks whether the memory address associated with this external resource isnullptr.- Returns:
trueif the memory address is 0, otherwisefalse
-
-