Home Explore Blog CI



neovim

22th chunk of `runtime/doc/treesitter.txt`
0a400f4a2b3d6d14861a4ae79900599839590c9d5ac681f00000000100000fac
 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()
<

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

TSQuery:disable_pattern({pattern_index})           *TSQuery:disable_pattern()*
    Disable a specific pattern in this query; once disabled the pattern cannot
    be re-enabled. The {pattern_index} for a particular match can be obtained
    with |:Inspect!|, or by reading the source of the query (i.e. from
    |vim.treesitter.query.get_files()|).

    Example: To disable `|` links in vimdoc but keep other `@markup.link`s
    highlighted: >lua
        local link_pattern = 9 -- from :Inspect!
        local query = vim.treesitter.query.get('vimdoc', 'highlights')
        query.query:disable_pattern(link_pattern)
        local tree = vim.treesitter.get_parser():parse()[1]
<

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


==============================================================================
Lua module: vim.treesitter.languagetree              *treesitter-languagetree*

A *LanguageTree* contains a tree of parsers: the root treesitter parser for
{lang} and any "injected" language parsers, which themselves may inject other
languages, recursively. For example a Lua buffer containing some Vimscript
commands needs multiple parsers to fully understand its contents.

To create a LanguageTree (parser object) for a given buffer and language, use: >lua
    local parser = vim.treesitter.get_parser(bufnr, lang)
<

(where `bufnr=0` means current buffer). `lang` defaults to 'filetype'. Note:
currently the parser is retained for the lifetime of a buffer but this may
change; a plugin should keep a reference to the parser object if it wants
incremental updates.

Whenever you need to access the current syntax tree, parse the buffer: >lua
    local tree = parser:parse({ start_row, end_row })
<

This returns a table of immutable |treesitter-tree| objects representing the
current state of the buffer. When the plugin wants to access the state after a
(possible) edit it must call `parse()` again. If the buffer wasn't edited, the
same tree will be returned again without extra work. If the buffer was parsed
before, incremental parsing will be done of the changed parts.

Note: To use the parser directly inside a |nvim_buf_attach()| Lua callback,
you must call |vim.treesitter.get_parser()| before you register your callback.
But preferably parsing shouldn't be done directly in the change callback
anyway as they will be very frequent. Rather a plugin that does any kind of
analysis on a tree should use a timer to throttle too frequent updates.


LanguageTree:children()                              *LanguageTree:children()*
    Returns a map of language to child tree.

    Return: ~
        (`table<string,vim.treesitter.LanguageTree>`)

LanguageTree:contains({range})                       *LanguageTree:contains()*
    Determines whether {range} is contained in the |LanguageTree|.

    Parameters: ~
      • {range}  (`table`) A table with the following fields:
                 • {[1]} (`integer`) start row
  

Title: TSQuery:disable_capture(), TSQuery:disable_pattern(), and LanguageTree - Disabling query elements and managing language parsers
Summary
This section details the `TSQuery:disable_capture()` and `TSQuery:disable_pattern()` functions for disabling specific captures and patterns in a query, providing examples for their usage. It then introduces the `LanguageTree` module, explaining its role in managing language parsers for a buffer, including injected languages. It covers how to obtain a parser, parse the buffer, and access the syntax tree, along with notes on using the parser in `nvim_buf_attach()` callbacks. Finally, it describes the `LanguageTree:children()` and `LanguageTree:contains()` functions for navigating the tree of parsers.