2020-09-14 10:04:33 -07:00
|
|
|
|
*treesitter.txt* Nvim
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NVIM REFERENCE MANUAL
|
|
|
|
|
|
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Treesitter integration *treesitter*
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Nvim integrates the `tree-sitter` library for incremental parsing of buffers:
|
|
|
|
|
https://tree-sitter.github.io/tree-sitter/
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
WARNING: Treesitter support is still experimental and subject to frequent
|
|
|
|
|
changes. This documentation may also not fully reflect the latest changes.
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Type |gO| to see the table of contents.
|
2021-03-30 13:40:29 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
==============================================================================
|
|
|
|
|
PARSER FILES *treesitter-parsers*
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2024-01-28 18:53:14 -07:00
|
|
|
|
Parsers are the heart of treesitter. They are libraries that treesitter will
|
2024-04-30 04:30:21 -07:00
|
|
|
|
search for in the `parser` runtime directory.
|
|
|
|
|
|
|
|
|
|
Nvim includes these parsers:
|
|
|
|
|
|
2024-06-14 16:04:27 -07:00
|
|
|
|
- Bash
|
2024-04-30 04:30:21 -07:00
|
|
|
|
- C
|
|
|
|
|
- Lua
|
|
|
|
|
- Markdown
|
2024-06-14 16:04:27 -07:00
|
|
|
|
- Python
|
2024-04-30 04:30:21 -07:00
|
|
|
|
- Vimscript
|
|
|
|
|
- Vimdoc
|
|
|
|
|
- Treesitter query files |ft-query-plugin|
|
|
|
|
|
|
|
|
|
|
You can install more parsers manually, or with a plugin like
|
|
|
|
|
https://github.com/nvim-treesitter/nvim-treesitter .
|
2024-01-06 05:18:13 -07:00
|
|
|
|
|
|
|
|
|
Parsers are searched for as `parser/{lang}.*` in any 'runtimepath' directory.
|
|
|
|
|
If multiple parsers for the same language are found, the first one is used.
|
|
|
|
|
(NOTE: This typically implies the priority "user config > plugins > bundled".)
|
2024-04-30 04:30:21 -07:00
|
|
|
|
|
|
|
|
|
To load a parser from its filepath: >lua
|
2021-06-16 10:06:29 -07:00
|
|
|
|
|
2023-03-24 07:43:14 -07:00
|
|
|
|
vim.treesitter.language.add('python', { path = "/path/to/python.so" })
|
2022-09-14 02:08:31 -07:00
|
|
|
|
<
|
2024-04-28 07:27:47 -07:00
|
|
|
|
Parser names are assumed to be lower case if the file system is
|
|
|
|
|
case-sensitive.
|
|
|
|
|
|
2024-01-06 05:18:13 -07:00
|
|
|
|
To associate certain |filetypes| with a treesitter language (name of parser),
|
|
|
|
|
use |vim.treesitter.language.register()|. For example, to use the `xml`
|
|
|
|
|
treesitter parser for buffers with filetype `svg` or `xslt`, use: >lua
|
|
|
|
|
|
|
|
|
|
vim.treesitter.language.register('xml', { 'svg', 'xslt' })
|
|
|
|
|
<
|
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
==============================================================================
|
|
|
|
|
TREESITTER TREES *treesitter-tree*
|
2023-02-04 07:58:38 -07:00
|
|
|
|
*TSTree*
|
2022-09-14 02:08:31 -07:00
|
|
|
|
|
|
|
|
|
A "treesitter tree" represents the parsed contents of a buffer, which can be
|
2023-08-03 08:35:10 -07:00
|
|
|
|
used to perform further analysis. It is a |userdata| reference to an object
|
2024-01-28 18:53:14 -07:00
|
|
|
|
held by the treesitter library.
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
An instance `TSTree` of a treesitter tree supports the following methods.
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSTree:root() *TSTree:root()*
|
2020-09-14 10:04:33 -07:00
|
|
|
|
Return the root node of this tree.
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSTree:copy() *TSTree:copy()*
|
|
|
|
|
Returns a copy of the `TSTree`.
|
2020-11-03 10:43:41 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
==============================================================================
|
|
|
|
|
TREESITTER NODES *treesitter-node*
|
2023-02-04 07:58:38 -07:00
|
|
|
|
*TSNode*
|
2022-09-14 02:08:31 -07:00
|
|
|
|
|
|
|
|
|
A "treesitter node" represents one specific element of the parsed contents of
|
2023-08-03 08:35:10 -07:00
|
|
|
|
a buffer, which can be captured by a |Query| for, e.g., highlighting. It is
|
2024-01-28 18:53:14 -07:00
|
|
|
|
a |userdata| reference to an object held by the treesitter library.
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
An instance `TSNode` of a treesitter node supports the following methods.
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSNode:parent() *TSNode:parent()*
|
2020-09-14 10:04:33 -07:00
|
|
|
|
Get the node's immediate parent.
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSNode:next_sibling() *TSNode:next_sibling()*
|
2021-08-19 22:35:25 -07:00
|
|
|
|
Get the node's next sibling.
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSNode:prev_sibling() *TSNode:prev_sibling()*
|
2021-08-19 22:35:25 -07:00
|
|
|
|
Get the node's previous sibling.
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSNode:next_named_sibling() *TSNode:next_named_sibling()*
|
2021-08-19 22:35:25 -07:00
|
|
|
|
Get the node's next named sibling.
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSNode:prev_named_sibling() *TSNode:prev_named_sibling()*
|
2021-08-19 22:35:25 -07:00
|
|
|
|
Get the node's previous named sibling.
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSNode:iter_children() *TSNode:iter_children()*
|
|
|
|
|
Iterates over all the direct children of {TSNode}, regardless of whether
|
2020-09-14 10:04:33 -07:00
|
|
|
|
they are named or not.
|
|
|
|
|
Returns the child node plus the eventual field name corresponding to this
|
|
|
|
|
child node.
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSNode:field({name}) *TSNode:field()*
|
2020-09-14 10:04:33 -07:00
|
|
|
|
Returns a table of the nodes corresponding to the {name} field.
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSNode:child_count() *TSNode:child_count()*
|
2020-09-14 10:04:33 -07:00
|
|
|
|
Get the node's number of children.
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSNode:child({index}) *TSNode:child()*
|
2020-09-14 10:04:33 -07:00
|
|
|
|
Get the node's child at the given {index}, where zero represents the first
|
|
|
|
|
child.
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSNode:named_child_count() *TSNode:named_child_count()*
|
2020-09-14 10:04:33 -07:00
|
|
|
|
Get the node's number of named children.
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSNode:named_child({index}) *TSNode:named_child()*
|
2020-09-14 10:04:33 -07:00
|
|
|
|
Get the node's named child at the given {index}, where zero represents the
|
|
|
|
|
first named child.
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSNode:start() *TSNode:start()*
|
2020-09-14 10:04:33 -07:00
|
|
|
|
Get the node's start position. Return three values: the row, column and
|
|
|
|
|
total byte count (all zero-based).
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSNode:end_() *TSNode:end_()*
|
2020-09-14 10:04:33 -07:00
|
|
|
|
Get the node's end position. Return three values: the row, column and
|
|
|
|
|
total byte count (all zero-based).
|
|
|
|
|
|
2023-02-26 09:53:33 -07:00
|
|
|
|
TSNode:range({include_bytes}) *TSNode:range()*
|
|
|
|
|
Get the range of the node.
|
|
|
|
|
|
|
|
|
|
Return four or six values:
|
|
|
|
|
- start row
|
|
|
|
|
- start column
|
|
|
|
|
- start byte (if {include_bytes} is `true`)
|
|
|
|
|
- end row
|
|
|
|
|
- end column
|
|
|
|
|
- end byte (if {include_bytes} is `true`)
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSNode:type() *TSNode:type()*
|
2020-09-14 10:04:33 -07:00
|
|
|
|
Get the node's type as a string.
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSNode:symbol() *TSNode:symbol()*
|
2020-09-14 10:04:33 -07:00
|
|
|
|
Get the node's type as a numerical id.
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSNode:named() *TSNode:named()*
|
2020-09-14 10:04:33 -07:00
|
|
|
|
Check if the node is named. Named nodes correspond to named rules in the
|
|
|
|
|
grammar, whereas anonymous nodes correspond to string literals in the
|
|
|
|
|
grammar.
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSNode:missing() *TSNode:missing()*
|
2020-09-14 10:04:33 -07:00
|
|
|
|
Check if the node is missing. Missing nodes are inserted by the parser in
|
|
|
|
|
order to recover from certain kinds of syntax errors.
|
|
|
|
|
|
2023-02-26 09:53:33 -07:00
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
TSNode:has_changes() *TSNode:has_changes()*
|
|
|
|
|
Check if a syntax node has been edited.
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSNode:has_error() *TSNode:has_error()*
|
2020-09-14 10:04:33 -07:00
|
|
|
|
Check if the node is a syntax error or contains any syntax errors.
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSNode:sexpr() *TSNode:sexpr()*
|
2020-09-14 10:04:33 -07:00
|
|
|
|
Get an S-expression representing the node as a string.
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
TSNode:id() *TSNode:id()*
|
2024-03-27 18:32:32 -07:00
|
|
|
|
Get a unique identifier for the node inside its own tree.
|
2020-10-30 02:51:41 -07:00
|
|
|
|
|
2022-02-13 06:43:25 -07:00
|
|
|
|
No guarantees are made about this identifier's internal representation,
|
2022-09-14 02:08:31 -07:00
|
|
|
|
except for being a primitive Lua type with value equality (so not a
|
2022-02-13 06:43:25 -07:00
|
|
|
|
table). Presently it is a (non-printable) string.
|
2020-10-30 02:51:41 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Note: The `id` is not guaranteed to be unique for nodes from different
|
2021-06-16 10:06:29 -07:00
|
|
|
|
trees.
|
2020-10-30 02:51:41 -07:00
|
|
|
|
|
2023-02-26 09:53:33 -07:00
|
|
|
|
TSNode:tree() *TSNode:tree()*
|
|
|
|
|
Get the |TSTree| of the node.
|
2023-02-04 07:58:38 -07:00
|
|
|
|
*TSNode:descendant_for_range()*
|
|
|
|
|
TSNode:descendant_for_range({start_row}, {start_col}, {end_row}, {end_col})
|
2020-09-14 10:04:33 -07:00
|
|
|
|
Get the smallest node within this node that spans the given range of (row,
|
|
|
|
|
column) positions
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
*TSNode:named_descendant_for_range()*
|
|
|
|
|
TSNode:named_descendant_for_range({start_row}, {start_col}, {end_row}, {end_col})
|
2020-09-14 10:04:33 -07:00
|
|
|
|
Get the smallest named node within this node that spans the given range of
|
|
|
|
|
(row, column) positions
|
2023-02-26 09:53:33 -07:00
|
|
|
|
*TSNode:equal()*
|
|
|
|
|
TSNode:equal({node})
|
|
|
|
|
Check if {node} refers to the same node within the same tree.
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2023-11-29 07:59:36 -07:00
|
|
|
|
*TSNode:byte_length()*
|
|
|
|
|
TSNode:byte_length()
|
|
|
|
|
Return the number of bytes spanned by this node.
|
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
==============================================================================
|
|
|
|
|
TREESITTER QUERIES *treesitter-query*
|
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
|
Treesitter queries are a way to extract information about a parsed |TSTree|,
|
2022-09-14 02:08:31 -07:00
|
|
|
|
e.g., for the purpose of highlighting. Briefly, a `query` consists of one or
|
|
|
|
|
more patterns. A `pattern` is defined over node types in the syntax tree. A
|
|
|
|
|
`match` corresponds to specific elements of the syntax tree which match a
|
|
|
|
|
pattern. Patterns may optionally define captures and predicates. A `capture`
|
|
|
|
|
allows you to associate names with a specific node in a pattern. A `predicate`
|
|
|
|
|
adds arbitrary metadata and conditional data to a match.
|
|
|
|
|
|
|
|
|
|
Queries are written in a lisp-like language documented in
|
|
|
|
|
https://tree-sitter.github.io/tree-sitter/using-parsers#query-syntax
|
|
|
|
|
Note: The predicates listed there page differ from those Nvim supports. See
|
|
|
|
|
|treesitter-predicates| for a complete list of predicates supported by Nvim.
|
|
|
|
|
|
|
|
|
|
Nvim looks for queries as `*.scm` files in a `queries` directory under
|
|
|
|
|
`runtimepath`, where each file contains queries for a specific language and
|
|
|
|
|
purpose, e.g., `queries/lua/highlights.scm` for highlighting Lua files.
|
2022-09-08 02:17:29 -07:00
|
|
|
|
By default, the first query on `runtimepath` is used (which usually implies
|
|
|
|
|
that user config takes precedence over plugins, which take precedence over
|
2023-03-05 16:15:29 -07:00
|
|
|
|
queries bundled with Nvim). If a query should extend other queries instead
|
2022-09-16 00:05:05 -07:00
|
|
|
|
of replacing them, use |treesitter-query-modeline-extends|.
|
2022-09-08 02:17:29 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
See |lua-treesitter-query| for the list of available methods for working with
|
|
|
|
|
treesitter queries from Lua.
|
2020-11-04 10:13:00 -07:00
|
|
|
|
|
2022-09-08 00:47:36 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
TREESITTER QUERY PREDICATES *treesitter-predicates*
|
2022-09-08 00:47:36 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Predicates are special scheme nodes that are evaluated to conditionally capture
|
2023-04-01 03:29:38 -07:00
|
|
|
|
nodes. For example, the `eq?` predicate can be used as follows: >query
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2024-01-24 00:33:03 -07:00
|
|
|
|
((identifier) @variable.builtin
|
|
|
|
|
(#eq? @variable.builtin "self"))
|
2022-09-14 02:08:31 -07:00
|
|
|
|
<
|
2024-01-24 00:33:03 -07:00
|
|
|
|
to only match identifier corresponding to the `"self"` text. Such queries can
|
|
|
|
|
be used to highlight built-in functions or variables differently, for instance.
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
The following predicates are built in:
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2022-09-16 00:05:05 -07:00
|
|
|
|
`eq?` *treesitter-predicate-eq?*
|
2023-04-01 03:29:38 -07:00
|
|
|
|
Match a string against the text corresponding to a node: >query
|
2024-01-24 00:33:03 -07:00
|
|
|
|
((identifier) @variable.builtin (#eq? @variable.builtin "self"))
|
2020-09-14 10:04:33 -07:00
|
|
|
|
((node1) @left (node2) @right (#eq? @left @right))
|
|
|
|
|
<
|
2024-02-16 10:54:47 -07:00
|
|
|
|
`any-eq?` *treesitter-predicate-any-eq?*
|
|
|
|
|
Like `eq?`, but for quantified patterns only one captured node must
|
|
|
|
|
match.
|
|
|
|
|
|
2022-09-16 00:05:05 -07:00
|
|
|
|
`match?` *treesitter-predicate-match?*
|
|
|
|
|
`vim-match?` *treesitter-predicate-vim-match?*
|
2023-04-01 03:29:38 -07:00
|
|
|
|
Match a |regexp| against the text corresponding to a node: >query
|
2020-09-14 10:04:33 -07:00
|
|
|
|
((identifier) @constant (#match? @constant "^[A-Z_]+$"))
|
2024-01-24 00:33:03 -07:00
|
|
|
|
<
|
|
|
|
|
Note: The `^` and `$` anchors will match the start and end of the
|
2022-09-14 02:08:31 -07:00
|
|
|
|
node's text.
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
2024-02-16 10:54:47 -07:00
|
|
|
|
`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.
|
|
|
|
|
|
2022-09-16 00:05:05 -07:00
|
|
|
|
`lua-match?` *treesitter-predicate-lua-match?*
|
2022-09-25 16:58:27 -07:00
|
|
|
|
Match |lua-patterns| against the text corresponding to a node,
|
2022-09-14 02:08:31 -07:00
|
|
|
|
similar to `match?`
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
2024-02-16 10:54:47 -07:00
|
|
|
|
`any-lua-match?` *treesitter-predicate-any-lua-match?*
|
|
|
|
|
Like `lua-match?`, but for quantified patterns only one captured node
|
|
|
|
|
must match.
|
|
|
|
|
|
2022-09-16 00:05:05 -07:00
|
|
|
|
`contains?` *treesitter-predicate-contains?*
|
2023-05-13 03:56:21 -07:00
|
|
|
|
Match a string against parts of the text corresponding to a node: >query
|
2020-09-14 10:04:33 -07:00
|
|
|
|
((identifier) @foo (#contains? @foo "foo"))
|
2022-11-10 01:12:28 -07:00
|
|
|
|
((identifier) @foo-bar (#contains? @foo-bar "foo" "bar"))
|
2021-05-14 08:41:20 -07:00
|
|
|
|
<
|
2024-02-16 10:54:47 -07:00
|
|
|
|
`any-contains?` *treesitter-predicate-any-contains?*
|
|
|
|
|
Like `contains?`, but for quantified patterns only one captured node
|
|
|
|
|
must match.
|
|
|
|
|
|
2022-09-16 00:05:05 -07:00
|
|
|
|
`any-of?` *treesitter-predicate-any-of?*
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Match any of the given strings against the text corresponding to
|
2023-04-01 03:29:38 -07:00
|
|
|
|
a node: >query
|
2022-03-09 23:34:55 -07:00
|
|
|
|
((identifier) @foo (#any-of? @foo "foo" "bar"))
|
|
|
|
|
<
|
2021-06-16 10:06:29 -07:00
|
|
|
|
This is the recommended way to check if the node matches one of many
|
2022-09-14 02:08:31 -07:00
|
|
|
|
keywords, as it has been optimized for this.
|
|
|
|
|
|
2023-05-13 03:56:21 -07:00
|
|
|
|
`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))
|
|
|
|
|
<
|
2022-02-13 06:43:25 -07:00
|
|
|
|
*lua-treesitter-not-predicate*
|
2020-09-14 10:04:33 -07:00
|
|
|
|
Each predicate has a `not-` prefixed predicate that is just the negation of
|
|
|
|
|
the predicate.
|
|
|
|
|
|
2024-02-16 10:54:47 -07:00
|
|
|
|
*lua-treesitter-all-predicate*
|
|
|
|
|
*lua-treesitter-any-predicate*
|
|
|
|
|
Queries can use quantifiers to capture multiple nodes. When a capture contains
|
|
|
|
|
multiple nodes, predicates match only if ALL nodes contained by the capture
|
|
|
|
|
match the predicate. Some predicates (`eq?`, `match?`, `lua-match?`,
|
|
|
|
|
`contains?`) accept an `any-` prefix to instead match if ANY of the nodes
|
|
|
|
|
contained by the capture match the predicate.
|
|
|
|
|
|
|
|
|
|
As an example, consider the following Lua code: >lua
|
|
|
|
|
|
|
|
|
|
-- TODO: This is a
|
|
|
|
|
-- very long
|
|
|
|
|
-- comment (just imagine it)
|
|
|
|
|
<
|
|
|
|
|
using the following predicated query:
|
|
|
|
|
>query
|
|
|
|
|
(((comment)+ @comment)
|
|
|
|
|
(#match? @comment "TODO"))
|
|
|
|
|
<
|
|
|
|
|
This query will not match because not all of the nodes captured by @comment
|
|
|
|
|
match the predicate. Instead, use:
|
|
|
|
|
>query
|
|
|
|
|
(((comment)+ @comment)
|
|
|
|
|
(#any-match? @comment "TODO"))
|
|
|
|
|
<
|
|
|
|
|
|
2023-03-24 07:43:14 -07:00
|
|
|
|
Further predicates can be added via |vim.treesitter.query.add_predicate()|.
|
|
|
|
|
Use |vim.treesitter.query.list_predicates()| to list all available predicates.
|
2021-04-11 12:05:22 -07:00
|
|
|
|
|
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
TREESITTER QUERY DIRECTIVES *treesitter-directives*
|
2021-04-11 12:05:22 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Treesitter directives store metadata for a node or match and perform side
|
2023-04-01 03:29:38 -07:00
|
|
|
|
effects. For example, the `set!` directive sets metadata on the match or node: >query
|
2020-11-24 07:50:33 -07:00
|
|
|
|
|
2024-07-28 03:14:23 -07:00
|
|
|
|
((identifier) @foo (#set! type "parameter"))
|
2022-09-14 02:08:31 -07:00
|
|
|
|
<
|
|
|
|
|
The following directives are built in:
|
2020-11-24 07:50:33 -07:00
|
|
|
|
|
2022-09-16 00:05:05 -07:00
|
|
|
|
`set!` *treesitter-directive-set!*
|
2022-05-28 10:22:18 -07:00
|
|
|
|
Sets key/value metadata for a specific match or capture. Value is
|
|
|
|
|
accessible as either `metadata[key]` (match specific) or
|
|
|
|
|
`metadata[capture_id][key]` (capture specific).
|
2021-04-11 11:53:52 -07:00
|
|
|
|
|
2022-05-28 10:22:18 -07:00
|
|
|
|
Parameters: ~
|
|
|
|
|
{capture_id} (optional)
|
|
|
|
|
{key}
|
|
|
|
|
{value}
|
|
|
|
|
|
2023-04-01 03:29:38 -07:00
|
|
|
|
Examples: >query
|
2024-07-28 03:14:23 -07:00
|
|
|
|
((identifier) @foo (#set! @foo kind "parameter"))
|
|
|
|
|
((node1) @left (node2) @right (#set! type "pair"))
|
|
|
|
|
((codeblock) @markup.raw.block (#set! priority 90))
|
2022-05-28 10:22:18 -07:00
|
|
|
|
<
|
2022-09-16 00:05:05 -07:00
|
|
|
|
`offset!` *treesitter-directive-offset!*
|
2022-05-28 10:22:18 -07:00
|
|
|
|
Takes the range of the captured node and applies an offset. This will
|
2024-01-24 00:33:03 -07:00
|
|
|
|
set a new `Range4` object for the captured node with `capture_id` as
|
|
|
|
|
`metadata[capture_id].range`. Useful for |treesitter-language-injections|.
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
2022-05-28 10:22:18 -07:00
|
|
|
|
Parameters: ~
|
|
|
|
|
{capture_id}
|
|
|
|
|
{start_row}
|
|
|
|
|
{start_col}
|
|
|
|
|
{end_row}
|
|
|
|
|
{end_col}
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
2023-04-01 03:29:38 -07:00
|
|
|
|
Example: >query
|
2022-05-28 10:22:18 -07:00
|
|
|
|
((identifier) @constant (#offset! @constant 0 1 0 -1))
|
|
|
|
|
<
|
2023-07-01 02:08:06 -07:00
|
|
|
|
`gsub!` *treesitter-directive-gsub!*
|
2024-01-24 00:33:03 -07:00
|
|
|
|
Transforms the content of the node using a |lua-pattern|. This will set
|
2023-07-01 02:08:06 -07:00
|
|
|
|
a new `metadata[capture_id].text`.
|
2021-04-11 11:53:52 -07:00
|
|
|
|
|
2023-07-01 02:08:06 -07:00
|
|
|
|
Parameters: ~
|
|
|
|
|
{capture_id}
|
|
|
|
|
{pattern}
|
2024-01-24 00:33:03 -07:00
|
|
|
|
{replacement}
|
2023-07-01 02:08:06 -07:00
|
|
|
|
|
|
|
|
|
Example: >query
|
|
|
|
|
(#gsub! @_node ".*%.(.*)" "%1")
|
|
|
|
|
<
|
|
|
|
|
`trim!` *treesitter-directive-trim!*
|
|
|
|
|
Trim blank lines from the end of the node. This will set a new
|
|
|
|
|
`metadata[capture_id].range`.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{capture_id}
|
|
|
|
|
|
|
|
|
|
Example: >query
|
2023-08-11 08:05:17 -07:00
|
|
|
|
(#trim! @fold)
|
2023-07-01 02:08:06 -07:00
|
|
|
|
<
|
2023-03-24 07:43:14 -07:00
|
|
|
|
Further directives can be added via |vim.treesitter.query.add_directive()|.
|
|
|
|
|
Use |vim.treesitter.query.list_directives()| to list all available directives.
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
|
|
|
|
|
2022-09-16 00:05:05 -07:00
|
|
|
|
TREESITTER QUERY MODELINES *treesitter-query-modeline*
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2023-03-05 16:15:29 -07:00
|
|
|
|
Nvim supports to customize the behavior of the queries using a set of
|
2022-09-14 02:08:31 -07:00
|
|
|
|
"modelines", that is comments in the queries starting with `;`. Here are the
|
|
|
|
|
currently supported modeline alternatives:
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2022-09-16 00:05:05 -07:00
|
|
|
|
`inherits: {lang}...` *treesitter-query-modeline-inherits*
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Specifies that this query should inherit the queries from {lang}.
|
|
|
|
|
This will recursively descend in the queries of {lang} unless wrapped
|
|
|
|
|
in parentheses: `({lang})`.
|
|
|
|
|
Note: This is meant to be used to include queries from another
|
|
|
|
|
language. If you want your query to extend the queries of the same
|
|
|
|
|
language, use `extends`.
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2022-09-16 00:05:05 -07:00
|
|
|
|
`extends` *treesitter-query-modeline-extends*
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Specifies that this query should be used as an extension for the
|
|
|
|
|
query, i.e. that it should be merged with the others.
|
|
|
|
|
Note: The order of the extensions, and the query that will be used as
|
|
|
|
|
a base depends on your 'runtimepath' value.
|
|
|
|
|
|
|
|
|
|
Note: These modeline comments must be at the top of the query, but can be
|
2023-04-01 03:29:38 -07:00
|
|
|
|
repeated, for example, the following two modeline blocks are both valid:
|
|
|
|
|
>query
|
2024-01-24 00:33:03 -07:00
|
|
|
|
;; inherits: typescript,jsx
|
2022-09-14 02:08:31 -07:00
|
|
|
|
;; extends
|
2023-05-13 12:33:22 -07:00
|
|
|
|
<
|
|
|
|
|
>query
|
2022-09-14 02:08:31 -07:00
|
|
|
|
;; extends
|
|
|
|
|
;;
|
2024-01-24 00:33:03 -07:00
|
|
|
|
;; inherits: css
|
2022-09-14 02:08:31 -07:00
|
|
|
|
<
|
|
|
|
|
==============================================================================
|
|
|
|
|
TREESITTER SYNTAX HIGHLIGHTING *treesitter-highlight*
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Syntax highlighting is specified through queries named `highlights.scm`,
|
2023-02-04 07:58:38 -07:00
|
|
|
|
which match a |TSNode| in the parsed |TSTree| to a `capture` that can be
|
2023-04-01 03:29:38 -07:00
|
|
|
|
assigned a highlight group. For example, the query >query
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2024-01-24 00:33:03 -07:00
|
|
|
|
(parameters (identifier) @variable.parameter)
|
|
|
|
|
<
|
|
|
|
|
matches any `identifier` node inside a function `parameters` node to the
|
|
|
|
|
capture named `@variable.parameter`. For example, for a Lua code >lua
|
|
|
|
|
|
|
|
|
|
function f(foo, bar) end
|
2022-09-14 02:08:31 -07:00
|
|
|
|
<
|
2024-01-24 00:33:03 -07:00
|
|
|
|
which will be parsed as (see |:InspectTree|): >query
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2024-01-24 00:33:03 -07:00
|
|
|
|
(function_declaration ; [1:1 - 24]
|
|
|
|
|
name: (identifier) ; [1:10 - 10]
|
|
|
|
|
parameters: (parameters ; [1:11 - 20]
|
|
|
|
|
name: (identifier) ; [1:12 - 14]
|
|
|
|
|
name: (identifier))) ; [1:17 - 19]
|
|
|
|
|
<
|
|
|
|
|
the above query will highlight `foo` and `bar` as `@variable.parameter`.
|
|
|
|
|
|
|
|
|
|
It is also possible to match literal expressions (provided the parser returns
|
|
|
|
|
them):
|
|
|
|
|
>query
|
|
|
|
|
[
|
|
|
|
|
"if"
|
|
|
|
|
"else"
|
|
|
|
|
] @keyword.conditional
|
2022-09-14 02:08:31 -07:00
|
|
|
|
<
|
|
|
|
|
Assuming a suitable parser and `highlights.scm` query is found in runtimepath,
|
|
|
|
|
treesitter highlighting for the current buffer can be enabled simply via
|
|
|
|
|
|vim.treesitter.start()|.
|
2022-08-24 14:48:52 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
*treesitter-highlight-groups*
|
2024-01-24 00:33:03 -07:00
|
|
|
|
The capture names, prefixed with `@`, are directly usable as highlight groups.
|
2022-09-14 02:08:31 -07:00
|
|
|
|
For many commonly used captures, the corresponding highlight groups are linked
|
2024-01-24 00:33:03 -07:00
|
|
|
|
to Nvim's standard |highlight-groups| by default (e.g., `@comment` links to
|
|
|
|
|
`Comment`) but can be overridden in colorschemes.
|
2022-09-14 02:08:31 -07:00
|
|
|
|
|
2022-08-24 14:48:52 -07:00
|
|
|
|
A fallback system is implemented, so that more specific groups fallback to
|
2024-01-24 00:33:03 -07:00
|
|
|
|
more generic ones. For instance, in a language that has separate doc comments
|
|
|
|
|
(e.g., c, java, etc.), `@comment.documentation` could be used. If this group
|
|
|
|
|
is not defined, the highlighting for an ordinary `@comment` is used. This way,
|
|
|
|
|
existing color schemes already work out of the box, but it is possible to add
|
|
|
|
|
more specific variants for queries that make them available.
|
2022-08-24 14:48:52 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
As an additional rule, capture highlights can always be specialized by
|
2022-08-24 14:48:52 -07:00
|
|
|
|
language, by appending the language name after an additional dot. For
|
2022-11-22 10:41:00 -07:00
|
|
|
|
instance, to highlight comments differently per language: >vim
|
2022-08-24 14:48:52 -07:00
|
|
|
|
|
|
|
|
|
hi @comment.c guifg=Blue
|
2022-11-22 10:41:00 -07:00
|
|
|
|
hi @comment.lua guifg=DarkBlue
|
2024-01-24 00:33:03 -07:00
|
|
|
|
hi link @comment.documentation.java String
|
2022-10-18 09:46:09 -07:00
|
|
|
|
<
|
2024-01-25 02:33:24 -07:00
|
|
|
|
The following is a list of standard captures used in queries for Nvim,
|
|
|
|
|
highlighted according to the current colorscheme (use |:Inspect| on one to see
|
|
|
|
|
the exact definition):
|
2024-01-17 08:55:52 -07:00
|
|
|
|
|
2024-03-03 03:18:34 -07:00
|
|
|
|
@variable various variable names
|
|
|
|
|
@variable.builtin built-in variable names (e.g. `this`, `self`)
|
|
|
|
|
@variable.parameter parameters of a function
|
|
|
|
|
@variable.parameter.builtin special parameters (e.g. `_`, `it`)
|
|
|
|
|
@variable.member object and struct fields
|
2024-01-17 08:55:52 -07:00
|
|
|
|
|
|
|
|
|
@constant constant identifiers
|
|
|
|
|
@constant.builtin built-in constant values
|
|
|
|
|
@constant.macro constants defined by the preprocessor
|
|
|
|
|
|
|
|
|
|
@module modules or namespaces
|
|
|
|
|
@module.builtin built-in modules or namespaces
|
2024-03-03 03:18:34 -07:00
|
|
|
|
@label `GOTO` and other labels (e.g. `label:` in C), including heredoc labels
|
2024-01-17 08:55:52 -07:00
|
|
|
|
|
|
|
|
|
@string string literals
|
|
|
|
|
@string.documentation string documenting code (e.g. Python docstrings)
|
|
|
|
|
@string.regexp regular expressions
|
|
|
|
|
@string.escape escape sequences
|
|
|
|
|
@string.special other special strings (e.g. dates)
|
|
|
|
|
@string.special.symbol symbols or atoms
|
|
|
|
|
@string.special.path filenames
|
|
|
|
|
@string.special.url URIs (e.g. hyperlinks)
|
|
|
|
|
|
|
|
|
|
@character character literals
|
|
|
|
|
@character.special special characters (e.g. wildcards)
|
|
|
|
|
|
|
|
|
|
@boolean boolean literals
|
|
|
|
|
@number numeric literals
|
|
|
|
|
@number.float floating-point number literals
|
|
|
|
|
|
|
|
|
|
@type type or class definitions and annotations
|
|
|
|
|
@type.builtin built-in types
|
|
|
|
|
@type.definition identifiers in type definitions (e.g. `typedef <type> <identifier>` in C)
|
|
|
|
|
|
2024-03-03 03:18:34 -07:00
|
|
|
|
@attribute attribute annotations (e.g. Python decorators, Rust lifetimes)
|
|
|
|
|
@attribute.builtin builtin annotations (e.g. `@property` in Python)
|
2024-01-17 08:55:52 -07:00
|
|
|
|
@property the key in key/value pairs
|
|
|
|
|
|
|
|
|
|
@function function definitions
|
|
|
|
|
@function.builtin built-in functions
|
|
|
|
|
@function.call function calls
|
|
|
|
|
@function.macro preprocessor macros
|
|
|
|
|
|
|
|
|
|
@function.method method definitions
|
|
|
|
|
@function.method.call method calls
|
|
|
|
|
|
|
|
|
|
@constructor constructor calls and definitions
|
2024-03-03 03:18:34 -07:00
|
|
|
|
@operator symbolic operators (e.g. `+`, `*`)
|
2024-01-17 08:55:52 -07:00
|
|
|
|
|
|
|
|
|
@keyword keywords not fitting into specific categories
|
|
|
|
|
@keyword.coroutine keywords related to coroutines (e.g. `go` in Go, `async/await` in Python)
|
|
|
|
|
@keyword.function keywords that define a function (e.g. `func` in Go, `def` in Python)
|
2024-03-03 03:18:34 -07:00
|
|
|
|
@keyword.operator operators that are English words (e.g. `and`, `or`)
|
|
|
|
|
@keyword.import keywords for including modules (e.g. `import`, `from` in Python)
|
|
|
|
|
@keyword.type keywords defining composite types (e.g. `struct`, `enum`)
|
2024-03-27 18:32:32 -07:00
|
|
|
|
@keyword.modifier keywords defining type modifiers (e.g. `const`, `static`, `public`)
|
2024-03-03 03:18:34 -07:00
|
|
|
|
@keyword.repeat keywords related to loops (e.g. `for`, `while`)
|
2024-01-17 08:55:52 -07:00
|
|
|
|
@keyword.return keywords like `return` and `yield`
|
|
|
|
|
@keyword.debug keywords related to debugging
|
2024-03-03 03:18:34 -07:00
|
|
|
|
@keyword.exception keywords related to exceptions (e.g. `throw`, `catch`)
|
2024-01-17 08:55:52 -07:00
|
|
|
|
|
2024-03-03 03:18:34 -07:00
|
|
|
|
@keyword.conditional keywords related to conditionals (e.g. `if`, `else`)
|
|
|
|
|
@keyword.conditional.ternary ternary operator (e.g. `?`, `:`)
|
2024-01-17 08:55:52 -07:00
|
|
|
|
|
|
|
|
|
@keyword.directive various preprocessor directives and shebangs
|
|
|
|
|
@keyword.directive.define preprocessor definition directives
|
|
|
|
|
|
2024-03-03 03:18:34 -07:00
|
|
|
|
@punctuation.delimiter delimiters (e.g. `;`, `.`, `,`)
|
|
|
|
|
@punctuation.bracket brackets (e.g. `()`, `{}`, `[]`)
|
2024-01-17 08:55:52 -07:00
|
|
|
|
@punctuation.special special symbols (e.g. `{}` in string interpolation)
|
|
|
|
|
|
|
|
|
|
@comment line and block comments
|
|
|
|
|
@comment.documentation comments documenting code
|
|
|
|
|
|
2024-01-24 00:33:03 -07:00
|
|
|
|
@comment.error error-type comments (e.g. `ERROR`, `FIXME`, `DEPRECATED`)
|
|
|
|
|
@comment.warning warning-type comments (e.g. `WARNING`, `FIX`, `HACK`)
|
2024-03-03 03:18:34 -07:00
|
|
|
|
@comment.todo todo-type comments (e.g. `TODO`, `WIP`)
|
2024-01-24 00:33:03 -07:00
|
|
|
|
@comment.note note-type comments (e.g. `NOTE`, `INFO`, `XXX`)
|
2024-01-17 08:55:52 -07:00
|
|
|
|
|
|
|
|
|
@markup.strong bold text
|
|
|
|
|
@markup.italic italic text
|
|
|
|
|
@markup.strikethrough struck-through text
|
|
|
|
|
@markup.underline underlined text (only for literal underline markup!)
|
|
|
|
|
|
|
|
|
|
@markup.heading headings, titles (including markers)
|
2024-03-03 03:18:34 -07:00
|
|
|
|
@markup.heading.1 top-level heading
|
|
|
|
|
@markup.heading.2 section heading
|
|
|
|
|
@markup.heading.3 subsection heading
|
|
|
|
|
@markup.heading.4 and so on
|
|
|
|
|
@markup.heading.5 and so forth
|
|
|
|
|
@markup.heading.6 six levels ought to be enough for anybody
|
2024-01-17 08:55:52 -07:00
|
|
|
|
|
|
|
|
|
@markup.quote block quotes
|
|
|
|
|
@markup.math math environments (e.g. `$ ... $` in LaTeX)
|
|
|
|
|
|
|
|
|
|
@markup.link text references, footnotes, citations, etc.
|
|
|
|
|
@markup.link.label link, reference descriptions
|
|
|
|
|
@markup.link.url URL-style links
|
|
|
|
|
|
|
|
|
|
@markup.raw literal or verbatim text (e.g. inline code)
|
|
|
|
|
@markup.raw.block literal or verbatim text as a stand-alone block
|
|
|
|
|
|
|
|
|
|
@markup.list list markers
|
|
|
|
|
@markup.list.checked checked todo-style list markers
|
|
|
|
|
@markup.list.unchecked unchecked todo-style list markers
|
|
|
|
|
|
|
|
|
|
@diff.plus added text (for diff files)
|
|
|
|
|
@diff.minus deleted text (for diff files)
|
|
|
|
|
@diff.delta changed text (for diff files)
|
|
|
|
|
|
|
|
|
|
@tag XML-style tag names (e.g. in XML, HTML, etc.)
|
2024-03-03 03:18:34 -07:00
|
|
|
|
@tag.builtin XML-style tag names (e.g. HTML5 tags)
|
2024-01-17 08:55:52 -07:00
|
|
|
|
@tag.attribute XML-style tag attributes
|
|
|
|
|
@tag.delimiter XML-style tag delimiters
|
2023-12-18 08:49:44 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
*treesitter-highlight-spell*
|
|
|
|
|
The special `@spell` capture can be used to indicate that a node should be
|
|
|
|
|
spell checked by Nvim's builtin |spell| checker. For example, the following
|
2023-04-01 03:29:38 -07:00
|
|
|
|
capture marks comments as to be checked: >query
|
2022-09-14 02:08:31 -07:00
|
|
|
|
|
|
|
|
|
(comment) @spell
|
|
|
|
|
<
|
2022-10-28 05:09:22 -07:00
|
|
|
|
|
|
|
|
|
There is also `@nospell` which disables spellchecking regions with `@spell`.
|
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
*treesitter-highlight-conceal*
|
|
|
|
|
Treesitter highlighting supports |conceal| via the `conceal` metadata. By
|
|
|
|
|
convention, nodes to be concealed are captured as `@conceal`, but any capture
|
|
|
|
|
can be used. For example, the following query can be used to hide code block
|
2023-04-01 03:29:38 -07:00
|
|
|
|
delimiters in Markdown: >query
|
2022-08-24 14:48:52 -07:00
|
|
|
|
|
2023-04-01 03:29:38 -07:00
|
|
|
|
(fenced_code_block_delimiter @conceal (#set! conceal ""))
|
2022-08-24 14:48:52 -07:00
|
|
|
|
<
|
2022-09-14 02:08:31 -07:00
|
|
|
|
It is also possible to replace a node with a single character, which (unlike
|
|
|
|
|
legacy syntax) can be given a custom highlight. For example, the following
|
|
|
|
|
(ill-advised) query replaces the `!=` operator by a Unicode glyph, which is
|
2023-04-01 03:29:38 -07:00
|
|
|
|
still highlighted the same as other operators: >query
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
"!=" @operator (#set! conceal "≠")
|
2021-05-01 05:19:48 -07:00
|
|
|
|
<
|
2022-09-25 16:58:27 -07:00
|
|
|
|
Conceals specified in this way respect 'conceallevel'.
|
2021-07-18 06:27:54 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
*treesitter-highlight-priority*
|
|
|
|
|
Treesitter uses |nvim_buf_set_extmark()| to set highlights with a default
|
2021-07-18 06:27:54 -07:00
|
|
|
|
priority of 100. This enables plugins to set a highlighting priority lower or
|
2024-01-28 18:53:14 -07:00
|
|
|
|
higher than treesitter. It is also possible to change the priority of an
|
2022-02-13 06:43:25 -07:00
|
|
|
|
individual query pattern manually by setting its `"priority"` metadata
|
2023-04-01 03:29:38 -07:00
|
|
|
|
attribute: >query
|
2021-07-18 06:27:54 -07:00
|
|
|
|
|
2024-07-28 03:14:23 -07:00
|
|
|
|
((super_important_node) @superimportant (#set! priority 105))
|
2023-04-01 03:29:38 -07:00
|
|
|
|
<
|
2023-03-08 04:03:11 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
|
|
|
|
TREESITTER LANGUAGE INJECTIONS *treesitter-language-injections*
|
2021-07-18 06:27:54 -07:00
|
|
|
|
<
|
2023-03-08 04:03:11 -07:00
|
|
|
|
|
|
|
|
|
Note the following information is adapted from:
|
|
|
|
|
https://tree-sitter.github.io/tree-sitter/syntax-highlighting#language-injection
|
|
|
|
|
|
|
|
|
|
Some source files contain code written in multiple different languages.
|
|
|
|
|
Examples include:
|
|
|
|
|
|
|
|
|
|
• HTML files, which can contain JavaScript inside of `<script>` tags and
|
|
|
|
|
CSS inside of `<style>` tags
|
|
|
|
|
• ERB files, which contain Ruby inside of `<%` `%>` tags, and HTML outside of
|
|
|
|
|
those tags
|
|
|
|
|
• PHP files, which can contain HTML between the `<php` tags
|
|
|
|
|
• JavaScript files, which contain regular expression syntax within regex
|
|
|
|
|
literals
|
|
|
|
|
• Ruby, which can contain snippets of code inside of heredoc literals,
|
|
|
|
|
where the heredoc delimiter often indicates the language
|
|
|
|
|
• Lua, which can contain snippets of Vimscript inside |vim.cmd()| calls.
|
|
|
|
|
• Vimscript, which can contain snippets of Lua inside |:lua-heredoc|
|
|
|
|
|
blocks.
|
|
|
|
|
|
|
|
|
|
All of these examples can be modeled in terms of a parent syntax tree and one
|
|
|
|
|
or more injected syntax trees, which reside inside of certain nodes in the
|
|
|
|
|
parent tree. The language injection query allows you to specify these
|
|
|
|
|
“injections” using the following captures:
|
|
|
|
|
|
|
|
|
|
• `@injection.content` - indicates that the captured node should have its
|
|
|
|
|
contents re-parsed using another language.
|
|
|
|
|
• `@injection.language` - indicates that the captured node’s text may
|
|
|
|
|
contain the name of a language that should be used to re-parse the
|
|
|
|
|
`@injection.content`.
|
2024-04-02 01:54:40 -07:00
|
|
|
|
• `@injection.filename` - indicates that the captured node’s text may
|
|
|
|
|
contain a filename; the corresponding filetype is then looked-up up via
|
|
|
|
|
|vim.filetype.match()| and treated as the name of a language that should
|
|
|
|
|
be used to re-parse the `@injection.content`.
|
2023-03-08 04:03:11 -07:00
|
|
|
|
|
|
|
|
|
The language injection behavior can also be configured by some properties
|
|
|
|
|
associated with patterns:
|
|
|
|
|
|
|
|
|
|
• `injection.language` - can be used to hard-code the name of a specific
|
|
|
|
|
language.
|
|
|
|
|
• `injection.combined` - indicates that all of the matching nodes in the
|
|
|
|
|
tree should have their content parsed as one nested document.
|
|
|
|
|
• `injection.include-children` - indicates that the `@injection.content`
|
|
|
|
|
node's entire text should be re-parsed, including the text of its child
|
|
|
|
|
nodes. By default, child nodes' text will be excluded from the injected
|
|
|
|
|
document.
|
2023-08-21 21:51:38 -07:00
|
|
|
|
• `injection.self` - indicates that the node's text should be parsed with
|
|
|
|
|
the same language as the node's LanguageTree.
|
|
|
|
|
• `injection.parent` - indicates that the captured node’s text should
|
|
|
|
|
be parsed with the same language as the node's parent LanguageTree.
|
2023-03-08 04:03:11 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
==============================================================================
|
|
|
|
|
VIM.TREESITTER *lua-treesitter*
|
|
|
|
|
|
|
|
|
|
The remainder of this document is a reference manual for the `vim.treesitter`
|
2024-01-28 18:53:14 -07:00
|
|
|
|
Lua module, which is the main interface for Nvim's treesitter integration.
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Most of the following content is automatically generated from the function
|
|
|
|
|
documentation.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*vim.treesitter.language_version*
|
2024-01-28 18:53:14 -07:00
|
|
|
|
The latest parser ABI version that is supported by the bundled treesitter
|
2022-09-14 02:08:31 -07:00
|
|
|
|
library.
|
|
|
|
|
|
|
|
|
|
*vim.treesitter.minimum_language_version*
|
2024-01-28 18:53:14 -07:00
|
|
|
|
The earliest parser ABI version that is supported by the bundled treesitter
|
2022-09-14 02:08:31 -07:00
|
|
|
|
library.
|
2020-09-14 10:04:33 -07:00
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
|
==============================================================================
|
|
|
|
|
Lua module: vim.treesitter *lua-treesitter-core*
|
2021-03-02 12:51:08 -07:00
|
|
|
|
|
2023-02-23 10:05:20 -07:00
|
|
|
|
foldexpr({lnum}) *vim.treesitter.foldexpr()*
|
|
|
|
|
Returns the fold level for {lnum} in the current buffer. Can be set
|
|
|
|
|
directly to 'foldexpr': >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
|
2023-02-23 10:05:20 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {lnum} (`integer?`) Line number to calculate fold level for
|
2023-02-23 10:05:20 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string`)
|
2023-02-23 10:05:20 -07:00
|
|
|
|
|
2022-12-07 01:27:41 -07:00
|
|
|
|
*vim.treesitter.get_captures_at_cursor()*
|
|
|
|
|
get_captures_at_cursor({winnr})
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Returns a list of highlight capture names under the cursor
|
2022-09-05 23:50:06 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {winnr} (`integer?`) Window handle or 0 for current window (default)
|
2022-09-05 23:50:06 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string[]`) List of capture names
|
2022-09-05 23:50:06 -07:00
|
|
|
|
|
2022-12-07 01:27:41 -07:00
|
|
|
|
*vim.treesitter.get_captures_at_pos()*
|
|
|
|
|
get_captures_at_pos({bufnr}, {row}, {col})
|
2022-09-16 00:05:05 -07:00
|
|
|
|
Returns a list of highlight captures at the given position
|
|
|
|
|
|
|
|
|
|
Each capture is represented by a table containing the capture name as a
|
|
|
|
|
string as well as a table of metadata (`priority`, `conceal`, ...; empty
|
|
|
|
|
if none are defined).
|
|
|
|
|
|
2022-08-25 12:41:52 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {bufnr} (`integer`) Buffer number (0 for current buffer)
|
|
|
|
|
• {row} (`integer`) Position row
|
|
|
|
|
• {col} (`integer`) Position column
|
2022-08-25 12:41:52 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-04-26 06:07:47 -07:00
|
|
|
|
(`{capture: string, lang: string, metadata: table}[]`)
|
2022-09-05 23:50:06 -07:00
|
|
|
|
|
2023-02-22 08:01:08 -07:00
|
|
|
|
get_node({opts}) *vim.treesitter.get_node()*
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Returns the smallest named node at the given position
|
2022-09-05 23:50:06 -07:00
|
|
|
|
|
2023-09-27 02:51:42 -07:00
|
|
|
|
NOTE: Calling this on an unparsed tree can yield an invalid node. If the
|
|
|
|
|
tree is not known to be parsed by, e.g., an active highlighter, parse the
|
|
|
|
|
tree first via >lua
|
|
|
|
|
vim.treesitter.get_parser(bufnr):parse(range)
|
|
|
|
|
<
|
|
|
|
|
|
2022-09-05 23:50:06 -07:00
|
|
|
|
Parameters: ~
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {opts} (`table?`) Optional keyword arguments:
|
|
|
|
|
• {bufnr} (`integer?`) Buffer number (nil or 0 for current
|
2023-02-22 08:01:08 -07:00
|
|
|
|
buffer)
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {pos} (`{ [1]: integer, [2]: integer }?`) 0-indexed (row,
|
|
|
|
|
col) tuple. Defaults to cursor position in the current
|
|
|
|
|
window. Required if {bufnr} is not the current buffer
|
|
|
|
|
• {lang} (`string?`) Parser language. (default: from buffer
|
2023-12-04 02:00:49 -07:00
|
|
|
|
filetype)
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {ignore_injections} (`boolean?`) Ignore injected languages
|
|
|
|
|
(default true)
|
2022-09-05 23:50:06 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`TSNode?`) Node at the given position
|
2022-08-25 12:41:52 -07:00
|
|
|
|
|
2022-12-07 01:27:41 -07:00
|
|
|
|
get_node_range({node_or_range}) *vim.treesitter.get_node_range()*
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Returns the node's range or an unpacked range table
|
2022-08-25 06:52:56 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {node_or_range} (`TSNode|table`) Node or table of positions
|
2022-08-25 06:52:56 -07:00
|
|
|
|
|
2023-07-06 06:32:39 -07:00
|
|
|
|
Return (multiple): ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`integer`) start_row
|
|
|
|
|
(`integer`) start_col
|
|
|
|
|
(`integer`) end_row
|
|
|
|
|
(`integer`) end_col
|
2022-08-25 06:52:56 -07:00
|
|
|
|
|
2023-03-24 07:43:14 -07:00
|
|
|
|
*vim.treesitter.get_node_text()*
|
|
|
|
|
get_node_text({node}, {source}, {opts})
|
|
|
|
|
Gets the text corresponding to a given node
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {node} (`TSNode`)
|
|
|
|
|
• {source} (`integer|string`) Buffer or string from which the {node} is
|
2023-03-24 07:43:14 -07:00
|
|
|
|
extracted
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {opts} (`table?`) Optional parameters.
|
2023-03-24 07:43:14 -07:00
|
|
|
|
• metadata (table) Metadata of a specific capture. This
|
|
|
|
|
would be set to `metadata[capture_id]` when using
|
|
|
|
|
|vim.treesitter.query.add_directive()|.
|
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string`)
|
2023-03-24 07:43:14 -07:00
|
|
|
|
|
2022-12-07 01:27:41 -07:00
|
|
|
|
get_parser({bufnr}, {lang}, {opts}) *vim.treesitter.get_parser()*
|
2023-03-30 02:26:28 -07:00
|
|
|
|
Returns the parser for a specific buffer and attaches it to the buffer
|
2021-03-02 12:51:08 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
If needed, this will create the parser.
|
2021-03-02 12:51:08 -07:00
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {bufnr} (`integer?`) Buffer the parser should be tied to (default:
|
2022-09-05 06:52:27 -07:00
|
|
|
|
current buffer)
|
2024-03-02 07:29:41 -07:00
|
|
|
|
• {lang} (`string?`) Language of this parser (default: from buffer
|
2022-09-05 06:52:27 -07:00
|
|
|
|
filetype)
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {opts} (`table?`) Options to pass to the created language tree
|
2021-03-02 12:51:08 -07:00
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
|
Return: ~
|
2024-02-27 08:20:32 -07:00
|
|
|
|
(`vim.treesitter.LanguageTree`) object to use for parsing
|
2021-03-02 12:51:08 -07:00
|
|
|
|
|
2023-03-24 07:43:14 -07:00
|
|
|
|
get_range({node}, {source}, {metadata}) *vim.treesitter.get_range()*
|
|
|
|
|
Get the range of a |TSNode|. Can also supply {source} and {metadata} to
|
|
|
|
|
get the range with directives applied.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {node} (`TSNode`)
|
|
|
|
|
• {source} (`integer|string?`) Buffer or string from which the {node}
|
2023-03-24 07:43:14 -07:00
|
|
|
|
is extracted
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {metadata} (`vim.treesitter.query.TSMetadata?`)
|
2023-03-24 07:43:14 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`Range6`)
|
2023-03-24 07:43:14 -07:00
|
|
|
|
|
2022-12-07 01:27:41 -07:00
|
|
|
|
*vim.treesitter.get_string_parser()*
|
|
|
|
|
get_string_parser({str}, {lang}, {opts})
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Returns a string parser
|
2021-03-02 12:51:08 -07:00
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {str} (`string`) Text to parse
|
|
|
|
|
• {lang} (`string`) Language of this string
|
|
|
|
|
• {opts} (`table?`) Options to pass to the created language tree
|
2022-09-14 02:08:31 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-02-27 08:20:32 -07:00
|
|
|
|
(`vim.treesitter.LanguageTree`) object to use for parsing
|
2021-03-02 12:51:08 -07:00
|
|
|
|
|
2023-03-02 08:01:42 -07:00
|
|
|
|
inspect_tree({opts}) *vim.treesitter.inspect_tree()*
|
|
|
|
|
Open a window that displays a textual representation of the nodes in the
|
|
|
|
|
language tree.
|
|
|
|
|
|
|
|
|
|
While in the window, press "a" to toggle display of anonymous nodes, "I"
|
2023-08-25 11:17:36 -07:00
|
|
|
|
to toggle the display of the source language of each node, "o" to toggle
|
2023-09-15 03:10:55 -07:00
|
|
|
|
the query editor, and press <Enter> to jump to the node under the cursor
|
2024-02-22 13:58:59 -07:00
|
|
|
|
in the source buffer. Folding also works (try |zo|, |zc|, etc.).
|
2023-03-02 08:01:42 -07:00
|
|
|
|
|
2023-03-02 10:03:11 -07:00
|
|
|
|
Can also be shown with `:InspectTree`. *:InspectTree*
|
|
|
|
|
|
2023-03-02 08:01:42 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {opts} (`table?`) Optional options table with the following possible
|
2023-03-02 08:01:42 -07:00
|
|
|
|
keys:
|
|
|
|
|
• lang (string|nil): The language of the source buffer. If
|
2024-03-02 07:29:41 -07:00
|
|
|
|
omitted, detect from the filetype of the source buffer.
|
2023-03-02 08:01:42 -07:00
|
|
|
|
• bufnr (integer|nil): Buffer to draw the tree into. If
|
|
|
|
|
omitted, a new buffer is created.
|
|
|
|
|
• winid (integer|nil): Window id to display the tree buffer
|
|
|
|
|
in. If omitted, a new window is created with {command}.
|
|
|
|
|
• command (string|nil): Vimscript command to create the
|
2023-03-17 04:41:57 -07:00
|
|
|
|
window. Default value is "60vnew". Only used when {winid} is
|
|
|
|
|
nil.
|
2023-03-02 08:01:42 -07:00
|
|
|
|
• title (string|fun(bufnr:integer):string|nil): Title of the
|
|
|
|
|
window. If a function, it accepts the buffer number of the
|
|
|
|
|
source buffer as its only argument and should return a
|
|
|
|
|
string.
|
|
|
|
|
|
2022-12-07 01:27:41 -07:00
|
|
|
|
is_ancestor({dest}, {source}) *vim.treesitter.is_ancestor()*
|
2022-08-25 06:52:56 -07:00
|
|
|
|
Determines whether a node is the ancestor of another
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {dest} (`TSNode`) Possible ancestor
|
|
|
|
|
• {source} (`TSNode`) Possible descendant
|
2022-08-25 06:52:56 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`boolean`) True if {dest} is an ancestor of {source}
|
2022-08-25 06:52:56 -07:00
|
|
|
|
|
2022-12-07 01:27:41 -07:00
|
|
|
|
*vim.treesitter.is_in_node_range()*
|
|
|
|
|
is_in_node_range({node}, {line}, {col})
|
2022-08-25 12:41:52 -07:00
|
|
|
|
Determines whether (line, col) position is in node range
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {node} (`TSNode`) defining the range
|
|
|
|
|
• {line} (`integer`) Line (0-based)
|
|
|
|
|
• {col} (`integer`) Column (0-based)
|
2022-09-05 06:52:27 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`boolean`) True if the position is in node range
|
2022-08-25 12:41:52 -07:00
|
|
|
|
|
2022-12-07 01:27:41 -07:00
|
|
|
|
node_contains({node}, {range}) *vim.treesitter.node_contains()*
|
2022-08-25 06:52:56 -07:00
|
|
|
|
Determines if a node contains a range
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {node} (`TSNode`)
|
|
|
|
|
• {range} (`table`)
|
2022-08-25 06:52:56 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`boolean`) True if the {node} contains the {range}
|
2022-08-25 06:52:56 -07:00
|
|
|
|
|
2022-12-07 01:27:41 -07:00
|
|
|
|
start({bufnr}, {lang}) *vim.treesitter.start()*
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Starts treesitter highlighting for a buffer
|
2022-09-05 06:52:27 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Can be used in an ftplugin or FileType autocommand.
|
2022-09-05 06:52:27 -07:00
|
|
|
|
|
|
|
|
|
Note: By default, disables regex syntax highlighting, which may be
|
2022-09-07 21:54:41 -07:00
|
|
|
|
required for some plugins. In this case, add `vim.bo.syntax = 'on'` after
|
|
|
|
|
the call to `start`.
|
2022-09-05 06:52:27 -07:00
|
|
|
|
|
2022-11-23 04:31:49 -07:00
|
|
|
|
Example: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.api.nvim_create_autocmd( 'FileType', { pattern = 'tex',
|
|
|
|
|
callback = function(args)
|
|
|
|
|
vim.treesitter.start(args.buf, 'latex')
|
|
|
|
|
vim.bo[args.buf].syntax = 'on' -- only if additional legacy syntax is needed
|
|
|
|
|
end
|
|
|
|
|
})
|
2022-09-05 06:52:27 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {bufnr} (`integer?`) Buffer to be highlighted (default: current
|
2022-09-05 06:52:27 -07:00
|
|
|
|
buffer)
|
2024-03-02 07:29:41 -07:00
|
|
|
|
• {lang} (`string?`) Language of the parser (default: from buffer
|
|
|
|
|
filetype)
|
2022-09-05 06:52:27 -07:00
|
|
|
|
|
2022-12-07 01:27:41 -07:00
|
|
|
|
stop({bufnr}) *vim.treesitter.stop()*
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Stops treesitter highlighting for a buffer
|
2022-09-05 06:52:27 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {bufnr} (`integer?`) Buffer to stop highlighting (default: current
|
2022-09-05 06:52:27 -07:00
|
|
|
|
buffer)
|
|
|
|
|
|
2021-03-02 12:51:08 -07:00
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
|
==============================================================================
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Lua module: vim.treesitter.language *lua-treesitter-language*
|
2021-03-02 12:51:08 -07:00
|
|
|
|
|
2023-03-24 07:43:14 -07:00
|
|
|
|
add({lang}, {opts}) *vim.treesitter.language.add()*
|
2023-03-30 02:26:28 -07:00
|
|
|
|
Load parser with name {lang}
|
2023-02-21 10:09:18 -07:00
|
|
|
|
|
|
|
|
|
Parsers are searched in the `parser` runtime directory, or the provided
|
|
|
|
|
{path}
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {lang} (`string`) Name of the parser (alphanumerical and `_` only)
|
|
|
|
|
• {opts} (`table?`) Options:
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {filetype}? (`string|string[]`, default: {lang}) Default
|
|
|
|
|
filetype the parser should be associated with.
|
|
|
|
|
• {path}? (`string`) Optional path the parser is located at
|
|
|
|
|
• {symbol_name}? (`string`) Internal symbol name for the
|
2023-02-21 10:09:18 -07:00
|
|
|
|
language to load
|
|
|
|
|
|
2023-03-30 02:26:28 -07:00
|
|
|
|
get_filetypes({lang}) *vim.treesitter.language.get_filetypes()*
|
|
|
|
|
Get the filetypes associated with the parser named {lang}.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {lang} (`string`) Name of parser
|
2023-03-30 02:26:28 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string[]`) filetypes
|
2023-03-30 02:26:28 -07:00
|
|
|
|
|
2023-03-24 07:43:14 -07:00
|
|
|
|
get_lang({filetype}) *vim.treesitter.language.get_lang()*
|
2024-02-15 10:16:04 -07:00
|
|
|
|
|
2023-02-21 10:09:18 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {filetype} (`string`)
|
2023-02-21 10:09:18 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string?`)
|
2023-02-21 10:09:18 -07:00
|
|
|
|
|
2023-03-24 07:43:14 -07:00
|
|
|
|
inspect({lang}) *vim.treesitter.language.inspect()*
|
2021-05-01 05:19:48 -07:00
|
|
|
|
Inspects the provided language.
|
2021-03-02 12:51:08 -07:00
|
|
|
|
|
2021-11-27 09:26:49 -07:00
|
|
|
|
Inspecting provides some useful information on the language like node
|
2021-05-01 05:19:48 -07:00
|
|
|
|
names, ...
|
2021-03-02 12:51:08 -07:00
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {lang} (`string`) Language
|
2022-09-14 02:08:31 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`table`)
|
2021-03-02 12:51:08 -07:00
|
|
|
|
|
2023-03-24 07:43:14 -07:00
|
|
|
|
register({lang}, {filetype}) *vim.treesitter.language.register()*
|
2023-03-30 02:26:28 -07:00
|
|
|
|
Register a parser named {lang} to be used for {filetype}(s).
|
2021-03-02 12:51:08 -07:00
|
|
|
|
|
2024-01-06 05:18:13 -07:00
|
|
|
|
Note: this adds or overrides the mapping for {filetype}, any existing
|
|
|
|
|
mappings from other filetypes to {lang} will be preserved.
|
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {lang} (`string`) Name of parser
|
|
|
|
|
• {filetype} (`string|string[]`) Filetype(s) to associate with lang
|
2022-09-14 02:08:31 -07:00
|
|
|
|
|
2021-03-02 12:51:08 -07:00
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
|
==============================================================================
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Lua module: vim.treesitter.query *lua-treesitter-query*
|
2021-03-02 12:51:08 -07:00
|
|
|
|
|
2023-03-24 07:43:14 -07:00
|
|
|
|
*vim.treesitter.query.add_directive()*
|
2024-02-16 10:54:47 -07:00
|
|
|
|
add_directive({name}, {handler}, {opts})
|
2021-05-01 05:19:48 -07:00
|
|
|
|
Adds a new directive to be used in queries
|
|
|
|
|
|
2022-05-28 10:22:18 -07:00
|
|
|
|
Handlers can set match level data by setting directly on the metadata
|
2024-02-16 10:54:47 -07:00
|
|
|
|
object `metadata.key = value`. Additionally, handlers can set node level
|
2022-05-28 10:22:18 -07:00
|
|
|
|
data by using the capture id on the metadata table
|
|
|
|
|
`metadata[capture_id].key = value`
|
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {name} (`string`) Name of the directive, without leading #
|
2024-03-16 10:11:42 -07:00
|
|
|
|
• {handler} (`fun(match: table<integer,TSNode[]>, pattern: integer, source: integer|string, predicate: any[], metadata: table)`)
|
2024-02-16 10:54:47 -07:00
|
|
|
|
• match: A table mapping capture IDs to a list of captured
|
|
|
|
|
nodes
|
|
|
|
|
• pattern: the index of the matching pattern in the query
|
|
|
|
|
file
|
2023-01-16 05:39:19 -07:00
|
|
|
|
• predicate: list of strings containing the full directive
|
|
|
|
|
being called, e.g. `(node (#set! conceal "-"))` would get
|
|
|
|
|
the predicate `{ "#set!", "conceal", "-" }`
|
2024-03-16 10:11:42 -07:00
|
|
|
|
• {opts} (`table`) A table with the following fields:
|
|
|
|
|
• {force}? (`boolean`) Override an existing predicate of
|
|
|
|
|
the same name
|
|
|
|
|
• {all}? (`boolean`) Use the correct implementation of the
|
2024-02-16 10:54:47 -07:00
|
|
|
|
match table where capture IDs map to a list of nodes
|
|
|
|
|
instead of a single node. Defaults to false (for backward
|
|
|
|
|
compatibility). This option will eventually become the
|
|
|
|
|
default and removed.
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2023-03-24 07:43:14 -07:00
|
|
|
|
*vim.treesitter.query.add_predicate()*
|
2024-02-16 10:54:47 -07:00
|
|
|
|
add_predicate({name}, {handler}, {opts})
|
2021-05-01 05:19:48 -07:00
|
|
|
|
Adds a new predicate to be used in queries
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {name} (`string`) Name of the predicate, without leading #
|
2024-03-16 10:11:42 -07:00
|
|
|
|
• {handler} (`fun(match: table<integer,TSNode[]>, pattern: integer, source: integer|string, predicate: any[], metadata: table)`)
|
2023-03-24 07:43:14 -07:00
|
|
|
|
• see |vim.treesitter.query.add_directive()| for argument
|
2023-01-16 05:39:19 -07:00
|
|
|
|
meanings
|
2024-03-16 10:11:42 -07:00
|
|
|
|
• {opts} (`table`) A table with the following fields:
|
|
|
|
|
• {force}? (`boolean`) Override an existing predicate of
|
|
|
|
|
the same name
|
|
|
|
|
• {all}? (`boolean`) Use the correct implementation of the
|
2024-02-16 10:54:47 -07:00
|
|
|
|
match table where capture IDs map to a list of nodes
|
|
|
|
|
instead of a single node. Defaults to false (for backward
|
|
|
|
|
compatibility). This option will eventually become the
|
|
|
|
|
default and removed.
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2023-09-16 10:05:59 -07:00
|
|
|
|
edit({lang}) *vim.treesitter.query.edit()*
|
2023-09-20 04:15:23 -07:00
|
|
|
|
Opens a live editor to query the buffer you started from.
|
2023-09-15 03:10:55 -07:00
|
|
|
|
|
2023-09-20 04:15:23 -07:00
|
|
|
|
Can also be shown with *:EditQuery*.
|
2023-09-15 03:10:55 -07:00
|
|
|
|
|
2023-09-20 04:15:23 -07:00
|
|
|
|
If you move the cursor to a capture name ("@foo"), text matching the
|
|
|
|
|
capture is highlighted in the source buffer. The query editor is a scratch
|
|
|
|
|
buffer, use `:write` to save it. You can find example queries at
|
|
|
|
|
`$VIMRUNTIME/queries/`.
|
2023-09-15 03:10:55 -07:00
|
|
|
|
|
2023-09-16 10:05:59 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {lang} (`string?`) language to open the query editor for. If omitted,
|
|
|
|
|
inferred from the current buffer's filetype.
|
2023-09-16 10:05:59 -07:00
|
|
|
|
|
2023-03-24 07:43:14 -07:00
|
|
|
|
get({lang}, {query_name}) *vim.treesitter.query.get()*
|
2021-05-01 05:19:48 -07:00
|
|
|
|
Returns the runtime query {query_name} for {lang}.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {lang} (`string`) Language to use for the query
|
|
|
|
|
• {query_name} (`string`) Name of the query (e.g. "highlights")
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-25 11:27:48 -07:00
|
|
|
|
(`vim.treesitter.Query?`) Parsed query. `nil` if no query files are
|
|
|
|
|
found.
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2023-03-24 07:43:14 -07:00
|
|
|
|
*vim.treesitter.query.get_files()*
|
|
|
|
|
get_files({lang}, {query_name}, {is_included})
|
2021-05-01 05:19:48 -07:00
|
|
|
|
Gets the list of files used to make up a query
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {lang} (`string`) Language to get query for
|
|
|
|
|
• {query_name} (`string`) Name of the query to load (e.g.,
|
|
|
|
|
"highlights")
|
|
|
|
|
• {is_included} (`boolean?`) Internal parameter, most of the time left
|
2022-09-14 02:08:31 -07:00
|
|
|
|
as `nil`
|
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string[]`) query_files List of files to load for given query and
|
2022-09-14 02:08:31 -07:00
|
|
|
|
language
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2023-04-29 09:22:26 -07:00
|
|
|
|
lint({buf}, {opts}) *vim.treesitter.query.lint()*
|
|
|
|
|
Lint treesitter queries using installed parser, or clear lint errors.
|
|
|
|
|
|
|
|
|
|
Use |treesitter-parsers| in runtimepath to check the query file in {buf}
|
|
|
|
|
for errors:
|
|
|
|
|
• verify that used nodes are valid identifiers in the grammar.
|
|
|
|
|
• verify that predicates and directives are valid.
|
|
|
|
|
• verify that top-level s-expressions are valid.
|
|
|
|
|
|
|
|
|
|
The found diagnostics are reported using |diagnostic-api|. By default, the
|
|
|
|
|
parser used for verification is determined by the containing folder of the
|
2024-02-15 10:16:04 -07:00
|
|
|
|
query file, e.g., if the path ends in `/lua/highlights.scm`, the parser
|
2024-01-04 09:09:13 -07:00
|
|
|
|
for the `lua` language will be used.
|
2023-04-29 09:22:26 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {buf} (`integer`) Buffer handle
|
|
|
|
|
• {opts} (`table?`) Optional keyword arguments:
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {langs}? (`string|string[]`) Language(s) to use for checking
|
2023-04-29 09:22:26 -07:00
|
|
|
|
the query. If multiple languages are specified, queries are
|
|
|
|
|
validated for all of them
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {clear} (`boolean`) Just clear current lint errors
|
2023-04-29 09:22:26 -07:00
|
|
|
|
|
2023-03-24 07:43:14 -07:00
|
|
|
|
list_directives() *vim.treesitter.query.list_directives()*
|
2022-05-28 10:22:18 -07:00
|
|
|
|
Lists the currently available directives to use in queries.
|
|
|
|
|
|
2021-07-25 11:47:24 -07:00
|
|
|
|
Return: ~
|
2024-03-16 10:11:42 -07:00
|
|
|
|
(`string[]`) Supported directives.
|
2021-07-25 11:47:24 -07:00
|
|
|
|
|
2023-03-24 07:43:14 -07:00
|
|
|
|
list_predicates() *vim.treesitter.query.list_predicates()*
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Lists the currently available predicates to use in queries.
|
|
|
|
|
|
2021-07-25 11:47:24 -07:00
|
|
|
|
Return: ~
|
2024-03-16 10:11:42 -07:00
|
|
|
|
(`string[]`) Supported predicates.
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2023-04-30 02:01:54 -07:00
|
|
|
|
omnifunc({findstart}, {base}) *vim.treesitter.query.omnifunc()*
|
|
|
|
|
Omnifunc for completing node names and predicates in treesitter queries.
|
|
|
|
|
|
|
|
|
|
Use via >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.bo.omnifunc = 'v:lua.vim.treesitter.query.omnifunc'
|
2023-04-30 02:01:54 -07:00
|
|
|
|
<
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Parameters: ~
|
|
|
|
|
• {findstart} (`0|1`)
|
|
|
|
|
• {base} (`string`)
|
|
|
|
|
|
2023-03-24 07:43:14 -07:00
|
|
|
|
parse({lang}, {query}) *vim.treesitter.query.parse()*
|
2021-05-01 05:19:48 -07:00
|
|
|
|
Parse {query} as a string. (If the query is in a file, the caller should
|
|
|
|
|
read the contents into a string before calling).
|
|
|
|
|
|
2024-01-04 09:09:13 -07:00
|
|
|
|
Returns a `Query` (see |lua-treesitter-query|) object which can be used to
|
2024-01-25 11:27:48 -07:00
|
|
|
|
search nodes in the syntax tree for the patterns defined in {query} using
|
|
|
|
|
the `iter_captures` and `iter_matches` methods.
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2022-02-13 06:43:25 -07:00
|
|
|
|
Exposes `info` and `captures` with additional context about {query}.
|
2021-05-01 05:19:48 -07:00
|
|
|
|
• `captures` contains the list of unique capture names defined in {query}.
|
2024-01-25 11:27:48 -07:00
|
|
|
|
• `info.captures` also points to `captures`.
|
2021-05-01 05:19:48 -07:00
|
|
|
|
• `info.patterns` contains information about predicates.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {lang} (`string`) Language to use for the query
|
|
|
|
|
• {query} (`string`) Query in s-expr syntax
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-25 11:27:48 -07:00
|
|
|
|
(`vim.treesitter.Query`) Parsed query
|
|
|
|
|
|
|
|
|
|
See also: ~
|
|
|
|
|
• |vim.treesitter.query.get()|
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
|
|
|
|
*Query:iter_captures()*
|
2023-07-17 02:39:52 -07:00
|
|
|
|
Query:iter_captures({node}, {source}, {start}, {stop})
|
2021-05-01 05:19:48 -07:00
|
|
|
|
Iterate over all captures from all matches inside {node}
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
{source} is needed if the query contains predicates; then the caller must
|
2021-05-01 05:19:48 -07:00
|
|
|
|
ensure to use a freshly parsed tree consistent with the current text of
|
2023-05-13 12:33:22 -07:00
|
|
|
|
the buffer (if relevant). {start} and {stop} can be used to limit matches
|
|
|
|
|
inside a row range (this is typically used with root node as the {node},
|
|
|
|
|
i.e., to get syntax highlight matches in the current viewport). When
|
|
|
|
|
omitted, the {start} and {stop} row values are used from the given node.
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
2024-03-17 11:02:40 -07:00
|
|
|
|
The iterator returns four values: a numeric id identifying the capture,
|
|
|
|
|
the captured node, metadata from any directives processing the match, and
|
|
|
|
|
the match itself. The following example shows how to get captures by name: >lua
|
|
|
|
|
for id, node, metadata, match in query:iter_captures(tree:root(), bufnr, first, last) do
|
2023-09-14 06:23:01 -07:00
|
|
|
|
local name = query.captures[id] -- name of the capture in the query
|
|
|
|
|
-- typically useful info about the node:
|
|
|
|
|
local type = node:type() -- type of the captured node
|
|
|
|
|
local row1, col1, row2, col2 = node:range() -- range of the capture
|
|
|
|
|
-- ... use the info here ...
|
|
|
|
|
end
|
2021-05-01 05:19:48 -07:00
|
|
|
|
<
|
|
|
|
|
|
2024-03-18 16:19:01 -07:00
|
|
|
|
Note: ~
|
|
|
|
|
• Captures are only returned if the query pattern of a specific capture
|
|
|
|
|
contained predicates.
|
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {node} (`TSNode`) under which the search will occur
|
|
|
|
|
• {source} (`integer|string`) Source buffer or string to extract text
|
2023-02-04 07:58:38 -07:00
|
|
|
|
from
|
2024-02-01 23:51:35 -07:00
|
|
|
|
• {start} (`integer?`) Starting line for the search. Defaults to
|
|
|
|
|
`node:start()`.
|
|
|
|
|
• {stop} (`integer?`) Stopping line for the search (end-exclusive).
|
|
|
|
|
Defaults to `node:end_()`.
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-03-25 15:06:31 -07:00
|
|
|
|
(`fun(end_line: integer?): integer, TSNode, vim.treesitter.query.TSMetadata, TSQueryMatch`)
|
2024-03-17 11:02:40 -07:00
|
|
|
|
capture id, capture node, metadata, match
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
|
|
|
|
*Query:iter_matches()*
|
2023-07-17 02:39:52 -07:00
|
|
|
|
Query:iter_matches({node}, {source}, {start}, {stop}, {opts})
|
2021-05-01 05:19:48 -07:00
|
|
|
|
Iterates the matches of self on a given range.
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Iterate over all matches within a {node}. The arguments are the same as
|
2022-09-25 16:58:27 -07:00
|
|
|
|
for |Query:iter_captures()| but the iterated values are different: an
|
2021-05-01 05:19:48 -07:00
|
|
|
|
(1-based) index of the pattern in the query, a table mapping capture
|
2024-02-16 10:54:47 -07:00
|
|
|
|
indices to a list of nodes, and metadata from any directives processing
|
|
|
|
|
the match.
|
2023-09-14 06:23:01 -07:00
|
|
|
|
|
2024-02-16 10:54:47 -07:00
|
|
|
|
WARNING: Set `all=true` to ensure all matching nodes in a match are
|
|
|
|
|
returned, otherwise only the last node in a match is returned, breaking
|
|
|
|
|
captures involving quantifiers such as `(comment)+ @comment`. The default
|
|
|
|
|
option `all=false` is only provided for backward compatibility and will be
|
|
|
|
|
removed after Nvim 0.10.
|
2023-09-14 06:23:01 -07:00
|
|
|
|
|
2024-02-16 10:54:47 -07:00
|
|
|
|
Example: >lua
|
|
|
|
|
for pattern, match, metadata in cquery:iter_matches(tree:root(), bufnr, 0, -1, { all = true }) 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
|
2023-09-14 06:23:01 -07:00
|
|
|
|
end
|
|
|
|
|
end
|
2021-05-01 05:19:48 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {node} (`TSNode`) under which the search will occur
|
|
|
|
|
• {source} (`integer|string`) Source buffer or string to search
|
2024-02-01 23:51:35 -07:00
|
|
|
|
• {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:
|
2023-05-11 03:13:32 -07:00
|
|
|
|
• max_start_depth (integer) if non-zero, sets the maximum
|
|
|
|
|
start depth for each match. This is used to prevent
|
2024-02-01 23:51:35 -07:00
|
|
|
|
traversing too deep into a tree.
|
2024-03-18 16:19:01 -07:00
|
|
|
|
• match_limit (integer) Set the maximum number of
|
|
|
|
|
in-progress matches (Default: 256).
|
2024-02-16 10:54:47 -07:00
|
|
|
|
• all (boolean) When set, the returned match table maps
|
|
|
|
|
capture IDs to a list of nodes. Older versions of
|
|
|
|
|
iter_matches incorrectly mapped capture IDs to a single
|
|
|
|
|
node, which is incorrect behavior. This option will
|
|
|
|
|
eventually become the default and removed.
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-02-16 10:54:47 -07:00
|
|
|
|
(`fun(): integer, table<integer, TSNode[]>, table`) pattern id, match,
|
2023-02-04 07:58:38 -07:00
|
|
|
|
metadata
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2023-03-24 07:43:14 -07:00
|
|
|
|
set({lang}, {query_name}, {text}) *vim.treesitter.query.set()*
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Sets the runtime query named {query_name} for {lang}
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
|
|
|
|
This allows users to override any runtime files and/or configuration set
|
|
|
|
|
by plugins.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {lang} (`string`) Language to use for the query
|
|
|
|
|
• {query_name} (`string`) Name of the query (e.g., "highlights")
|
|
|
|
|
• {text} (`string`) Query text (unparsed).
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
==============================================================================
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Lua module: vim.treesitter.languagetree *lua-treesitter-languagetree*
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
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.
|
2023-03-05 16:15:29 -07:00
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
To create a LanguageTree (parser object) for a given buffer and language, use: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
local parser = vim.treesitter.get_parser(bufnr, lang)
|
2023-03-05 16:15:29 -07:00
|
|
|
|
<
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
(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.
|
2023-03-05 16:15:29 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
Whenever you need to access the current syntax tree, parse the buffer: >lua
|
|
|
|
|
local tree = parser:parse({ start_row, end_row })
|
2023-03-05 16:15:29 -07:00
|
|
|
|
<
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
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.
|
2023-03-05 16:15:29 -07:00
|
|
|
|
|
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
LanguageTree:children() *LanguageTree:children()*
|
2021-05-01 05:19:48 -07:00
|
|
|
|
Returns a map of language to child tree.
|
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
LanguageTree:contains({range}) *LanguageTree:contains()*
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Determines whether {range} is contained in the |LanguageTree|.
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {range} (`Range4`) `{ start_line, start_col, end_line, end_col }`
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`boolean`)
|
2022-09-14 02:08:31 -07:00
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
LanguageTree:destroy() *LanguageTree:destroy()*
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Destroys this |LanguageTree| and all its children.
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2022-02-13 06:43:25 -07:00
|
|
|
|
Any cleanup logic should be performed here.
|
|
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
|
Note: This DOES NOT remove this tree from a parent. Instead,
|
|
|
|
|
`remove_child` must be called on the parent to remove it.
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
LanguageTree:for_each_tree({fn}) *LanguageTree:for_each_tree()*
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Invokes the callback for each |LanguageTree| recursively.
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Note: This includes the invoking tree's child trees as well.
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {fn} (`fun(tree: TSTree, ltree: vim.treesitter.LanguageTree)`)
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
LanguageTree:included_regions() *LanguageTree:included_regions()*
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Gets the set of included regions managed by this LanguageTree. This can be
|
2024-01-09 10:36:46 -07:00
|
|
|
|
different from the regions set by injection query, because a partial
|
2024-01-04 09:09:13 -07:00
|
|
|
|
|LanguageTree:parse()| drops the regions outside the requested range.
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2023-03-09 09:09:39 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`table<integer, Range6[]>`)
|
2023-03-09 09:09:39 -07:00
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
LanguageTree:invalidate({reload}) *LanguageTree:invalidate()*
|
2024-05-02 06:57:21 -07:00
|
|
|
|
Invalidates this parser and its children.
|
|
|
|
|
|
|
|
|
|
Should only be called when the tracked state of the LanguageTree is not
|
|
|
|
|
valid against the parse tree in treesitter. Doesn't clear filesystem
|
|
|
|
|
cache. Called often, so needs to be fast.
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {reload} (`boolean?`)
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
LanguageTree:is_valid({exclude_children}) *LanguageTree:is_valid()*
|
2024-01-04 09:09:13 -07:00
|
|
|
|
Returns whether this LanguageTree is valid, i.e., |LanguageTree:trees()|
|
|
|
|
|
reflects the latest state of the source. If invalid, user should call
|
|
|
|
|
|LanguageTree:parse()|.
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {exclude_children} (`boolean?`) whether to ignore the validity of
|
2023-09-17 01:54:30 -07:00
|
|
|
|
children (default `false`)
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2023-03-09 09:09:39 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`boolean`)
|
2023-03-09 09:09:39 -07:00
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
LanguageTree:lang() *LanguageTree:lang()*
|
2021-05-01 05:19:48 -07:00
|
|
|
|
Gets the language of this tree node.
|
|
|
|
|
|
|
|
|
|
*LanguageTree:language_for_range()*
|
2023-07-17 02:39:52 -07:00
|
|
|
|
LanguageTree:language_for_range({range})
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Gets the appropriate language that contains {range}.
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {range} (`Range4`) `{ start_line, start_col, end_line, end_col }`
|
2022-08-25 06:52:56 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Return: ~
|
2024-03-16 10:11:42 -07:00
|
|
|
|
(`vim.treesitter.LanguageTree`) tree Managing {range}
|
2022-09-14 02:08:31 -07:00
|
|
|
|
|
2022-08-25 06:52:56 -07:00
|
|
|
|
*LanguageTree:named_node_for_range()*
|
2023-07-17 02:39:52 -07:00
|
|
|
|
LanguageTree:named_node_for_range({range}, {opts})
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Gets the smallest named node that contains {range}.
|
2022-08-25 06:52:56 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {range} (`Range4`) `{ start_line, start_col, end_line, end_col }`
|
2024-03-16 10:11:42 -07:00
|
|
|
|
• {opts} (`table?`) A table with the following fields:
|
|
|
|
|
• {ignore_injections}? (`boolean`, default: `true`) Ignore
|
|
|
|
|
injected languages
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Return: ~
|
2024-03-16 10:11:42 -07:00
|
|
|
|
(`TSNode?`)
|
2022-09-14 02:08:31 -07:00
|
|
|
|
|
2023-08-10 06:21:56 -07:00
|
|
|
|
LanguageTree:parse({range}) *LanguageTree:parse()*
|
|
|
|
|
Recursively parse all regions in the language tree using
|
|
|
|
|
|treesitter-parsers| for the corresponding languages and run injection
|
|
|
|
|
queries on the parsed trees to determine whether child trees should be
|
|
|
|
|
created and parsed.
|
|
|
|
|
|
|
|
|
|
Any region with empty range (`{}`, typically only the root tree) is always
|
|
|
|
|
parsed; otherwise (typically injections) only if it intersects {range} (or
|
|
|
|
|
if {range} is `true`).
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {range} (`boolean|Range?`) Parse this range in the parser's source.
|
2023-08-10 06:21:56 -07:00
|
|
|
|
Set to `true` to run a complete parse of the source (Note:
|
|
|
|
|
Can be slow!) Set to `false|nil` to only parse regions with
|
|
|
|
|
empty ranges (typically only the root tree without
|
|
|
|
|
injections).
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`table<integer, TSTree>`)
|
2022-09-14 02:08:31 -07:00
|
|
|
|
|
2023-04-30 08:11:38 -07:00
|
|
|
|
*LanguageTree:register_cbs()*
|
2023-07-17 02:39:52 -07:00
|
|
|
|
LanguageTree:register_cbs({cbs}, {recursive})
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Registers callbacks for the |LanguageTree|.
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
|
Parameters: ~
|
2024-03-16 10:11:42 -07:00
|
|
|
|
• {cbs} (`table<TSCallbackNameOn,function>`) An
|
|
|
|
|
|nvim_buf_attach()|-like table argument with the
|
|
|
|
|
following handlers:
|
2024-01-04 09:09:13 -07:00
|
|
|
|
• `on_bytes` : see |nvim_buf_attach()|, but this will be
|
|
|
|
|
called after the parsers callback.
|
2023-07-16 01:34:50 -07:00
|
|
|
|
• `on_changedtree` : a callback that will be called every
|
|
|
|
|
time the tree has syntactical changes. It will be
|
|
|
|
|
passed two arguments: a table of the ranges (as node
|
|
|
|
|
ranges) that changed and the changed tree.
|
|
|
|
|
• `on_child_added` : emitted when a child is added to the
|
|
|
|
|
tree.
|
|
|
|
|
• `on_child_removed` : emitted when a child is removed
|
|
|
|
|
from the tree.
|
|
|
|
|
• `on_detach` : emitted when the buffer is detached, see
|
|
|
|
|
|nvim_buf_detach_event|. Takes one argument, the number
|
|
|
|
|
of the buffer.
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {recursive} (`boolean?`) Apply callbacks recursively for all
|
2023-07-16 01:34:50 -07:00
|
|
|
|
children. Any new children will also inherit the
|
|
|
|
|
callbacks.
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
LanguageTree:source() *LanguageTree:source()*
|
2021-05-01 05:19:48 -07:00
|
|
|
|
Returns the source content of the language tree (bufnr or string).
|
|
|
|
|
|
2022-08-25 06:52:56 -07:00
|
|
|
|
*LanguageTree:tree_for_range()*
|
2023-07-17 02:39:52 -07:00
|
|
|
|
LanguageTree:tree_for_range({range}, {opts})
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Gets the tree that contains {range}.
|
2022-08-25 06:52:56 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {range} (`Range4`) `{ start_line, start_col, end_line, end_col }`
|
2024-03-16 10:11:42 -07:00
|
|
|
|
• {opts} (`table?`) A table with the following fields:
|
|
|
|
|
• {ignore_injections}? (`boolean`, default: `true`) Ignore
|
|
|
|
|
injected languages
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`TSTree?`)
|
2022-09-14 02:08:31 -07:00
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
LanguageTree:trees() *LanguageTree:trees()*
|
2023-09-17 01:54:30 -07:00
|
|
|
|
Returns all trees of the regions parsed by this parser. Does not include
|
|
|
|
|
child languages. The result is list-like if
|
2024-01-04 09:09:13 -07:00
|
|
|
|
• this LanguageTree is the root, in which case the result is empty or a
|
|
|
|
|
singleton list; or
|
2023-09-17 01:54:30 -07:00
|
|
|
|
• the root LanguageTree is fully parsed.
|
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`table<integer, TSTree>`)
|
2021-05-01 05:19:48 -07:00
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
|
2020-09-14 10:04:33 -07:00
|
|
|
|
vim:tw=78:ts=8:sw=4:sts=4:et:ft=help:norl:
|