-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbound_tree.go
More file actions
78 lines (68 loc) · 1.96 KB
/
Copy pathbound_tree.go
File metadata and controls
78 lines (68 loc) · 1.96 KB
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
package xref
// BoundTree pairs a Tree with its Language and source, eliminating the need
// to pass *Language and []byte to every node method call.
type BoundTree struct {
tree *Tree
}
// Bind creates a BoundTree from a Tree. The Tree must have been created with
// a Language (via NewTree or a Parser). Returns a BoundTree that delegates to
// the underlying Tree's Language and Source.
func Bind(tree *Tree) *BoundTree {
return &BoundTree{tree: tree}
}
// RootNode returns the tree's root node.
func (bt *BoundTree) RootNode() *Node {
if bt == nil || bt.tree == nil {
return nil
}
return bt.tree.RootNode()
}
// Language returns the tree's language.
func (bt *BoundTree) Language() *Language {
if bt == nil || bt.tree == nil {
return nil
}
return bt.tree.Language()
}
// Source returns the tree's source bytes.
func (bt *BoundTree) Source() []byte {
if bt == nil || bt.tree == nil {
return nil
}
return bt.tree.Source()
}
// NodeType returns the node's type name, resolved via the bound language.
func (bt *BoundTree) NodeType(n *Node) string {
if bt == nil || bt.tree == nil || n == nil {
return ""
}
return n.Type(bt.tree.Language())
}
// NodeText returns the source text covered by the node.
func (bt *BoundTree) NodeText(n *Node) string {
if bt == nil || bt.tree == nil || n == nil {
return ""
}
return n.Text(bt.tree.Source())
}
// ChildByField returns the first child assigned to the given field name.
func (bt *BoundTree) ChildByField(n *Node, fieldName string) *Node {
if bt == nil || bt.tree == nil || n == nil {
return nil
}
return n.ChildByFieldName(fieldName, bt.tree.Language())
}
// TreeCursor returns a new TreeCursor starting at the tree's root node.
func (bt *BoundTree) TreeCursor() *TreeCursor {
if bt == nil || bt.tree == nil {
return nil
}
return NewTreeCursorFromTree(bt.tree)
}
// Release releases the underlying tree's arena memory.
func (bt *BoundTree) Release() {
if bt == nil || bt.tree == nil {
return
}
bt.tree.Release()
}