aboutsummaryrefslogtreecommitdiff
path: root/runtime/doc/treesitter.txt
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2023-11-29 21:52:58 +0000
committerJosh Rahm <joshuarahm@gmail.com>2023-11-29 21:52:58 +0000
commit931bffbda3668ddc609fc1da8f9eb576b170aa52 (patch)
treed8c1843a95da5ea0bb4acc09f7e37843d9995c86 /runtime/doc/treesitter.txt
parent142d9041391780ac15b89886a54015fdc5c73995 (diff)
parent4a8bf24ac690004aedf5540fa440e788459e5e34 (diff)
downloadrneovim-userreg.tar.gz
rneovim-userreg.tar.bz2
rneovim-userreg.zip
Merge remote-tracking branch 'upstream/master' into userreguserreg
Diffstat (limited to 'runtime/doc/treesitter.txt')
-rw-r--r--runtime/doc/treesitter.txt904
1 files changed, 535 insertions, 369 deletions
diff --git a/runtime/doc/treesitter.txt b/runtime/doc/treesitter.txt
index 917863eef8..9bdc6b8d24 100644
--- a/runtime/doc/treesitter.txt
+++ b/runtime/doc/treesitter.txt
@@ -18,150 +18,129 @@ changes. This documentation may also not fully reflect the latest changes.
PARSER FILES *treesitter-parsers*
Parsers are the heart of tree-sitter. They are libraries that tree-sitter will
-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".
+search for in the `parser` runtime directory. By default, Nvim bundles parsers
+for C, Lua, Vimscript, Vimdoc and Treesitter query files, 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".
A parser can also be loaded manually using a full path: >lua
- vim.treesitter.require_language("python", "/path/to/python.so")
+ vim.treesitter.language.add('python', { path = "/path/to/python.so" })
<
==============================================================================
-LANGUAGE TREES *treesitter-languagetree*
- *LanguageTree*
-
-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.
-
-To create a LanguageTree (parser object) for a buffer and a given language,
-use >lua
-
- tsparser = vim.treesitter.get_parser(bufnr, lang)
-<
-`bufnr=0` can be used for current buffer. `lang` will default to 'filetype'.
-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.
-
-Whenever you need to access the current syntax tree, parse the buffer: >lua
-
- tstree = tsparser:parse()
-<
-This will return a table of immutable |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 |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.
-
-See |lua-treesitter-languagetree| for the list of available methods.
-
-==============================================================================
TREESITTER TREES *treesitter-tree*
- *tstree*
+ *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.
+used to perform further analysis. It is a |userdata| reference to an object
+held by the tree-sitter library.
-An instance `tstree` of a treesitter tree supports the following methods.
+An instance `TSTree` of a treesitter tree supports the following methods.
-tstree:root() *tstree:root()*
+TSTree:root() *TSTree:root()*
Return the root node of this tree.
-tstree:copy() *tstree:copy()*
- Returns a copy of the `tstree`.
+TSTree:copy() *TSTree:copy()*
+ Returns a copy of the `TSTree`.
==============================================================================
TREESITTER NODES *treesitter-node*
- *tsnode*
+ *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.
+a buffer, which can be captured by a |Query| for, e.g., highlighting. It is
+a |userdata| reference to an object held by the tree-sitter library.
-An instance `tsnode` of a treesitter node supports the following methods.
+An instance `TSNode` of a treesitter node supports the following methods.
-tsnode:parent() *tsnode:parent()*
+TSNode:parent() *TSNode:parent()*
Get the node's immediate parent.
-tsnode:next_sibling() *tsnode:next_sibling()*
+TSNode:next_sibling() *TSNode:next_sibling()*
Get the node's next sibling.
-tsnode:prev_sibling() *tsnode:prev_sibling()*
+TSNode:prev_sibling() *TSNode:prev_sibling()*
Get the node's previous sibling.
-tsnode:next_named_sibling() *tsnode:next_named_sibling()*
+TSNode:next_named_sibling() *TSNode:next_named_sibling()*
Get the node's next named sibling.
-tsnode:prev_named_sibling() *tsnode:prev_named_sibling()*
+TSNode:prev_named_sibling() *TSNode:prev_named_sibling()*
Get the node's previous named sibling.
-tsnode:iter_children() *tsnode:iter_children()*
- Iterates over all the direct children of {tsnode}, regardless of whether
+TSNode:iter_children() *TSNode:iter_children()*
+ Iterates over all the direct children of {TSNode}, regardless of whether
they are named or not.
Returns the child node plus the eventual field name corresponding to this
child node.
-tsnode:field({name}) *tsnode:field()*
+TSNode:field({name}) *TSNode:field()*
Returns a table of the nodes corresponding to the {name} field.
-tsnode:child_count() *tsnode:child_count()*
+TSNode:child_count() *TSNode:child_count()*
Get the node's number of children.
-tsnode:child({index}) *tsnode:child()*
+TSNode:child({index}) *TSNode:child()*
Get the node's child at the given {index}, where zero represents the first
child.
-tsnode:named_child_count() *tsnode:named_child_count()*
+TSNode:named_child_count() *TSNode:named_child_count()*
Get the node's number of named children.
-tsnode:named_child({index}) *tsnode:named_child()*
+TSNode:named_child({index}) *TSNode:named_child()*
Get the node's named child at the given {index}, where zero represents the
first named child.
-tsnode:start() *tsnode:start()*
+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_()*
+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: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`)
-tsnode:type() *tsnode:type()*
+TSNode:type() *TSNode:type()*
Get the node's type as a string.
-tsnode:symbol() *tsnode:symbol()*
+TSNode:symbol() *TSNode:symbol()*
Get the node's type as a numerical id.
-tsnode:named() *tsnode:named()*
+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()*
+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()*
+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.
+
+TSNode:has_error() *TSNode:has_error()*
Check if the node is a syntax error or contains any syntax errors.
-tsnode:sexpr() *tsnode:sexpr()*
+TSNode:sexpr() *TSNode:sexpr()*
Get an S-expression representing the node as a string.
-tsnode:id() *tsnode:id()*
+TSNode:id() *TSNode:id()*
Get an unique identifier for the node inside its own tree.
No guarantees are made about this identifier's internal representation,
@@ -171,20 +150,29 @@ tsnode:id() *tsnode:id()*
Note: The `id` is not guaranteed to be unique for nodes from different
trees.
- *tsnode:descendant_for_range()*
-tsnode:descendant_for_range({start_row}, {start_col}, {end_row}, {end_col})
+TSNode:tree() *TSNode:tree()*
+ Get the |TSTree| of the node.
+ *TSNode:descendant_for_range()*
+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
- *tsnode:named_descendant_for_range()*
-tsnode:named_descendant_for_range({start_row}, {start_col}, {end_row}, {end_col})
+ *TSNode:named_descendant_for_range()*
+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
+ *TSNode:equal()*
+TSNode:equal({node})
+ Check if {node} refers to the same node within the same tree.
+
+ *TSNode:byte_length()*
+TSNode:byte_length()
+ Return the number of bytes spanned by this node.
==============================================================================
TREESITTER QUERIES *treesitter-query*
-Treesitter queries are a way to extract information about a parsed |tstree|,
+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
@@ -202,7 +190,7 @@ Nvim looks for queries as `*.scm` files in a `queries` directory under
purpose, e.g., `queries/lua/highlights.scm` for highlighting Lua files.
By default, the first query on `runtimepath` is used (which usually implies
that user config takes precedence over plugins, which take precedence over
-queries bundled with Neovim). If a query should extend other queries instead
+queries bundled with Nvim). If a query should extend other queries instead
of replacing them, use |treesitter-query-modeline-extends|.
See |lua-treesitter-query| for the list of available methods for working with
@@ -212,7 +200,7 @@ treesitter queries from Lua.
TREESITTER QUERY PREDICATES *treesitter-predicates*
Predicates are special scheme nodes that are evaluated to conditionally capture
-nodes. For example, the `eq?` predicate can be used as follows: >
+nodes. For example, the `eq?` predicate can be used as follows: >query
((identifier) @foo (#eq? @foo "foo"))
<
@@ -221,13 +209,13 @@ to only match identifier corresponding to the `"foo"` text.
The following predicates are built in:
`eq?` *treesitter-predicate-eq?*
- Match a string against the text corresponding to a node: >
+ Match a string against the text corresponding to a node: >query
((identifier) @foo (#eq? @foo "foo"))
((node1) @left (node2) @right (#eq? @left @right))
<
`match?` *treesitter-predicate-match?*
`vim-match?` *treesitter-predicate-vim-match?*
- Match a |regexp| against the text corresponding to a node: >
+ Match a |regexp| against the text corresponding to a node: >query
((identifier) @constant (#match? @constant "^[A-Z_]+$"))
< Note: The `^` and `$` anchors will match the start and end of the
node's text.
@@ -237,31 +225,43 @@ The following predicates are built in:
similar to `match?`
`contains?` *treesitter-predicate-contains?*
- Match a string against parts of the text corresponding to a node: >
+ Match a string against parts of the text corresponding to a node: >query
((identifier) @foo (#contains? @foo "foo"))
((identifier) @foo-bar (#contains? @foo-bar "foo" "bar"))
<
`any-of?` *treesitter-predicate-any-of?*
Match any of the given strings against the text corresponding to
- a node: >
+ a node: >query
((identifier) @foo (#any-of? @foo "foo" "bar"))
<
This is the recommended way to check if the node matches one of many
keywords, as it has been optimized for this.
+ `has-ancestor?` *treesitter-predicate-has-ancestor?*
+ Match any of the given node types against all ancestors of a node: >query
+ ((identifier) @variable.builtin
+ (#any-of? @variable.builtin "begin" "end")
+ (#has-ancestor? @variable.builtin range_expression))
+<
+ `has-parent?` *treesitter-predicate-has-parent?*
+ Match any of the given node types against the direct ancestor of a
+ node: >query
+ (((field_expression
+ (field_identifier) @method)) @_parent
+ (#has-parent? @_parent template_method function_declarator))
+<
*lua-treesitter-not-predicate*
Each predicate has a `not-` prefixed predicate that is just the negation of
the predicate.
Further predicates can be added via |vim.treesitter.query.add_predicate()|.
-Use |vim.treesitter.query.list_predicates()| to list all available
-predicates.
+Use |vim.treesitter.query.list_predicates()| to list all available predicates.
TREESITTER QUERY DIRECTIVES *treesitter-directives*
Treesitter directives store metadata for a node or match and perform side
-effects. For example, the `set!` directive sets metadata on the match or node: >
+effects. For example, the `set!` directive sets metadata on the match or node: >query
((identifier) @foo (#set! "type" "parameter"))
<
@@ -277,7 +277,7 @@ The following directives are built in:
{key}
{value}
- Examples: >
+ Examples: >query
((identifier) @foo (#set! @foo "kind" "parameter"))
((node1) @left (node2) @right (#set! "type" "pair"))
<
@@ -293,18 +293,37 @@ The following directives are built in:
{end_row}
{end_col}
- Example: >
+ Example: >query
((identifier) @constant (#offset! @constant 0 1 0 -1))
<
+ `gsub!` *treesitter-directive-gsub!*
+ Transforms the content of the node using a Lua pattern. This will set
+ a new `metadata[capture_id].text`.
+
+ Parameters: ~
+ {capture_id}
+ {pattern}
+
+ 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
+ (#trim! @fold)
+<
Further directives can be added via |vim.treesitter.query.add_directive()|.
-Use |vim.treesitter.query.list_directives()| to list all available
-directives.
+Use |vim.treesitter.query.list_directives()| to list all available directives.
TREESITTER QUERY MODELINES *treesitter-query-modeline*
-Neovim supports to customize the behavior of the queries using a set of
+Nvim 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:
@@ -323,11 +342,12 @@ currently supported modeline alternatives:
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: >
-
+repeated, for example, the following two modeline blocks are both valid:
+>query
;; inherits: foo,bar
;; extends
-
+<
+>query
;; extends
;;
;; inherits: baz
@@ -336,14 +356,14 @@ repeated, for example, the following two modeline blocks are both valid: >
TREESITTER SYNTAX HIGHLIGHTING *treesitter-highlight*
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 >
+which match a |TSNode| in the parsed |TSTree| to a `capture` that can be
+assigned a highlight group. For example, the query >query
(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): >
+match literal expressions (provided the parser returns them): >query
"return" @keyword.return
<
@@ -428,7 +448,7 @@ The following captures are linked by default to standard |group-name|s:
*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: >
+capture marks comments as to be checked: >query
(comment) @spell
<
@@ -439,14 +459,14 @@ There is also `@nospell` which disables spellchecking regions with `@spell`.
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: >
+delimiters in Markdown: >query
- (fenced_code_block_delimiter) @conceal (#set! conceal "")
+ (fenced_code_block_delimiter @conceal (#set! conceal ""))
<
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: >
+still highlighted the same as other operators: >query
"!=" @operator (#set! conceal "≠")
<
@@ -457,10 +477,61 @@ Treesitter uses |nvim_buf_set_extmark()| to set highlights with a default
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
individual query pattern manually by setting its `"priority"` metadata
-attribute: >
+attribute: >query
+
+ ((super_important_node) @superimportant (#set! "priority" 105))
+<
- (super_important_node) @ImportantHighlight (#set! "priority" 105)
+==============================================================================
+TREESITTER LANGUAGE INJECTIONS *treesitter-language-injections*
<
+
+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`.
+
+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.
+ • `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.
+
==============================================================================
VIM.TREESITTER *lua-treesitter*
@@ -481,12 +552,34 @@ library.
==============================================================================
Lua module: vim.treesitter *lua-treesitter-core*
+foldexpr({lnum}) *vim.treesitter.foldexpr()*
+ Returns the fold level for {lnum} in the current buffer. Can be set
+ directly to 'foldexpr': >lua
+ vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
+<
+
+ Parameters: ~
+ • {lnum} (integer|nil) Line number to calculate fold level for
+
+ Return: ~
+ (string)
+
+foldtext() *vim.treesitter.foldtext()*
+ Returns the highlighted content of the first line of the fold or falls
+ back to |foldtext()| if no treesitter parser is found. Can be set directly
+ to 'foldtext': >lua
+ vim.wo.foldtext = 'v:lua.vim.treesitter.foldtext()'
+<
+
+ Return: ~
+ `{ [1]: string, [2]: string[] }[]` | string
+
*vim.treesitter.get_captures_at_cursor()*
get_captures_at_cursor({winnr})
Returns a list of highlight capture names under the cursor
Parameters: ~
- • {winnr} (number|nil) Window handle or 0 for current window (default)
+ • {winnr} (integer|nil) Window handle or 0 for current window (default)
Return: ~
string[] List of capture names
@@ -500,63 +593,90 @@ get_captures_at_pos({bufnr}, {row}, {col})
if none are defined).
Parameters: ~
- • {bufnr} (number) Buffer number (0 for current buffer)
- • {row} (number) Position row
- • {col} (number) Position column
+ • {bufnr} (integer) Buffer number (0 for current buffer)
+ • {row} (integer) Position row
+ • {col} (integer) Position column
Return: ~
- table[] List of captures `{ capture = "capture name", metadata = { ...
- } }`
+ table[] List of captures `{ capture = "name", metadata = { ... } }`
+
+get_node({opts}) *vim.treesitter.get_node()*
+ Returns the smallest named node at the given position
-get_node_at_cursor({winnr}) *vim.treesitter.get_node_at_cursor()*
- Returns the smallest named node under the cursor
+ 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)
+<
Parameters: ~
- • {winnr} (number|nil) Window handle or 0 for current window (default)
+ • {opts} (table|nil) Optional keyword arguments:
+ • bufnr integer|nil Buffer number (nil or 0 for current
+ buffer)
+ • pos table|nil 0-indexed (row, col) tuple. Defaults to cursor
+ position in the current window. Required if {bufnr} is not
+ the current buffer
+ • ignore_injections boolean Ignore injected languages (default
+ true)
Return: ~
- (string) Name of node under the cursor
+ |TSNode| | nil Node at the given position
- *vim.treesitter.get_node_at_pos()*
-get_node_at_pos({bufnr}, {row}, {col}, {opts})
- Returns the smallest named node at the given position
+get_node_range({node_or_range}) *vim.treesitter.get_node_range()*
+ Returns the node's range or an unpacked range table
Parameters: ~
- • {bufnr} (number) Buffer number (0 for current buffer)
- • {row} (number) Position row
- • {col} (number) Position column
- • {opts} (table) Optional keyword arguments:
- • lang string|nil Parser language
- • ignore_injections boolean Ignore injected languages
- (default true)
+ • {node_or_range} (|TSNode| | table) Node or table of positions
- Return: ~
- userdata|nil |tsnode| under the cursor
+ Return (multiple): ~
+ (integer) start_row
+ (integer) start_col
+ (integer) end_row
+ (integer) end_col
-get_node_range({node_or_range}) *vim.treesitter.get_node_range()*
- Returns the node's range or an unpacked range table
+ *vim.treesitter.get_node_text()*
+get_node_text({node}, {source}, {opts})
+ Gets the text corresponding to a given node
Parameters: ~
- • {node_or_range} (userdata|table) |tsnode| or table of positions
+ • {node} |TSNode|
+ • {source} (integer|string) Buffer or string from which the {node} is
+ extracted
+ • {opts} (table|nil) Optional parameters.
+ • metadata (table) Metadata of a specific capture. This
+ would be set to `metadata[capture_id]` when using
+ |vim.treesitter.query.add_directive()|.
Return: ~
- (table) `{ start_row, start_col, end_row, end_col }`
+ (string)
get_parser({bufnr}, {lang}, {opts}) *vim.treesitter.get_parser()*
- Returns the parser for a specific buffer and filetype and attaches it to
- the buffer
+ Returns the parser for a specific buffer and attaches it to the buffer
If needed, this will create the parser.
Parameters: ~
- • {bufnr} (number|nil) Buffer the parser should be tied to (default:
+ • {bufnr} (integer|nil) Buffer the parser should be tied to (default:
current buffer)
• {lang} (string|nil) Filetype of this parser (default: buffer
filetype)
• {opts} (table|nil) Options to pass to the created language tree
Return: ~
- LanguageTree |LanguageTree| object to use for parsing
+ |LanguageTree| object to use for parsing
+
+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: ~
+ • {node} |TSNode|
+ • {source} integer|string|nil Buffer or string from which the {node}
+ is extracted
+ • {metadata} TSMetadata|nil
+
+ Return: ~
+ (table)
*vim.treesitter.get_string_parser()*
get_string_parser({str}, {lang}, {opts})
@@ -568,14 +688,42 @@ get_string_parser({str}, {lang}, {opts})
• {opts} (table|nil) Options to pass to the created language tree
Return: ~
- LanguageTree |LanguageTree| object to use for parsing
+ |LanguageTree| object to use for parsing
+
+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"
+ to toggle the display of the source language of each node, "o" to toggle
+ the query editor, and press <Enter> to jump to the node under the cursor
+ in the source buffer.
+
+ Can also be shown with `:InspectTree`. *:InspectTree*
+
+ Parameters: ~
+ • {opts} (table|nil) Optional options table with the following possible
+ keys:
+ • lang (string|nil): The language of the source buffer. If
+ omitted, the filetype of the source buffer is used.
+ • 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
+ window. Default value is "60vnew". Only used when {winid} is
+ nil.
+ • 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.
is_ancestor({dest}, {source}) *vim.treesitter.is_ancestor()*
Determines whether a node is the ancestor of another
Parameters: ~
- • {dest} userdata Possible ancestor |tsnode|
- • {source} userdata Possible descendant |tsnode|
+ • {dest} |TSNode| Possible ancestor
+ • {source} |TSNode| Possible descendant
Return: ~
(boolean) True if {dest} is an ancestor of {source}
@@ -585,9 +733,9 @@ is_in_node_range({node}, {line}, {col})
Determines whether (line, col) position is in node range
Parameters: ~
- • {node} userdata |tsnode| defining the range
- • {line} (number) Line (0-based)
- • {col} (number) Column (0-based)
+ • {node} |TSNode| defining the range
+ • {line} (integer) Line (0-based)
+ • {col} (integer) Column (0-based)
Return: ~
(boolean) True if the position is in node range
@@ -596,37 +744,12 @@ node_contains({node}, {range}) *vim.treesitter.node_contains()*
Determines if a node contains a range
Parameters: ~
- • {node} userdata |tsnode|
+ • {node} |TSNode|
• {range} (table)
Return: ~
(boolean) True if the {node} contains the {range}
-show_tree({opts}) *vim.treesitter.show_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"
- to toggle the display of the source language of each node, and press
- <Enter> to jump to the node under the cursor in the source buffer.
-
- Parameters: ~
- • {opts} (table|nil) Optional options table with the following possible
- keys:
- • lang (string|nil): The language of the source buffer. If
- omitted, the filetype of the source buffer is used.
- • bufnr (number|nil): Buffer to draw the tree into. If
- omitted, a new buffer is created.
- • winid (number|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
- window. Default value is "topleft 60vnew". Only used when
- {winid} is nil.
- • title (string|fun(bufnr:number):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.
-
start({bufnr}, {lang}) *vim.treesitter.start()*
Starts treesitter highlighting for a buffer
@@ -637,17 +760,16 @@ start({bufnr}, {lang}) *vim.treesitter.start()*
the call to `start`.
Example: >lua
-
- 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
- })
+ 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
+ })
<
Parameters: ~
- • {bufnr} (number|nil) Buffer to be highlighted (default: current
+ • {bufnr} (integer|nil) Buffer to be highlighted (default: current
buffer)
• {lang} (string|nil) Language of the parser (default: buffer
filetype)
@@ -656,14 +778,45 @@ stop({bufnr}) *vim.treesitter.stop()*
Stops treesitter highlighting for a buffer
Parameters: ~
- • {bufnr} (number|nil) Buffer to stop highlighting (default: current
+ • {bufnr} (integer|nil) Buffer to stop highlighting (default: current
buffer)
==============================================================================
Lua module: vim.treesitter.language *lua-treesitter-language*
-inspect_language({lang}) *vim.treesitter.language.inspect_language()*
+add({lang}, {opts}) *vim.treesitter.language.add()*
+ Load parser with name {lang}
+
+ Parsers are searched in the `parser` runtime directory, or the provided
+ {path}
+
+ Parameters: ~
+ • {lang} (string) Name of the parser (alphanumerical and `_` only)
+ • {opts} (table|nil) Options:
+ • filetype (string|string[]) Default filetype the parser
+ should be associated with. Defaults to {lang}.
+ • path (string|nil) Optional path the parser is located at
+ • symbol_name (string|nil) Internal symbol name for the
+ language to load
+
+get_filetypes({lang}) *vim.treesitter.language.get_filetypes()*
+ Get the filetypes associated with the parser named {lang}.
+
+ Parameters: ~
+ • {lang} (string) Name of parser
+
+ Return: ~
+ string[] filetypes
+
+get_lang({filetype}) *vim.treesitter.language.get_lang()*
+ Parameters: ~
+ • {filetype} (string)
+
+ Return: ~
+ (string|nil)
+
+inspect({lang}) *vim.treesitter.language.inspect()*