Home Explore Blog CI



neovim

2nd chunk of `runtime/doc/treesitter.txt`
11c52a84f961bc7ec4e19f10eebf3522e238784e92b124c50000000100000fa2

`runtimepath`, where each file contains queries for a specific language and
purpose, e.g., `queries/lua/highlights.scm` for highlighting Lua files.
By default, the first query on `runtimepath` is used (which usually implies
that user config takes precedence over plugins, which take precedence over
queries bundled with Nvim). If a query should extend other queries instead
of replacing them, use |treesitter-query-modeline-extends|.

The Lua interface is described at |lua-treesitter-query|.


TREESITTER QUERY PREDICATES                            *treesitter-predicates*

Predicates are special scheme nodes that are evaluated to conditionally capture
nodes. For example, the `eq?` predicate can be used as follows: >query

    ((identifier) @variable.builtin
      (#eq? @variable.builtin "self"))
<
to only match identifier corresponding to the `"self"` text. Such queries can
be used to highlight built-in functions or variables differently, for instance.

The following predicates are built in:

    `eq?`                                            *treesitter-predicate-eq?*
        Match a string against the text corresponding to a node: >query
            ((identifier) @variable.builtin (#eq? @variable.builtin "self"))
            ((node1) @left (node2) @right (#eq? @left @right))
<
    `any-eq?`                                    *treesitter-predicate-any-eq?*
        Like `eq?`, but for quantified patterns only one captured node must
        match.

    `match?`                                      *treesitter-predicate-match?*
    `vim-match?`                              *treesitter-predicate-vim-match?*
         Match a |regexp| against the text corresponding to a node: >query
            ((identifier) @constant (#match? @constant "^[A-Z_]+$"))
<
         Note: The `^` and `$` anchors will match the start and end of the
               node's text.

    `any-match?`                              *treesitter-predicate-any-match?*
    `any-vim-match?`                      *treesitter-predicate-any-vim-match?*
        Like `match?`, but for quantified patterns only one captured node must
        match.

    `lua-match?`                              *treesitter-predicate-lua-match?*
         Match |lua-pattern|s against the text corresponding to a node,
         similar to `match?`

    `any-lua-match?`                      *treesitter-predicate-any-lua-match?*
         Like `lua-match?`, but for quantified patterns only one captured node
         must match.

    `contains?`                                *treesitter-predicate-contains?*
        Match a string against parts of the text corresponding to a node: >query
            ((identifier) @foo (#contains? @foo "foo"))
            ((identifier) @foo-bar (#contains? @foo-bar "foo" "bar"))
<
    `any-contains?`                        *treesitter-predicate-any-contains?*
        Like `contains?`, but for quantified patterns only one captured node
        must match.

    `any-of?`                                    *treesitter-predicate-any-of?*
        Match any of the given strings against the text corresponding to
        a node: >query
            ((identifier) @foo (#any-of? @foo "foo" "bar"))
<
        This is the recommended way to check if the node matches one of many
        keywords, as it has been optimized for this.

    `has-ancestor?`                        *treesitter-predicate-has-ancestor?*
        Match any of the given node types against all ancestors of a node: >query
            ((identifier) @variable.builtin
              (#any-of? @variable.builtin "begin" "end")
              (#has-ancestor? @variable.builtin range_expression))
<
    `has-parent?`                            *treesitter-predicate-has-parent?*
        Match any of the given node types against the direct ancestor of a
        node: >query
            (((field_expression
                 (field_identifier) @method)) @_parent
             (#has-parent? @_parent template_method function_declarator))

Title: Treesitter Query Predicates in Detail
Summary
This section explains the built-in Treesitter query predicates, which are scheme nodes that conditionally capture nodes during query evaluation. It describes predicates like `eq?`, `any-eq?`, `match?`, `any-match?`, `lua-match?`, `any-lua-match?`, `contains?`, `any-contains?`, `any-of?`, `has-ancestor?`, and `has-parent?`, providing examples of how each predicate can be used to match specific text or node types within the syntax tree. The section highlights how these predicates enhance the precision of queries for tasks like syntax highlighting.