2023-04-05 09:19:53 -07:00
|
|
|
local api = vim.api
|
2023-03-24 07:43:14 -07:00
|
|
|
local query = vim.treesitter.query
|
2023-08-10 06:21:56 -07:00
|
|
|
local Range = require('vim.treesitter._range')
|
2019-09-28 05:27:20 -07:00
|
|
|
|
2023-12-20 03:48:39 -07:00
|
|
|
local ns = api.nvim_create_namespace('treesitter/highlighter')
|
2019-09-28 05:27:20 -07:00
|
|
|
|
2023-12-20 03:48:39 -07:00
|
|
|
---@alias vim.TSHlIter fun(end_line: integer|nil): integer, TSNode, TSMetadata
|
2020-09-12 01:29:30 -07:00
|
|
|
|
2023-12-20 03:48:39 -07:00
|
|
|
---@class vim.TSHighlighterQuery
|
|
|
|
---@field private _query Query?
|
|
|
|
---@field private lang string
|
|
|
|
---@field private hl_cache table<integer,integer>
|
2020-11-04 10:03:36 -07:00
|
|
|
local TSHighlighterQuery = {}
|
|
|
|
TSHighlighterQuery.__index = TSHighlighterQuery
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
---@private
|
2023-12-20 03:48:39 -07:00
|
|
|
---@param lang string
|
|
|
|
---@param query_string string?
|
|
|
|
---@return vim.TSHighlighterQuery
|
2020-11-04 10:03:36 -07:00
|
|
|
function TSHighlighterQuery.new(lang, query_string)
|
2023-12-20 03:48:39 -07:00
|
|
|
local self = setmetatable({}, TSHighlighterQuery)
|
|
|
|
self.lang = lang
|
|
|
|
self.hl_cache = {}
|
2020-11-04 10:03:36 -07:00
|
|
|
|
|
|
|
if query_string then
|
2023-03-24 07:43:14 -07:00
|
|
|
self._query = query.parse(lang, query_string)
|
2020-11-04 10:03:36 -07:00
|
|
|
else
|
2023-03-24 07:43:14 -07:00
|
|
|
self._query = query.get(lang, 'highlights')
|
2020-11-04 10:03:36 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2023-12-20 03:48:39 -07:00
|
|
|
---@package
|
|
|
|
---@param capture integer
|
|
|
|
---@return integer?
|
|
|
|
function TSHighlighterQuery:get_hl_from_capture(capture)
|
|
|
|
if not self.hl_cache[capture] then
|
|
|
|
local name = self._query.captures[capture]
|
|
|
|
local id = 0
|
|
|
|
if not vim.startswith(name, '_') then
|
|
|
|
id = api.nvim_get_hl_id_by_name('@' .. name .. '.' .. self.lang)
|
|
|
|
end
|
|
|
|
self.hl_cache[capture] = id
|
|
|
|
end
|
|
|
|
|
|
|
|
return self.hl_cache[capture]
|
|
|
|
end
|
|
|
|
|
2023-03-23 04:23:51 -07:00
|
|
|
---@package
|
2020-11-04 10:03:36 -07:00
|
|
|
function TSHighlighterQuery:query()
|
|
|
|
return self._query
|
|
|
|
end
|
|
|
|
|
2023-12-20 03:48:39 -07:00
|
|
|
---@class vim.TSHighlightState
|
|
|
|
---@field tstree TSTree
|
|
|
|
---@field next_row integer
|
|
|
|
---@field iter vim.TSHlIter?
|
|
|
|
---@field highlighter_query vim.TSHighlighterQuery
|
|
|
|
|
|
|
|
---@class vim.TSHighlighter
|
|
|
|
---@field active table<integer,vim.TSHighlighter>
|
|
|
|
---@field bufnr integer
|
|
|
|
---@field orig_spelloptions string
|
|
|
|
--- A map of highlight states.
|
|
|
|
--- This state is kept during rendering across each line update.
|
|
|
|
---@field _highlight_states vim.TSHighlightState[]
|
|
|
|
---@field _queries table<string,vim.TSHighlighterQuery>
|
|
|
|
---@field tree LanguageTree
|
|
|
|
---@field redraw_count integer
|
|
|
|
local TSHighlighter = {
|
|
|
|
active = {},
|
|
|
|
}
|
|
|
|
|
|
|
|
TSHighlighter.__index = TSHighlighter
|
|
|
|
|
2023-03-23 04:23:51 -07:00
|
|
|
---@package
|
2023-03-02 12:46:59 -07:00
|
|
|
---
|
|
|
|
--- Creates a highlighter for `tree`.
|
2021-05-01 05:19:48 -07:00
|
|
|
---
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param tree LanguageTree parser object to use for highlighting
|
2022-09-14 02:08:31 -07:00
|
|
|
---@param opts (table|nil) Configuration of the highlighter:
|
|
|
|
--- - queries table overwrite queries used by the highlighter
|
2023-12-20 03:48:39 -07:00
|
|
|
---@return vim.TSHighlighter Created highlighter object
|
2020-11-04 10:03:36 -07:00
|
|
|
function TSHighlighter.new(tree, opts)
|
2019-09-28 05:27:20 -07:00
|
|
|
local self = setmetatable({}, TSHighlighter)
|
2020-09-21 05:53:32 -07:00
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
if type(tree:source()) ~= 'number' then
|
|
|
|
error('TSHighlighter can not be used with a string parser source.')
|
|
|
|
end
|
|
|
|
|
2023-02-04 07:58:38 -07:00
|
|
|
opts = opts or {} ---@type { queries: table<string,string> }
|
2020-11-04 10:03:36 -07:00
|
|
|
self.tree = tree
|
|
|
|
tree:register_cbs({
|
2021-02-06 02:17:40 -07:00
|
|
|
on_bytes = function(...)
|
|
|
|
self:on_bytes(...)
|
|
|
|
end,
|
2023-03-23 04:23:51 -07:00
|
|
|
on_detach = function()
|
|
|
|
self:on_detach()
|
2021-02-06 02:17:40 -07:00
|
|
|
end,
|
2020-09-21 05:53:32 -07:00
|
|
|
})
|
|
|
|
|
2023-04-30 08:11:38 -07:00
|
|
|
tree:register_cbs({
|
|
|
|
on_changedtree = function(...)
|
|
|
|
self:on_changedtree(...)
|
|
|
|
end,
|
|
|
|
on_child_removed = function(child)
|
|
|
|
child:for_each_tree(function(t)
|
|
|
|
self:on_changedtree(t:included_ranges(true))
|
|
|
|
end)
|
|
|
|
end,
|
|
|
|
}, true)
|
|
|
|
|
2023-12-20 03:48:39 -07:00
|
|
|
local source = tree:source()
|
|
|
|
assert(type(source) == 'number')
|
|
|
|
|
|
|
|
self.bufnr = source
|
2019-09-28 05:27:20 -07:00
|
|
|
self.redraw_count = 0
|
2020-11-04 10:03:36 -07:00
|
|
|
self._highlight_states = {}
|
|
|
|
self._queries = {}
|
|
|
|
|
|
|
|
-- Queries for a specific language can be overridden by a custom
|
|
|
|
-- string query... if one is not provided it will be looked up by file.
|
|
|
|
if opts.queries then
|
|
|
|
for lang, query_string in pairs(opts.queries) do
|
|
|
|
self._queries[lang] = TSHighlighterQuery.new(lang, query_string)
|
|
|
|
end
|
2020-10-05 23:40:54 -07:00
|
|
|
end
|
|
|
|
|
2022-12-14 15:54:58 -07:00
|
|
|
self.orig_spelloptions = vim.bo[self.bufnr].spelloptions
|
|
|
|
|
2022-09-06 11:22:05 -07:00
|
|
|
vim.bo[self.bufnr].syntax = ''
|
2022-12-14 08:00:48 -07:00
|
|
|
vim.b[self.bufnr].ts_highlight = true
|
2020-11-04 10:03:36 -07:00
|
|
|
|
|
|
|
TSHighlighter.active[self.bufnr] = self
|
2020-09-11 06:57:08 -07:00
|
|
|
|
2019-09-28 05:27:20 -07:00
|
|
|
-- Tricky: if syntax hasn't been enabled, we need to reload color scheme
|
|
|
|
-- but use synload.vim rather than syntax.vim to not enable
|
|
|
|
-- syntax FileType autocmds. Later on we should integrate with the
|
|
|
|
-- `:syntax` and `set syntax=...` machinery properly.
|
|
|
|
if vim.g.syntax_on ~= 1 then
|
2022-09-06 11:22:05 -07:00
|
|
|
vim.cmd.runtime({ 'syntax/synload.vim', bang = true })
|
2019-09-28 05:27:20 -07:00
|
|
|
end
|
2022-09-06 11:22:05 -07:00
|
|
|
|
2023-04-05 09:19:53 -07:00
|
|
|
api.nvim_buf_call(self.bufnr, function()
|
2022-09-06 11:22:05 -07:00
|
|
|
vim.opt_local.spelloptions:append('noplainbuffer')
|
|
|
|
end)
|
2020-11-04 10:03:36 -07:00
|
|
|
|
|
|
|
self.tree:parse()
|
|
|
|
|
2019-09-28 05:27:20 -07:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2023-08-10 06:21:56 -07:00
|
|
|
--- @nodoc
|
2021-05-01 05:19:48 -07:00
|
|
|
--- Removes all internal references to the highlighter
|
2020-11-04 10:03:36 -07:00
|
|
|
function TSHighlighter:destroy()
|
2023-12-20 03:48:39 -07:00
|
|
|
TSHighlighter.active[self.bufnr] = nil
|
2022-12-14 08:00:48 -07:00
|
|
|
|
2023-12-20 03:48:39 -07:00
|
|
|
if api.nvim_buf_is_loaded(self.bufnr) then
|
2022-12-14 15:54:58 -07:00
|
|
|
vim.bo[self.bufnr].spelloptions = self.orig_spelloptions
|
2023-01-17 09:56:23 -07:00
|
|
|
vim.b[self.bufnr].ts_highlight = nil
|
|
|
|
if vim.g.syntax_on == 1 then
|
2023-04-05 09:19:53 -07:00
|
|
|
api.nvim_exec_autocmds('FileType', { group = 'syntaxset', buffer = self.bufnr })
|
2023-01-17 09:56:23 -07:00
|
|
|
end
|
2022-12-14 15:54:58 -07:00
|
|
|
end
|
2020-07-06 13:11:30 -07:00
|
|
|
end
|
|
|
|
|
2023-12-16 23:25:15 -07:00
|
|
|
---@param srow integer
|
|
|
|
---@param erow integer exclusive
|
|
|
|
---@private
|
|
|
|
function TSHighlighter:prepare_highlight_states(srow, erow)
|
2023-12-18 05:02:32 -07:00
|
|
|
self._highlight_states = {}
|
|
|
|
|
2023-12-16 23:25:15 -07:00
|
|
|
self.tree:for_each_tree(function(tstree, tree)
|
|
|
|
if not tstree then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local root_node = tstree:root()
|
|
|
|
local root_start_row, _, root_end_row, _ = root_node:range()
|
|
|
|
|
2023-12-20 03:48:39 -07:00
|
|
|
-- Only consider trees within the visible range
|
2023-12-16 23:25:15 -07:00
|
|
|
if root_start_row > erow or root_end_row < srow then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local highlighter_query = self:get_query(tree:lang())
|
|
|
|
|
|
|
|
-- Some injected languages may not have highlight queries.
|
|
|
|
if not highlighter_query:query() then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- _highlight_states should be a list so that the highlights are added in the same order as
|
|
|
|
-- for_each_tree traversal. This ensures that parents' highlight don't override children's.
|
|
|
|
table.insert(self._highlight_states, {
|
|
|
|
tstree = tstree,
|
2020-11-04 10:03:36 -07:00
|
|
|
next_row = 0,
|
|
|
|
iter = nil,
|
2023-12-16 23:25:15 -07:00
|
|
|
highlighter_query = highlighter_query,
|
|
|
|
})
|
|
|
|
end)
|
|
|
|
end
|
2020-07-06 13:11:30 -07:00
|
|
|
|
2023-12-20 03:48:39 -07:00
|
|
|
---@param fn fun(state: vim.TSHighlightState)
|
2023-12-16 23:25:15 -07:00
|
|
|
---@package
|
|
|
|
function TSHighlighter:for_each_highlight_state(fn)
|
|
|
|
for _, state in ipairs(self._highlight_states) do
|
|
|
|
fn(state)
|
|
|
|
end
|
2020-11-04 10:03:36 -07:00
|
|
|
end
|
2020-07-06 13:11:30 -07:00
|
|
|
|
2023-03-23 04:23:51 -07:00
|
|
|
---@package
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param start_row integer
|
|
|
|
---@param new_end integer
|
2020-11-04 10:03:36 -07:00
|
|
|
function TSHighlighter:on_bytes(_, _, start_row, _, _, _, _, _, new_end)
|
2023-04-05 09:19:53 -07:00
|
|
|
api.nvim__buf_redraw_range(self.bufnr, start_row, start_row + new_end + 1)
|
2020-07-06 13:11:30 -07:00
|
|
|
end
|
|
|
|
|
2023-03-23 04:23:51 -07:00
|
|
|
---@package
|
2021-02-06 02:17:40 -07:00
|
|
|
function TSHighlighter:on_detach()
|
|
|
|
self:destroy()
|
|
|
|
end
|
|
|
|
|
2023-03-23 04:23:51 -07:00
|
|
|
---@package
|
2023-08-10 06:21:56 -07:00
|
|
|
---@param changes Range6[]
|
2020-09-11 06:57:08 -07:00
|
|
|
function TSHighlighter:on_changedtree(changes)
|
2023-04-30 08:11:38 -07:00
|
|
|
for _, ch in ipairs(changes) do
|
|
|
|
api.nvim__buf_redraw_range(self.bufnr, ch[1], ch[4] + 1)
|
2020-09-11 06:57:08 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
--- Gets the query used for @param lang
|
2022-09-14 02:08:31 -07:00
|
|
|
--
|
2023-03-23 04:23:51 -07:00
|
|
|
---@package
|
2022-09-14 02:08:31 -07:00
|
|
|
---@param lang string Language used by the highlighter.
|
2023-12-20 03:48:39 -07:00
|
|
|
---@return vim.TSHighlighterQuery
|
2020-11-04 10:03:36 -07:00
|
|
|
function TSHighlighter:get_query(lang)
|
|
|
|
if not self._queries[lang] then
|
|
|
|
self._queries[lang] = TSHighlighterQuery.new(lang)
|
2019-09-28 05:27:20 -07:00
|
|
|
end
|
2020-07-14 12:50:57 -07:00
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
return self._queries[lang]
|
|
|
|
end
|
2019-09-28 05:27:20 -07:00
|
|
|
|
2023-12-20 03:48:39 -07:00
|
|
|
---@param self vim.TSHighlighter
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param buf integer
|
|
|
|
---@param line integer
|
|
|
|
---@param is_spell_nav boolean
|
2022-10-28 05:09:22 -07:00
|
|
|
local function on_line_impl(self, buf, line, is_spell_nav)
|
2023-12-16 23:25:15 -07:00
|
|
|
self:for_each_highlight_state(function(state)
|
|
|
|
local root_node = state.tstree:root()
|
2020-11-04 10:03:36 -07:00
|
|
|
local root_start_row, _, root_end_row, _ = root_node:range()
|
|
|
|
|
2023-12-20 03:48:39 -07:00
|
|
|
-- Only consider trees that contain this line
|
2020-11-04 10:03:36 -07:00
|
|
|
if root_start_row > line or root_end_row < line then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-05-26 08:29:31 -07:00
|
|
|
if state.iter == nil or state.next_row < line then
|
2020-11-04 10:03:36 -07:00
|
|
|
state.iter =
|
2023-12-16 23:25:15 -07:00
|
|
|
state.highlighter_query:query():iter_captures(root_node, self.bufnr, line, root_end_row + 1)
|
2020-07-06 13:11:30 -07:00
|
|
|
end
|
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
while line >= state.next_row do
|
2023-09-16 02:48:49 -07:00
|
|
|
local capture, node, metadata = state.iter(line)
|
2019-09-28 05:27:20 -07:00
|
|
|
|
2023-09-16 02:48:49 -07:00
|
|
|
local range = { root_end_row + 1, 0, root_end_row + 1, 0 }
|
|
|
|
if node then
|
|
|
|
range = vim.treesitter.get_range(node, buf, metadata and metadata[capture])
|
2020-11-04 10:03:36 -07:00
|
|
|
end
|
2023-08-10 06:21:56 -07:00
|
|
|
local start_row, start_col, end_row, end_col = Range.unpack4(range)
|
2022-10-28 05:09:22 -07:00
|
|
|
|
2023-09-16 02:48:49 -07:00
|
|
|
if capture then
|
2023-12-20 03:48:39 -07:00
|
|
|
local hl = state.highlighter_query:get_hl_from_capture(capture)
|
2023-09-16 02:48:49 -07:00
|
|
|
|
2023-12-16 23:25:15 -07:00
|
|
|
local capture_name = state.highlighter_query:query().captures[capture]
|
2023-09-16 02:48:49 -07:00
|
|
|
local spell = nil ---@type boolean?
|
|
|
|
if capture_name == 'spell' then
|
|
|
|
spell = true
|
|
|
|
elseif capture_name == 'nospell' then
|
|
|
|
spell = false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Give nospell a higher priority so it always overrides spell captures.
|
|
|
|
local spell_pri_offset = capture_name == 'nospell' and 1 or 0
|
|
|
|
|
|
|
|
if hl and end_row >= line and (not is_spell_nav or spell ~= nil) then
|
|
|
|
local priority = (tonumber(metadata.priority) or vim.highlight.priorities.treesitter)
|
|
|
|
+ spell_pri_offset
|
|
|
|
api.nvim_buf_set_extmark(buf, ns, start_row, start_col, {
|
|
|
|
end_line = end_row,
|
|
|
|
end_col = end_col,
|
|
|
|
hl_group = hl,
|
|
|
|
ephemeral = true,
|
|
|
|
priority = priority,
|
|
|
|
conceal = metadata.conceal,
|
|
|
|
spell = spell,
|
|
|
|
})
|
|
|
|
end
|
2020-11-04 10:03:36 -07:00
|
|
|
end
|
2023-09-16 02:48:49 -07:00
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
if start_row > line then
|
|
|
|
state.next_row = start_row
|
|
|
|
end
|
2020-09-11 06:57:08 -07:00
|
|
|
end
|
2023-03-23 04:23:51 -07:00
|
|
|
end)
|
2019-09-28 05:27:20 -07:00
|
|
|
end
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
---@private
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param _win integer
|
|
|
|
---@param buf integer
|
|
|
|
---@param line integer
|
2020-11-04 10:03:36 -07:00
|
|
|
function TSHighlighter._on_line(_, _win, buf, line, _)
|
|
|
|
local self = TSHighlighter.active[buf]
|
|
|
|
if not self then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-07-18 05:21:40 -07:00
|
|
|
on_line_impl(self, buf, line, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param buf integer
|
|
|
|
---@param srow integer
|
|
|
|
---@param erow integer
|
2022-07-18 05:21:40 -07:00
|
|
|
function TSHighlighter._on_spell_nav(_, _, buf, srow, _, erow, _)
|
|
|
|
local self = TSHighlighter.active[buf]
|
|
|
|
if not self then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-12-18 05:02:32 -07:00
|
|
|
self:prepare_highlight_states(srow, erow)
|
2022-07-18 05:21:40 -07:00
|
|
|
|
|
|
|
for row = srow, erow do
|
|
|
|
on_line_impl(self, buf, row, true)
|
|
|
|
end
|
2020-09-12 01:29:30 -07:00
|
|
|
end
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
---@private
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param _win integer
|
|
|
|
---@param buf integer
|
2023-08-10 06:21:56 -07:00
|
|
|
---@param topline integer
|
|
|
|
---@param botline integer
|
|
|
|
function TSHighlighter._on_win(_, _win, buf, topline, botline)
|
2020-11-04 10:03:36 -07:00
|
|
|
local self = TSHighlighter.active[buf]
|
|
|
|
if not self then
|
|
|
|
return false
|
|
|
|
end
|
2023-09-11 12:29:39 -07:00
|
|
|
self.tree:parse({ topline, botline + 1 })
|
2023-12-16 23:25:15 -07:00
|
|
|
self:prepare_highlight_states(topline, botline + 1)
|
2020-11-04 10:03:36 -07:00
|
|
|
self.redraw_count = self.redraw_count + 1
|
2020-09-12 01:29:30 -07:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2023-04-05 09:19:53 -07:00
|
|
|
api.nvim_set_decoration_provider(ns, {
|
2020-09-12 01:29:30 -07:00
|
|
|
on_win = TSHighlighter._on_win,
|
|
|
|
on_line = TSHighlighter._on_line,
|
2022-07-18 05:21:40 -07:00
|
|
|
_on_spell_nav = TSHighlighter._on_spell_nav,
|
2020-09-21 01:37:28 -07:00
|
|
|
})
|
2020-09-12 01:29:30 -07:00
|
|
|
|
2019-09-28 05:27:20 -07:00
|
|
|
return TSHighlighter
|