Home Explore Blog CI



neovim

10th chunk of `runtime/doc/treesitter.txt`
5a8bfb104ff9b987c6e18b2e9681b9f6e0969889f782052a0000000100000fb9
 function
documentation.


                                             *vim.treesitter.language_version*
The latest parser ABI version that is supported by the bundled treesitter
library.

                                     *vim.treesitter.minimum_language_version*
The earliest parser ABI version that is supported by the bundled treesitter
library.

==============================================================================
TREESITTER TREES                                    *treesitter-tree* *TSTree*

A "treesitter tree" represents the parsed contents of a buffer, which can be
used to perform further analysis. It is a |userdata| reference to an object
held by the treesitter library.

An instance `TSTree` of a treesitter tree supports the following methods.


TSTree:copy()                                                  *TSTree:copy()*
    Returns a copy of the `TSTree`.

    Return: ~
        (`TSTree`)

TSTree:root()                                                  *TSTree:root()*
    Return the root node of this tree.

    Return: ~
        (`TSNode`)


==============================================================================
TREESITTER NODES                                    *treesitter-node* *TSNode*

A "treesitter node" represents one specific element of the parsed contents of
a buffer, which can be captured by a |Query| for, e.g., highlighting. It is a
|userdata| reference to an object held by the treesitter library.

An instance `TSNode` of a treesitter node supports the following methods.


TSNode:byte_length()                                    *TSNode:byte_length()*
    Return the number of bytes spanned by this node.

    Return: ~
        (`integer`)

TSNode:child({index})                                         *TSNode:child()*
    Get the node's child at the given {index}, where zero represents the first
    child.

    Parameters: ~
      • {index}  (`integer`)

    Return: ~
        (`TSNode?`)

TSNode:child_count()                                    *TSNode:child_count()*
    Get the node's number of children.

    Return: ~
        (`integer`)

                                              *TSNode:child_with_descendant()*
TSNode:child_with_descendant({descendant})
    Get the node's child that contains {descendant} (includes {descendant}).

    For example, with the following node hierarchy: >
        a -> b -> c

        a:child_with_descendant(c) == b
        a:child_with_descendant(b) == b
        a:child_with_descendant(a) == nil
<

    Parameters: ~
      • {descendant}  (`TSNode`)

    Return: ~
        (`TSNode?`)

                                               *TSNode:descendant_for_range()*
TSNode:descendant_for_range({start_row}, {start_col}, {end_row}, {end_col})
    Get the smallest node within this node that spans the given range of (row,
    column) positions

    Parameters: ~
      • {start_row}  (`integer`)
      • {start_col}  (`integer`)
      • {end_row}    (`integer`)
      • {end_col}    (`integer`)

    Return: ~
        (`TSNode?`)

TSNode:end_()                                                  *TSNode:end_()*
    Get the node's end position. Return three values: the row, column and
    total byte count (all zero-based).

    Return (multiple): ~
        (`integer`)
        (`integer`)
        (`integer`)

TSNode:equal({node})                                          *TSNode:equal()*
    Check if {node} refers to the same node within the same tree.

    Parameters: ~
      • {node}  (`TSNode`)

    Return: ~
        (`boolean`)

TSNode:extra()                                                *TSNode:extra()*
    Check if the node is extra. Extra nodes represent things like comments,
    which are not required by the grammar but can appear anywhere.

    Return: ~
        (`boolean`)

TSNode:field({name})                                          *TSNode:field()*
    Returns a list of all the node's children that have the given field name.

    Parameters: ~
      • {name}  (`string`)

Title: Treesitter API: TSTree and TSNode Methods
Summary
This section details the methods available for `TSTree` objects, including `copy()` (returning a copy of the tree) and `root()` (returning the root node). It then introduces `TSNode`, representing an element of the parsed buffer. It lists `TSNode` methods such as `byte_length()` (returning the byte length), `child({index})` (getting a child node by index), `child_count()` (getting the number of children), `child_with_descendant({descendant})` (getting a child that contains a descendant), `descendant_for_range({start_row}, {start_col}, {end_row}, {end_col})` (getting the smallest node spanning a range), `end_()` (returning the end position), `equal({node})` (checking if nodes are equal), `extra()` (checking if the node is extra), and `field({name})` (returning children with a field name).