vim.treesitter
Methods17
function M._create_parser(buf: integer, lang: string, opts: nil | table) -> object vim.treesitter.LanguageTree
Creates a new parser
It is not recommended to use this; use |get_parser()| instead.
bufintegerBuffer the parser will be tied to (0 for current buffer)
langstringLanguage of the parser
optsnil | tableOptions to pass to the created language tree
objectvim.treesitter.LanguageTreeto use for parsing
function M.get_parser(buf: nil | integer, lang: nil | string, opts: nil | table) -> (object vim.treesitter.LanguageTree | nil, error nil | string)
Returns the parser for a specific buffer and attaches it to the buffer
If needed, this will create the parser.
If no parser can be created, nil (and an error message) is returned.
bufnil | integer(default: current buffer) Buffer the parser should be tied to
langnil | string(default: from 'filetype') Language of this parser
optsnil | tableOptions to pass to the created language tree
objectvim.treesitter.LanguageTree | nilto use for parsing
errornil | stringmessage, if applicable
function M.get_string_parser(str: string, lang: string, opts: nil | table) -> object vim.treesitter.LanguageTree
Returns a string parser
strstringText to parse
langstringLanguage of this string
optsnil | tableOptions to pass to the created language tree
objectvim.treesitter.LanguageTreeto use for parsing
function M.get_range(node: TSNode, source: nil | string | integer, metadata: vim.treesitter.query.TSMetadata | nil) -> Range6
Get the range of a |TSNode|. Can also supply {source} and {metadata} to get the range with directives applied.
nodeTSNodesourcenil | string | integerBuffer or string from which the {node} is extracted
metadatavim.treesitter.query.TSMetadata | nil
function M.get_node_text(node: TSNode, source: string | integer, opts: nil | table) -> string
Gets the text corresponding to a given node
nodeTSNodesourcestring | integerBuffer or string from which the {node} is extracted
optsnil | tableOptional parameters.
- metadata (table) Metadata of a specific capture. This would be
set to
metadata[capture_id]when using |vim.treesitter.query.add_directive()|.
- string
function M.is_in_node_range(node: TSNode, line: integer, col: integer) -> True boolean
Determines whether (line, col) position is in node range
nodeTSNodedefining the range
lineintegerLine (0-based)
colintegerColumn (0-based)
Truebooleanif the position is in node range
function M.node_contains(node: TSNode, range: table) -> True boolean
Determines if a node contains a range
nodeTSNoderangetable
Truebooleanif the {node} contains the {range}
function M.get_captures_at_pos(buf: integer, row: integer, col: integer) -> { capture: string, id: integer, lang: string, metadata: vim.treesitter.query.TSMetadata, pattern_id: integer }[]
Returns a list of highlight captures at the given position
Each capture is represented by a table containing the capture name as a string, the capture's language, a table of metadata (priority, conceal, ...; empty if none are defined), the id of the capture, and the (0-indexed) id of the matched pattern in the query.
bufintegerBuffer number (0 for current buffer)
rowintegerPosition row
colintegerPosition column
- { capture: string, id: integer, lang: string, metadata: vim.treesitter.query.TSMetadata, pattern_id: integer }[]
function M.get_captures_at_cursor(win: nil | integer) -> List string[]
Returns a list of highlight capture names under the cursor
winnil | integer|window-ID| or 0 for current window (default)
Liststring[]of capture names
function M.get_node(opts: vim.treesitter.get_node.Opts | nil) -> Node TSNode | nil
Returns the smallest named node at the given position
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
vim.treesitter.get_parser(bufnr):parse(range)
optsvim.treesitter.get_node.Opts | nil
NodeTSNode | nilat the given position
function M.start(buf: nil | integer, lang: nil | string) -> nil
Starts treesitter highlighting for a buffer
Can be used in an ftplugin or FileType autocommand.
Note: By default, disables regex syntax highlighting, which may be required for some plugins. In this case, add vim.bo.syntax = 'ON' after the call to start.
Note: By default, the highlighter parses code asynchronously, using a segment time of 3ms.
Example:
vim.api.nvim_create_autocmd( 'FileType', { pattern = 'tex',
callback = function(ev)
vim.treesitter.start(ev.buf, 'latex')
vim.bo[ev.buf].syntax = 'ON' -- only if additional legacy syntax is needed
end
})
bufnil | integer(default: current buffer) Buffer to be highlighted
langnil | string(default: from 'filetype') Language of the parser
- nil
function M.stop(buf: nil | integer) -> nil
Stops treesitter highlighting for a buffer
bufnil | integer(default: current buffer) Buffer to stop highlighting
- nil
function M.inspect_tree(opts: nil | table) -> nil
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. Folding also works (try |zo|, |zc|, etc.).
Can also be shown with :InspectTree. :InspectTree
optsnil | tableOptional options table with the following possible keys:
- bufnr (integer|nil): Buffer to draw the tree into. If omitted, a new
- command (string|nil): Vimscript command to create the window. Default
- lang (string|nil): The language of the source buffer. If omitted, detect
- title (string|fun(bufnr:integer):string|nil): Title of the window. If a
- winid (integer|nil): Window id to display the tree buffer in. If omitted,
buffer is created.
value is "60vnew". Only used when {winid} is nil.
from the filetype of the source buffer.
function, it accepts the buffer number of the source buffer as its only argument and should return a string.
a new window is created with {command}.
- nil
@since 11
function M.foldexpr(lnum: nil | integer) -> string
Returns the fold level for {lnum} in the current buffer. Can be set directly to 'foldexpr':
vim.wo.foldexpr = vim.treesitter.foldexpr
lnumnil | integerLine number to calculate fold level for
- string
@since 11
function M.select(target: "parent" | "child" | "next" | "prev" | "extend_next" | "extend_prev", count: nil | integer) -> nil
Starts or adjusts a |Visual| selection at cursor, based on tree nodes. The target parameter decides the selection behavior.
target"parent" | "child" | "next" | "prev" | "extend_next" | "extend_prev"Decides the selection behavior.
countnil | integer(default: 1) Expand or adjust the selection this many times.
- nil
Fields11
M._fold : vim.treesitter._fold
M._query_linter : vim.treesitter._query_linter
M._range : vim.treesitter._range
M.dev : vim.treesitter.dev
M.highlighter : vim.treesitter.highlighter
M.language : vim.treesitter.language
M.languagetree : vim.treesitter.languagetree
M.query : vim.treesitter.query
M._select : vim.treesitter._select
M.language_version : integer
@nodoc
M.minimum_language_version : integer
@nodoc