2019-09-28 05:27:20 -07:00
|
|
|
local a = vim.api
|
2020-11-04 10:03:36 -07:00
|
|
|
local query = require('vim.treesitter.query')
|
2019-09-28 05:27:20 -07:00
|
|
|
|
|
|
|
-- support reload for quick experimentation
|
2022-09-14 02:08:31 -07:00
|
|
|
---@class TSHighlighter
|
2019-09-28 05:27:20 -07:00
|
|
|
local TSHighlighter = rawget(vim.treesitter, 'TSHighlighter') or {}
|
|
|
|
TSHighlighter.__index = TSHighlighter
|
|
|
|
|
2020-09-12 01:29:30 -07:00
|
|
|
TSHighlighter.active = TSHighlighter.active or {}
|
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
local TSHighlighterQuery = {}
|
|
|
|
TSHighlighterQuery.__index = TSHighlighterQuery
|
|
|
|
|
2020-09-21 01:37:28 -07:00
|
|
|
local ns = a.nvim_create_namespace('treesitter/highlighter')
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
---@private
|
2020-11-04 10:03:36 -07:00
|
|
|
function TSHighlighterQuery.new(lang, query_string)
|
|
|
|
local self = setmetatable({}, { __index = TSHighlighterQuery })
|
|
|
|
|
|
|
|
self.hl_cache = setmetatable({}, {
|
|
|
|
__index = function(table, capture)
|
2022-08-24 14:48:52 -07:00
|
|
|
local name = self._query.captures[capture]
|
|
|
|
local id = 0
|
|
|
|
if not vim.startswith(name, '_') then
|
|
|
|
id = a.nvim_get_hl_id_by_name('@' .. name .. '.' .. lang)
|
2021-03-29 13:07:21 -07:00
|
|
|
end
|
2020-11-04 10:03:36 -07:00
|
|
|
|
2021-08-19 07:45:04 -07:00
|
|
|
rawset(table, capture, id)
|
|
|
|
return id
|
2020-11-04 10:03:36 -07:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
if query_string then
|
|
|
|
self._query = query.parse_query(lang, query_string)
|
|
|
|
else
|
|
|
|
self._query = query.get_query(lang, 'highlights')
|
|
|
|
end
|
|
|
|
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
---@private
|
2020-11-04 10:03:36 -07:00
|
|
|
function TSHighlighterQuery:query()
|
|
|
|
return self._query
|
|
|
|
end
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
--- Creates a new highlighter using @param tree
|
|
|
|
---
|
2022-09-14 02:08:31 -07:00
|
|
|
---@param tree LanguageTree |LanguageTree| parser object to use for highlighting
|
|
|
|
---@param opts (table|nil) Configuration of the highlighter:
|
|
|
|
--- - queries table overwrite queries used by the highlighter
|
|
|
|
---@return 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
|
|
|
|
|
|
|
|
opts = opts or {}
|
|
|
|
self.tree = tree
|
|
|
|
tree:register_cbs({
|
2021-02-06 02:17:40 -07:00
|
|
|
on_changedtree = function(...)
|
|
|
|
self:on_changedtree(...)
|
|
|
|
end,
|
|
|
|
on_bytes = function(...)
|
|
|
|
self:on_bytes(...)
|
|
|
|
end,
|
|
|
|
on_detach = function(...)
|
|
|
|
self:on_detach(...)
|
|
|
|
end,
|
2020-09-21 05:53:32 -07:00
|
|
|
})
|
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
self.bufnr = tree:source()
|
2019-09-28 05:27:20 -07:00
|
|
|
self.edit_count = 0
|
|
|
|
self.redraw_count = 0
|
|
|
|
self.line_count = {}
|
2020-11-04 10:03:36 -07:00
|
|
|
-- A map of highlight states.
|
|
|
|
-- This state is kept during rendering across each line update.
|
|
|
|
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-09-06 11:22:05 -07:00
|
|
|
vim.bo[self.bufnr].syntax = ''
|
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
|
|
|
|
|
|
|
a.nvim_buf_call(self.bufnr, function()
|
|
|
|
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
|
|
|
|
|
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()
|
|
|
|
if TSHighlighter.active[self.bufnr] then
|
|
|
|
TSHighlighter.active[self.bufnr] = nil
|
|
|
|
end
|
2020-07-06 13:11:30 -07:00
|
|
|
end
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
---@private
|
2020-11-04 10:03:36 -07:00
|
|
|
function TSHighlighter:get_highlight_state(tstree)
|
|
|
|
if not self._highlight_states[tstree] then
|
|
|
|
self._highlight_states[tstree] = {
|
|
|
|
next_row = 0,
|
|
|
|
iter = nil,
|
|
|
|
}
|
|
|
|
end
|
2020-07-06 13:11:30 -07:00
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
return self._highlight_states[tstree]
|
|
|
|
end
|
2020-07-06 13:11:30 -07:00
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
---@private
|
2020-11-04 10:03:36 -07:00
|
|
|
function TSHighlighter:reset_highlight_state()
|
|
|
|
self._highlight_states = {}
|
|
|
|
end
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
---@private
|
2020-11-04 10:03:36 -07:00
|
|
|
function TSHighlighter:on_bytes(_, _, start_row, _, _, _, _, _, new_end)
|
|
|
|
a.nvim__buf_redraw_range(self.bufnr, start_row, start_row + new_end + 1)
|
2020-07-06 13:11:30 -07:00
|
|
|
end
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
---@private
|
2021-02-06 02:17:40 -07:00
|
|
|
function TSHighlighter:on_detach()
|
|
|
|
self:destroy()
|
|
|
|
end
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
---@private
|
2020-09-11 06:57:08 -07:00
|
|
|
function TSHighlighter:on_changedtree(changes)
|
|
|
|
for _, ch in ipairs(changes or {}) do
|
2020-11-04 10:03:36 -07:00
|
|
|
a.nvim__buf_redraw_range(self.bufnr, ch[1], ch[3] + 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
|
|
|
--
|
|
|
|
---@private
|
|
|
|
---@param lang string Language used by the highlighter.
|
|
|
|
---@return Query
|
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
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
---@private
|
2022-10-28 05:09:22 -07:00
|
|
|
local function on_line_impl(self, buf, line, is_spell_nav)
|
2020-11-04 10:03:36 -07:00
|
|
|
self.tree:for_each_tree(function(tstree, tree)
|
|
|
|
if not tstree then
|
|
|
|
return
|
|
|
|
end
|
2020-07-06 13:11:30 -07:00
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
local root_node = tstree:root()
|
|
|
|
local root_start_row, _, root_end_row, _ = root_node:range()
|
|
|
|
|
|
|
|
-- Only worry about trees within the line range
|
|
|
|
if root_start_row > line or root_end_row < line then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local state = self:get_highlight_state(tstree)
|
|
|
|
local highlighter_query = self:get_query(tree:lang())
|
|
|
|
|
2021-03-26 09:25:19 -07:00
|
|
|
-- Some injected languages may not have highlight queries.
|
|
|
|
if not highlighter_query:query() 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 =
|
|
|
|
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
|
2021-07-17 05:41:17 -07:00
|
|
|
local capture, node, metadata = state.iter()
|
2019-09-28 05:27:20 -07:00
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
if capture == nil then
|
|
|
|
break
|
|
|
|
end
|
2020-10-05 23:40:54 -07:00
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
local start_row, start_col, end_row, end_col = node:range()
|
|
|
|
local hl = highlighter_query.hl_cache[capture]
|
2020-09-11 06:57:08 -07:00
|
|
|
|
2022-10-28 05:09:22 -07:00
|
|
|
local capture_name = highlighter_query:query().captures[capture]
|
|
|
|
local spell = nil
|
|
|
|
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
|
2022-07-18 05:21:40 -07:00
|
|
|
|
2022-10-28 05:09:22 -07:00
|
|
|
if hl and end_row >= line and (not is_spell_nav or spell ~= nil) then
|
2020-11-04 10:03:36 -07:00
|
|
|
a.nvim_buf_set_extmark(buf, ns, start_row, start_col, {
|
|
|
|
end_line = end_row,
|
|
|
|
end_col = end_col,
|
|
|
|
hl_group = hl,
|
2020-11-23 04:25:36 -07:00
|
|
|
ephemeral = true,
|
2022-10-28 05:09:22 -07:00
|
|
|
priority = (tonumber(metadata.priority) or 100) + spell_pri_offset, -- Low but leaves room below
|
2022-03-19 05:48:03 -07:00
|
|
|
conceal = metadata.conceal,
|
2022-10-28 05:09:22 -07:00
|
|
|
spell = spell,
|
2020-11-04 10:03:36 -07:00
|
|
|
})
|
|
|
|
end
|
|
|
|
if start_row > line then
|
|
|
|
state.next_row = start_row
|
|
|
|
end
|
2020-09-11 06:57:08 -07:00
|
|
|
end
|
2020-11-04 10:03:36 -07:00
|
|
|
end, true)
|
2019-09-28 05:27:20 -07:00
|
|
|
end
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
---@private
|
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
|
|
|
|
function TSHighlighter._on_spell_nav(_, _, buf, srow, _, erow, _)
|
|
|
|
local self = TSHighlighter.active[buf]
|
|
|
|
if not self then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
self:reset_highlight_state()
|
|
|
|
|
|
|
|
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
|
2020-10-05 23:40:54 -07:00
|
|
|
function TSHighlighter._on_buf(_, buf)
|
2020-11-04 10:03:36 -07:00
|
|
|
local self = TSHighlighter.active[buf]
|
|
|
|
if self then
|
|
|
|
self.tree:parse()
|
|
|
|
end
|
2020-10-05 23:40:54 -07:00
|
|
|
end
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
---@private
|
2020-11-04 10:03:36 -07:00
|
|
|
function TSHighlighter._on_win(_, _win, buf, _topline)
|
|
|
|
local self = TSHighlighter.active[buf]
|
|
|
|
if not self then
|
|
|
|
return false
|
|
|
|
end
|
2020-09-12 01:29:30 -07:00
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
self:reset_highlight_state()
|
|
|
|
self.redraw_count = self.redraw_count + 1
|
2020-09-12 01:29:30 -07:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2020-09-21 01:37:28 -07:00
|
|
|
a.nvim_set_decoration_provider(ns, {
|
|
|
|
on_buf = TSHighlighter._on_buf,
|
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
|