2023-03-05 16:15:29 -07:00
|
|
|
--- @defgroup lua-treesitter-languagetree
|
|
|
|
---
|
|
|
|
--- @brief A \*LanguageTree\* contains a tree of parsers: the root treesitter parser for {lang} and
|
|
|
|
--- any "injected" language parsers, which themselves may inject other languages, recursively.
|
|
|
|
--- For example a Lua buffer containing some Vimscript commands needs multiple parsers to fully
|
|
|
|
--- understand its contents.
|
|
|
|
---
|
|
|
|
--- To create a LanguageTree (parser object) for a given buffer and language, use:
|
|
|
|
---
|
2023-09-14 06:23:01 -07:00
|
|
|
--- ```lua
|
|
|
|
--- local parser = vim.treesitter.get_parser(bufnr, lang)
|
|
|
|
--- ```
|
2023-03-05 16:15:29 -07:00
|
|
|
---
|
|
|
|
--- (where `bufnr=0` means current buffer). `lang` defaults to 'filetype'.
|
|
|
|
--- Note: currently the parser is retained for the lifetime of a buffer but this may change;
|
|
|
|
--- a plugin should keep a reference to the parser object if it wants incremental updates.
|
|
|
|
---
|
|
|
|
--- Whenever you need to access the current syntax tree, parse the buffer:
|
|
|
|
---
|
2023-09-14 06:23:01 -07:00
|
|
|
--- ```lua
|
|
|
|
--- local tree = parser:parse({ start_row, end_row })
|
|
|
|
--- ```
|
2023-03-05 16:15:29 -07:00
|
|
|
---
|
|
|
|
--- This returns a table of immutable |treesitter-tree| objects representing the current state of
|
|
|
|
--- the buffer. When the plugin wants to access the state after a (possible) edit it must call
|
|
|
|
--- `parse()` again. If the buffer wasn't edited, the same tree will be returned again without extra
|
|
|
|
--- work. If the buffer was parsed before, incremental parsing will be done of the changed parts.
|
|
|
|
---
|
|
|
|
--- Note: To use the parser directly inside a |nvim_buf_attach()| Lua callback, you must call
|
|
|
|
--- |vim.treesitter.get_parser()| before you register your callback. But preferably parsing
|
|
|
|
--- shouldn't be done directly in the change callback anyway as they will be very frequent. Rather
|
|
|
|
--- a plugin that does any kind of analysis on a tree should use a timer to throttle too frequent
|
|
|
|
--- updates.
|
2023-05-17 03:42:18 -07:00
|
|
|
---
|
|
|
|
|
|
|
|
-- Debugging:
|
|
|
|
--
|
|
|
|
-- vim.g.__ts_debug levels:
|
|
|
|
-- - 1. Messages from languagetree.lua
|
|
|
|
-- - 2. Parse messages from treesitter
|
|
|
|
-- - 2. Lex messages from treesitter
|
|
|
|
--
|
|
|
|
-- Log file can be found in stdpath('log')/treesitter.log
|
2023-03-05 16:15:29 -07:00
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
local query = require('vim.treesitter.query')
|
|
|
|
local language = require('vim.treesitter.language')
|
2023-02-23 08:19:52 -07:00
|
|
|
local Range = require('vim.treesitter._range')
|
2020-11-04 10:03:36 -07:00
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
---@alias TSCallbackName
|
|
|
|
---| 'changedtree'
|
|
|
|
---| 'bytes'
|
|
|
|
---| 'detach'
|
|
|
|
---| 'child_added'
|
|
|
|
---| 'child_removed'
|
|
|
|
|
|
|
|
---@alias TSCallbackNameOn
|
|
|
|
---| 'on_changedtree'
|
|
|
|
---| 'on_bytes'
|
|
|
|
---| 'on_detach'
|
|
|
|
---| 'on_child_added'
|
|
|
|
---| 'on_child_removed'
|
2022-09-14 02:08:31 -07:00
|
|
|
|
2023-04-30 08:11:38 -07:00
|
|
|
--- @type table<TSCallbackNameOn,TSCallbackName>
|
|
|
|
local TSCallbackNames = {
|
|
|
|
on_changedtree = 'changedtree',
|
|
|
|
on_bytes = 'bytes',
|
|
|
|
on_detach = 'detach',
|
|
|
|
on_child_added = 'child_added',
|
|
|
|
on_child_removed = 'child_removed',
|
|
|
|
}
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
---@class LanguageTree
|
|
|
|
---@field private _callbacks table<TSCallbackName,function[]> Callback handlers
|
2023-04-30 08:11:38 -07:00
|
|
|
---@field package _callbacks_rec table<TSCallbackName,function[]> Callback handlers (recursive)
|
2023-02-04 07:58:38 -07:00
|
|
|
---@field private _children table<string,LanguageTree> Injected languages
|
|
|
|
---@field private _injection_query Query Queries defining injected languages
|
2023-08-10 06:21:56 -07:00
|
|
|
---@field private _injections_processed boolean
|
2023-02-04 07:58:38 -07:00
|
|
|
---@field private _opts table Options
|
|
|
|
---@field private _parser TSParser Parser for language
|
2023-04-06 07:16:44 -07:00
|
|
|
---@field private _has_regions boolean
|
2023-09-17 01:54:30 -07:00
|
|
|
---@field private _regions table<integer, Range6[]>?
|
2023-04-04 04:58:16 -07:00
|
|
|
---List of regions this tree should manage and parse. If nil then regions are
|
|
|
|
---taken from _trees. This is mostly a short-lived cache for included_regions()
|
2023-02-04 07:58:38 -07:00
|
|
|
---@field private _lang string Language name
|
2023-08-21 21:51:38 -07:00
|
|
|
---@field private _parent_lang? string Parent language name
|
2023-02-04 07:58:38 -07:00
|
|
|
---@field private _source (integer|string) Buffer or string to parse
|
2023-09-17 01:54:30 -07:00
|
|
|
---@field private _trees table<integer, TSTree> Reference to parsed tree (one for each language).
|
|
|
|
---Each key is the index of region, which is synced with _regions and _valid.
|
2023-02-23 11:09:44 -07:00
|
|
|
---@field private _valid boolean|table<integer,boolean> If the parsed tree is valid
|
2023-05-17 03:42:18 -07:00
|
|
|
---@field private _logger? fun(logtype: string, msg: string)
|
|
|
|
---@field private _logfile? file*
|
2020-11-04 10:03:36 -07:00
|
|
|
local LanguageTree = {}
|
2023-02-04 07:58:38 -07:00
|
|
|
|
|
|
|
---@class LanguageTreeOpts
|
|
|
|
---@field queries table<string,string> -- Deprecated
|
|
|
|
---@field injections table<string,string>
|
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
LanguageTree.__index = LanguageTree
|
|
|
|
|
2023-03-23 04:23:51 -07:00
|
|
|
--- @package
|
2021-05-01 05:19:48 -07:00
|
|
|
---
|
2023-03-02 12:46:59 -07:00
|
|
|
--- |LanguageTree| contains a tree of parsers: the root treesitter parser for {lang} and any
|
|
|
|
--- "injected" language parsers, which themselves may inject other languages, recursively.
|
|
|
|
---
|
|
|
|
---@param source (integer|string) Buffer or text string to parse
|
2023-04-30 08:11:38 -07:00
|
|
|
---@param lang string Root language of this tree
|
2023-03-02 12:46:59 -07:00
|
|
|
---@param opts (table|nil) Optional arguments:
|
|
|
|
--- - injections table Map of language to injection query strings. Overrides the
|
|
|
|
--- built-in runtime file searching for language injections.
|
2023-08-21 21:51:38 -07:00
|
|
|
---@param parent_lang? string Parent language name of this tree
|
2023-02-04 07:58:38 -07:00
|
|
|
---@return LanguageTree parser object
|
2023-08-21 21:51:38 -07:00
|
|
|
function LanguageTree.new(source, lang, opts, parent_lang)
|
2023-02-21 10:09:18 -07:00
|
|
|
language.add(lang)
|
2023-02-04 07:58:38 -07:00
|
|
|
---@type LanguageTreeOpts
|
2020-11-04 10:03:36 -07:00
|
|
|
opts = opts or {}
|
|
|
|
|
2023-08-07 10:22:36 -07:00
|
|
|
if source == 0 then
|
|
|
|
source = vim.api.nvim_get_current_buf()
|
|
|
|
end
|
|
|
|
|
2021-03-02 12:51:08 -07:00
|
|
|
local injections = opts.injections or {}
|
2023-08-10 06:21:56 -07:00
|
|
|
|
|
|
|
--- @type LanguageTree
|
|
|
|
local self = {
|
2021-02-06 23:10:37 -07:00
|
|
|
_source = source,
|
|
|
|
_lang = lang,
|
2023-08-21 21:51:38 -07:00
|
|
|
_parent_lang = parent_lang,
|
2020-11-04 10:03:36 -07:00
|
|
|
_children = {},
|
|
|
|
_trees = {},
|
|
|
|
_opts = opts,
|
2023-03-24 07:43:14 -07:00
|
|
|
_injection_query = injections[lang] and query.parse(lang, injections[lang])
|
|
|
|
or query.get(lang, 'injections'),
|
2023-08-10 06:21:56 -07:00
|
|
|
_has_regions = false,
|
|
|
|
_injections_processed = false,
|
2020-11-04 10:03:36 -07:00
|
|
|
_valid = false,
|
|
|
|
_parser = vim._create_ts_parser(lang),
|
2023-04-30 08:11:38 -07:00
|
|
|
_callbacks = {},
|
|
|
|
_callbacks_rec = {},
|
2023-08-10 06:21:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
setmetatable(self, LanguageTree)
|
2020-11-04 10:03:36 -07:00
|
|
|
|
2023-05-17 03:42:18 -07:00
|
|
|
if vim.g.__ts_debug and type(vim.g.__ts_debug) == 'number' then
|
|
|
|
self:_set_logger()
|
2023-08-10 06:21:56 -07:00
|
|
|
self:_log('START')
|
2023-05-17 03:42:18 -07:00
|
|
|
end
|
|
|
|
|
2023-04-30 08:11:38 -07:00
|
|
|
for _, name in pairs(TSCallbackNames) do
|
|
|
|
self._callbacks[name] = {}
|
|
|
|
self._callbacks_rec[name] = {}
|
|
|
|
end
|
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2023-08-10 06:21:56 -07:00
|
|
|
--- @private
|
2023-05-17 03:42:18 -07:00
|
|
|
function LanguageTree:_set_logger()
|
|
|
|
local source = self:source()
|
|
|
|
source = type(source) == 'string' and 'text' or tostring(source)
|
|
|
|
|
|
|
|
local lang = self:lang()
|
|
|
|
|
2023-08-13 00:03:56 -07:00
|
|
|
vim.fn.mkdir(vim.fn.stdpath('log'), 'p')
|
2023-05-26 12:51:18 -07:00
|
|
|
local logfilename = vim.fs.joinpath(vim.fn.stdpath('log'), 'treesitter.log')
|
2023-05-17 03:42:18 -07:00
|
|
|
|
|
|
|
local logfile, openerr = io.open(logfilename, 'a+')
|
|
|
|
|
|
|
|
if not logfile or openerr then
|
|
|
|
error(string.format('Could not open file (%s) for logging: %s', logfilename, openerr))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
self._logfile = logfile
|
|
|
|
|
|
|
|
self._logger = function(logtype, msg)
|
|
|
|
self._logfile:write(string.format('%s:%s:(%s) %s\n', source, lang, logtype, msg))
|
|
|
|
self._logfile:flush()
|
|
|
|
end
|
|
|
|
|
|
|
|
local log_lex = vim.g.__ts_debug >= 3
|
|
|
|
local log_parse = vim.g.__ts_debug >= 2
|
|
|
|
self._parser:_set_logger(log_lex, log_parse, self._logger)
|
|
|
|
end
|
|
|
|
|
2023-03-09 09:09:39 -07:00
|
|
|
---Measure execution time of a function
|
|
|
|
---@generic R1, R2, R3
|
|
|
|
---@param f fun(): R1, R2, R2
|
2023-08-10 06:21:56 -07:00
|
|
|
---@return number, R1, R2, R3
|
2023-03-09 09:09:39 -07:00
|
|
|
local function tcall(f, ...)
|
2023-06-03 03:06:00 -07:00
|
|
|
local start = vim.uv.hrtime()
|
2023-03-09 09:09:39 -07:00
|
|
|
---@diagnostic disable-next-line
|
|
|
|
local r = { f(...) }
|
2023-04-30 08:11:38 -07:00
|
|
|
--- @type number
|
2023-06-03 03:06:00 -07:00
|
|
|
local duration = (vim.uv.hrtime() - start) / 1000000
|
2023-03-09 09:09:39 -07:00
|
|
|
return duration, unpack(r)
|
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
|
|
|
---@vararg any
|
|
|
|
function LanguageTree:_log(...)
|
2023-05-17 03:42:18 -07:00
|
|
|
if not self._logger then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if not vim.g.__ts_debug or vim.g.__ts_debug < 1 then
|
2023-03-09 09:09:39 -07:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local args = { ... }
|
|
|
|
if type(args[1]) == 'function' then
|
|
|
|
args = { args[1]() }
|
|
|
|
end
|
|
|
|
|
|
|
|
local info = debug.getinfo(2, 'nl')
|
2023-09-17 01:54:30 -07:00
|
|
|
local nregions = vim.tbl_count(self:included_regions())
|
2023-08-13 00:03:56 -07:00
|
|
|
local prefix =
|
|
|
|
string.format('%s:%d: (#regions=%d) ', info.name or '???', info.currentline or 0, nregions)
|
2023-03-09 09:09:39 -07:00
|
|
|
|
2023-05-17 03:42:18 -07:00
|
|
|
local msg = { prefix }
|
2023-03-09 09:09:39 -07:00
|
|
|
for _, x in ipairs(args) do
|
|
|
|
if type(x) == 'string' then
|
2023-05-17 03:42:18 -07:00
|
|
|
msg[#msg + 1] = x
|
2023-03-09 09:09:39 -07:00
|
|
|
else
|
2023-05-17 03:42:18 -07:00
|
|
|
msg[#msg + 1] = vim.inspect(x, { newline = ' ', indent = '' })
|
2023-03-09 09:09:39 -07:00
|
|
|
end
|
|
|
|
end
|
2023-05-17 03:42:18 -07:00
|
|
|
self._logger('nvim', table.concat(msg, ' '))
|
2023-03-09 09:09:39 -07:00
|
|
|
end
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
--- Invalidates this parser and all its children
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param reload boolean|nil
|
2021-02-06 23:32:19 -07:00
|
|
|
function LanguageTree:invalidate(reload)
|
2020-11-04 10:03:36 -07:00
|
|
|
self._valid = false
|
|
|
|
|
2021-02-06 23:32:19 -07:00
|
|
|
-- buffer was reloaded, reparse all trees
|
|
|
|
if reload then
|
2023-08-10 06:21:56 -07:00
|
|
|
for _, t in pairs(self._trees) do
|
2023-04-30 08:11:38 -07:00
|
|
|
self:_do_callback('changedtree', t:included_ranges(true), t)
|
|
|
|
end
|
2021-02-06 23:32:19 -07:00
|
|
|
self._trees = {}
|
|
|
|
end
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
for _, child in pairs(self._children) do
|
2021-02-06 23:32:19 -07:00
|
|
|
child:invalidate(reload)
|
2020-11-04 10:03:36 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-17 01:54:30 -07:00
|
|
|
--- Returns all trees of the regions parsed by this parser.
|
2021-05-01 05:19:48 -07:00
|
|
|
--- Does not include child languages.
|
2023-09-17 01:54:30 -07:00
|
|
|
--- The result is list-like if
|
|
|
|
--- * this LanguageTree is the root, in which case the result is empty or a singleton list; or
|
|
|
|
--- * the root LanguageTree is fully parsed.
|
|
|
|
---
|
|
|
|
---@return table<integer, TSTree>
|
2020-11-04 10:03:36 -07:00
|
|
|
function LanguageTree:trees()
|
|
|
|
return self._trees
|
|
|
|
end
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
--- Gets the language of this tree node.
|
2020-11-04 10:03:36 -07:00
|
|
|
function LanguageTree:lang()
|
|
|
|
return self._lang
|
|
|
|
end
|
|
|
|
|
2023-09-17 01:54:30 -07:00
|
|
|
--- Returns whether this LanguageTree is valid, i.e., |LanguageTree:trees()| reflects the latest
|
|
|
|
--- state of the source. If invalid, user should call |LanguageTree:parse()|.
|
|
|
|
---@param exclude_children boolean|nil whether to ignore the validity of children (default `false`)
|
2023-03-09 09:09:39 -07:00
|
|
|
---@return boolean
|
|
|
|
function LanguageTree:is_valid(exclude_children)
|
2023-02-23 11:09:44 -07:00
|
|
|
local valid = self._valid
|
|
|
|
|
|
|
|
if type(valid) == 'table' then
|
2023-09-17 01:54:30 -07:00
|
|
|
for i, _ in pairs(self:included_regions()) do
|
2023-08-10 06:21:56 -07:00
|
|
|
if not valid[i] then
|
2023-02-23 11:09:44 -07:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2023-02-23 08:19:52 -07:00
|
|
|
end
|
2023-02-23 11:09:44 -07:00
|
|
|
|
2023-03-09 09:09:39 -07:00
|
|
|
if not exclude_children then
|
2023-08-10 06:21:56 -07:00
|
|
|
if not self._injections_processed then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-03-09 09:09:39 -07:00
|
|
|
for _, child in pairs(self._children) do
|
|
|
|
if not child:is_valid(exclude_children) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-10 06:21:56 -07:00
|
|
|
if type(valid) == 'boolean' then
|
|
|
|
return valid
|
|
|
|
end
|
2023-03-09 09:09:39 -07:00
|
|
|
|
2023-08-10 06:21:56 -07:00
|
|
|
self._valid = true
|
|
|
|
return true
|
2020-11-04 10:03:36 -07:00
|
|
|
end
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
--- Returns a map of language to child tree.
|
2020-11-04 10:03:36 -07:00
|
|
|
function LanguageTree:children()
|
|
|
|
return self._children
|
|
|
|
end
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
--- Returns the source content of the language tree (bufnr or string).
|
2020-11-04 10:03:36 -07:00
|
|
|
function LanguageTree:source()
|
|
|
|
return self._source
|
|
|
|
end
|
|
|
|
|
2023-08-10 06:21:56 -07:00
|
|
|
--- @param region Range6[]
|
|
|
|
--- @param range? boolean|Range
|
|
|
|
--- @return boolean
|
|
|
|
local function intercepts_region(region, range)
|
|
|
|
if #region == 0 then
|
|
|
|
return true
|
2020-11-04 10:03:36 -07:00
|
|
|
end
|
|
|
|
|
2023-08-10 06:21:56 -07:00
|
|
|
if range == nil then
|
|
|
|
return false
|
|
|
|
end
|
2020-11-04 10:03:36 -07:00
|
|
|
|
2023-08-10 06:21:56 -07:00
|
|
|
if type(range) == 'boolean' then
|
|
|
|
return range
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, r in ipairs(region) do
|
|
|
|
if Range.intercepts(r, range) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
--- @private
|
|
|
|
--- @param range boolean|Range?
|
2023-09-17 01:54:30 -07:00
|
|
|
--- @return Range6[] changes
|
2023-08-10 06:21:56 -07:00
|
|
|
--- @return integer no_regions_parsed
|
|
|
|
--- @return number total_parse_time
|
|
|
|
function LanguageTree:_parse_regions(range)
|
|
|
|
local changes = {}
|
|
|
|
local no_regions_parsed = 0
|
2023-03-09 09:09:39 -07:00
|
|
|
local total_parse_time = 0
|
|
|
|
|
2023-08-10 06:21:56 -07:00
|
|
|
if type(self._valid) ~= 'table' then
|
|
|
|
self._valid = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
-- If there are no ranges, set to an empty list
|
|
|
|
-- so the included ranges in the parser are cleared.
|
|
|
|
for i, ranges in pairs(self:included_regions()) do
|
2023-12-23 03:45:19 -07:00
|
|
|
if
|
|
|
|
not self._valid[i]
|
|
|
|
and (
|
|
|
|
intercepts_region(ranges, range)
|
|
|
|
or (self._trees[i] and intercepts_region(self._trees[i]:included_ranges(false), range))
|
|
|
|
)
|
|
|
|
then
|
2023-08-10 06:21:56 -07:00
|
|
|
self._parser:set_included_ranges(ranges)
|
|
|
|
local parse_time, tree, tree_changes =
|
|
|
|
tcall(self._parser.parse, self._parser, self._trees[i], self._source, true)
|
|
|
|
|
|
|
|
-- Pass ranges if this is an initial parse
|
|
|
|
local cb_changes = self._trees[i] and tree_changes or tree:included_ranges(true)
|
|
|
|
|
|
|
|
self:_do_callback('changedtree', cb_changes, tree)
|
|
|
|
self._trees[i] = tree
|
|
|
|
vim.list_extend(changes, tree_changes)
|
|
|
|
|
|
|
|
total_parse_time = total_parse_time + parse_time
|
|
|
|
no_regions_parsed = no_regions_parsed + 1
|
|
|
|
self._valid[i] = true
|
2020-11-04 10:03:36 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-10 06:21:56 -07:00
|
|
|
return changes, no_regions_parsed, total_parse_time
|
|
|
|
end
|
|
|
|
|
|
|
|
--- @private
|
|
|
|
--- @return number
|
|
|
|
function LanguageTree:_add_injections()
|
2023-02-04 07:58:38 -07:00
|
|
|
local seen_langs = {} ---@type table<string,boolean>
|
2020-11-04 10:03:36 -07:00
|
|
|
|
2023-03-09 09:09:39 -07:00
|
|
|
local query_time, injections_by_lang = tcall(self._get_injections, self)
|
2023-09-17 01:54:30 -07:00
|
|
|
for lang, injection_regions in pairs(injections_by_lang) do
|
2023-02-24 02:50:59 -07:00
|
|
|
local has_lang = pcall(language.add, lang)
|
2020-11-04 10:03:36 -07:00
|
|
|
|
2020-12-04 15:56:29 -07:00
|
|
|
-- Child language trees should just be ignored if not found, since
|
|
|
|
-- they can depend on the text of a node. Intermediate strings
|
|
|
|
-- would cause errors for unknown parsers.
|
|
|
|
if has_lang then
|
|
|
|
local child = self._children[lang]
|
2020-11-04 10:03:36 -07:00
|
|
|
|
2020-12-04 15:56:29 -07:00
|
|
|
if not child then
|
|
|
|
child = self:add_child(lang)
|
|
|
|
end
|
2020-11-04 10:03:36 -07:00
|
|
|
|
2023-09-17 01:54:30 -07:00
|
|
|
child:set_included_regions(injection_regions)
|
2020-12-04 15:56:29 -07:00
|
|
|
seen_langs[lang] = true
|
|
|
|
end
|
2020-11-04 10:03:36 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
for lang, _ in pairs(self._children) do
|
|
|
|
if not seen_langs[lang] then
|
|
|
|
self:remove_child(lang)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-10 06:21:56 -07:00
|
|
|
return query_time
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Recursively parse all regions in the language tree using |treesitter-parsers|
|
|
|
|
--- for the corresponding languages and run injection queries on the parsed trees
|
|
|
|
--- to determine whether child trees should be created and parsed.
|
|
|
|
---
|
|
|
|
--- Any region with empty range (`{}`, typically only the root tree) is always parsed;
|
|
|
|
--- otherwise (typically injections) only if it intersects {range} (or if {range} is `true`).
|
|
|
|
---
|
|
|
|
--- @param range boolean|Range|nil: Parse this range in the parser's source.
|
|
|
|
--- Set to `true` to run a complete parse of the source (Note: Can be slow!)
|
|
|
|
--- Set to `false|nil` to only parse regions with empty ranges (typically
|
|
|
|
--- only the root tree without injections).
|
2023-09-17 01:54:30 -07:00
|
|
|
--- @return table<integer, TSTree>
|
2023-08-10 06:21:56 -07:00
|
|
|
function LanguageTree:parse(range)
|
|
|
|
if self:is_valid() then
|
|
|
|
self:_log('valid')
|
|
|
|
return self._trees
|
|
|
|
end
|
|
|
|
|
2023-09-17 01:54:30 -07:00
|
|
|
local changes --- @type Range6[]?
|
2023-08-10 06:21:56 -07:00
|
|
|
|
|
|
|
-- Collect some stats
|
|
|
|
local no_regions_parsed = 0
|
|
|
|
local query_time = 0
|
|
|
|
local total_parse_time = 0
|
|
|
|
|
|
|
|
--- At least 1 region is invalid
|
|
|
|
if not self:is_valid(true) then
|
|
|
|
changes, no_regions_parsed, total_parse_time = self:_parse_regions(range)
|
|
|
|
-- Need to run injections when we parsed something
|
|
|
|
if no_regions_parsed > 0 then
|
|
|
|
self._injections_processed = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not self._injections_processed and range ~= false and range ~= nil then
|
|
|
|
query_time = self:_add_injections()
|
|
|
|
self._injections_processed = true
|
|
|
|
end
|
|
|
|
|
2023-03-09 09:09:39 -07:00
|
|
|
self:_log({
|
2023-08-10 06:21:56 -07:00
|
|
|
changes = changes and #changes > 0 and changes or nil,
|
|
|
|
regions_parsed = no_regions_parsed,
|
2023-03-09 09:09:39 -07:00
|
|
|
parse_time = total_parse_time,
|
|
|
|
query_time = query_time,
|
2023-08-10 06:21:56 -07:00
|
|
|
range = range,
|
2023-03-09 09:09:39 -07:00
|
|
|
})
|
|
|
|
|
2023-09-11 23:15:24 -07:00
|
|
|
for _, child in pairs(self._children) do
|
2023-08-10 06:21:56 -07:00
|
|
|
child:parse(range)
|
2023-09-11 23:15:24 -07:00
|
|
|
end
|
2023-03-09 09:09:39 -07:00
|
|
|
|
2023-03-10 09:16:49 -07:00
|
|
|
return self._trees
|
2020-11-04 10:03:36 -07:00
|
|
|
end
|
|
|
|
|
2023-09-14 03:36:16 -07:00
|
|
|
---@deprecated Misleading name. Use `LanguageTree:children()` (non-recursive) instead,
|
|
|
|
--- add recursion yourself if needed.
|
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
|
|
|
---
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param fn fun(tree: LanguageTree, lang: string)
|
|
|
|
---@param include_self boolean|nil Whether to include the invoking tree in the results
|
2020-11-04 10:03:36 -07:00
|
|
|
function LanguageTree:for_each_child(fn, include_self)
|
2023-09-14 03:36:16 -07:00
|
|
|
vim.deprecate('LanguageTree:for_each_child()', 'LanguageTree:children()', '0.11')
|
2020-11-04 10:03:36 -07:00
|
|
|
if include_self then
|
|
|
|
fn(self, self._lang)
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, child in pairs(self._children) do
|
|
|
|
child:for_each_child(fn, true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
---
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param fn fun(tree: TSTree, ltree: LanguageTree)
|
2020-11-04 10:03:36 -07:00
|
|
|
function LanguageTree:for_each_tree(fn)
|
2023-08-10 06:21:56 -07:00
|
|
|
for _, tree in pairs(self._trees) do
|
2020-11-04 10:03:36 -07:00
|
|
|
fn(tree, self)
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, child in pairs(self._children) do
|
|
|
|
child:for_each_tree(fn)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
--- Adds a child language to this |LanguageTree|.
|
2021-05-01 05:19:48 -07:00
|
|
|
---
|
|
|
|
--- If the language already exists as a child, it will first be removed.
|
|
|
|
---
|
2022-09-14 02:08:31 -07:00
|
|
|
---@private
|
|
|
|
---@param lang string Language to add.
|
2023-02-04 07:58:38 -07:00
|
|
|
---@return LanguageTree injected
|
2020-11-04 10:03:36 -07:00
|
|
|
function LanguageTree:add_child(lang)
|
|
|
|
if self._children[lang] then
|
|
|
|
self:remove_child(lang)
|
|
|
|
end
|
|
|
|
|
2023-08-21 21:51:38 -07:00
|
|
|
local child = LanguageTree.new(self._source, lang, self._opts, self:lang())
|
2023-04-30 08:11:38 -07:00
|
|
|
|
|
|
|
-- Inherit recursive callbacks
|
|
|
|
for nm, cb in pairs(self._callbacks_rec) do
|
|
|
|
vim.list_extend(child._callbacks_rec[nm], cb)
|
|
|
|
end
|
|
|
|
|
|
|
|
self._children[lang] = child
|
2020-11-04 10:03:36 -07:00
|
|
|
self:_do_callback('child_added', self._children[lang])
|
|
|
|
|
|
|
|
return self._children[lang]
|
|
|
|
end
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
--- Removes a child language from this |LanguageTree|.
|
2021-05-01 05:19:48 -07:00
|
|
|
---
|
2022-09-14 02:08:31 -07:00
|
|
|
---@private
|
|
|
|
---@param lang string Language to remove.
|
2020-11-04 10:03:36 -07:00
|
|
|
function LanguageTree:remove_child(lang)
|
|
|
|
local child = self._children[lang]
|
|
|
|
|
|
|
|
if child then
|
|
|
|
self._children[lang] = nil
|
|
|
|
child:destroy()
|
|
|
|
self:_do_callback('child_removed', child)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
--- Destroys this |LanguageTree| and all its children.
|
2021-05-01 05:19:48 -07:00
|
|
|
---
|
|
|
|
--- Any cleanup logic should be performed here.
|
2022-02-13 06:43:25 -07:00
|
|
|
---
|
2022-09-14 02:08:31 -07:00
|
|
|
--- Note: This DOES NOT remove this tree from a parent. Instead,
|
2021-05-01 05:19:48 -07:00
|
|
|
--- `remove_child` must be called on the parent to remove it.
|
2020-11-04 10:03:36 -07:00
|
|
|
function LanguageTree:destroy()
|
|
|
|
-- Cleanup here
|
2023-02-04 07:58:38 -07:00
|
|
|
for _, child in pairs(self._children) do
|
2020-11-04 10:03:36 -07:00
|
|
|
child:destroy()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-09 09:09:39 -07:00
|
|
|
---@param region Range6[]
|
|
|
|
local function region_tostr(region)
|
2023-06-27 11:05:44 -07:00
|
|
|
if #region == 0 then
|
|
|
|
return '[]'
|
|
|
|
end
|
2023-03-09 09:09:39 -07:00
|
|
|
local srow, scol = region[1][1], region[1][2]
|
|
|
|
local erow, ecol = region[#region][4], region[#region][5]
|
|
|
|
return string.format('[%d:%d-%d:%d]', srow, scol, erow, ecol)
|
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
|
|
|
---Iterate through all the regions. fn returns a boolean to indicate if the
|
|
|
|
---region is valid or not.
|
|
|
|
---@param fn fun(index: integer, region: Range6[]): boolean
|
|
|
|
function LanguageTree:_iter_regions(fn)
|
|
|
|
if not self._valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-08-10 06:21:56 -07:00
|
|
|
local was_valid = type(self._valid) ~= 'table'
|
|
|
|
|
|
|
|
if was_valid then
|
|
|
|
self:_log('was valid', self._valid)
|
2023-03-09 09:09:39 -07:00
|
|
|
self._valid = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
local all_valid = true
|
|
|
|
|
2023-09-17 01:54:30 -07:00
|
|
|
for i, region in pairs(self:included_regions()) do
|
2023-08-10 06:21:56 -07:00
|
|
|
if was_valid or self._valid[i] then
|
2023-03-09 09:09:39 -07:00
|
|
|
self._valid[i] = fn(i, region)
|
|
|
|
if not self._valid[i] then
|
|
|
|
self:_log(function()
|
|
|
|
return 'invalidating region', i, region_tostr(region)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not self._valid[i] then
|
|
|
|
all_valid = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Compress the valid value to 'true' if there are no invalid regions
|
|
|
|
if all_valid then
|
|
|
|
self._valid = all_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
--- Sets the included regions that should be parsed by this |LanguageTree|.
|
2021-05-01 05:19:48 -07:00
|
|
|
--- A region is a set of nodes and/or ranges that will be parsed in the same context.
|
|
|
|
---
|
2022-09-14 02:08:31 -07:00
|
|
|
--- For example, `{ { node1 }, { node2} }` contains two separate regions.
|
|
|
|
--- They will be parsed by the parser in two different contexts, thus resulting
|
2021-05-01 05:19:48 -07:00
|
|
|
--- in two separate trees.
|
|
|
|
---
|
2022-09-14 02:08:31 -07:00
|
|
|
--- On the other hand, `{ { node1, node2 } }` is a single region consisting of
|
|
|
|
--- two nodes. This will be parsed by the parser in a single context, thus resulting
|
2021-05-01 05:19:48 -07:00
|
|
|
--- in a single tree.
|
|
|
|
---
|
|
|
|
--- This allows for embedded languages to be parsed together across different
|
|
|
|
--- nodes, which is useful for templating languages like ERB and EJS.
|
|
|
|
---
|
2022-09-14 02:08:31 -07:00
|
|
|
---@private
|
2023-09-17 01:54:30 -07:00
|
|
|
---@param new_regions (Range4|Range6|TSNode)[][] List of regions this tree should manage and parse.
|
2023-03-09 09:09:39 -07:00
|
|
|
function LanguageTree:set_included_regions(new_regions)
|
2023-04-06 07:16:44 -07:00
|
|
|
self._has_regions = true
|
|
|
|
|
2022-06-02 08:35:16 -07:00
|
|
|
-- Transform the tables from 4 element long to 6 element long (with byte offset)
|
2023-03-09 09:09:39 -07:00
|
|
|
for _, region in ipairs(new_regions) do
|
2022-06-02 08:35:16 -07:00
|
|
|
for i, range in ipairs(region) do
|
|
|
|
if type(range) == 'table' and #range == 4 then
|
2023-09-17 01:54:30 -07:00
|
|
|
region[i] = Range.add_bytes(self._source, range --[[@as Range4]])
|
2023-08-10 06:21:56 -07:00
|
|
|
elseif type(range) == 'userdata' then
|
|
|
|
region[i] = { range:range(true) }
|
2023-02-23 08:19:52 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-06-02 08:35:16 -07:00
|
|
|
|
2023-09-17 01:54:30 -07:00
|
|
|
-- included_regions is not guaranteed to be list-like, but this is still sound, i.e. if
|
|
|
|
-- new_regions is different from included_regions, then outdated regions in included_regions are
|
|
|
|
-- invalidated. For example, if included_regions = new_regions ++ hole ++ outdated_regions, then
|
|
|
|
-- outdated_regions is invalidated by _iter_regions in else branch.
|
2023-04-04 04:58:16 -07:00
|
|
|
if #self:included_regions() ~= #new_regions then
|
2023-04-30 08:11:38 -07:00
|
|
|
-- TODO(lewis6991): inefficient; invalidate trees incrementally
|
2023-09-17 01:54:30 -07:00
|
|
|
for _, t in pairs(self._trees) do
|
2023-04-30 08:11:38 -07:00
|
|
|
self:_do_callback('changedtree', t:included_ranges(true), t)
|
|
|
|
end
|
2023-02-23 08:19:52 -07:00
|
|
|
self._trees = {}
|
|
|
|
self:invalidate()
|
2023-03-09 09:09:39 -07:00
|
|
|
else
|
|
|
|
self:_iter_regions(function(i, region)
|
|
|
|
return vim.deep_equal(new_regions[i], region)
|
|
|
|
end)
|
2023-03-08 10:22:28 -07:00
|
|
|
end
|
2023-04-04 04:58:16 -07:00
|
|
|
|
2023-03-09 09:09:39 -07:00
|
|
|
self._regions = new_regions
|
2023-03-08 10:59:45 -07:00
|
|
|
end
|
|
|
|
|
2023-09-17 01:54:30 -07:00
|
|
|
---Gets the set of included regions managed by this LanguageTree. This can be different from the
|
|
|
|
---regions set by injection query, because a partial |LanguageTree:parse()| drops the regions
|
|
|
|
---outside the requested range.
|
|
|
|
---@return table<integer, Range6[]>
|
2023-03-08 10:59:45 -07:00
|
|
|
function LanguageTree:included_regions()
|
2023-04-04 04:58:16 -07:00
|
|
|
if self._regions then
|
|
|
|
return self._regions
|
|
|
|
end
|
|
|
|
|
fix(languagetree): don't treat unparsed nodes as occupying full range
This is incorrect in the following scenario:
1. The language tree is Lua > Vim > Lua.
2. An edit simultaneously wipes out the `_regions` of all nodes, while
taking the Vim injection off-screen.
3. The Vim injection is not re-parsed, so the child Lua `_regions` is
still `nil`.
4. The child Lua is assumed, incorrectly, to occupy the whole document.
5. This causes the injections to be parsed again, resulting in Lua > Vim
> Lua > Vim.
6. Now, by the same process, Vim ends up with its range assumed over the
whole document. Now the parse is broken and results in broken
highlighting and poor performance.
It should be fine to instead treat an unparsed node as occupying
nothing (i.e. effectively non-existent). Since, either:
- The parent was just parsed, hence defining `_regions`
- The parent was not just parsed, in which case this node doesn't need
to be parsed either.
Also, the name `has_regions` is confusing; it seems to simply
mean the opposite of "root" or "full_document". However, this PR does
not touch it.
2023-09-19 21:41:07 -07:00
|
|
|
if not self._has_regions then
|
|
|
|
-- treesitter.c will default empty ranges to { -1, -1, -1, -1, -1, -1} (the full range)
|
2023-04-04 04:58:16 -07:00
|
|
|
return { {} }
|
|
|
|
end
|
|
|
|
|
|
|
|
local regions = {} ---@type Range6[][]
|
2023-09-17 01:54:30 -07:00
|
|
|
for i, _ in pairs(self._trees) do
|
2023-04-04 04:58:16 -07:00
|
|
|
regions[i] = self._trees[i]:included_ranges(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
self._regions = regions
|
|
|
|
return regions
|
2020-11-04 10:03:36 -07:00
|
|
|
end
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param node TSNode
|
2023-03-09 09:09:39 -07:00
|
|
|
---@param source string|integer
|
2023-03-08 04:03:11 -07:00
|
|
|
---@param metadata TSMetadata
|
2023-08-07 10:22:36 -07:00
|
|
|
---@param include_children boolean
|
2023-03-09 09:09:39 -07:00
|
|
|
---@return Range6[]
|
|
|
|
local function get_node_ranges(node, source, metadata, include_children)
|
2023-03-24 07:43:14 -07:00
|
|
|
local range = vim.treesitter.get_range(node, source, metadata)
|
2023-08-07 10:22:36 -07:00
|
|
|
local child_count = node:named_child_count()
|
2023-03-08 04:03:11 -07:00
|
|
|
|
2023-08-07 10:22:36 -07:00
|
|
|
if include_children or child_count == 0 then
|
2023-03-08 04:03:11 -07:00
|
|
|
return { range }
|
|
|
|
end
|
|
|
|
|
2023-03-09 09:09:39 -07:00
|
|
|
local ranges = {} ---@type Range6[]
|
2023-03-08 04:03:11 -07:00
|
|
|
|
2023-03-09 09:09:39 -07:00
|
|
|
local srow, scol, sbyte, erow, ecol, ebyte = Range.unpack6(range)
|
2023-03-08 04:03:11 -07:00
|
|
|
|
2023-08-07 10:22:36 -07:00
|
|
|
-- We are excluding children so we need to mask out their ranges
|
|
|
|
for i = 0, child_count - 1 do
|
2023-08-10 06:21:56 -07:00
|
|
|
local child = assert(node:named_child(i))
|
2023-03-09 09:09:39 -07:00
|
|
|
local c_srow, c_scol, c_sbyte, c_erow, c_ecol, c_ebyte = child:range(true)
|
|
|
|
if c_srow > srow or c_scol > scol then
|
|
|
|
ranges[#ranges + 1] = { srow, scol, sbyte, c_srow, c_scol, c_sbyte }
|
2023-03-08 04:03:11 -07:00
|
|
|
end
|
2023-03-09 09:09:39 -07:00
|
|
|
srow = c_erow
|
|
|
|
scol = c_ecol
|
|
|
|
sbyte = c_ebyte
|
2023-03-08 04:03:11 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
if erow > srow or ecol > scol then
|
2023-03-09 09:09:39 -07:00
|
|
|
ranges[#ranges + 1] = Range.add_bytes(source, { srow, scol, sbyte, erow, ecol, ebyte })
|
2023-03-08 04:03:11 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
return ranges
|
|
|
|
end
|
|
|
|
|
2023-03-09 09:09:39 -07:00
|
|
|
---@class TSInjectionElem
|
|
|
|
---@field combined boolean
|
|
|
|
---@field regions Range6[][]
|
|
|
|
|
|
|
|
---@alias TSInjection table<string,table<integer,TSInjectionElem>>
|
2023-03-08 04:03:11 -07:00
|
|
|
|
|
|
|
---@param t table<integer,TSInjection>
|
|
|
|
---@param tree_index integer
|
|
|
|
---@param pattern integer
|
|
|
|
---@param lang string
|
|
|
|
---@param combined boolean
|
2023-03-10 09:40:27 -07:00
|
|
|
---@param ranges Range6[]
|
2023-03-08 04:03:11 -07:00
|
|
|
local function add_injection(t, tree_index, pattern, lang, combined, ranges)
|
2023-08-07 10:22:36 -07:00
|
|
|
if #ranges == 0 then
|
|
|
|
-- Make sure not to add an empty range set as this is interpreted to mean the whole buffer.
|
|
|
|
return
|
|
|
|
end
|
2023-03-08 04:03:11 -07:00
|
|
|
|
|
|
|
-- Each tree index should be isolated from the other nodes.
|
|
|
|
if not t[tree_index] then
|
|
|
|
t[tree_index] = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
if not t[tree_index][lang] then
|
|
|
|
t[tree_index][lang] = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Key this by pattern. If combined is set to true all captures of this pattern
|
|
|
|
-- will be parsed by treesitter as the same "source".
|
|
|
|
-- If combined is false, each "region" will be parsed as a single source.
|
|
|
|
if not t[tree_index][lang][pattern] then
|
|
|
|
t[tree_index][lang][pattern] = { combined = combined, regions = {} }
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(t[tree_index][lang][pattern].regions, ranges)
|
|
|
|
end
|
|
|
|
|
2023-08-11 08:05:17 -07:00
|
|
|
-- TODO(clason): replace by refactored `ts.has_parser` API (without registering)
|
2023-09-16 15:10:30 -07:00
|
|
|
--- The result of this function is cached to prevent nvim_get_runtime_file from being
|
|
|
|
--- called too often
|
|
|
|
--- @param lang string parser name
|
|
|
|
--- @return boolean # true if parser for {lang} exists on rtp
|
|
|
|
local has_parser = vim.func._memoize(1, function(lang)
|
2023-08-11 08:05:17 -07:00
|
|
|
return vim._ts_has_language(lang)
|
|
|
|
or #vim.api.nvim_get_runtime_file('parser/' .. lang .. '.*', false) > 0
|
2023-09-16 15:10:30 -07:00
|
|
|
end)
|
2023-08-11 08:05:17 -07:00
|
|
|
|
2023-09-13 16:51:54 -07:00
|
|
|
--- Return parser name for language (if exists) or filetype (if registered and exists).
|
|
|
|
--- Also attempts with the input lower-cased.
|
2023-08-11 08:05:17 -07:00
|
|
|
---
|
|
|
|
---@param alias string language or filetype name
|
|
|
|
---@return string? # resolved parser name
|
|
|
|
local function resolve_lang(alias)
|
2024-01-18 07:12:03 -07:00
|
|
|
-- validate that `alias` is a legal language
|
|
|
|
if not (alias and alias:match('[%w_]+') == alias) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-08-11 08:05:17 -07:00
|
|
|
if has_parser(alias) then
|
|
|
|
return alias
|
|
|
|
end
|
|
|
|
|
2023-09-13 16:51:54 -07:00
|
|
|
if has_parser(alias:lower()) then
|
|
|
|
return alias:lower()
|
|
|
|
end
|
|
|
|
|
2023-08-11 08:05:17 -07:00
|
|
|
local lang = vim.treesitter.language.get_lang(alias)
|
|
|
|
if lang and has_parser(lang) then
|
|
|
|
return lang
|
|
|
|
end
|
2023-09-13 16:51:54 -07:00
|
|
|
|
|
|
|
lang = vim.treesitter.language.get_lang(alias:lower())
|
|
|
|
if lang and has_parser(lang) then
|
|
|
|
return lang
|
|
|
|
end
|
2023-08-11 08:05:17 -07:00
|
|
|
end
|
|
|
|
|
2023-03-08 04:03:11 -07:00
|
|
|
---@private
|
|
|
|
--- Extract injections according to:
|
|
|
|
--- https://tree-sitter.github.io/tree-sitter/syntax-highlighting#language-injection
|
2024-02-16 10:54:47 -07:00
|
|
|
---@param match table<integer,TSNode[]>
|
2023-03-10 09:10:05 -07:00
|
|
|
---@param metadata TSMetadata
|
2023-03-10 09:40:27 -07:00
|
|
|
---@return string?, boolean, Range6[]
|
2023-03-08 04:03:11 -07:00
|
|
|
function LanguageTree:_get_injection(match, metadata)
|
2023-03-10 09:40:27 -07:00
|
|
|
local ranges = {} ---@type Range6[]
|
2023-03-08 04:03:11 -07:00
|
|
|
local combined = metadata['injection.combined'] ~= nil
|
2023-09-13 16:51:54 -07:00
|
|
|
local injection_lang = metadata['injection.language'] --[[@as string?]]
|
2023-08-21 21:51:38 -07:00
|
|
|
local lang = metadata['injection.self'] ~= nil and self:lang()
|
|
|
|
or metadata['injection.parent'] ~= nil and self._parent_lang
|
2023-09-13 16:51:54 -07:00
|
|
|
or (injection_lang and resolve_lang(injection_lang))
|
2023-03-08 04:03:11 -07:00
|
|
|
local include_children = metadata['injection.include-children'] ~= nil
|
|
|
|
|
2024-02-16 10:54:47 -07:00
|
|
|
for id, nodes in pairs(match) do
|
|
|
|
for _, node in ipairs(nodes) do
|
|
|
|
local name = self._injection_query.captures[id]
|
|
|
|
-- Lang should override any other language tag
|
|
|
|
if name == 'injection.language' then
|
|
|
|
local text = vim.treesitter.get_node_text(node, self._source, { metadata = metadata[id] })
|
|
|
|
lang = resolve_lang(text)
|
|
|
|
elseif name == 'injection.content' then
|
|
|
|
ranges = get_node_ranges(node, self._source, metadata[id], include_children)
|
|
|
|
end
|
2023-03-08 04:03:11 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return lang, combined, ranges
|
|
|
|
end
|
|
|
|
|
2023-09-16 07:43:22 -07:00
|
|
|
--- Can't use vim.tbl_flatten since a range is just a table.
|
|
|
|
---@param regions Range6[][]
|
|
|
|
---@return Range6[]
|
|
|
|
local function combine_regions(regions)
|
|
|
|
local result = {} ---@type Range6[]
|
|
|
|
for _, region in ipairs(regions) do
|
|
|
|
for _, range in ipairs(region) do
|
|
|
|
result[#result + 1] = range
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2023-09-17 01:54:30 -07:00
|
|
|
--- Gets language injection regions by language.
|
2021-05-01 05:19:48 -07:00
|
|
|
---
|
|
|
|
--- This is where most of the injection processing occurs.
|
|
|
|
---
|
|
|
|
--- TODO: Allow for an offset predicate to tailor the injection range
|
|
|
|
--- instead of using the entire nodes range.
|
2023-08-10 06:21:56 -07:00
|
|
|
--- @private
|
|
|
|
--- @return table<string, Range6[][]>
|
2020-11-04 10:03:36 -07:00
|
|
|
function LanguageTree:_get_injections()
|
|
|
|
if not self._injection_query then
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
|
2023-03-08 04:03:11 -07:00
|
|
|
---@type table<integer,TSInjection>
|
2020-11-04 10:03:36 -07:00
|
|
|
local injections = {}
|
|
|
|
|
2023-08-10 06:21:56 -07:00
|
|
|
for index, tree in pairs(self._trees) do
|
2020-11-04 10:03:36 -07:00
|
|
|
local root_node = tree:root()
|
|
|
|
local start_line, _, end_line, _ = root_node:range()
|
|
|
|
|
2020-11-24 07:50:33 -07:00
|
|
|
for pattern, match, metadata in
|
2024-02-16 10:54:47 -07:00
|
|
|
self._injection_query:iter_matches(
|
|
|
|
root_node,
|
|
|
|
self._source,
|
|
|
|
start_line,
|
|
|
|
end_line + 1,
|
|
|
|
{ all = true }
|
|
|
|
)
|
2020-11-24 07:50:33 -07:00
|
|
|
do
|
2023-03-08 04:03:11 -07:00
|
|
|
local lang, combined, ranges = self:_get_injection(match, metadata)
|
2023-08-12 08:54:04 -07:00
|
|
|
if lang then
|
|
|
|
add_injection(injections, index, pattern, lang, combined, ranges)
|
|
|
|
else
|
|
|
|
self:_log('match from injection query failed for pattern', pattern)
|
2020-11-04 10:03:36 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-09 09:09:39 -07:00
|
|
|
---@type table<string,Range6[][]>
|
2020-11-04 10:03:36 -07:00
|
|
|
local result = {}
|
|
|
|
|
|
|
|
-- Generate a map by lang of node lists.
|
2021-03-02 12:51:08 -07:00
|
|
|
-- Each list is a set of ranges that should be parsed together.
|
2023-02-27 08:01:09 -07:00
|
|
|
for _, lang_map in pairs(injections) do
|
2020-11-04 10:03:36 -07:00
|
|
|
for lang, patterns in pairs(lang_map) do
|
|
|
|
if not result[lang] then
|
|
|
|
result[lang] = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, entry in pairs(patterns) do
|
|
|
|
if entry.combined then
|
2023-09-16 07:43:22 -07:00
|
|
|
table.insert(result[lang], combine_regions(entry.regions))
|
2020-11-04 10:03:36 -07:00
|
|
|
else
|
2023-08-10 06:21:56 -07:00
|
|
|
for _, ranges in pairs(entry.regions) do
|
2021-03-02 12:51:08 -07:00
|
|
|
table.insert(result[lang], ranges)
|
2020-11-04 10:03:36 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
---@private
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param cb_name TSCallbackName
|
2020-11-04 10:03:36 -07:00
|
|
|
function LanguageTree:_do_callback(cb_name, ...)
|
|
|
|
for _, cb in ipairs(self._callbacks[cb_name]) do
|
|
|
|
cb(...)
|
|
|
|
end
|
2023-04-30 08:11:38 -07:00
|
|
|
for _, cb in ipairs(self._callbacks_rec[cb_name]) do
|
|
|
|
cb(...)
|
|
|
|
end
|
2020-11-04 10:03:36 -07:00
|
|
|
end
|
|
|
|
|
2023-03-23 04:23:51 -07:00
|
|
|
---@package
|
2023-03-09 09:09:39 -07:00
|
|
|
function LanguageTree:_edit(
|
|
|
|
start_byte,
|
|
|
|
end_byte_old,
|
|
|
|
end_byte_new,
|
|
|
|
start_row,
|
|
|
|
start_col,
|
|
|
|
end_row_old,
|
|
|
|
end_col_old,
|
|
|
|
end_row_new,
|
|
|
|
end_col_new
|
|
|
|
)
|
2023-08-10 06:21:56 -07:00
|
|
|
for _, tree in pairs(self._trees) do
|
2023-03-09 09:09:39 -07:00
|
|
|
tree:edit(
|
|
|
|
start_byte,
|
|
|
|
end_byte_old,
|
|
|
|
end_byte_new,
|
|
|
|
start_row,
|
|
|
|
start_col,
|
|
|
|
end_row_old,
|
|
|
|
end_col_old,
|
|
|
|
end_row_new,
|
|
|
|
end_col_new
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-04-04 04:58:16 -07:00
|
|
|
self._regions = nil
|
|
|
|
|
2023-03-09 09:09:39 -07:00
|
|
|
local changed_range = {
|
|
|
|
start_row,
|
|
|
|
start_col,
|
|
|
|
start_byte,
|
|
|
|
end_row_old,
|
|
|
|
end_col_old,
|
|
|
|
end_byte_old,
|
|
|
|
}
|
|
|
|
|
|
|
|
-- Validate regions after editing the tree
|
|
|
|
self:_iter_regions(function(_, region)
|
2023-04-06 07:16:44 -07:00
|
|
|
if #region == 0 then
|
|
|
|
-- empty region, use the full source
|
|
|
|
return false
|
|
|
|
end
|
2023-04-04 04:58:16 -07:00
|
|
|
for _, r in ipairs(region) do
|
2023-03-09 09:09:39 -07:00
|
|
|
if Range.intercepts(r, changed_range) then
|
|
|
|
return false
|
2023-03-08 10:59:45 -07:00
|
|
|
end
|
2023-02-23 08:19:52 -07:00
|
|
|
end
|
2023-03-09 09:09:39 -07:00
|
|
|
return true
|
|
|
|
end)
|
2023-09-12 03:38:31 -07:00
|
|
|
|
|
|
|
for _, child in pairs(self._children) do
|
|
|
|
child:_edit(
|
|
|
|
start_byte,
|
|
|
|
end_byte_old,
|
|
|
|
end_byte_new,
|
|
|
|
start_row,
|
|
|
|
start_col,
|
|
|
|
end_row_old,
|
|
|
|
end_col_old,
|
|
|
|
end_row_new,
|
|
|
|
end_col_new
|
|
|
|
)
|
|
|
|
end
|
2023-02-23 08:19:52 -07:00
|
|
|
end
|
|
|
|
|
2023-03-23 04:23:51 -07:00
|
|
|
---@package
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param bufnr integer
|
|
|
|
---@param changed_tick integer
|
|
|
|
---@param start_row integer
|
|
|
|
---@param start_col integer
|
|
|
|
---@param start_byte integer
|
|
|
|
---@param old_row integer
|
|
|
|
---@param old_col integer
|
|
|
|
---@param old_byte integer
|
|
|
|
---@param new_row integer
|
|
|
|
---@param new_col integer
|
|
|
|
---@param new_byte integer
|
2020-11-04 10:03:36 -07:00
|
|
|
function LanguageTree:_on_bytes(
|
|
|
|
bufnr,
|
|
|
|
changed_tick,
|
|
|
|
start_row,
|
|
|
|
start_col,
|
|
|
|
start_byte,
|
|
|
|
old_row,
|
|
|
|
old_col,
|
|
|
|
old_byte,
|
|
|
|
new_row,
|
|
|
|
new_col,
|
|
|
|
new_byte
|
|
|
|
)
|
|
|
|
local old_end_col = old_col + ((old_row == 0) and start_col or 0)
|
|
|
|
local new_end_col = new_col + ((new_row == 0) and start_col or 0)
|
|
|
|
|
2023-03-09 09:09:39 -07:00
|
|
|
self:_log(
|
|
|
|
'on_bytes',
|
|
|
|
bufnr,
|
|
|
|
changed_tick,
|
2023-03-08 10:59:45 -07:00
|
|
|
start_row,
|
|
|
|
start_col,
|
|
|
|
start_byte,
|
2023-03-09 09:09:39 -07:00
|
|
|
old_row,
|
|
|
|
old_col,
|
|
|
|
old_byte,
|
|
|
|
new_row,
|
|
|
|
new_col,
|
|
|
|
new_byte
|
|
|
|
)
|
2023-02-23 08:19:52 -07:00
|
|
|
|
|
|
|
-- Edit trees together BEFORE emitting a bytes callback.
|
2023-09-12 03:38:31 -07:00
|
|
|
self:_edit(
|
|
|
|
start_byte,
|
|
|
|
start_byte + old_byte,
|
|
|
|
start_byte + new_byte,
|
|
|
|
start_row,
|
|
|
|
start_col,
|
|
|
|
start_row + old_row,
|
|
|
|
old_end_col,
|
|
|
|
start_row + new_row,
|
|
|
|
new_end_col
|
|
|
|
)
|
2020-11-04 10:03:36 -07:00
|
|
|
|
2023-03-09 09:09:39 -07:00
|
|
|
self:_do_callback(
|
|
|
|
'bytes',
|
|
|
|
bufnr,
|
|
|
|
changed_tick,
|
|
|
|
start_row,
|
|
|
|
start_col,
|
|
|
|
start_byte,
|
|
|
|
old_row,
|
|
|
|
old_col,
|
|
|
|
old_byte,
|
|
|
|
new_row,
|
|
|
|
new_col,
|
|
|
|
new_byte
|
|
|
|
)
|
2020-11-04 10:03:36 -07:00
|
|
|
end
|
|
|
|
|
2023-03-23 04:23:51 -07:00
|
|
|
---@package
|
2021-02-06 23:32:19 -07:00
|
|
|
function LanguageTree:_on_reload()
|
|
|
|
self:invalidate(true)
|
|
|
|
end
|
|
|
|
|
2023-03-23 04:23:51 -07:00
|
|
|
---@package
|
2021-02-06 02:17:40 -07:00
|
|
|
function LanguageTree:_on_detach(...)
|
2021-02-06 23:32:19 -07:00
|
|
|
self:invalidate(true)
|
2021-02-06 02:17:40 -07:00
|
|
|
self:_do_callback('detach', ...)
|
2023-05-17 03:42:18 -07:00
|
|
|
if self._logfile then
|
|
|
|
self._logger('nvim', 'detaching')
|
|
|
|
self._logger = nil
|
|
|
|
self._logfile:close()
|
|
|
|
end
|
2021-02-06 02:17:40 -07:00
|
|
|
end
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
--- Registers callbacks for the |LanguageTree|.
|
|
|
|
---@param cbs table An |nvim_buf_attach()|-like table argument with the 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.
|
2023-05-13 12:33:22 -07:00
|
|
|
--- It will be passed two arguments: a table of the ranges (as node ranges) that
|
|
|
|
--- changed and the changed tree.
|
2022-02-13 06:43:25 -07:00
|
|
|
--- - `on_child_added` : emitted when a child is added to the tree.
|
|
|
|
--- - `on_child_removed` : emitted when a child is removed from the tree.
|
2023-05-13 12:33:22 -07:00
|
|
|
--- - `on_detach` : emitted when the buffer is detached, see |nvim_buf_detach_event|.
|
|
|
|
--- Takes one argument, the number of the buffer.
|
2023-04-30 08:11:38 -07:00
|
|
|
--- @param recursive? boolean Apply callbacks recursively for all children. Any new children will
|
|
|
|
--- also inherit the callbacks.
|
|
|
|
function LanguageTree:register_cbs(cbs, recursive)
|
2023-02-04 07:58:38 -07:00
|
|
|
---@cast cbs table<TSCallbackNameOn,function>
|
2020-11-04 10:03:36 -07:00
|
|
|
if not cbs then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-04-30 08:11:38 -07:00
|
|
|
local callbacks = recursive and self._callbacks_rec or self._callbacks
|
2020-11-04 10:03:36 -07:00
|
|
|
|
2023-04-30 08:11:38 -07:00
|
|
|
for name, cbname in pairs(TSCallbackNames) do
|
|
|
|
if cbs[name] then
|
|
|
|
table.insert(callbacks[cbname], cbs[name])
|
|
|
|
end
|
2020-11-04 10:03:36 -07:00
|
|
|
end
|
|
|
|
|
2023-04-30 08:11:38 -07:00
|
|
|
if recursive then
|
2023-09-12 03:38:31 -07:00
|
|
|
for _, child in pairs(self._children) do
|
2023-04-30 08:11:38 -07:00
|
|
|
child:register_cbs(cbs, true)
|
2023-09-12 03:38:31 -07:00
|
|
|
end
|
2020-11-04 10:03:36 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param tree TSTree
|
2023-03-10 09:40:27 -07:00
|
|
|
---@param range Range
|
2023-02-04 07:58:38 -07:00
|
|
|
---@return boolean
|
2021-01-19 06:51:41 -07:00
|
|
|
local function tree_contains(tree, range)
|
2023-02-23 08:19:52 -07:00
|
|
|
return Range.contains({ tree:root():range() }, range)
|
2020-11-04 10:03:36 -07:00
|
|
|
end
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
--- Determines whether {range} is contained in the |LanguageTree|.
|
2021-05-01 05:19:48 -07:00
|
|
|
---
|
2023-02-23 08:19:52 -07:00
|
|
|
---@param range Range4 `{ start_line, start_col, end_line, end_col }`
|
2022-09-14 02:08:31 -07:00
|
|
|
---@return boolean
|
2020-11-04 10:03:36 -07:00
|
|
|
function LanguageTree:contains(range)
|
2021-01-19 06:51:41 -07:00
|
|
|
for _, tree in pairs(self._trees) do
|
|
|
|
if tree_contains(tree, range) then
|
2020-11-04 10:03:36 -07:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
--- Gets the tree that contains {range}.
|
2022-04-30 01:43:26 -07:00
|
|
|
---
|
2023-02-23 08:19:52 -07:00
|
|
|
---@param range Range4 `{ start_line, start_col, end_line, end_col }`
|
2022-09-14 02:08:31 -07:00
|
|
|
---@param opts table|nil Optional keyword arguments:
|
|
|
|
--- - ignore_injections boolean Ignore injected languages (default true)
|
2023-02-04 07:58:38 -07:00
|
|
|
---@return TSTree|nil
|
2022-04-30 01:43:26 -07:00
|
|
|
function LanguageTree:tree_for_range(range, opts)
|
|
|
|
opts = opts or {}
|
|
|
|
local ignore = vim.F.if_nil(opts.ignore_injections, true)
|
|
|
|
|
|
|
|
if not ignore then
|
|
|
|
for _, child in pairs(self._children) do
|
2023-02-23 08:19:52 -07:00
|
|
|
local tree = child:tree_for_range(range, opts)
|
|
|
|
if tree then
|
|
|
|
return tree
|
2022-04-30 01:43:26 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, tree in pairs(self._trees) do
|
|
|
|
if tree_contains(tree, range) then
|
|
|
|
return tree
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
--- Gets the smallest named node that contains {range}.
|
2022-04-30 01:43:26 -07:00
|
|
|
---
|
2023-02-23 08:19:52 -07:00
|
|
|
---@param range Range4 `{ start_line, start_col, end_line, end_col }`
|
2022-09-14 02:08:31 -07:00
|
|
|
---@param opts table|nil Optional keyword arguments:
|
|
|
|
--- - ignore_injections boolean Ignore injected languages (default true)
|
2023-02-21 09:03:04 -07:00
|
|
|
---@return TSNode | nil Found node
|
2022-04-30 01:43:26 -07:00
|
|
|
function LanguageTree:named_node_for_range(range, opts)
|
|
|
|
local tree = self:tree_for_range(range, opts)
|
2022-12-08 09:22:57 -07:00
|
|
|
if tree then
|
|
|
|
return tree:root():named_descendant_for_range(unpack(range))
|
|
|
|
end
|
2022-04-30 01:43:26 -07:00
|
|
|
end
|
|
|
|
|
2022-09-14 02:08:31 -07:00
|
|
|
--- Gets the appropriate language that contains {range}.
|
2021-05-01 05:19:48 -07:00
|
|
|
---
|
2023-02-23 08:19:52 -07:00
|
|
|
---@param range Range4 `{ start_line, start_col, end_line, end_col }`
|
2022-09-14 02:08:31 -07:00
|
|
|
---@return LanguageTree Managing {range}
|
2020-11-04 10:03:36 -07:00
|
|
|
function LanguageTree:language_for_range(range)
|
|
|
|
for _, child in pairs(self._children) do
|
|
|
|
if child:contains(range) then
|
2020-12-04 15:15:47 -07:00
|
|
|
return child:language_for_range(range)
|
2020-11-04 10:03:36 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
return LanguageTree
|