Skip to content

Commit 24f70e3

Browse files
committed
refactor: change Node pointers to values & return (Node, bool)
1 parent adc13ff commit 24f70e3

File tree

8 files changed

+253
-233
lines changed

8 files changed

+253
-233
lines changed

node.go

Lines changed: 59 additions & 59 deletions
Large diffs are not rendered by default.

node_test.go

Lines changed: 132 additions & 112 deletions
Large diffs are not rendered by default.

parser_test.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func TestParsingSimpleString(t *testing.T) {
133133

134134
assert.Equal(t, rootNode.ToSexp(), "(source_file (struct_item name: (type_identifier) body: (field_declaration_list)) (function_item name: (identifier) parameters: (parameters) body: (block)))")
135135

136-
structNode := rootNode.Child(0)
136+
structNode := nodeMust(rootNode.Child(0))
137137
assert.Equal(t, structNode.Kind(), "struct_item")
138138
}
139139

@@ -232,7 +232,7 @@ func TestParsingWithCustomUTF8Input(t *testing.T) {
232232
assert.Equal(t, root.ToSexp(), "(source_file (function_item (visibility_modifier) name: (identifier) parameters: (parameters) body: (block (integer_literal))))")
233233
assert.Equal(t, root.Kind(), "source_file")
234234
assert.False(t, root.HasError())
235-
assert.Equal(t, root.Child(0).Kind(), "function_item")
235+
assert.Equal(t, nodeMust(root.Child(0)).Kind(), "function_item")
236236
}
237237

238238
func TestParsingWithCustomUTF16LEInput(t *testing.T) {
@@ -266,7 +266,7 @@ func TestParsingWithCustomUTF16LEInput(t *testing.T) {
266266
assert.Equal(t, root.ToSexp(), "(source_file (function_item (visibility_modifier) name: (identifier) parameters: (parameters) body: (block (integer_literal))))")
267267
assert.Equal(t, root.Kind(), "source_file")
268268
assert.False(t, root.HasError())
269-
assert.Equal(t, root.Child(0).Kind(), "function_item")
269+
assert.Equal(t, nodeMust(root.Child(0)).Kind(), "function_item")
270270
}
271271

272272
func TestParsingWithCustomUTF16BEInput(t *testing.T) {
@@ -311,7 +311,7 @@ func TestParsingWithCustomUTF16BEInput(t *testing.T) {
311311
assert.Equal(t, root.ToSexp(), "(source_file (function_item (visibility_modifier) name: (identifier) parameters: (parameters) body: (block (integer_literal))))")
312312
assert.Equal(t, root.Kind(), "source_file")
313313
assert.False(t, root.HasError())
314-
assert.Equal(t, root.Child(0).Kind(), "function_item")
314+
assert.Equal(t, nodeMust(root.Child(0)).Kind(), "function_item")
315315
}
316316

317317
func TestParsingWithCallbackReturningOwnedStrings(t *testing.T) {
@@ -717,7 +717,7 @@ func TestParsingWithTimeout(t *testing.T) {
717717
nil,
718718
nil,
719719
)
720-
assert.Equal(t, "array", tree.RootNode().Child(0).Kind())
720+
assert.Equal(t, "array", nodeMust(tree.RootNode().Child(0)).Kind())
721721
}
722722

723723
func TestParsingWithTimeoutAndReset(t *testing.T) {
@@ -753,7 +753,7 @@ func TestParsingWithTimeoutAndReset(t *testing.T) {
753753
// it does not see the changes to the beginning of the source code.
754754
code = []byte("[null, 1, 2, 3, 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]")
755755
tree = parser.ParseWithOptions(callback, nil, nil)
756-
assert.Equal(t, "string", tree.RootNode().NamedChild(0).NamedChild(0).Kind())
756+
assert.Equal(t, "string", nodeMust(nodeMust(tree.RootNode().NamedChild(0)).NamedChild(0)).Kind())
757757

758758
code = []byte("[\"ok\", 1, 2, 3, 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]")
759759
tree = parser.ParseWithOptions(
@@ -770,7 +770,7 @@ func TestParsingWithTimeoutAndReset(t *testing.T) {
770770
parser.Reset()
771771
code = []byte("[null, 1, 2, 3, 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]")
772772
tree = parser.ParseWithOptions(callback, nil, nil)
773-
assert.Equal(t, "null", tree.RootNode().NamedChild(0).NamedChild(0).Kind())
773+
assert.Equal(t, "null", nodeMust(nodeMust(tree.RootNode().NamedChild(0)).NamedChild(0)).Kind())
774774
}
775775

776776
func TestParsingWithTimeoutAndImplicitReset(t *testing.T) {
@@ -805,7 +805,7 @@ func TestParsingWithTimeoutAndImplicitReset(t *testing.T) {
805805
parser.SetLanguage(getLanguage("json"))
806806
code = []byte("[null, 1, 2, 3, 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]")
807807
tree = parser.ParseWithOptions(callback, nil, nil)
808-
assert.Equal(t, "null", tree.RootNode().NamedChild(0).NamedChild(0).Kind())
808+
assert.Equal(t, "null", nodeMust(nodeMust(tree.RootNode().NamedChild(0)).NamedChild(0)).Kind())
809809
}
810810

811811
func TestParsingWithTimeoutAndNoCompletion(t *testing.T) {
@@ -951,7 +951,7 @@ func TestParsingWithOneIncludedRange(t *testing.T) {
951951
defer parser.Close()
952952
parser.SetLanguage(getLanguage("html"))
953953
htmlTree := parser.Parse([]byte(sourceCode), nil)
954-
scriptContentNode := htmlTree.RootNode().Child(1).Child(1)
954+
scriptContentNode := nodeMust(nodeMust(htmlTree.RootNode().Child(1)).Child(1))
955955
assert.Equal(t, "raw_text", scriptContentNode.Kind())
956956

957957
assert.Equal(t, []Range{
@@ -985,16 +985,16 @@ func TestParsingWithMultipleIncludedRanges(t *testing.T) {
985985
defer parser.Close()
986986
parser.SetLanguage(getLanguage("javascript"))
987987
jsTree := parser.Parse([]byte(sourceCode), nil)
988-
templateStringNode := jsTree.RootNode().DescendantForByteRange(
988+
templateStringNode := nodeMust(jsTree.RootNode().DescendantForByteRange(
989989
uint(strings.Index(sourceCode, "`<")),
990990
uint(strings.Index(sourceCode, ">`")),
991-
)
991+
))
992992
assert.Equal(t, "template_string", templateStringNode.Kind())
993993

994-
openQuoteNode := templateStringNode.Child(0)
995-
interpolationNode1 := templateStringNode.Child(2)
996-
interpolationNode2 := templateStringNode.Child(4)
997-
closeQuoteNode := templateStringNode.Child(6)
994+
openQuoteNode := nodeMust(templateStringNode.Child(0))
995+
interpolationNode1 := nodeMust(templateStringNode.Child(2))
996+
interpolationNode2 := nodeMust(templateStringNode.Child(4))
997+
closeQuoteNode := nodeMust(templateStringNode.Child(6))
998998

999999
parser.SetLanguage(getLanguage("html"))
10001000
htmlRanges := []Range{
@@ -1027,11 +1027,11 @@ func TestParsingWithMultipleIncludedRanges(t *testing.T) {
10271027
)
10281028
assert.Equal(t, htmlRanges, htmlTree.IncludedRanges())
10291029

1030-
divElementNode := htmlTree.RootNode().Child(0)
1031-
helloTextNode := divElementNode.Child(1)
1032-
bElementNode := divElementNode.Child(2)
1033-
bStartTagNode := bElementNode.Child(0)
1034-
bEndTagNode := bElementNode.Child(1)
1030+
divElementNode := nodeMust(htmlTree.RootNode().Child(0))
1031+
helloTextNode := nodeMust(divElementNode.Child(1))
1032+
bElementNode := nodeMust(divElementNode.Child(2))
1033+
bStartTagNode := nodeMust(bElementNode.Child(0))
1034+
bEndTagNode := nodeMust(bElementNode.Child(1))
10351035

10361036
assert.Equal(t, "text", helloTextNode.Kind())
10371037
assert.Equal(t, uint(strings.Index(sourceCode, "Hello")), helloTextNode.StartByte())
@@ -1164,8 +1164,8 @@ func TestParsingWithExternalScannerThatUsesIncludedRangeBoundaries(t *testing.T)
11641164
tree := parser.Parse([]byte(sourceCode), nil)
11651165
defer tree.Close()
11661166
root := tree.RootNode()
1167-
statement1 := root.Child(0)
1168-
statement2 := root.Child(1)
1167+
statement1 := nodeMust(root.Child(0))
1168+
statement2 := nodeMust(root.Child(1))
11691169

11701170
assert.Equal(
11711171
t,

query.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ func (qc *QueryCursor) DidExceedMatchLimit() bool {
748748
// captures. Because multiple patterns can match the same set of nodes,
749749
// one match may contain captures that appear *before* some of the
750750
// captures from a previous match.
751-
func (qc *QueryCursor) Matches(query *Query, node *Node, text []byte) QueryMatches {
751+
func (qc *QueryCursor) Matches(query *Query, node Node, text []byte) QueryMatches {
752752
C.ts_query_cursor_exec(qc._inner, query._inner, node._inner)
753753
qm := QueryMatches{
754754
_inner: qc._inner,
@@ -783,7 +783,7 @@ func queryProgressCallback(state *C.TSQueryCursorState) C.bool {
783783
// captures. Because multiple patterns can match the same set of nodes,
784784
// one match may contain captures that appear *before* some of the
785785
// captures from a previous match.
786-
func (qc *QueryCursor) MatchesWithOptions(query *Query, node *Node, text []byte, options QueryCursorOptions) QueryMatches {
786+
func (qc *QueryCursor) MatchesWithOptions(query *Query, node Node, text []byte, options QueryCursorOptions) QueryMatches {
787787
cOptions := &C.TSQueryCursorOptions{
788788
payload: pointer.Save(&options),
789789
progress_callback: (*[0]byte)(C.queryProgressCallback),
@@ -813,7 +813,7 @@ func (qc *QueryCursor) MatchesWithOptions(query *Query, node *Node, text []byte,
813813
//
814814
// This is useful if you don't care about which pattern matched, and just
815815
// want a single, ordered sequence of captures.
816-
func (qc *QueryCursor) Captures(query *Query, node *Node, text []byte) QueryCaptures {
816+
func (qc *QueryCursor) Captures(query *Query, node Node, text []byte) QueryCaptures {
817817
C.ts_query_cursor_exec(qc._inner, query._inner, node._inner)
818818
return QueryCaptures{
819819
_inner: qc._inner,

query_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4887,7 +4887,7 @@ func TestQueryMaxStartDepthMore(t *testing.T) {
48874887
for _, row := range rows {
48884888
cursor.SetMaxStartDepth(&row.depth)
48894889

4890-
matches := cursor.Matches(query, &node, []byte(source))
4890+
matches := cursor.Matches(query, node, []byte(source))
48914891
assert.Equal(t, row.matches, collectMatches(matches, query, source))
48924892
}
48934893
}

tree.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ func newTree(inner *C.TSTree) *Tree {
2222
}
2323

2424
// Get the root node of the syntax tree.
25-
func (t *Tree) RootNode() *Node {
26-
return &Node{_inner: C.ts_tree_root_node(t._inner)}
25+
func (t *Tree) RootNode() Node {
26+
return Node{_inner: C.ts_tree_root_node(t._inner)}
2727
}
2828

2929
// Get the root node of the syntax tree, but with its position shifted
3030
// forward by the given offset.
31-
func (t *Tree) RootNodeWithOffset(offsetBytes int, offsetExtent Point) *Node {
32-
return &Node{_inner: C.ts_tree_root_node_with_offset(t._inner, C.uint(offsetBytes), offsetExtent.toTSPoint())}
31+
func (t *Tree) RootNodeWithOffset(offsetBytes int, offsetExtent Point) Node {
32+
return Node{_inner: C.ts_tree_root_node_with_offset(t._inner, C.uint(offsetBytes), offsetExtent.toTSPoint())}
3333
}
3434

3535
// Get the language that was used to parse the syntax tree.

tree_cursor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ func (tc *TreeCursor) Copy() *TreeCursor {
2424
}
2525

2626
// Get the tree cursor's current [Node].
27-
func (tc *TreeCursor) Node() *Node {
28-
return newNode(C.ts_tree_cursor_current_node(&tc._inner))
27+
func (tc *TreeCursor) Node() Node {
28+
return Node{_inner: C.ts_tree_cursor_current_node(&tc._inner)}
2929
}
3030

3131
// Get the numerical field id of this tree cursor's current node.

tree_test.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ func TestTreeEdit(t *testing.T) {
8888
NewEndPosition: Point{Row: 0, Column: 2},
8989
})
9090

91-
expr := tree.RootNode().Child(0).Child(0)
92-
child1 := expr.Child(0)
93-
child2 := expr.Child(1)
91+
expr := nodeMust(nodeMust(tree.RootNode().Child(0)).Child(0))
92+
child1 := nodeMust(expr.Child(0))
93+
child2 := nodeMust(expr.Child(1))
9494

9595
assert.True(t, expr.HasChanges())
9696
assert.EqualValues(t, expr.StartByte(), 3)
@@ -116,9 +116,9 @@ func TestTreeEdit(t *testing.T) {
116116
NewEndPosition: Point{Row: 0, Column: 5},
117117
})
118118

119-
expr := tree.RootNode().Child(0).Child(0)
120-
child1 := expr.Child(0)
121-
child2 := expr.Child(1)
119+
expr := nodeMust(nodeMust(tree.RootNode().Child(0)).Child(0))
120+
child1 := nodeMust(expr.Child(0))
121+
child2 := nodeMust(expr.Child(1))
122122

123123
assert.True(t, expr.HasChanges())
124124
assert.EqualValues(t, expr.StartByte(), 5)
@@ -155,9 +155,9 @@ func TestTreeEdit(t *testing.T) {
155155
// assert!(!child2.has_changes());
156156
// assert_eq!(child2.byte_range(), 9..12);
157157

158-
expr := tree.RootNode().Child(0).Child(0)
159-
child1 := expr.Child(0)
160-
child2 := expr.Child(1)
158+
expr := nodeMust(nodeMust(tree.RootNode().Child(0)).Child(0))
159+
child1 := nodeMust(expr.Child(0))
160+
child2 := nodeMust(expr.Child(1))
161161

162162
assert.True(t, expr.HasChanges())
163163
assert.EqualValues(t, expr.StartByte(), 4)
@@ -183,9 +183,9 @@ func TestTreeEdit(t *testing.T) {
183183
NewEndPosition: Point{Row: 0, Column: 4},
184184
})
185185

186-
expr := tree.RootNode().Child(0).Child(0)
187-
child1 := expr.Child(0)
188-
child2 := expr.Child(1)
186+
expr := nodeMust(nodeMust(tree.RootNode().Child(0)).Child(0))
187+
child1 := nodeMust(expr.Child(0))
188+
child2 := nodeMust(expr.Child(1))
189189

190190
assert.True(t, expr.HasChanges())
191191
assert.EqualValues(t, expr.StartByte(), 4)
@@ -211,10 +211,10 @@ func TestTreeEdit(t *testing.T) {
211211
NewEndPosition: Point{Row: 0, Column: 4},
212212
})
213213

214-
expr := tree.RootNode().Child(0).Child(0)
215-
child1 := expr.Child(0)
216-
child2 := expr.Child(1)
217-
child3 := expr.Child(2)
214+
expr := nodeMust(nodeMust(tree.RootNode().Child(0)).Child(0))
215+
child1 := nodeMust(expr.Child(0))
216+
child2 := nodeMust(expr.Child(1))
217+
child3 := nodeMust(expr.Child(2))
218218

219219
assert.True(t, expr.HasChanges())
220220
assert.EqualValues(t, expr.StartByte(), 4)
@@ -243,10 +243,10 @@ func TestTreeEdit(t *testing.T) {
243243
NewEndPosition: Point{Row: 0, Column: 16},
244244
})
245245

246-
expr := tree.RootNode().Child(0).Child(0)
247-
child1 := expr.Child(0)
248-
child2 := expr.Child(1)
249-
child3 := expr.Child(2)
246+
expr := nodeMust(nodeMust(tree.RootNode().Child(0)).Child(0))
247+
child1 := nodeMust(expr.Child(0))
248+
child2 := nodeMust(expr.Child(1))
249+
child3 := nodeMust(expr.Child(2))
250250

251251
assert.True(t, expr.HasChanges())
252252
assert.EqualValues(t, expr.StartByte(), 2)
@@ -275,10 +275,10 @@ func TestTreeEdit(t *testing.T) {
275275
NewEndPosition: Point{Row: 0, Column: 4},
276276
})
277277

278-
expr := tree.RootNode().Child(0).Child(0)
279-
child1 := expr.Child(0)
280-
child2 := expr.Child(1)
281-
child3 := expr.Child(2)
278+
expr := nodeMust(nodeMust(tree.RootNode().Child(0)).Child(0))
279+
child1 := nodeMust(expr.Child(0))
280+
child2 := nodeMust(expr.Child(1))
281+
child3 := nodeMust(expr.Child(2))
282282

283283
assert.True(t, expr.HasChanges())
284284
assert.EqualValues(t, expr.StartByte(), 2)
@@ -307,10 +307,10 @@ func TestTreeEdit(t *testing.T) {
307307
NewEndPosition: Point{Row: 0, Column: 8},
308308
})
309309

310-
expr := tree.RootNode().Child(0).Child(0)
311-
child1 := expr.Child(0)
312-
child2 := expr.Child(1)
313-
child3 := expr.Child(2)
310+
expr := nodeMust(nodeMust(tree.RootNode().Child(0)).Child(0))
311+
child1 := nodeMust(expr.Child(0))
312+
child2 := nodeMust(expr.Child(1))
313+
child3 := nodeMust(expr.Child(2))
314314

315315
assert.True(t, expr.HasChanges())
316316
assert.EqualValues(t, expr.StartByte(), 2)
@@ -602,8 +602,8 @@ func TestTreeNodeEquality(t *testing.T) {
602602
node2 := tree.RootNode()
603603

604604
assert.Equal(t, node1, node2)
605-
assert.Equal(t, node1.Child(0), node2.Child(0))
606-
assert.NotEqual(t, node1.Child(0), node2)
605+
assert.Equal(t, nodeMust(node1.Child(0)), nodeMust(node2.Child(0)))
606+
assert.NotEqual(t, nodeMust(node1.Child(0)), node2)
607607
}
608608

609609
func TestGetChangedRanges(t *testing.T) {

0 commit comments

Comments
 (0)