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
Parsers are the heart of tree-sitter. They are libraries that tree-sitter will
2022-09-14 02:08:31 -07:00
search for in the `parser` runtime directory. By default, Nvim bundles only
parsers for C, Lua, and Vimscript, but parsers can be installed manually or
via a plugin like https://github.com/nvim-treesitter/nvim-treesitter.
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.
(This typically implies the priority "user config > plugins > bundled".
2021-06-16 10:06:29 -07:00
A parser can also be loaded manually using a full path: >
vim.treesitter.require_language("python", "/path/to/python.so")
2022-09-14 02:08:31 -07:00
<
==============================================================================
LANGUAGE TREES *treesitter-languagetree*
*LanguageTree*
2021-06-16 10:06:29 -07:00
2022-09-14 02:08:31 -07:00
As buffers can contain multiple languages (e.g., Vimscript commands in a Lua
file), multiple parsers may be needed to parse the full buffer. These are
combined in a |LanguageTree| object.
2021-06-16 10:06:29 -07:00
2022-09-14 02:08:31 -07:00
To create a LanguageTree (parser object) for a buffer and a given language,
use >
2021-06-16 10:06:29 -07:00
2022-09-14 02:08:31 -07:00
tsparser = vim.treesitter.get_parser(bufnr, lang)
<
`bufnr=0` can be used for current buffer. `lang` will default to 'filetype'.
2021-06-16 10:06:29 -07:00
Currently, the parser will be retained for the lifetime of a buffer but this
is subject to change. A plugin should keep a reference to the parser object as
long as it wants incremental updates.
2020-09-14 10:04:33 -07:00
Whenever you need to access the current syntax tree, parse the buffer: >
2022-09-14 02:08:31 -07:00
tstree = tsparser:parse()
<
This will return a table of immutable |lua-treesitter-tree|s that represent the
current state of the buffer. When the plugin wants to access the state after a
(possible) edit it should 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 |get_parser()| before you register your callback. But preferably
2021-06-16 10:06:29 -07:00
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.
2020-09-14 10:04:33 -07:00
2022-09-14 02:08:31 -07:00
See |lua-treesitter-languagetree| for the list of available methods.
==============================================================================
TREESITTER TREES *treesitter-tree*
*tstree*
A "treesitter tree" represents the parsed contents of a buffer, which can be
used to perform further analysis. It is a |luaref-userdata| reference to an
object held by the tree-sitter library.
2020-09-14 10:04:33 -07:00
2022-09-14 02:08:31 -07:00
An instance `tstree` of a treesitter tree supports the following methods.
2020-09-14 10:04:33 -07:00
tstree:root() *tstree:root()*
Return the root node of this tree.
2020-11-03 10:43:41 -07:00
tstree:copy() *tstree:copy()*
Returns a copy of the `tstree`.
2022-09-14 02:08:31 -07:00
==============================================================================
TREESITTER NODES *treesitter-node*
*tsnode*
A "treesitter node" represents one specific element of the parsed contents of
a buffer, which can be captured by a |Query| for, e.g., highlighting. It is a
|luaref-userdata| reference to an object held by the tree-sitter library.
2020-09-14 10:04:33 -07:00
2022-09-14 02:08:31 -07:00
An instance `tsnode` of a treesitter node supports the following methods.
2020-09-14 10:04:33 -07:00
tsnode:parent() *tsnode:parent()*
Get the node's immediate parent.
2021-08-19 22:35:25 -07:00
tsnode:next_sibling() *tsnode:next_sibling()*
Get the node's next sibling.
tsnode:prev_sibling() *tsnode:prev_sibling()*
Get the node's previous sibling.
2022-02-13 06:43:25 -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.
2022-02-13 06:43:25 -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.
2022-02-13 06:43:25 -07:00
tsnode:iter_children() *tsnode:iter_children()*
2020-09-14 10:04:33 -07:00
Iterates over all the direct children of {tsnode}, regardless of whether
2021-12-28 10:15:16 -07:00
they are named or not.
2020-09-14 10:04:33 -07:00
Returns the child node plus the eventual field name corresponding to this
child node.
tsnode:field({name}) *tsnode:field()*
Returns a table of the nodes corresponding to the {name} field.
tsnode:child_count() *tsnode:child_count()*
Get the node's number of children.
2021-05-01 05:19:48 -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.
2022-02-13 06:43:25 -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.
2022-02-13 06:43:25 -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.
tsnode:start() *tsnode:start()*
Get the node's start position. Return three values: the row, column and
total byte count (all zero-based).
tsnode:end_() *tsnode:end_()*
Get the node's end position. Return three values: the row, column and
total byte count (all zero-based).
tsnode:range() *tsnode:range()*
Get the range of the node. Return four values: the row, column of the
start position, then the row, column of the end position.
tsnode:type() *tsnode:type()*
Get the node's type as a string.
tsnode:symbol() *tsnode:symbol()*
Get the node's type as a numerical id.
tsnode:named() *tsnode:named()*
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.
tsnode:missing() *tsnode:missing()*
Check if the node is missing. Missing nodes are inserted by the parser in
order to recover from certain kinds of syntax errors.
tsnode:has_error() *tsnode:has_error()*
Check if the node is a syntax error or contains any syntax errors.
tsnode:sexpr() *tsnode:sexpr()*
Get an S-expression representing the node as a string.
2021-05-01 05:19:48 -07:00
tsnode:id() *tsnode:id()*
2021-12-28 10:15:16 -07:00
Get an 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
2022-02-13 06:43:25 -07:00
*tsnode:descendant_for_range()*
2020-09-14 10:04:33 -07:00
tsnode:descendant_for_range({start_row}, {start_col}, {end_row}, {end_col})
Get the smallest node within this node that spans the given range of (row,
column) positions
2022-02-13 06:43:25 -07:00
*tsnode:named_descendant_for_range()*
2020-09-14 10:04:33 -07:00
tsnode:named_descendant_for_range({start_row}, {start_col}, {end_row}, {end_col})
Get the smallest named node within this node that spans the given range of
(row, column) positions
2022-09-14 02:08:31 -07:00
==============================================================================
TREESITTER QUERIES *treesitter-query*
Treesitter queries are a way to extract information about a parsed |tstree|,
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
queries bundled with Neovim). 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
nodes. For example, the |eq?| predicate can be used as follows: >
2020-09-14 10:04:33 -07:00
((identifier) @foo (#eq? @foo "foo"))
2022-09-14 02:08:31 -07:00
<
to only match identifier corresponding to the `"foo"` text.
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?*
2022-09-14 02:08:31 -07:00
Match a string against the text corresponding to a node: >
2020-09-14 10:04:33 -07:00
((identifier) @foo (#eq? @foo "foo"))
((node1) @left (node2) @right (#eq? @left @right))
<
2022-09-16 00:05:05 -07:00
`match?` *treesitter-predicate-match?*
`vim-match?` *treesitter-predicate-vim-match?*
2022-09-14 02:08:31 -07:00
Match a |regexp| against the text corresponding to a node: >
2021-12-28 10:15:16 -07:00
((identifier) @constant (#match? @constant "^[A-Z_]+$"))
2022-09-14 02:08:31 -07:00
< Note: The `^` and `$` anchors will match the start and end of the
node's text.
2022-08-11 05:25:48 -07:00
2022-09-16 00:05:05 -07:00
`lua-match?` *treesitter-predicate-lua-match?*
2022-09-14 02:08:31 -07:00
Match a |lua-pattern| against the text corresponding to a node,
similar to `match?`
2022-08-11 05:25:48 -07:00
2022-09-16 00:05:05 -07:00
`contains?` *treesitter-predicate-contains?*
2022-09-14 02:08:31 -07:00
Match a string against parts of the text corresponding to a node: >
2020-09-14 10:04:33 -07:00
((identifier) @foo (#contains? @foo "foo"))
((identifier) @foo-bar (#contains @foo-bar "foo" "bar"))
2021-05-14 08:41:20 -07:00
<
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
a node: >
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.
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.
2022-09-14 02:08:31 -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
effects. For example, the |set!| predicate sets metadata on the match or node: >
2020-11-24 07:50:33 -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}
Examples: >
((identifier) @foo (#set! @foo "kind" "parameter"))
((node1) @left (node2) @right (#set! "type" "pair"))
<
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
generate a new range object for the captured node as
`metadata[capture_id].range`.
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
2022-05-28 10:22:18 -07:00
Example: >
((identifier) @constant (#offset! @constant 0 1 0 -1))
<
2021-04-11 11:53:52 -07:00
2022-09-14 02:08:31 -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
2022-09-14 02:08:31 -07:00
Neovim supports to customize the behavior of the queries using a set of
"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
repeated, for example, the following two modeline blocks are both valid: >
2020-09-14 10:04:33 -07:00
2022-09-14 02:08:31 -07:00
;; inherits: foo,bar
;; extends
;; extends
;;
;; inherits: baz
<
==============================================================================
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`,
which match a |tsnode| in the parsed |tstree| to a `capture` that can be
assigned a highlight group. For example, the query >
2020-09-14 10:04:33 -07:00
2022-09-14 02:08:31 -07:00
(parameters (identifier) @parameter)
<
matches any `identifier` node inside a function `parameter` node (e.g., the
`bar` in `foo(bar)`) to the capture named `@parameter`. It is also possible to
match literal expressions (provided the parser returns them): >
2020-09-14 10:04:33 -07:00
2022-09-14 02:08:31 -07:00
"return" @keyword.return
<
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*
2022-08-24 14:48:52 -07:00
The capture names, with `@` included, 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
to Nvim's standard |highlight-groups| by default but can be overridden in
colorschemes.
2022-08-24 14:48:52 -07:00
A fallback system is implemented, so that more specific groups fallback to
2022-09-14 02:08:31 -07:00
more generic ones. For instance, in a language that has separate doc comments,
`@comment.doc` 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
instance, to highlight comments differently per language: >
hi @comment.c guifg=Blue
hi @comment.lua @guifg=DarkBlue
hi link @comment.doc.java String
<
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
capture marks comments as to be checked: >
(comment) @spell
<
*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
delimiters in Markdown: >
2022-08-24 14:48:52 -07:00
2022-09-14 02:08:31 -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
still highlighted the same as other operators: >
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-14 02:08:31 -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
higher than tree-sitter. 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
attribute: >
2021-07-18 06:27:54 -07:00
2022-09-14 02:08:31 -07:00
(super_important_node) @ImportantHighlight (#set! "priority" 105)
2021-07-18 06:27:54 -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`
Lua module, which is the main interface for Nvim's tree-sitter integration.
Most of the following content is automatically generated from the function
documentation.
*vim.treesitter.language_version*
The latest parser ABI version that is supported by the bundled tree-sitter
library.
*vim.treesitter.minimum_language_version*
The earliest parser ABI version that is supported by the bundled tree-sitter
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
2022-09-05 23:50:06 -07:00
get_captures_at_cursor({winnr}) *get_captures_at_cursor()*
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: ~
{winnr} (number|nil) Window handle or 0 for current window (default)
Return: ~
2022-09-14 02:08:31 -07:00
string[] List of capture names
2022-09-05 23:50:06 -07:00
2022-09-24 15:45:15 -07:00
get_captures_at_pos({bufnr}, {row}, {col}) *get_captures_at_pos()*
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: ~
2022-09-05 06:52:27 -07:00
{bufnr} (number) Buffer number (0 for current buffer)
{row} (number) Position row
{col} (number) Position column
2022-08-25 12:41:52 -07:00
Return: ~
2022-09-16 00:05:05 -07:00
table[] List of captures `{ capture = "capture name", metadata = { ...
} }`
2022-09-05 23:50:06 -07:00
get_node_at_cursor({winnr}) *get_node_at_cursor()*
2022-09-14 02:08:31 -07:00
Returns the smallest named node under the cursor
2022-09-05 23:50:06 -07:00
Parameters: ~
{winnr} (number|nil) Window handle or 0 for current window (default)
Return: ~
2022-09-14 02:08:31 -07:00
(string) Name of node under the cursor
2022-09-05 23:50:06 -07:00
2022-09-24 15:45:15 -07:00
get_node_at_pos({bufnr}, {row}, {col}, {opts}) *get_node_at_pos()*
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
Parameters: ~
{bufnr} (number) Buffer number (0 for current buffer)
{row} (number) Position row
{col} (number) Position column
{opts} (table) Optional keyword arguments:
• ignore_injections boolean Ignore injected languages
(default true)
Return: ~
2022-09-14 02:08:31 -07:00
userdata |tsnode| under the cursor
2022-08-25 12:41:52 -07:00
2022-08-25 06:52:56 -07:00
get_node_range({node_or_range}) *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: ~
2022-09-14 02:08:31 -07:00
{node_or_range} (userdata|table) |tsnode| or table of positions
2022-08-25 06:52:56 -07:00
Return: ~
2022-09-14 02:08:31 -07:00
(table) `{ start_row, start_col, end_row, end_col }`
2022-08-25 06:52:56 -07:00
2021-05-01 05:19:48 -07:00
get_parser({bufnr}, {lang}, {opts}) *get_parser()*
2022-09-14 02:08:31 -07:00
Returns the parser for a specific buffer and filetype 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: ~
2022-09-05 23:50:06 -07:00
{bufnr} (number|nil) Buffer the parser should be tied to (default:
2022-09-05 06:52:27 -07:00
current buffer)
2022-09-14 02:08:31 -07:00
{lang} (string|nil) Filetype of this parser (default: buffer
2022-09-05 06:52:27 -07:00
filetype)
{opts} (table|nil) 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: ~
2022-09-14 02:08:31 -07:00
LanguageTree |LanguageTree| object to use for parsing
2021-03-02 12:51:08 -07:00
2021-05-01 05:19:48 -07:00
get_string_parser({str}, {lang}, {opts}) *get_string_parser()*
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: ~
2022-09-14 02:08:31 -07:00
{str} (string) Text to parse
{lang} (string) Language of this string
{opts} (table|nil) Options to pass to the created language tree
Return: ~
LanguageTree |LanguageTree| object to use for parsing
2021-03-02 12:51:08 -07:00
2022-08-25 06:52:56 -07:00
is_ancestor({dest}, {source}) *is_ancestor()*
Determines whether a node is the ancestor of another
Parameters: ~
2022-09-14 02:08:31 -07:00
{dest} userdata Possible ancestor |tsnode|
{source} userdata Possible descendant |tsnode|
2022-08-25 06:52:56 -07:00
Return: ~
2022-09-14 02:08:31 -07:00
(boolean) True if {dest} is an ancestor of {source}
2022-08-25 06:52:56 -07:00
2022-08-25 12:41:52 -07:00
is_in_node_range({node}, {line}, {col}) *is_in_node_range()*
Determines whether (line, col) position is in node range
Parameters: ~
2022-09-14 02:08:31 -07:00
{node} userdata |tsnode| defining the range
2022-09-05 06:52:27 -07:00
{line} (number) Line (0-based)
{col} (number) Column (0-based)
Return: ~
(boolean) True if the position is in node range
2022-08-25 12:41:52 -07:00
2022-08-25 06:52:56 -07:00
node_contains({node}, {range}) *node_contains()*
Determines if a node contains a range
Parameters: ~
2022-09-14 02:08:31 -07:00
{node} userdata |tsnode|
2022-09-05 06:52:27 -07:00
{range} (table)
2022-08-25 06:52:56 -07:00
Return: ~
2022-09-14 02:08:31 -07:00
(boolean) True if the {node} contains the {range}
2022-08-25 06:52:56 -07:00
2022-09-06 08:33:44 -07:00
start({bufnr}, {lang}) *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-09-14 02:08:31 -07:00
Example: >
2022-09-05 06:52:27 -07:00
2022-09-14 02:08:31 -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: ~
{bufnr} (number|nil) Buffer to be highlighted (default: current
buffer)
{lang} (string|nil) Language of the parser (default: buffer
filetype)
stop({bufnr}) *stop()*
2022-09-14 02:08:31 -07:00
Stops treesitter highlighting for a buffer
2022-09-05 06:52:27 -07:00
Parameters: ~
{bufnr} (number|nil) Buffer to stop highlighting (default: current
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
2021-05-01 05:19:48 -07:00
inspect_language({lang}) *inspect_language()*
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: ~
2022-09-14 02:08:31 -07:00
{lang} (string) Language
Return: ~
(table)
2021-03-02 12:51:08 -07:00
2022-07-25 03:23:04 -07:00
*require_language()*
require_language({lang}, {path}, {silent}, {symbol_name})
2022-09-14 02:08:31 -07:00
Asserts that a parser for the language {lang} is installed.
2021-03-02 12:51:08 -07:00
2022-09-14 02:08:31 -07:00
Parsers are searched in the `parser` runtime directory, or the provided
{path}
2021-03-02 12:51:08 -07:00
2021-05-01 05:19:48 -07:00
Parameters: ~
2022-09-14 02:08:31 -07:00
{lang} (string) Language the parser should parse
2022-07-25 03:23:04 -07:00
{path} (string|nil) Optional path the parser is located at
{silent} (boolean|nil) Don't throw an error if language not
found
{symbol_name} (string|nil) Internal symbol name for the language to
load
2021-03-02 12:51:08 -07:00
2022-09-14 02:08:31 -07:00
Return: ~
(boolean) If the specified language is installed
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
2021-05-01 05:19:48 -07:00
add_directive({name}, {handler}, {force}) *add_directive()*
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
object `metadata.key = value`, additionally, handlers can set node level
data by using the capture id on the metadata table
`metadata[capture_id].key = value`
2021-05-01 05:19:48 -07:00
Parameters: ~
2022-09-14 02:08:31 -07:00
{name} (string) Name of the directive, without leading #
{handler} function(match:string, pattern:string, bufnr:number,
predicate:function, metadata:table)
2021-05-01 05:19:48 -07:00
add_predicate({name}, {handler}, {force}) *add_predicate()*
Adds a new predicate to be used in queries
Parameters: ~
2022-09-14 02:08:31 -07:00
{name} (string) Name of the predicate, without leading #
{handler} function(match:string, pattern:string, bufnr:number,
predicate:function)
2021-05-01 05:19:48 -07:00
2022-08-25 06:52:56 -07:00
get_node_text({node}, {source}, {opts}) *get_node_text()*
2021-05-01 05:19:48 -07:00
Gets the text corresponding to a given node
Parameters: ~
2022-09-14 02:08:31 -07:00
{node} userdata |tsnode|
{source} (number|string) Buffer or string from which the {node} is
2022-08-25 06:52:56 -07:00
extracted
2022-09-14 02:08:31 -07:00
{opts} (table|nil) Optional parameters.
• concat: (boolean) Concatenate result in a string (default
true)
Return: ~
(string[]|string)
2021-05-01 05:19:48 -07:00
get_query({lang}, {query_name}) *get_query()*
Returns the runtime query {query_name} for {lang}.
Parameters: ~
2022-09-14 02:08:31 -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: ~
2022-09-14 02:08:31 -07:00
Query Parsed query
2021-05-01 05:19:48 -07:00
*get_query_files()*
get_query_files({lang}, {query_name}, {is_included})
Gets the list of files used to make up a query
Parameters: ~
2022-09-14 02:08:31 -07:00
{lang} (string) Language to get query for
{query_name} (string) Name of the query to load (e.g., 'highlights')
{is_included} (boolean|nil) Internal parameter, most of the time left
as `nil`
Return: ~
string[] query_files List of files to load for given query and
language
2021-05-01 05:19:48 -07:00
2021-07-25 11:47:24 -07:00
list_directives() *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: ~
2022-09-14 02:08:31 -07:00
string[] List of supported directives.
2021-07-25 11:47:24 -07:00
2021-05-01 05:19:48 -07:00
list_predicates() *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: ~
2022-09-14 02:08:31 -07:00
string[] List of supported predicates.
2021-05-01 05:19:48 -07:00
parse_query({lang}, {query}) *parse_query()*
Parse {query} as a string. (If the query is in a file, the caller should
read the contents into a string before calling).
2022-09-14 02:08:31 -07:00
Returns a `Query` (see |lua-treesitter-query|) object which can be used to search nodes in
the syntax tree for the patterns defined in {query} using `iter_*` methods below.
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}.
2022-03-13 05:48:14 -07:00
-`info.captures` also points to `captures`.
2021-05-01 05:19:48 -07:00
• `info.patterns` contains information about predicates.
Parameters: ~
2022-09-14 02:08:31 -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: ~
2022-09-14 02:08:31 -07:00
Query Parsed query
2021-05-01 05:19:48 -07:00
*Query:iter_captures()*
Query:iter_captures({self}, {node}, {source}, {start}, {stop})
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
2021-11-27 09:26:49 -07:00
the buffer (if relevant). {start_row} and {end_row} can be used to limit
2021-05-01 05:19:48 -07:00
matches inside a row range (this is typically used with root node as the
2022-09-14 02:08:31 -07:00
{node}, i.e., to get syntax highlight matches in the current viewport).
When omitted, the {start} and {end} row values are used from the given
node.
2022-08-11 05:25:48 -07:00
2022-09-14 02:08:31 -07:00
The iterator returns three values: a numeric id identifying the capture,
2021-05-01 05:19:48 -07:00
the captured node, and metadata from any directives processing the match.
2022-09-14 02:08:31 -07:00
The following example shows how to get captures by name: >
for id, node, metadata in query:iter_captures(tree:root(), bufnr, first, last) do
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
<
Parameters: ~
2022-09-14 02:08:31 -07:00
{node} userdata |tsnode| under which the search will occur
{source} (number|string) Source buffer or string to extract text from
{start} (number) Starting line for the search
{stop} (number) Stopping line for the search (end-exclusive)
2021-05-01 05:19:48 -07:00
{self}
Return: ~
2022-09-14 02:08:31 -07:00
(number) capture Matching capture id
(table) capture_node Capture for {node}
(table) metadata for the {capture}
2021-05-01 05:19:48 -07:00
*Query:iter_matches()*
Query:iter_matches({self}, {node}, {source}, {start}, {stop})
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
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
indices to nodes, and metadata from any directives processing the match.
2022-09-14 02:08:31 -07:00
If the query has more than one pattern, the capture table might be sparse
and e.g. `pairs()` method should be used over `ipairs` . Here is an example iterating over all captures in every match: >
2021-05-01 05:19:48 -07:00
2022-09-14 02:08:31 -07:00
for pattern, match, metadata in cquery:iter_matches(tree:root(), bufnr, first, last) do
for id, node in pairs(match) do
local name = query.captures[id]
-- `node` was captured by the `name` capture in the match
2021-12-15 08:19:54 -07:00
2022-09-14 02:08:31 -07:00
local node_data = metadata[id] -- Node level metadata
2021-12-15 08:19:54 -07:00
2022-09-14 02:08:31 -07:00
... use the info here ...
end
end
2021-05-01 05:19:48 -07:00
<
Parameters: ~
2022-09-14 02:08:31 -07:00
{node} userdata |tsnode| under which the search will occur
{source} (number|string) Source buffer or string to search
{start} (number) Starting line for the search
{stop} (number) Stopping line for the search (end-exclusive)
2021-05-01 05:19:48 -07:00
{self}
Return: ~
2022-09-14 02:08:31 -07:00
(number) pattern id
(table) match
(table) metadata
2021-05-01 05:19:48 -07:00
set_query({lang}, {query_name}, {text}) *set_query()*
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: ~
2022-09-14 02:08:31 -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.highlighter *lua-treesitter-highlighter*
2021-05-01 05:19:48 -07:00
new({tree}, {opts}) *highlighter.new()*
Creates a new highlighter using
Parameters: ~
2022-09-14 02:08:31 -07:00
{tree} LanguageTree |LanguageTree| parser object to use for highlighting
{opts} (table|nil) Configuration of the highlighter:
• queries table overwrite queries used by the highlighter
Return: ~
TSHighlighter Created highlighter object
2021-05-01 05:19:48 -07:00
TSHighlighter:destroy({self}) *TSHighlighter:destroy()*
Removes all internal references to the highlighter
Parameters: ~
{self}
==============================================================================
2022-09-14 02:08:31 -07:00
Lua module: vim.treesitter.languagetree *lua-treesitter-languagetree*
2021-05-01 05:19:48 -07:00
LanguageTree:children({self}) *LanguageTree:children()*
Returns a map of language to child tree.
Parameters: ~
{self}
LanguageTree:contains({self}, {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: ~
2022-09-14 02:08:31 -07:00
{range} (table) `{ start_line, start_col, end_line, end_col }`
2021-05-01 05:19:48 -07:00
{self}
2022-09-14 02:08:31 -07:00
Return: ~
(boolean)
2021-05-01 05:19:48 -07:00
LanguageTree:destroy({self}) *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.
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
Parameters: ~
{self}
*LanguageTree:for_each_child()*
LanguageTree:for_each_child({self}, {fn}, {include_self})
2022-09-14 02:08:31 -07:00
Invokes the callback for each |LanguageTree| and its children recursively
2021-05-01 05:19:48 -07:00
Parameters: ~
2022-09-14 02:08:31 -07:00
{fn} function(tree: LanguageTree, lang: string)
{include_self} (boolean) Whether to include the invoking tree in the
results
2021-05-01 05:19:48 -07:00
{self}
LanguageTree:for_each_tree({self}, {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: ~
2022-09-14 02:08:31 -07:00
{fn} function(tree: TSTree, languageTree: LanguageTree)
2021-05-01 05:19:48 -07:00
{self}
LanguageTree:included_regions({self}) *LanguageTree:included_regions()*
Gets the set of included regions
Parameters: ~
{self}
LanguageTree:invalidate({self}, {reload}) *LanguageTree:invalidate()*
Invalidates this parser and all its children
Parameters: ~
{self}
LanguageTree:is_valid({self}) *LanguageTree:is_valid()*
2022-02-13 06:43:25 -07:00
Determines whether this tree is valid. If the tree is invalid, call `parse()` . This will return the updated tree.
2021-05-01 05:19:48 -07:00
Parameters: ~
{self}
LanguageTree:lang({self}) *LanguageTree:lang()*
Gets the language of this tree node.
Parameters: ~
{self}
*LanguageTree:language_for_range()*
LanguageTree:language_for_range({self}, {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: ~
2022-09-14 02:08:31 -07:00
{range} (table) `{ start_line, start_col, end_line, end_col }`
2022-08-25 06:52:56 -07:00
{self}
2022-09-14 02:08:31 -07:00
Return: ~
LanguageTree Managing {range}
2022-08-25 06:52:56 -07:00
*LanguageTree:named_node_for_range()*
LanguageTree:named_node_for_range({self}, {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: ~
2022-09-14 02:08:31 -07:00
{range} (table) `{ start_line, start_col, end_line, end_col }`
{opts} (table|nil) Optional keyword arguments:
• ignore_injections boolean Ignore injected languages
(default true)
2021-05-01 05:19:48 -07:00
{self}
2022-09-14 02:08:31 -07:00
Return: ~
userdata|nil Found |tsnode|
2021-05-01 05:19:48 -07:00
LanguageTree:parse({self}) *LanguageTree:parse()*
Parses all defined regions using a treesitter parser for the language this
tree represents. This will run the injection query for this language to
determine if any child languages should be created.
Parameters: ~
{self}
2022-09-14 02:08:31 -07:00
Return: ~
userdata[] Table of parsed |tstree|
(table) Change list
2021-05-01 05:19:48 -07:00
LanguageTree:register_cbs({self}, {cbs}) *LanguageTree:register_cbs()*
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: ~
2022-05-12 07:02:46 -07:00
{cbs} (table) An |nvim_buf_attach()|-like table argument with the
2022-09-14 02:08:31 -07:00
following handlers:
2022-02-13 06:43:25 -07:00
• `on_bytes` : see |nvim_buf_attach()|, but this will be called after the parsers callback.
• `on_changedtree` : a callback that will be called every time
the tree has syntactical changes. It will only be passed one
argument, which is a table of the ranges (as node ranges)
that changed.
• `on_child_added` : emitted when a child is added to the
tree.
• `on_child_removed` : emitted when a child is removed from
the tree.
2021-05-01 05:19:48 -07:00
{self}
LanguageTree:source({self}) *LanguageTree:source()*
Returns the source content of the language tree (bufnr or string).
Parameters: ~
2022-08-25 06:52:56 -07:00
{self}
*LanguageTree:tree_for_range()*
LanguageTree:tree_for_range({self}, {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: ~
2022-09-14 02:08:31 -07:00
{range} (table) `{ start_line, start_col, end_line, end_col }`
{opts} (table|nil) Optional keyword arguments:
• ignore_injections boolean Ignore injected languages
(default true)
2021-05-01 05:19:48 -07:00
{self}
2022-09-14 02:08:31 -07:00
Return: ~
userdata|nil Contained |tstree|
2021-05-01 05:19:48 -07:00
LanguageTree:trees({self}) *LanguageTree:trees()*
Returns all trees this language tree contains. Does not include child
languages.
Parameters: ~
{self}
new({source}, {lang}, {opts}) *languagetree.new()*
2022-09-14 02:08:31 -07:00
A |LanguageTree| holds the treesitter parser for a given language {lang}
used to parse a buffer. As the buffer may contain injected languages, the LanguageTree needs to store parsers for these child languages as well (which in turn
may contain child languages themselves, hence the name).
2022-08-11 05:25:48 -07:00
2021-05-01 05:19:48 -07:00
Parameters: ~
2022-09-14 02:08:31 -07:00
{source} (number|string) Buffer or a string of text to parse
{lang} (string) Root language this tree represents
{opts} (table|nil) Optional keyword arguments:
• injections table Mapping language to injection query
strings. This is useful for overriding the built-in
runtime file searching for the injection language query
per language.
Return: ~
LanguageTree |LanguageTree| parser object
2022-08-11 05:25:48 -07:00
2020-09-14 10:04:33 -07:00
vim:tw=78:ts=8:sw=4:sts=4:et:ft=help:norl: