-
Notifications
You must be signed in to change notification settings - Fork 10
/
tree_cursor.go
153 lines (133 loc) · 5.01 KB
/
tree_cursor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package tree_sitter
/*
#cgo CFLAGS: -Iinclude -Isrc -std=c11 -D_POSIX_C_SOURCE=200112L -D_DEFAULT_SOURCE
#include <tree_sitter/api.h>
*/
import "C"
// A stateful object for walking a syntax [Tree] efficiently.
type TreeCursor struct {
_inner C.TSTreeCursor
}
func newTreeCursor(node Node) *TreeCursor {
return &TreeCursor{_inner: C.ts_tree_cursor_new(node._inner)}
}
func (tc *TreeCursor) Close() {
C.ts_tree_cursor_delete(&tc._inner)
}
func (tc *TreeCursor) Copy() *TreeCursor {
return &TreeCursor{_inner: C.ts_tree_cursor_copy(&tc._inner)}
}
// Get the tree cursor's current [Node].
func (tc *TreeCursor) Node() *Node {
return newNode(C.ts_tree_cursor_current_node(&tc._inner))
}
// Get the numerical field id of this tree cursor's current node.
//
// See also [TreeCursor.FieldName].
func (tc *TreeCursor) FieldId() uint16 {
return uint16(C.ts_tree_cursor_current_field_id(&tc._inner))
}
// Get the field name of this tree cursor's current node.
func (tc *TreeCursor) FieldName() string {
return C.GoString(C.ts_tree_cursor_current_field_name(&tc._inner))
}
// Get the depth of the cursor's current node relative to the original
// node that the cursor was constructed with.
func (tc *TreeCursor) Depth() uint32 {
return uint32(C.ts_tree_cursor_current_depth(&tc._inner))
}
// Get the index of the cursor's current node out of all of the
// descendants of the original node that the cursor was constructed with.
func (tc *TreeCursor) DescendantIndex() uint32 {
return uint32(C.ts_tree_cursor_current_descendant_index(&tc._inner))
}
// Move this cursor to the first child of its current node.
//
// This returns `true` if the cursor successfully moved, and returns
// `false` if there were no children.
func (tc *TreeCursor) GotoFirstChild() bool {
return bool(C.ts_tree_cursor_goto_first_child(&tc._inner))
}
// Move this cursor to the last child of its current node.
//
// This returns `true` if the cursor successfully moved, and returns
// `false` if there were no children.
//
// Note that this function may be slower than
// [TreeCursor.GotoFirstChild] because it needs to
// iterate through all the children to compute the child's position.
func (tc *TreeCursor) GotoLastChild() bool {
return bool(C.ts_tree_cursor_goto_last_child(&tc._inner))
}
// Move this cursor to the parent of its current node.
//
// This returns `true` if the cursor successfully moved, and returns
// `false` if there was no parent node (the cursor was already on the
// root node).
func (tc *TreeCursor) GotoParent() bool {
return bool(C.ts_tree_cursor_goto_parent(&tc._inner))
}
// Move this cursor to the next sibling of its current node.
//
// This returns `true` if the cursor successfully moved, and returns
// `false` if there was no next sibling node.
func (tc *TreeCursor) GotoNextSibling() bool {
return bool(C.ts_tree_cursor_goto_next_sibling(&tc._inner))
}
// Move the cursor to the node that is the nth descendant of
// the original node that the cursor was constructed with, where
// zero represents the original node itself.
func (tc *TreeCursor) GotoDescendant(descendantIndex uint32) {
C.ts_tree_cursor_goto_descendant(&tc._inner, C.uint32_t(descendantIndex))
}
// Move this cursor to the previous sibling of its current node.
//
// This returns `true` if the cursor successfully moved, and returns
// `false` if there was no previous sibling node.
//
// Note, that this function may be slower than
// [TreeCursor.GotoNextSibling] due to how node
// positions are stored. In the worst case, this will need to iterate
// through all the children upto the previous sibling node to recalculate
// its position.
func (tc *TreeCursor) GotoPreviousSibling() bool {
return bool(C.ts_tree_cursor_goto_previous_sibling(&tc._inner))
}
// Move this cursor to the first child of its current node that extends
// beyond the given byte offset.
//
// This returns the index of the child node if one was found, and returns
// `nil` if no such child was found.
func (tc *TreeCursor) GotoFirstChildForByte(byteIndex uint32) *uint {
res := C.ts_tree_cursor_goto_first_child_for_byte(&tc._inner, C.uint32_t(byteIndex))
if res < 0 {
return nil
}
index := uint(res)
return &index
}
// Move this cursor to the first child of its current node that extends
// beyond the given byte offset.
//
// This returns the index of the child node if one was found, and returns
// `nil` if no such child was found.
func (tc *TreeCursor) GotoFirstChildForPoint(point Point) *uint {
res := C.ts_tree_cursor_goto_first_child_for_point(&tc._inner, point.toTSPoint())
if res < 0 {
return nil
}
index := uint(res)
return &index
}
// Re-initialize this tree cursor to start at the original node that the
// cursor was constructed with.
func (tc *TreeCursor) Reset(node Node) {
C.ts_tree_cursor_reset(&tc._inner, node._inner)
}
// Re-initialize a tree cursor to the same position as another cursor.
//
// Unlike [TreeCursor.Reset], this will not lose parent
// information and allows reusing already created cursors.
func (tc *TreeCursor) ResetTo(cursor *TreeCursor) {
C.ts_tree_cursor_reset_to(&tc._inner, &cursor._inner)
}