Skip to content

Commit 0e66c09

Browse files
committed
refactor: change Node pointers to values
1 parent 4d96fa6 commit 0e66c09

File tree

5 files changed

+77
-73
lines changed

5 files changed

+77
-73
lines changed

node.go

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -26,108 +26,108 @@ func newNode(node C.TSNode) *Node {
2626
// a new tree is created based on an older tree, and a node from the old
2727
// tree is reused in the process, then that node will have the same id in
2828
// both trees.
29-
func (n *Node) Id() uintptr {
29+
func (n Node) Id() uintptr {
3030
return uintptr(n._inner.id)
3131
}
3232

3333
// Get this node's type as a numerical id.
34-
func (n *Node) KindId() uint16 {
34+
func (n Node) KindId() uint16 {
3535
return uint16(C.ts_node_symbol(n._inner))
3636
}
3737

3838
// Get the node's type as a numerical id as it appears in the grammar
3939
// ignoring aliases.
40-
func (n *Node) GrammarId() uint16 {
40+
func (n Node) GrammarId() uint16 {
4141
return uint16(C.ts_node_grammar_symbol(n._inner))
4242
}
4343

4444
// Get this node's type as a string.
45-
func (n *Node) Kind() string {
45+
func (n Node) Kind() string {
4646
return C.GoString(C.ts_node_type(n._inner))
4747
}
4848

4949
// Get this node's symbol name as it appears in the grammar ignoring
5050
// aliases as a string.
51-
func (n *Node) GrammarName() string {
51+
func (n Node) GrammarName() string {
5252
return C.GoString(C.ts_node_grammar_type(n._inner))
5353
}
5454

5555
// Get the [Language] that was used to parse this node's syntax tree.
56-
func (n *Node) Language() *Language {
56+
func (n Node) Language() *Language {
5757
return &Language{Inner: C.ts_node_language(n._inner)}
5858
}
5959

6060
// Check if this node is *named*.
6161
//
6262
// Named nodes correspond to named rules in the grammar, whereas
6363
// *anonymous* nodes correspond to string literals in the grammar.
64-
func (n *Node) IsNamed() bool {
64+
func (n Node) IsNamed() bool {
6565
return bool(C.ts_node_is_named(n._inner))
6666
}
6767

6868
// Check if this node is *extra*.
6969
//
7070
// Extra nodes represent things like comments, which are not required in the
7171
// grammar, but can appear anywhere.
72-
func (n *Node) IsExtra() bool {
72+
func (n Node) IsExtra() bool {
7373
return bool(C.ts_node_is_extra(n._inner))
7474
}
7575

7676
// Check if this node has been edited.
77-
func (n *Node) HasChanges() bool {
77+
func (n Node) HasChanges() bool {
7878
return bool(C.ts_node_has_changes(n._inner))
7979
}
8080

8181
// Check if this node represents a syntax error or contains any syntax
8282
// errors anywhere within it.
83-
func (n *Node) HasError() bool {
83+
func (n Node) HasError() bool {
8484
return bool(C.ts_node_has_error(n._inner))
8585
}
8686

8787
// Check if this node represents a syntax error.
8888
//
8989
// Syntax errors represent parts of the code that could not be incorporated
9090
// into a valid syntax tree.
91-
func (n *Node) IsError() bool {
91+
func (n Node) IsError() bool {
9292
return bool(C.ts_node_is_error(n._inner))
9393
}
9494

9595
// Get this node's parse state.
96-
func (n *Node) ParseState() uint16 {
96+
func (n Node) ParseState() uint16 {
9797
return uint16(C.ts_node_parse_state(n._inner))
9898
}
9999

100100
// Get the parse state after this node.
101-
func (n *Node) NextParseState() uint16 {
101+
func (n Node) NextParseState() uint16 {
102102
return uint16(C.ts_node_next_parse_state(n._inner))
103103
}
104104

105105
// Check if this node is *missing*.
106106
//
107107
// Missing nodes are inserted by the parser in order to recover from
108108
// certain kinds of syntax errors.
109-
func (n *Node) IsMissing() bool {
109+
func (n Node) IsMissing() bool {
110110
return bool(C.ts_node_is_missing(n._inner))
111111
}
112112

113113
// Get the byte offsets where this node starts.
114-
func (n *Node) StartByte() uint {
114+
func (n Node) StartByte() uint {
115115
return uint(C.ts_node_start_byte(n._inner))
116116
}
117117

118118
// Get the byte offsets where this node end.
119-
func (n *Node) EndByte() uint {
119+
func (n Node) EndByte() uint {
120120
return uint(C.ts_node_end_byte(n._inner))
121121
}
122122

123123
// Get the byte range of source code that this node represents.
124-
func (n *Node) ByteRange() (uint, uint) {
124+
func (n Node) ByteRange() (uint, uint) {
125125
return n.StartByte(), n.EndByte()
126126
}
127127

128128
// Get the range of source code that this node represents, both in terms of
129129
// raw bytes and of row/column coordinates.
130-
func (n *Node) Range() Range {
130+
func (n Node) Range() Range {
131131
return Range{
132132
StartByte: n.StartByte(),
133133
EndByte: n.EndByte(),
@@ -137,14 +137,14 @@ func (n *Node) Range() Range {
137137
}
138138

139139
// Get this node's start position in terms of rows and columns.
140-
func (n *Node) StartPosition() Point {
140+
func (n Node) StartPosition() Point {
141141
p := Point{}
142142
p.fromTSPoint(C.ts_node_start_point(n._inner))
143143
return p
144144
}
145145

146146
// Get this node's end position in terms of rows and columns.
147-
func (n *Node) EndPosition() Point {
147+
func (n Node) EndPosition() Point {
148148
p := Point{}
149149
p.fromTSPoint(C.ts_node_end_point(n._inner))
150150
return p
@@ -156,12 +156,12 @@ func (n *Node) EndPosition() Point {
156156
// This method is fairly fast, but its cost is technically log(i), so if
157157
// you might be iterating over a long list of children, you should use
158158
// [Node.Children] instead.
159-
func (n *Node) Child(i uint) *Node {
159+
func (n Node) Child(i uint) *Node {
160160
return newNode(C.ts_node_child(n._inner, C.uint(i)))
161161
}
162162

163163
// Get this node's number of children.
164-
func (n *Node) ChildCount() uint {
164+
func (n Node) ChildCount() uint {
165165
return uint(C.ts_node_child_count(n._inner))
166166
}
167167

@@ -171,22 +171,22 @@ func (n *Node) ChildCount() uint {
171171
// This method is fairly fast, but its cost is technically log(i), so if
172172
// you might be iterating over a long list of children, you should use
173173
// [Node.NamedChildren] instead.
174-
func (n *Node) NamedChild(i uint) *Node {
174+
func (n Node) NamedChild(i uint) *Node {
175175
return newNode(C.ts_node_named_child(n._inner, C.uint(i)))
176176
}
177177

178178
// Get this node's number of *named* children.
179179
//
180180
// See also [Node.IsNamed].
181-
func (n *Node) NamedChildCount() uint {
181+
func (n Node) NamedChildCount() uint {
182182
return uint(C.ts_node_named_child_count(n._inner))
183183
}
184184

185185
// Get the first child with the given field name.
186186
//
187187
// If multiple children may have the same field name, access them using
188188
// [Node.ChildrenByFieldName]
189-
func (n *Node) ChildByFieldName(fieldName string) *Node {
189+
func (n Node) ChildByFieldName(fieldName string) *Node {
190190
cFieldName := C.CString(fieldName)
191191
defer go_free(unsafe.Pointer(cFieldName))
192192
return newNode(C.ts_node_child_by_field_name(n._inner, cFieldName, C.uint32_t(len(fieldName))))
@@ -196,12 +196,12 @@ func (n *Node) ChildByFieldName(fieldName string) *Node {
196196
//
197197
// See also [Node.ChildByFieldName]. You can
198198
// convert a field name to an id using [Language.FieldIdForName].
199-
func (n *Node) ChildByFieldId(fieldId uint16) *Node {
199+
func (n Node) ChildByFieldId(fieldId uint16) *Node {
200200
return newNode(C.ts_node_child_by_field_id(n._inner, C.uint16_t(fieldId)))
201201
}
202202

203203
// Get the field name of this node's child at the given index.
204-
func (n *Node) FieldNameForChild(childIndex uint32) string {
204+
func (n Node) FieldNameForChild(childIndex uint32) string {
205205
ptr := C.ts_node_field_name_for_child(n._inner, C.uint32_t(childIndex))
206206
if ptr == nil {
207207
return ""
@@ -210,7 +210,7 @@ func (n *Node) FieldNameForChild(childIndex uint32) string {
210210
}
211211

212212
// Get the field name of this node's named child at the given index.
213-
func (n *Node) FieldNameForNamedChild(namedChildIndex uint32) string {
213+
func (n Node) FieldNameForNamedChild(namedChildIndex uint32) string {
214214
ptr := C.ts_node_field_name_for_named_child(n._inner, C.uint32_t(namedChildIndex))
215215
if ptr == nil {
216216
return ""
@@ -227,8 +227,8 @@ func (n *Node) FieldNameForNamedChild(namedChildIndex uint32) string {
227227
//
228228
// If you're walking the tree recursively, you may want to use the
229229
// [TreeCursor] APIs directly instead.
230-
func (n *Node) Children(cursor *TreeCursor) []Node {
231-
cursor.Reset(*n)
230+
func (n Node) Children(cursor *TreeCursor) []Node {
231+
cursor.Reset(n)
232232
cursor.GotoFirstChild()
233233
childCount := n.ChildCount()
234234
result := make([]Node, 0, childCount)
@@ -242,8 +242,8 @@ func (n *Node) Children(cursor *TreeCursor) []Node {
242242
// Iterate over this node's named children.
243243
//
244244
// See also [Node.Children].
245-
func (n *Node) NamedChildren(cursor *TreeCursor) []Node {
246-
cursor.Reset(*n)
245+
func (n Node) NamedChildren(cursor *TreeCursor) []Node {
246+
cursor.Reset(n)
247247
cursor.GotoFirstChild()
248248
namedChildCount := n.NamedChildCount()
249249
result := make([]Node, 0, namedChildCount)
@@ -262,11 +262,11 @@ func (n *Node) NamedChildren(cursor *TreeCursor) []Node {
262262
// Iterate over this node's children with a given field name.
263263
//
264264
// See also [Node.Children].
265-
func (n *Node) ChildrenByFieldName(fieldName string, cursor *TreeCursor) []Node {
265+
func (n Node) ChildrenByFieldName(fieldName string, cursor *TreeCursor) []Node {
266266
fieldId := n.Language().FieldIdForName(fieldName)
267267
done := fieldId == 0
268268
if !done {
269-
cursor.Reset(*n)
269+
cursor.Reset(n)
270270
cursor.GotoFirstChild()
271271
}
272272
result := make([]Node, 0)
@@ -287,98 +287,98 @@ func (n *Node) ChildrenByFieldName(fieldName string, cursor *TreeCursor) []Node
287287
// Get this node's immediate parent.
288288
// Prefer [Node.ChildContainingDescendant]
289289
// for iterating over this node's ancestors.
290-
func (n *Node) Parent() *Node {
290+
func (n Node) Parent() *Node {
291291
return newNode(C.ts_node_parent(n._inner))
292292
}
293293

294294
// Deprecated: Prefer [Node.ChildWithDescendant] instead, this will be removed in 0.25
295295
// Get the node's child containing `descendant`. This will not return
296296
// the descendant if it is a direct child of `self`, for that use
297297
// [Node.ChildWithDescendant].
298-
func (n *Node) ChildContainingDescendant(descendant *Node) *Node {
298+
func (n Node) ChildContainingDescendant(descendant Node) *Node {
299299
return newNode(C.ts_node_child_containing_descendant(n._inner, descendant._inner))
300300
}
301301

302302
// Get the node that contains `descendant`.
303303
// Note that this can return `descendant` itself, unlike the deprecated function
304304
// [Node.ChildContainingDescendant].
305-
func (n *Node) ChildWithDescendant(descendant *Node) *Node {
305+
func (n Node) ChildWithDescendant(descendant Node) *Node {
306306
return newNode(C.ts_node_child_with_descendant(n._inner, descendant._inner))
307307
}
308308

309309
// Get this node's next sibling.
310-
func (n *Node) NextSibling() *Node {
310+
func (n Node) NextSibling() *Node {
311311
return newNode(C.ts_node_next_sibling(n._inner))
312312
}
313313

314314
// Get this node's previous sibling.
315-
func (n *Node) PrevSibling() *Node {
315+
func (n Node) PrevSibling() *Node {
316316
return newNode(C.ts_node_prev_sibling(n._inner))
317317
}
318318

319319
// Get this node's next named sibling.
320-
func (n *Node) NextNamedSibling() *Node {
320+
func (n Node) NextNamedSibling() *Node {
321321
return newNode(C.ts_node_next_named_sibling(n._inner))
322322
}
323323

324324
// Get this node's previous named sibling.
325-
func (n *Node) PrevNamedSibling() *Node {
325+
func (n Node) PrevNamedSibling() *Node {
326326
return newNode(C.ts_node_prev_named_sibling(n._inner))
327327
}
328328

329329
// Get the node's first child that extends beyond the given byte offset.
330-
func (n *Node) FirstChildForByte(byteOffset uint) *Node {
330+
func (n Node) FirstChildForByte(byteOffset uint) *Node {
331331
return newNode(C.ts_node_first_child_for_byte(n._inner, C.uint(byteOffset)))
332332
}
333333

334334
// Get the node's first named child that extends beyond the given byte offset.
335-
func (n *Node) FirstNamedChildForByte(byteOffset uint) *Node {
335+
func (n Node) FirstNamedChildForByte(byteOffset uint) *Node {
336336
return newNode(C.ts_node_first_named_child_for_byte(n._inner, C.uint(byteOffset)))
337337
}
338338

339339
// Get the node's number of descendants, including one for the node itself.
340-
func (n *Node) DescendantCount() uint {
340+
func (n Node) DescendantCount() uint {
341341
return uint(C.ts_node_descendant_count(n._inner))
342342
}
343343

344344
// Get the smallest node within this node that spans the given range.
345-
func (n *Node) DescendantForByteRange(start, end uint) *Node {
345+
func (n Node) DescendantForByteRange(start, end uint) *Node {
346346
return newNode(C.ts_node_descendant_for_byte_range(n._inner, C.uint(start), C.uint(end)))
347347
}
348348

349349
// Get the smallest named node within this node that spans the given range.
350-
func (n *Node) NamedDescendantForByteRange(start, end uint) *Node {
350+
func (n Node) NamedDescendantForByteRange(start, end uint) *Node {
351351
return newNode(C.ts_node_named_descendant_for_byte_range(n._inner, C.uint(start), C.uint(end)))
352352
}
353353

354354
// Get the smallest node within this node that spans the given range.
355-
func (n *Node) DescendantForPointRange(start, end Point) *Node {
355+
func (n Node) DescendantForPointRange(start, end Point) *Node {
356356
return newNode(C.ts_node_descendant_for_point_range(n._inner, start.toTSPoint(), end.toTSPoint()))
357357
}
358358

359359
// Get the smallest named node within this node that spans the given range.
360-
func (n *Node) NamedDescendantForPointRange(start, end Point) *Node {
360+
func (n Node) NamedDescendantForPointRange(start, end Point) *Node {
361361
return newNode(C.ts_node_named_descendant_for_point_range(n._inner, start.toTSPoint(), end.toTSPoint()))
362362
}
363363

364-
func (n *Node) ToSexp() string {
364+
func (n Node) ToSexp() string {
365365
cString := C.ts_node_string(n._inner)
366366
result := C.GoString(cString)
367367
go_free(unsafe.Pointer(cString))
368368
return result
369369
}
370370

371-
func (n *Node) Utf8Text(source []byte) string {
371+
func (n Node) Utf8Text(source []byte) string {
372372
return string(source[n.StartByte():n.EndByte()])
373373
}
374374

375-
func (n *Node) Utf16Text(source []uint16) []uint16 {
375+
func (n Node) Utf16Text(source []uint16) []uint16 {
376376
return source[n.StartByte():n.EndByte()]
377377
}
378378

379379
// Create a new [TreeCursor] starting from this node.
380-
func (n *Node) Walk() *TreeCursor {
381-
return newTreeCursor(*n)
380+
func (n Node) Walk() *TreeCursor {
381+
return newTreeCursor(n)
382382
}
383383

384384
// Edit this node to keep it in-sync with source code that has been edited.

0 commit comments

Comments
 (0)