2023-04-05 09:19:53 -07:00
|
|
|
local api = vim.api
|
2020-11-04 10:03:36 -07:00
|
|
|
local LanguageTree = require('vim.treesitter.languagetree')
|
2023-03-06 03:57:14 -07:00
|
|
|
local Range = require('vim.treesitter._range')
|
2019-06-07 05:21:00 -07:00
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
---@type table<integer,LanguageTree>
|
2022-09-06 23:39:56 -07:00
|
|
|
local parsers = setmetatable({}, { __mode = 'v' })
|
2019-06-17 12:46:31 -07:00
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
---@class TreesitterModule
|
|
|
|
---@field highlighter TSHighlighter
|
2023-03-24 07:43:14 -07:00
|
|
|
---@field query TSQueryModule
|
|
|
|
---@field language TSLanguageModule
|
|
|
|
local M = setmetatable({}, {
|
2019-09-28 05:27:20 -07:00
|
|
|
__index = function(t, k)
|
2023-02-04 07:58:38 -07:00
|
|
|
---@diagnostic disable:no-unknown
|
2021-03-15 17:41:28 -07:00
|
|
|
if k == 'highlighter' then
|
2020-07-14 12:50:57 -07:00
|
|
|
t[k] = require('vim.treesitter.highlighter')
|
2019-09-28 05:27:20 -07:00
|
|
|
return t[k]
|
2021-04-01 07:50:31 -07:00
|
|
|
elseif k == 'language' then
|
|
|
|
t[k] = require('vim.treesitter.language')
|
|
|
|
return t[k]
|
2021-08-23 06:34:08 -07:00
|
|
|
elseif k == 'query' then
|
|
|
|
t[k] = require('vim.treesitter.query')
|
|
|
|
return t[k]
|
2019-09-28 05:27:20 -07:00
|
|
|
end
|
2023-03-24 07:43:14 -07:00
|
|
|
|
|
|
|
local query = require('vim.treesitter.query')
|
|
|
|
if query[k] then
|
|
|
|
vim.deprecate('vim.treesitter.' .. k .. '()', 'vim.treesitter.query.' .. k .. '()', '0.10')
|
|
|
|
t[k] = query[k]
|
|
|
|
return t[k]
|
|
|
|
end
|
|
|
|
|
|
|
|
local language = require('vim.treesitter.language')
|
|
|
|
if language[k] then
|
|
|
|
vim.deprecate('vim.treesitter.' .. k .. '()', 'vim.treesitter.language.' .. k .. '()', '0.10')
|
|
|
|
t[k] = language[k]
|
|
|
|
return t[k]
|
|
|
|
end
|
2019-09-28 05:27:20 -07:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2023-07-18 07:42:30 -07:00
|
|
|
--- @nodoc
|
2023-03-24 07:43:14 -07:00
|
|
|
M.language_version = vim._ts_get_language_version()
|
2023-07-18 07:42:30 -07:00
|
|
|
|
|
|
|
--- @nodoc
|
2023-03-24 07:43:14 -07:00
|
|
|
M.minimum_language_version = vim._ts_get_minimum_language_version()
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
--- Creates a new parser
|
2021-05-01 05:19:48 -07:00
|
|
|
---
|
2022-09-14 02:08:31 -07:00
|
|
|
--- It is not recommended to use this; use |get_parser()| instead.
|
2021-05-01 05:19:48 -07:00
|
|
|
---
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param bufnr integer Buffer the parser will be tied to (0 for current buffer)
|
2022-09-05 06:52:27 -07:00
|
|
|
---@param lang string Language of the parser
|
2022-09-14 02:08:31 -07:00
|
|
|
---@param opts (table|nil) Options to pass to the created language tree
|
2022-09-05 06:52:27 -07:00
|
|
|
---
|
2023-02-04 07:58:38 -07:00
|
|
|
---@return LanguageTree object to use for parsing
|
2020-11-04 10:03:36 -07:00
|
|
|
function M._create_parser(bufnr, lang, opts)
|
2019-06-07 05:21:00 -07:00
|
|
|
if bufnr == 0 then
|
2023-02-04 07:58:38 -07:00
|
|
|
bufnr = vim.api.nvim_get_current_buf()
|
2019-06-07 05:21:00 -07:00
|
|
|
end
|
2020-07-10 06:33:27 -07:00
|
|
|
|
|
|
|
vim.fn.bufload(bufnr)
|
|
|
|
|
2023-02-21 10:09:18 -07:00
|
|
|
local self = LanguageTree.new(bufnr, lang, opts)
|
2023-02-04 07:58:38 -07:00
|
|
|
|
2020-03-21 11:55:19 -07:00
|
|
|
local function bytes_cb(_, ...)
|
2020-11-04 10:03:36 -07:00
|
|
|
self:_on_bytes(...)
|
2019-06-07 05:21:00 -07:00
|
|
|
end
|
2020-11-04 10:03:36 -07:00
|
|
|
|
2021-02-06 02:17:40 -07:00
|
|
|
local function detach_cb(_, ...)
|
2020-11-04 10:03:36 -07:00
|
|
|
if parsers[bufnr] == self then
|
|
|
|
parsers[bufnr] = nil
|
2019-06-17 12:46:31 -07:00
|
|
|
end
|
2021-02-06 02:17:40 -07:00
|
|
|
self:_on_detach(...)
|
2019-06-17 12:46:31 -07:00
|
|
|
end
|
2020-11-04 10:03:36 -07:00
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
local function reload_cb(_)
|
|
|
|
self:_on_reload()
|
2021-02-06 23:32:19 -07:00
|
|
|
end
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
local source = self:source() --[[@as integer]]
|
|
|
|
|
2023-04-05 09:19:53 -07:00
|
|
|
api.nvim_buf_attach(
|
2023-02-04 07:58:38 -07:00
|
|
|
source,
|
2021-02-06 23:32:19 -07:00
|
|
|
false,
|
|
|
|
{ on_bytes = bytes_cb, on_detach = detach_cb, on_reload = reload_cb, preview = true }
|
|
|
|
)
|
2020-11-04 10:03:36 -07:00
|
|
|
|
|
|
|
self:parse()
|
|
|
|
|
2019-06-07 09:19:59 -07:00
|
|
|
return self
|
2019-06-07 05:21:00 -07:00
|
|
|
end
|
|
|
|
|
2023-03-30 02:26:28 -07:00
|
|
|
local function valid_lang(lang)
|
|
|
|
return lang and lang ~= ''
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Returns the parser for a specific buffer and attaches it to the buffer
|
2021-05-01 05:19:48 -07:00
|
|
|
---
|
2022-09-14 02:08:31 -07:00
|
|
|
--- If needed, this will create the parser.
|
2021-05-01 05:19:48 -07:00
|
|
|
---
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param bufnr (integer|nil) Buffer the parser should be tied to (default: current buffer)
|
2022-09-14 02:08:31 -07:00
|
|
|
---@param lang (string|nil) Filetype of this parser (default: buffer filetype)
|
|
|
|
---@param opts (table|nil) Options to pass to the created language tree
|
2021-05-01 05:19:48 -07:00
|
|
|
---
|
2023-02-04 07:58:38 -07:00
|
|
|
---@return LanguageTree object to use for parsing
|
2020-11-04 10:03:36 -07:00
|
|
|
function M.get_parser(bufnr, lang, opts)
|
|
|
|
opts = opts or {}
|
|
|
|
|
2019-06-17 12:46:31 -07:00
|
|
|
if bufnr == nil or bufnr == 0 then
|
2023-04-05 09:19:53 -07:00
|
|
|
bufnr = api.nvim_get_current_buf()
|
2019-06-17 12:46:31 -07:00
|
|
|
end
|
2023-03-10 03:25:10 -07:00
|
|
|
|
2023-03-30 02:26:28 -07:00
|
|
|
if not valid_lang(lang) then
|
|
|
|
lang = M.language.get_lang(vim.bo[bufnr].filetype) or vim.bo[bufnr].filetype
|
|
|
|
end
|
|
|
|
|
|
|
|
if not valid_lang(lang) then
|
|
|
|
if not parsers[bufnr] then
|
2023-03-10 03:25:10 -07:00
|
|
|
error(
|
|
|
|
string.format(
|
|
|
|
'There is no parser available for buffer %d and one could not be'
|
|
|
|
.. ' created because lang could not be determined. Either pass lang'
|
|
|
|
.. ' or set the buffer filetype',
|
|
|
|
bufnr
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
2023-03-30 02:26:28 -07:00
|
|
|
elseif parsers[bufnr] == nil or parsers[bufnr]:lang() ~= lang then
|
2023-09-20 19:03:40 -07:00
|
|
|
assert(lang, 'lang should be valid')
|
2020-11-04 10:03:36 -07:00
|
|
|
parsers[bufnr] = M._create_parser(bufnr, lang, opts)
|
2019-09-28 05:27:20 -07:00
|
|
|
end
|
2020-07-08 13:47:57 -07:00
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
parsers[bufnr]:register_cbs(opts.buf_attach_cbs)
|
2020-07-08 13:47:57 -07:00
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
return parsers[bufnr]
|
2019-06-17 12:46:31 -07:00
|
|
|
end
|
2019-06-07 09:19:59 -07:00
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
--- Returns a string parser
|
2021-05-01 05:19:48 -07:00
|
|
|
---
|
2022-09-14 02:08:31 -07:00
|
|
|
---@param str string Text to parse
|
|
|
|
---@param lang string Language of this string
|
|
|
|
---@param opts (table|nil) Options to pass to the created language tree
|
|
|
|
---
|
2023-02-04 07:58:38 -07:00
|
|
|
---@return LanguageTree object to use for parsing
|
2020-11-04 10:03:36 -07:00
|
|
|
function M.get_string_parser(str, lang, opts)
|
2020-09-30 06:32:43 -07:00
|
|
|
vim.validate({
|
|
|
|
str = { str, 'string' },
|
|
|
|
lang = { lang, 'string' },
|
|
|
|
})
|
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
return LanguageTree.new(str, lang, opts)
|
2020-09-30 06:32:43 -07:00
|
|
|
end
|
|
|
|
|
2022-04-22 12:50:52 -07:00
|
|
|
--- Determines whether a node is the ancestor of another
|
|
|
|
---
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param dest TSNode Possible ancestor
|
|
|
|
---@param source TSNode Possible descendant
|
2022-04-22 12:50:52 -07:00
|
|
|
---
|
2022-09-14 02:08:31 -07:00
|
|
|
---@return boolean True if {dest} is an ancestor of {source}
|
2022-04-22 12:50:52 -07:00
|
|
|
function M.is_ancestor(dest, source)
|
|
|
|
if not (dest and source) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-09-20 19:03:40 -07:00
|
|
|
local current = source ---@type TSNode?
|
2022-04-22 12:50:52 -07:00
|
|
|
while current ~= nil do
|
|
|
|
if current == dest then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
current = current:parent()
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
--- Returns the node's range or an unpacked range table
|
2022-07-24 11:49:33 -07:00
|
|
|
---
|
2023-02-21 09:03:04 -07:00
|
|
|
---@param node_or_range (TSNode | table) Node or table of positions
|
2022-07-24 11:49:33 -07:00
|
|
|
---
|
2023-02-04 07:58:38 -07:00
|
|
|
---@return integer start_row
|
|
|
|
---@return integer start_col
|
|
|
|
---@return integer end_row
|
|
|
|
---@return integer end_col
|
2022-07-24 11:49:33 -07:00
|
|
|
function M.get_node_range(node_or_range)
|
|
|
|
if type(node_or_range) == 'table' then
|
|
|
|
return unpack(node_or_range)
|
|
|
|
else
|
|
|
|
return node_or_range:range()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-24 07:43:14 -07:00
|
|
|
---Get the range of a |TSNode|. Can also supply {source} and {metadata}
|
|
|
|
---to get the range with directives applied.
|
|
|
|
---@param node TSNode
|
|
|
|
---@param source integer|string|nil Buffer or string from which the {node} is extracted
|
|
|
|
---@param metadata TSMetadata|nil
|
|
|
|
---@return Range6
|
|
|
|
function M.get_range(node, source, metadata)
|
|
|
|
if metadata and metadata.range then
|
|
|
|
assert(source)
|
|
|
|
return Range.add_bytes(source, metadata.range)
|
|
|
|
end
|
|
|
|
return { node:range(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param buf integer
|
|
|
|
---@param range Range
|
|
|
|
---@returns string
|
|
|
|
local function buf_range_get_text(buf, range)
|
|
|
|
local start_row, start_col, end_row, end_col = Range.unpack4(range)
|
|
|
|
if end_col == 0 then
|
|
|
|
if start_row == end_row then
|
|
|
|
start_col = -1
|
|
|
|
start_row = start_row - 1
|
|
|
|
end
|
|
|
|
end_col = -1
|
|
|
|
end_row = end_row - 1
|
|
|
|
end
|
2023-04-05 09:19:53 -07:00
|
|
|
local lines = api.nvim_buf_get_text(buf, start_row, start_col, end_row, end_col, {})
|
2023-03-24 07:43:14 -07:00
|
|
|
return table.concat(lines, '\n')
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Gets the text corresponding to a given node
|
|
|
|
---
|
|
|
|
---@param node TSNode
|
|
|
|
---@param source (integer|string) Buffer or string from which the {node} is extracted
|
|
|
|
---@param 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 string
|
|
|
|
function M.get_node_text(node, source, opts)
|
|
|
|
opts = opts or {}
|
|
|
|
local metadata = opts.metadata or {}
|
|
|
|
|
|
|
|
if metadata.text then
|
|
|
|
return metadata.text
|
|
|
|
elseif type(source) == 'number' then
|
|
|
|
local range = vim.treesitter.get_range(node, source, metadata)
|
|
|
|
return buf_range_get_text(source, range)
|
|
|
|
end
|
|
|
|
|
|
|
|
---@cast source string
|
|
|
|
return source:sub(select(3, node:start()) + 1, select(3, node:end_()))
|
|
|
|
end
|
|
|
|
|
2022-09-16 00:05:05 -07:00
|
|
|
--- Determines whether (line, col) position is in node range
|
2022-08-25 12:41:52 -07:00
|
|
|
---
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param node TSNode defining the range
|
|
|
|
---@param line integer Line (0-based)
|
|
|
|
---@param col integer Column (0-based)
|
2022-09-05 06:52:27 -07:00
|
|
|
---
|
2022-09-14 02:08:31 -07:00
|
|
|
---@return boolean True if the position is in node range
|
2022-08-25 12:41:52 -07:00
|
|
|
function M.is_in_node_range(node, line, col)
|
2023-03-08 16:45:43 -07:00
|
|
|
return M.node_contains(node, { line, col, line, col + 1 })
|
2022-08-25 12:41:52 -07:00
|
|
|
end
|
|
|
|
|
2022-09-16 00:05:05 -07:00
|
|
|
--- Determines if a node contains a range
|
|
|
|
---
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param node TSNode
|
2022-09-05 06:52:27 -07:00
|
|
|
---@param range table
|
2022-06-02 09:13:05 -07:00
|
|
|
---
|
2022-09-14 02:08:31 -07:00
|
|
|
---@return boolean True if the {node} contains the {range}
|
2022-06-02 09:13:05 -07:00
|
|
|
function M.node_contains(node, range)
|
2023-03-06 03:57:14 -07:00
|
|
|
vim.validate({
|
2023-03-08 16:45:43 -07:00
|
|
|
-- allow a table so nodes can be mocked
|
|
|
|
node = { node, { 'userdata', 'table' } },
|
2023-03-06 03:57:14 -07:00
|
|
|
range = { range, Range.validate, 'integer list with 4 or 6 elements' },
|
|
|
|
})
|
|
|
|
return Range.contains({ node:range() }, range)
|
2022-06-02 09:13:05 -07:00
|
|
|
end
|
|
|
|
|
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-06-02 11:07:08 -07:00
|
|
|
---
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param bufnr integer Buffer number (0 for current buffer)
|
|
|
|
---@param row integer Position row
|
|
|
|
---@param col integer Position column
|
2022-09-05 23:50:06 -07:00
|
|
|
---
|
2023-02-21 09:39:29 -07:00
|
|
|
---@return table[] List of captures `{ capture = "name", metadata = { ... } }`
|
2022-09-24 15:45:15 -07:00
|
|
|
function M.get_captures_at_pos(bufnr, row, col)
|
2022-08-25 12:41:52 -07:00
|
|
|
if bufnr == 0 then
|
2023-04-05 09:19:53 -07:00
|
|
|
bufnr = api.nvim_get_current_buf()
|
2022-08-25 12:41:52 -07:00
|
|
|
end
|
|
|
|
local buf_highlighter = M.highlighter.active[bufnr]
|
2022-06-02 11:07:08 -07:00
|
|
|
|
|
|
|
if not buf_highlighter then
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
|
|
|
|
local matches = {}
|
|
|
|
|
|
|
|
buf_highlighter.tree:for_each_tree(function(tstree, tree)
|
|
|
|
if not tstree then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local root = tstree:root()
|
|
|
|
local root_start_row, _, root_end_row, _ = root:range()
|
|
|
|
|
|
|
|
-- Only worry about trees within the line range
|
|
|
|
if root_start_row > row or root_end_row < row then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local q = buf_highlighter:get_query(tree:lang())
|
|
|
|
|
|
|
|
-- Some injected languages may not have highlight queries.
|
|
|
|
if not q:query() then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local iter = q:query():iter_captures(root, buf_highlighter.bufnr, row, row + 1)
|
|
|
|
|
|
|
|
for capture, node, metadata in iter do
|
2022-08-25 12:41:52 -07:00
|
|
|
if M.is_in_node_range(node, row, col) then
|
2022-06-02 11:07:08 -07:00
|
|
|
local c = q._query.captures[capture] -- name of the capture in the query
|
|
|
|
if c ~= nil then
|
2022-12-14 02:46:54 -07:00
|
|
|
table.insert(matches, { capture = c, metadata = metadata, lang = tree:lang() })
|
2022-06-02 11:07:08 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-03-23 04:23:51 -07:00
|
|
|
end)
|
2022-06-02 11:07:08 -07:00
|
|
|
return matches
|
|
|
|
end
|
|
|
|
|
2022-09-16 00:05:05 -07:00
|
|
|
--- Returns a list of highlight capture names under the cursor
|
2022-04-30 02:51:14 -07:00
|
|
|
---
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param winnr (integer|nil) Window handle or 0 for current window (default)
|
2022-04-30 02:51:14 -07:00
|
|
|
---
|
2022-09-14 02:08:31 -07:00
|
|
|
---@return string[] List of capture names
|
2022-09-05 23:50:06 -07:00
|
|
|
function M.get_captures_at_cursor(winnr)
|
2022-04-30 02:51:14 -07:00
|
|
|
winnr = winnr or 0
|
2023-04-05 09:19:53 -07:00
|
|
|
local bufnr = api.nvim_win_get_buf(winnr)
|
|
|
|
local cursor = api.nvim_win_get_cursor(winnr)
|
2022-04-30 02:51:14 -07:00
|
|
|
|
2022-09-24 15:45:15 -07:00
|
|
|
local data = M.get_captures_at_pos(bufnr, cursor[1] - 1, cursor[2])
|
2022-09-05 23:50:06 -07:00
|
|
|
|
|
|
|
local captures = {}
|
|
|
|
|
|
|
|
for _, capture in ipairs(data) do
|
|
|
|
table.insert(captures, capture.capture)
|
|
|
|
end
|
|
|
|
|
|
|
|
return captures
|
|
|
|
end
|
|
|
|
|
2023-02-22 08:01:08 -07:00
|
|
|
--- Returns the smallest named node at the given position
|
|
|
|
---
|
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)
|
|
|
|
--- ```
|
|
|
|
---
|
2023-02-22 08:01:08 -07:00
|
|
|
---@param 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 TSNode | nil Node at the given position
|
|
|
|
function M.get_node(opts)
|
|
|
|
opts = opts or {}
|
|
|
|
|
|
|
|
local bufnr = opts.bufnr
|
|
|
|
|
|
|
|
if not bufnr or bufnr == 0 then
|
2023-04-05 09:19:53 -07:00
|
|
|
bufnr = api.nvim_get_current_buf()
|
2023-02-22 08:01:08 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local row, col
|
|
|
|
if opts.pos then
|
|
|
|
assert(#opts.pos == 2, 'Position must be a (row, col) tuple')
|
|
|
|
row, col = opts.pos[1], opts.pos[2]
|
|
|
|
else
|
|
|
|
assert(
|
2023-04-05 09:19:53 -07:00
|
|
|
bufnr == api.nvim_get_current_buf(),
|
2023-02-22 08:01:08 -07:00
|
|
|
'Position must be explicitly provided when not using the current buffer'
|
|
|
|
)
|
2023-04-05 09:19:53 -07:00
|
|
|
local pos = api.nvim_win_get_cursor(0)
|
2023-02-22 08:01:08 -07:00
|
|
|
-- Subtract one to account for 1-based row indexing in nvim_win_get_cursor
|
|
|
|
row, col = pos[1] - 1, pos[2]
|
|
|
|
end
|
|
|
|
|
|
|
|
assert(row >= 0 and col >= 0, 'Invalid position: row and col must be non-negative')
|
|
|
|
|
|
|
|
local ts_range = { row, col, row, col }
|
|
|
|
|
|
|
|
local root_lang_tree = M.get_parser(bufnr)
|
|
|
|
if not root_lang_tree then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
return root_lang_tree:named_node_for_range(ts_range, opts)
|
|
|
|
end
|
|
|
|
|
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-02-04 07:58:38 -07:00
|
|
|
---@param bufnr integer Buffer number (0 for current buffer)
|
|
|
|
---@param row integer Position row
|
|
|
|
---@param col integer Position column
|
2022-09-05 23:50:06 -07:00
|
|
|
---@param opts table Optional keyword arguments:
|
2022-12-08 09:51:46 -07:00
|
|
|
--- - lang string|nil Parser language
|
2022-09-05 23:50:06 -07:00
|
|
|
--- - ignore_injections boolean Ignore injected languages (default true)
|
|
|
|
---
|
2023-02-21 09:03:04 -07:00
|
|
|
---@return TSNode | nil Node at the given position
|
2023-02-22 08:01:08 -07:00
|
|
|
---@deprecated
|
2022-09-24 15:45:15 -07:00
|
|
|
function M.get_node_at_pos(bufnr, row, col, opts)
|
2023-02-22 08:01:08 -07:00
|
|
|
vim.deprecate('vim.treesitter.get_node_at_pos()', 'vim.treesitter.get_node()', '0.10')
|
2022-09-05 23:50:06 -07:00
|
|
|
if bufnr == 0 then
|
2023-04-05 09:19:53 -07:00
|
|
|
bufnr = api.nvim_get_current_buf()
|
2022-09-05 23:50:06 -07:00
|
|
|
end
|
|
|
|
local ts_range = { row, col, row, col }
|
|
|
|
|
2023-02-22 08:01:08 -07:00
|
|
|
opts = opts or {}
|
|
|
|
|
2022-12-08 09:51:46 -07:00
|
|
|
local root_lang_tree = M.get_parser(bufnr, opts.lang)
|
2022-04-30 02:51:14 -07:00
|
|
|
if not root_lang_tree then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-09-05 23:50:06 -07:00
|
|
|
return root_lang_tree:named_node_for_range(ts_range, opts)
|
|
|
|
end
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
--- Returns the smallest named node under the cursor
|
2022-09-05 23:50:06 -07:00
|
|
|
---
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param winnr (integer|nil) Window handle or 0 for current window (default)
|
2022-09-05 23:50:06 -07:00
|
|
|
---
|
2022-09-14 02:08:31 -07:00
|
|
|
---@return string Name of node under the cursor
|
2023-02-22 08:01:08 -07:00
|
|
|
---@deprecated
|
2022-09-05 23:50:06 -07:00
|
|
|
function M.get_node_at_cursor(winnr)
|
2023-02-22 08:01:08 -07:00
|
|
|
vim.deprecate('vim.treesitter.get_node_at_cursor()', 'vim.treesitter.get_node():type()', '0.10')
|
2022-09-05 23:50:06 -07:00
|
|
|
winnr = winnr or 0
|
2023-04-05 09:19:53 -07:00
|
|
|
local bufnr = api.nvim_win_get_buf(winnr)
|
2022-09-05 23:50:06 -07:00
|
|
|
|
2023-02-22 08:01:08 -07:00
|
|
|
return M.get_node({ bufnr = bufnr, ignore_injections = false }):type()
|
2022-04-30 02:51:14 -07:00
|
|
|
end
|
|
|
|
|
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 required for some plugins.
|
2022-09-07 21:54:41 -07:00
|
|
|
--- In this case, add ``vim.bo.syntax = 'on'`` after the call to `start`.
|
2022-09-05 06:52:27 -07:00
|
|
|
---
|
|
|
|
--- Example:
|
2023-09-14 06:23:01 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2022-09-05 06:52:27 -07:00
|
|
|
--- vim.api.nvim_create_autocmd( 'FileType', { pattern = 'tex',
|
|
|
|
--- callback = function(args)
|
2022-09-06 08:33:44 -07:00
|
|
|
--- vim.treesitter.start(args.buf, 'latex')
|
|
|
|
--- vim.bo[args.buf].syntax = 'on' -- only if additional legacy syntax is needed
|
2022-09-05 06:52:27 -07:00
|
|
|
--- end
|
|
|
|
--- })
|
2023-09-14 06:23:01 -07:00
|
|
|
--- ```
|
2022-09-05 06:52:27 -07:00
|
|
|
---
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param bufnr (integer|nil) Buffer to be highlighted (default: current buffer)
|
2022-09-14 02:08:31 -07:00
|
|
|
---@param lang (string|nil) Language of the parser (default: buffer filetype)
|
2022-09-06 08:33:44 -07:00
|
|
|
function M.start(bufnr, lang)
|
2023-04-05 09:19:53 -07:00
|
|
|
bufnr = bufnr or api.nvim_get_current_buf()
|
2022-09-05 06:52:27 -07:00
|
|
|
local parser = M.get_parser(bufnr, lang)
|
|
|
|
M.highlighter.new(parser)
|
|
|
|
end
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
--- Stops treesitter highlighting for a buffer
|
2022-09-05 06:52:27 -07:00
|
|
|
---
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param bufnr (integer|nil) Buffer to stop highlighting (default: current buffer)
|
2022-09-05 06:52:27 -07:00
|
|
|
function M.stop(bufnr)
|
2023-07-24 07:26:38 -07:00
|
|
|
bufnr = (bufnr and bufnr ~= 0) and bufnr or api.nvim_get_current_buf()
|
2022-09-05 06:52:27 -07:00
|
|
|
|
|
|
|
if M.highlighter.active[bufnr] then
|
|
|
|
M.highlighter.active[bufnr]:destroy()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-08 09:22:57 -07:00
|
|
|
--- 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
|
2023-09-15 03:10:55 -07:00
|
|
|
--- display of the source language of each node, "o" to toggle the query editor, and press
|
2023-08-25 11:17:36 -07:00
|
|
|
--- <Enter> to jump to the node under the cursor in the source buffer.
|
2022-12-08 09:22:57 -07:00
|
|
|
---
|
2023-03-02 10:03:11 -07:00
|
|
|
--- Can also be shown with `:InspectTree`. *:InspectTree*
|
|
|
|
---
|
2022-12-08 09:22:57 -07:00
|
|
|
---@param opts table|nil Optional options table with the following possible keys:
|
2022-12-08 09:51:46 -07:00
|
|
|
--- - lang (string|nil): The language of the source buffer. If omitted, the
|
|
|
|
--- filetype of the source buffer is used.
|
2023-02-04 07:58:38 -07:00
|
|
|
--- - bufnr (integer|nil): Buffer to draw the tree into. If omitted, a new
|
2022-12-08 09:22:57 -07:00
|
|
|
--- buffer is created.
|
2023-02-04 07:58:38 -07:00
|
|
|
--- - winid (integer|nil): Window id to display the tree buffer in. If omitted,
|
2022-12-08 09:22:57 -07:00
|
|
|
--- a new window is created with {command}.
|
|
|
|
--- - command (string|nil): Vimscript command to create the window. Default
|
2023-03-17 04:41:57 -07:00
|
|
|
--- value is "60vnew". Only used when {winid} is nil.
|
2023-02-04 07:58:38 -07:00
|
|
|
--- - title (string|fun(bufnr:integer):string|nil): Title of the window. If a
|
2022-12-08 09:22:57 -07:00
|
|
|
--- function, it accepts the buffer number of the source buffer as its only
|
|
|
|
--- argument and should return a string.
|
2023-03-02 08:01:42 -07:00
|
|
|
function M.inspect_tree(opts)
|
2023-09-20 19:03:40 -07:00
|
|
|
---@diagnostic disable-next-line: invisible
|
2023-06-06 08:23:20 -07:00
|
|
|
require('vim.treesitter.dev').inspect_tree(opts)
|
2022-12-08 09:22:57 -07:00
|
|
|
end
|
|
|
|
|
2023-02-23 10:05:20 -07:00
|
|
|
--- Returns the fold level for {lnum} in the current buffer. Can be set directly to 'foldexpr':
|
2023-09-14 06:23:01 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2023-02-23 10:05:20 -07:00
|
|
|
--- vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
|
2023-09-14 06:23:01 -07:00
|
|
|
--- ```
|
|
|
|
---
|
2023-02-23 10:05:20 -07:00
|
|
|
---@param lnum integer|nil Line number to calculate fold level for
|
|
|
|
---@return string
|
|
|
|
function M.foldexpr(lnum)
|
|
|
|
return require('vim.treesitter._fold').foldexpr(lnum)
|
|
|
|
end
|
|
|
|
|
2023-10-01 12:10:51 -07:00
|
|
|
--- 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
|
|
|
|
function M.foldtext()
|
|
|
|
return require('vim.treesitter._fold').foldtext()
|
|
|
|
end
|
|
|
|
|
2019-09-28 05:27:20 -07:00
|
|
|
return M
|