Skip to content

Commit

Permalink
refactor: change Node pointers to values
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Nov 24, 2024
1 parent 2c079a9 commit f22ab79
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 74 deletions.
108 changes: 54 additions & 54 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,108 +26,108 @@ func newNode(node C.TSNode) *Node {
// a new tree is created based on an older tree, and a node from the old
// tree is reused in the process, then that node will have the same id in
// both trees.
func (n *Node) Id() uintptr {
func (n Node) Id() uintptr {
return uintptr(n._inner.id)
}

// Get this node's type as a numerical id.
func (n *Node) KindId() uint16 {
func (n Node) KindId() uint16 {
return uint16(C.ts_node_symbol(n._inner))
}

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

// Get this node's type as a string.
func (n *Node) Kind() string {
func (n Node) Kind() string {
return C.GoString(C.ts_node_type(n._inner))
}

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

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

// Check if this node is *named*.
//
// Named nodes correspond to named rules in the grammar, whereas
// *anonymous* nodes correspond to string literals in the grammar.
func (n *Node) IsNamed() bool {
func (n Node) IsNamed() bool {
return bool(C.ts_node_is_named(n._inner))
}

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

// Check if this node has been edited.
func (n *Node) HasChanges() bool {
func (n Node) HasChanges() bool {
return bool(C.ts_node_has_changes(n._inner))
}

// Check if this node represents a syntax error or contains any syntax
// errors anywhere within it.
func (n *Node) HasError() bool {
func (n Node) HasError() bool {
return bool(C.ts_node_has_error(n._inner))
}

// Check if this node represents a syntax error.
//
// Syntax errors represent parts of the code that could not be incorporated
// into a valid syntax tree.
func (n *Node) IsError() bool {
func (n Node) IsError() bool {
return bool(C.ts_node_is_error(n._inner))
}

// Get this node's parse state.
func (n *Node) ParseState() uint16 {
func (n Node) ParseState() uint16 {
return uint16(C.ts_node_parse_state(n._inner))
}

// Get the parse state after this node.
func (n *Node) NextParseState() uint16 {
func (n Node) NextParseState() uint16 {
return uint16(C.ts_node_next_parse_state(n._inner))
}

// Check if this node is *missing*.
//
// Missing nodes are inserted by the parser in order to recover from
// certain kinds of syntax errors.
func (n *Node) IsMissing() bool {
func (n Node) IsMissing() bool {
return bool(C.ts_node_is_missing(n._inner))
}

// Get the byte offsets where this node starts.
func (n *Node) StartByte() uint {
func (n Node) StartByte() uint {
return uint(C.ts_node_start_byte(n._inner))
}

// Get the byte offsets where this node end.
func (n *Node) EndByte() uint {
func (n Node) EndByte() uint {
return uint(C.ts_node_end_byte(n._inner))
}

// Get the byte range of source code that this node represents.
func (n *Node) ByteRange() (uint, uint) {
func (n Node) ByteRange() (uint, uint) {
return n.StartByte(), n.EndByte()
}

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

// Get this node's start position in terms of rows and columns.
func (n *Node) StartPosition() Point {
func (n Node) StartPosition() Point {
p := Point{}
p.fromTSPoint(C.ts_node_start_point(n._inner))
return p
}

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

// Get this node's number of children.
func (n *Node) ChildCount() uint {
func (n Node) ChildCount() uint {
return uint(C.ts_node_child_count(n._inner))
}

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

// Get this node's number of *named* children.
//
// See also [Node.IsNamed].
func (n *Node) NamedChildCount() uint {
func (n Node) NamedChildCount() uint {
return uint(C.ts_node_named_child_count(n._inner))
}

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

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

// Get the field name of this node's named child at the given index.
func (n *Node) FieldNameForNamedChild(namedChildIndex uint32) string {
func (n Node) FieldNameForNamedChild(namedChildIndex uint32) string {
ptr := C.ts_node_field_name_for_named_child(n._inner, C.uint32_t(namedChildIndex))
if ptr == nil {
return ""
Expand All @@ -227,8 +227,8 @@ func (n *Node) FieldNameForNamedChild(namedChildIndex uint32) string {
//
// If you're walking the tree recursively, you may want to use the
// [TreeCursor] APIs directly instead.
func (n *Node) Children(cursor *TreeCursor) []Node {
cursor.Reset(*n)
func (n Node) Children(cursor *TreeCursor) []Node {
cursor.Reset(n)
cursor.GotoFirstChild()
childCount := n.ChildCount()
result := make([]Node, 0, childCount)
Expand All @@ -242,8 +242,8 @@ func (n *Node) Children(cursor *TreeCursor) []Node {
// Iterate over this node's named children.
//
// See also [Node.Children].
func (n *Node) NamedChildren(cursor *TreeCursor) []Node {
cursor.Reset(*n)
func (n Node) NamedChildren(cursor *TreeCursor) []Node {
cursor.Reset(n)
cursor.GotoFirstChild()
namedChildCount := n.NamedChildCount()
result := make([]Node, 0, namedChildCount)
Expand All @@ -262,11 +262,11 @@ func (n *Node) NamedChildren(cursor *TreeCursor) []Node {
// Iterate over this node's children with a given field name.
//
// See also [Node.Children].
func (n *Node) ChildrenByFieldName(fieldName string, cursor *TreeCursor) []Node {
func (n Node) ChildrenByFieldName(fieldName string, cursor *TreeCursor) []Node {
fieldId := n.Language().FieldIdForName(fieldName)
done := fieldId == 0
if !done {
cursor.Reset(*n)
cursor.Reset(n)
cursor.GotoFirstChild()
}
result := make([]Node, 0)
Expand All @@ -287,98 +287,98 @@ func (n *Node) ChildrenByFieldName(fieldName string, cursor *TreeCursor) []Node
// Get this node's immediate parent.
// Prefer [Node.ChildContainingDescendant]
// for iterating over this node's ancestors.
func (n *Node) Parent() *Node {
func (n Node) Parent() *Node {
return newNode(C.ts_node_parent(n._inner))
}

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

// Get the node that contains `descendant`.
// Note that this can return `descendant` itself, unlike the deprecated function
// [Node.ChildContainingDescendant].
func (n *Node) ChildWithDescendant(descendant *Node) *Node {
func (n Node) ChildWithDescendant(descendant Node) *Node {
return newNode(C.ts_node_child_with_descendant(n._inner, descendant._inner))
}

// Get this node's next sibling.
func (n *Node) NextSibling() *Node {
func (n Node) NextSibling() *Node {
return newNode(C.ts_node_next_sibling(n._inner))
}

// Get this node's previous sibling.
func (n *Node) PrevSibling() *Node {
func (n Node) PrevSibling() *Node {
return newNode(C.ts_node_prev_sibling(n._inner))
}

// Get this node's next named sibling.
func (n *Node) NextNamedSibling() *Node {
func (n Node) NextNamedSibling() *Node {
return newNode(C.ts_node_next_named_sibling(n._inner))
}

// Get this node's previous named sibling.
func (n *Node) PrevNamedSibling() *Node {
func (n Node) PrevNamedSibling() *Node {
return newNode(C.ts_node_prev_named_sibling(n._inner))
}

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

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

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

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

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

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

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

func (n *Node) ToSexp() string {
func (n Node) ToSexp() string {
cString := C.ts_node_string(n._inner)
result := C.GoString(cString)
go_free(unsafe.Pointer(cString))
return result
}

func (n *Node) Utf8Text(source []byte) string {
func (n Node) Utf8Text(source []byte) string {
return string(source[n.StartByte():n.EndByte()])
}

func (n *Node) Utf16Text(source []uint16) []uint16 {
func (n Node) Utf16Text(source []uint16) []uint16 {
return source[n.StartByte():n.EndByte()]
}

// Create a new [TreeCursor] starting from this node.
func (n *Node) Walk() *TreeCursor {
return newTreeCursor(*n)
func (n Node) Walk() *TreeCursor {
return newTreeCursor(n)
}

// Edit this node to keep it in-sync with source code that has been edited.
Expand All @@ -393,6 +393,6 @@ func (n *Node) Edit(edit *InputEdit) {
}

// Check if two nodes are identical.
func (n *Node) Equals(other Node) bool {
func (n Node) Equals(other Node) bool {
return bool(C.ts_node_eq(n._inner, other._inner))
}
Loading

0 comments on commit f22ab79

Please sign in to comment.