Skip to content

refactor: change Node pointers to values & return (Node, bool) #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 59 additions & 59 deletions node.go

Large diffs are not rendered by default.

244 changes: 132 additions & 112 deletions node_test.go

Large diffs are not rendered by default.

44 changes: 22 additions & 22 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestParsingSimpleString(t *testing.T) {

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

structNode := rootNode.Child(0)
structNode := nodeMust(rootNode.Child(0))
assert.Equal(t, structNode.Kind(), "struct_item")
}

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

func TestParsingWithCustomUTF16LEInput(t *testing.T) {
Expand Down Expand Up @@ -266,7 +266,7 @@ func TestParsingWithCustomUTF16LEInput(t *testing.T) {
assert.Equal(t, root.ToSexp(), "(source_file (function_item (visibility_modifier) name: (identifier) parameters: (parameters) body: (block (integer_literal))))")
assert.Equal(t, root.Kind(), "source_file")
assert.False(t, root.HasError())
assert.Equal(t, root.Child(0).Kind(), "function_item")
assert.Equal(t, nodeMust(root.Child(0)).Kind(), "function_item")
}

func TestParsingWithCustomUTF16BEInput(t *testing.T) {
Expand Down Expand Up @@ -311,7 +311,7 @@ func TestParsingWithCustomUTF16BEInput(t *testing.T) {
assert.Equal(t, root.ToSexp(), "(source_file (function_item (visibility_modifier) name: (identifier) parameters: (parameters) body: (block (integer_literal))))")
assert.Equal(t, root.Kind(), "source_file")
assert.False(t, root.HasError())
assert.Equal(t, root.Child(0).Kind(), "function_item")
assert.Equal(t, nodeMust(root.Child(0)).Kind(), "function_item")
}

func TestParsingWithCallbackReturningOwnedStrings(t *testing.T) {
Expand Down Expand Up @@ -717,7 +717,7 @@ func TestParsingWithTimeout(t *testing.T) {
nil,
nil,
)
assert.Equal(t, "array", tree.RootNode().Child(0).Kind())
assert.Equal(t, "array", nodeMust(tree.RootNode().Child(0)).Kind())
}

func TestParsingWithTimeoutAndReset(t *testing.T) {
Expand Down Expand Up @@ -753,7 +753,7 @@ func TestParsingWithTimeoutAndReset(t *testing.T) {
// it does not see the changes to the beginning of the source code.
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]")
tree = parser.ParseWithOptions(callback, nil, nil)
assert.Equal(t, "string", tree.RootNode().NamedChild(0).NamedChild(0).Kind())
assert.Equal(t, "string", nodeMust(nodeMust(tree.RootNode().NamedChild(0)).NamedChild(0)).Kind())

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]")
tree = parser.ParseWithOptions(
Expand All @@ -770,7 +770,7 @@ func TestParsingWithTimeoutAndReset(t *testing.T) {
parser.Reset()
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]")
tree = parser.ParseWithOptions(callback, nil, nil)
assert.Equal(t, "null", tree.RootNode().NamedChild(0).NamedChild(0).Kind())
assert.Equal(t, "null", nodeMust(nodeMust(tree.RootNode().NamedChild(0)).NamedChild(0)).Kind())
}

func TestParsingWithTimeoutAndImplicitReset(t *testing.T) {
Expand Down Expand Up @@ -805,7 +805,7 @@ func TestParsingWithTimeoutAndImplicitReset(t *testing.T) {
parser.SetLanguage(getLanguage("json"))
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]")
tree = parser.ParseWithOptions(callback, nil, nil)
assert.Equal(t, "null", tree.RootNode().NamedChild(0).NamedChild(0).Kind())
assert.Equal(t, "null", nodeMust(nodeMust(tree.RootNode().NamedChild(0)).NamedChild(0)).Kind())
}

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

assert.Equal(t, []Range{
Expand Down Expand Up @@ -985,16 +985,16 @@ func TestParsingWithMultipleIncludedRanges(t *testing.T) {
defer parser.Close()
parser.SetLanguage(getLanguage("javascript"))
jsTree := parser.Parse([]byte(sourceCode), nil)
templateStringNode := jsTree.RootNode().DescendantForByteRange(
templateStringNode := nodeMust(jsTree.RootNode().DescendantForByteRange(
uint(strings.Index(sourceCode, "`<")),
uint(strings.Index(sourceCode, ">`")),
)
))
assert.Equal(t, "template_string", templateStringNode.Kind())

openQuoteNode := templateStringNode.Child(0)
interpolationNode1 := templateStringNode.Child(2)
interpolationNode2 := templateStringNode.Child(4)
closeQuoteNode := templateStringNode.Child(6)
openQuoteNode := nodeMust(templateStringNode.Child(0))
interpolationNode1 := nodeMust(templateStringNode.Child(2))
interpolationNode2 := nodeMust(templateStringNode.Child(4))
closeQuoteNode := nodeMust(templateStringNode.Child(6))

parser.SetLanguage(getLanguage("html"))
htmlRanges := []Range{
Expand Down Expand Up @@ -1027,11 +1027,11 @@ func TestParsingWithMultipleIncludedRanges(t *testing.T) {
)
assert.Equal(t, htmlRanges, htmlTree.IncludedRanges())

divElementNode := htmlTree.RootNode().Child(0)
helloTextNode := divElementNode.Child(1)
bElementNode := divElementNode.Child(2)
bStartTagNode := bElementNode.Child(0)
bEndTagNode := bElementNode.Child(1)
divElementNode := nodeMust(htmlTree.RootNode().Child(0))
helloTextNode := nodeMust(divElementNode.Child(1))
bElementNode := nodeMust(divElementNode.Child(2))
bStartTagNode := nodeMust(bElementNode.Child(0))
bEndTagNode := nodeMust(bElementNode.Child(1))

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

assert.Equal(
t,
Expand Down
6 changes: 3 additions & 3 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ func (qc *QueryCursor) DidExceedMatchLimit() bool {
// captures. Because multiple patterns can match the same set of nodes,
// one match may contain captures that appear *before* some of the
// captures from a previous match.
func (qc *QueryCursor) Matches(query *Query, node *Node, text []byte) QueryMatches {
func (qc *QueryCursor) Matches(query *Query, node Node, text []byte) QueryMatches {
C.ts_query_cursor_exec(qc._inner, query._inner, node._inner)
qm := QueryMatches{
_inner: qc._inner,
Expand Down Expand Up @@ -783,7 +783,7 @@ func queryProgressCallback(state *C.TSQueryCursorState) C.bool {
// captures. Because multiple patterns can match the same set of nodes,
// one match may contain captures that appear *before* some of the
// captures from a previous match.
func (qc *QueryCursor) MatchesWithOptions(query *Query, node *Node, text []byte, options QueryCursorOptions) QueryMatches {
func (qc *QueryCursor) MatchesWithOptions(query *Query, node Node, text []byte, options QueryCursorOptions) QueryMatches {
cOptions := &C.TSQueryCursorOptions{
payload: pointer.Save(&options),
progress_callback: (*[0]byte)(C.queryProgressCallback),
Expand Down Expand Up @@ -813,7 +813,7 @@ func (qc *QueryCursor) MatchesWithOptions(query *Query, node *Node, text []byte,
//
// This is useful if you don't care about which pattern matched, and just
// want a single, ordered sequence of captures.
func (qc *QueryCursor) Captures(query *Query, node *Node, text []byte) QueryCaptures {
func (qc *QueryCursor) Captures(query *Query, node Node, text []byte) QueryCaptures {
C.ts_query_cursor_exec(qc._inner, query._inner, node._inner)
return QueryCaptures{
_inner: qc._inner,
Expand Down
2 changes: 1 addition & 1 deletion query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4887,7 +4887,7 @@ func TestQueryMaxStartDepthMore(t *testing.T) {
for _, row := range rows {
cursor.SetMaxStartDepth(&row.depth)

matches := cursor.Matches(query, &node, []byte(source))
matches := cursor.Matches(query, node, []byte(source))
assert.Equal(t, row.matches, collectMatches(matches, query, source))
}
}
Expand Down
8 changes: 4 additions & 4 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ func newTree(inner *C.TSTree) *Tree {
}

// Get the root node of the syntax tree.
func (t *Tree) RootNode() *Node {
return &Node{_inner: C.ts_tree_root_node(t._inner)}
func (t *Tree) RootNode() Node {
return Node{_inner: C.ts_tree_root_node(t._inner)}
}

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

// Get the language that was used to parse the syntax tree.
Expand Down
4 changes: 2 additions & 2 deletions tree_cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func (tc *TreeCursor) Copy() *TreeCursor {
}

// Get the tree cursor's current [Node].
func (tc *TreeCursor) Node() *Node {
return newNode(C.ts_tree_cursor_current_node(&tc._inner))
func (tc *TreeCursor) Node() Node {
return Node{_inner: C.ts_tree_cursor_current_node(&tc._inner)}
}

// Get the numerical field id of this tree cursor's current node.
Expand Down
60 changes: 30 additions & 30 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ func TestTreeEdit(t *testing.T) {
NewEndPosition: Point{Row: 0, Column: 2},
})

expr := tree.RootNode().Child(0).Child(0)
child1 := expr.Child(0)
child2 := expr.Child(1)
expr := nodeMust(nodeMust(tree.RootNode().Child(0)).Child(0))
child1 := nodeMust(expr.Child(0))
child2 := nodeMust(expr.Child(1))

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

expr := tree.RootNode().Child(0).Child(0)
child1 := expr.Child(0)
child2 := expr.Child(1)
expr := nodeMust(nodeMust(tree.RootNode().Child(0)).Child(0))
child1 := nodeMust(expr.Child(0))
child2 := nodeMust(expr.Child(1))

assert.True(t, expr.HasChanges())
assert.EqualValues(t, expr.StartByte(), 5)
Expand Down Expand Up @@ -155,9 +155,9 @@ func TestTreeEdit(t *testing.T) {
// assert!(!child2.has_changes());
// assert_eq!(child2.byte_range(), 9..12);

expr := tree.RootNode().Child(0).Child(0)
child1 := expr.Child(0)
child2 := expr.Child(1)
expr := nodeMust(nodeMust(tree.RootNode().Child(0)).Child(0))
child1 := nodeMust(expr.Child(0))
child2 := nodeMust(expr.Child(1))

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

expr := tree.RootNode().Child(0).Child(0)
child1 := expr.Child(0)
child2 := expr.Child(1)
expr := nodeMust(nodeMust(tree.RootNode().Child(0)).Child(0))
child1 := nodeMust(expr.Child(0))
child2 := nodeMust(expr.Child(1))

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

expr := tree.RootNode().Child(0).Child(0)
child1 := expr.Child(0)
child2 := expr.Child(1)
child3 := expr.Child(2)
expr := nodeMust(nodeMust(tree.RootNode().Child(0)).Child(0))
child1 := nodeMust(expr.Child(0))
child2 := nodeMust(expr.Child(1))
child3 := nodeMust(expr.Child(2))

assert.True(t, expr.HasChanges())
assert.EqualValues(t, expr.StartByte(), 4)
Expand Down Expand Up @@ -243,10 +243,10 @@ func TestTreeEdit(t *testing.T) {
NewEndPosition: Point{Row: 0, Column: 16},
})

expr := tree.RootNode().Child(0).Child(0)
child1 := expr.Child(0)
child2 := expr.Child(1)
child3 := expr.Child(2)
expr := nodeMust(nodeMust(tree.RootNode().Child(0)).Child(0))
child1 := nodeMust(expr.Child(0))
child2 := nodeMust(expr.Child(1))
child3 := nodeMust(expr.Child(2))

assert.True(t, expr.HasChanges())
assert.EqualValues(t, expr.StartByte(), 2)
Expand Down Expand Up @@ -275,10 +275,10 @@ func TestTreeEdit(t *testing.T) {
NewEndPosition: Point{Row: 0, Column: 4},
})

expr := tree.RootNode().Child(0).Child(0)
child1 := expr.Child(0)
child2 := expr.Child(1)
child3 := expr.Child(2)
expr := nodeMust(nodeMust(tree.RootNode().Child(0)).Child(0))
child1 := nodeMust(expr.Child(0))
child2 := nodeMust(expr.Child(1))
child3 := nodeMust(expr.Child(2))

assert.True(t, expr.HasChanges())
assert.EqualValues(t, expr.StartByte(), 2)
Expand Down Expand Up @@ -307,10 +307,10 @@ func TestTreeEdit(t *testing.T) {
NewEndPosition: Point{Row: 0, Column: 8},
})

expr := tree.RootNode().Child(0).Child(0)
child1 := expr.Child(0)
child2 := expr.Child(1)
child3 := expr.Child(2)
expr := nodeMust(nodeMust(tree.RootNode().Child(0)).Child(0))
child1 := nodeMust(expr.Child(0))
child2 := nodeMust(expr.Child(1))
child3 := nodeMust(expr.Child(2))

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

assert.Equal(t, node1, node2)
assert.Equal(t, node1.Child(0), node2.Child(0))
assert.NotEqual(t, node1.Child(0), node2)
assert.Equal(t, nodeMust(node1.Child(0)), nodeMust(node2.Child(0)))
assert.NotEqual(t, nodeMust(node1.Child(0)), node2)
}

func TestGetChangedRanges(t *testing.T) {
Expand Down