Home Explore Blog CI



neovim

21th chunk of `runtime/doc/treesitter.txt`
762a4c2cda3c4d8c5dec9dcff7f9fd71f353e87f32f8c03f0000000100000fc0
                                                *Query:iter_matches()*
Query:iter_matches({node}, {source}, {start}, {stop}, {opts})
    Iterates the matches of self on a given range.

    Iterate over all matches within a {node}. The arguments are the same as
    for |Query:iter_captures()| but the iterated values are different: an
    (1-based) index of the pattern in the query, a table mapping capture
    indices to a list of nodes, and metadata from any directives processing
    the match.

    Example: >lua
        for pattern, match, metadata in cquery:iter_matches(tree:root(), bufnr, 0, -1) do
          for id, nodes in pairs(match) do
            local name = query.captures[id]
            for _, node in ipairs(nodes) do
              -- `node` was captured by the `name` capture in the match

              local node_data = metadata[id] -- Node level metadata
              -- ... use the info here ...
            end
          end
        end
<

    Parameters: ~
      • {node}    (`TSNode`) under which the search will occur
      • {source}  (`integer|string`) Source buffer or string to search
      • {start}   (`integer?`) Starting line for the search. Defaults to
                  `node:start()`.
      • {stop}    (`integer?`) Stopping line for the search (end-exclusive).
                  Defaults to `node:end_()`.
      • {opts}    (`table?`) Optional keyword arguments:
                  • max_start_depth (integer) if non-zero, sets the maximum
                    start depth for each match. This is used to prevent
                    traversing too deep into a tree.
                  • match_limit (integer) Set the maximum number of
                    in-progress matches (Default: 256). all (boolean) When
                    `false` (default `true`), the returned table maps capture
                    IDs to a single (last) node instead of the full list of
                    matching nodes. This option is only for backward
                    compatibility and will be removed in a future release.

    Return: ~
        (`fun(): integer, table<integer, TSNode[]>, vim.treesitter.query.TSMetadata, TSTree`)
        pattern id, match, metadata, tree

set({lang}, {query_name}, {text})                 *vim.treesitter.query.set()*
    Sets the runtime query named {query_name} for {lang}

    This allows users to override or extend any runtime files and/or
    configuration set by plugins.

    For example, you could enable spellchecking of `C` identifiers with the
    following code: >lua
        vim.treesitter.query.set(
          'c',
          'highlights',
          [[;inherits c
          (identifier) @spell]])
        ]])
<

    Parameters: ~
      • {lang}        (`string`) Language to use for the query
      • {query_name}  (`string`) Name of the query (e.g., "highlights")
      • {text}        (`string`) Query text (unparsed).




*TSQuery*
    Extends: |userdata|

    Reference to an object held by the treesitter library that is used as a
    component of the |vim.treesitter.Query| for language feature support. See
    |treesitter-query| for more about queries or
    |vim.treesitter.query.parse()| for an example of how to obtain a query
    object.

    Fields: ~
      • {disable_capture}  (`fun(self: TSQuery, capture_name: string)`) See
                           |TSQuery:disable_capture()|.
      • {disable_pattern}  (`fun(self: TSQuery, pattern_index: integer)`) See
                           |TSQuery:disable_pattern()|.


TSQuery:disable_capture({capture_name})            *TSQuery:disable_capture()*
    Disable a specific capture in this query; once disabled the capture cannot
    be re-enabled. {capture_name} should not include a leading "@".

    Example: To disable the `@variable.parameter` capture from the vimdoc
    highlights query: >lua
        local query = vim.treesitter.query.get('vimdoc', 'highlights')
        query.query:disable_capture("variable.parameter")
        vim.treesitter.get_parser():parse()

Title: Query:iter_matches(), vim.treesitter.query.set(), and TSQuery - Iterating matches, setting queries, and disabling captures
Summary
This section describes the `Query:iter_matches()` function, which iterates over matches within a node in a syntax tree, detailing its parameters, return values, and usage example. It also covers `vim.treesitter.query.set()`, allowing users to override runtime queries, and introduces the `TSQuery` object, including its fields and the `TSQuery:disable_capture()` function for disabling specific captures in a query.