If your code works with a large number of nodes, consider using the traversal APIs, which are more efficient.
The result of parsing is a syntax tree of the entire source code (string, buffer). It contains syntax nodes that indicate the structure of the source code. Tree-sitter provides APIs to inspect and traverse this structure, but does not support modifying it directly (for the purposes of source code transformation or generation).
tsc-root-node
tree
tsc-changed-ranges
old-tree new-tree
tree-sitter-after-change-functions
hook. This function returns a sequence of ranges whose syntactic structure has changed. Each range is a vector in the form of [start-bytepos end-bytepos start-point end-point]
.
In tree-sitter
’s context, point
typically means a pair of (line-number . byte-column)
, instead of its usual meaning of current position. See Data Types.
tsc-tree-to-sexp
tsc-node-to-sexp
Functions that return a node’s property have the prefix tsc-node-
:
tsc-node-type
tsc-node-named-p
These less important nodes are called anonymous nodes. Their node types are strings. For example: "if"
, "else"
. The more important nodes are call named nodes. Their node types are symbols, corresponding to the named rules that define them in the language’s grammar. For example: identifier
, block
, if_expression
.
In Tree-sitter’s documentation, due to the low-level nature of C and JSON, node types are always represented as strings. Representing named node types as symbols makes it more Lisp-idiomatic, and is more consistent with tree queries.
tsc-node-extra-p
tsc-node-error-p
ERROR
.tsc-node-has-error-p
tsc-node-missing-p
tsc-node-start-byte
tsc-node-end-byte
tsc-node-start-position
tsc-node-end-position
tsc-node-range
[start-bytepos end-bytepos start-point end-point]
.As described in the previous section, the -named-
variants of the functions in this section allow working on the parse tree as if it is an abstract syntax tree.
tsc-get-parent
node
tsc-count-children
node
tsc-count-named-children
node
tsc-get-nth-child
node nth
tsc-get-nth-named-child
node nth
(let ((func (tree-sitter-node-at-pos 'function_item)))
(tsc-get-nth-child func 0) ; An "fn" node
(tsc-get-nth-named-child func 0)) ; An 'identifier node
tsc-get-child-by-field
node field
;; Get name of the current function definition.
(let ((func (tree-sitter-node-at-pos 'function_item)))
(tsc-node-text (tsc-get-child-by-field func :name)))
In Tree-sitter’s documentation, due to the low-level nature of C and JSON, field names are specified as strings. Representing field names as keywords makes it more Lisp-idiomatic.
tsc-get-next-sibling
node
tsc-get-prev-sibling
node
tsc-get-next-named-sibling
node
tsc-get-prev-named-sibling
node
tsc-get-descendant-for-position-range
node beg end
tsc-get-named-descendant-for-position-range
node beg end
;; Get the syntax node the cursor is on.
(let ((p (point)))
(tsc-get-descendant-for-position-range
(tsc-root-node tree-sitter-tree) p p))