fix: use luahl in treesitter

This commit is contained in:
Thomas Vigouroux 2020-09-11 15:57:08 +02:00 committed by Björn Linse
parent 18a3a89822
commit e4b5efa51e
2 changed files with 43 additions and 22 deletions

View File

@ -3,7 +3,6 @@ local a = vim.api
-- support reload for quick experimentation
local TSHighlighter = rawget(vim.treesitter, 'TSHighlighter') or {}
TSHighlighter.__index = TSHighlighter
local ts_hs_ns = a.nvim_create_namespace("treesitter_hl")
-- These are conventions defined by tree-sitter, though it
-- needs to be user extensible also.
@ -69,8 +68,15 @@ function TSHighlighter.new(query, bufnr, ft)
self.edit_count = 0
self.redraw_count = 0
self.line_count = {}
self.root = self.parser:parse():root()
a.nvim_buf_set_option(self.buf, "syntax", "")
a.nvim__buf_set_luahl(self.buf, {
on_start=function(...) return self:on_start(...) end,
on_window=function(...) return self:on_window(...) end,
on_line=function(...) return self:on_line(...) end,
})
-- 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
@ -100,6 +106,12 @@ function TSHighlighter:get_hl_from_capture(capture)
end
end
function TSHighlighter:on_changedtree(changes)
for _, ch in ipairs(changes or {}) do
a.nvim__buf_redraw_range(self.buf, ch[1], ch[3]+1)
end
end
function TSHighlighter:set_query(query)
if type(query) == "string" then
query = vim.treesitter.parse_query(self.parser.lang, query)
@ -123,26 +135,38 @@ function TSHighlighter:set_query(query)
end
})
self:on_changedtree({{self.parser:parse():root():range()}})
a.nvim__buf_redraw_range(self.buf, 0, a.nvim_buf_line_count(self.buf))
end
function TSHighlighter:on_changedtree(changes)
-- Get a fresh root
local root = self.parser:parse():root()
function TSHighlighter:on_window(_, _win, _buf, _topline, botline)
self.iter = nil
self.nextrow = 0
self.botline = botline
self.redraw_count = self.redraw_count + 1
end
for _, ch in ipairs(changes or {}) do
a.nvim_buf_clear_namespace(self.buf, ts_hs_ns, ch[1], ch[3]+1)
function TSHighlighter:on_start(_, _buf, _tick)
local tree = self.parser:parse()
self.root = tree:root()
end
for capture, node in self.query:iter_captures(root, self.buf, ch[1], ch[3] + 1) do
local start_row, start_col, end_row, end_col = node:range()
local hl = self.hl_cache[capture]
if hl then
a.nvim_buf_set_extmark(self.buf, ts_hs_ns, start_row, start_col, {
end_col = end_col,
end_line = end_row,
hl_group = hl
})
end
function TSHighlighter:on_line(_, _win, buf, line)
if self.iter == nil then
self.iter = self.query:iter_captures(self.root,buf,line,self.botline)
end
while line >= self.nextrow do
local capture, node = self.iter()
if capture == nil then
break
end
local start_row, start_col, end_row, end_col = node:range()
local hl = self.hl_cache[capture]
if hl and end_row >= line then
a.nvim__put_attr(start_row, start_col, { end_line = end_row, end_col = end_col, hl_group = hl })
end
if start_row > line then
self.nextrow = start_row
end
end
end

View File

@ -446,10 +446,7 @@ static int nlua_schedule(lua_State *const lstate)
]]}
feed("5Goc<esc>dd")
if true == true then
pending('reenable this check in luahl PR')
return
end
screen:expect{grid=[[
{2:/// Schedule Lua callback on main loop's event queue} |
{3:static} {3:int} {11:nlua_schedule}({3:lua_State} *{3:const} lstate) |
@ -480,7 +477,7 @@ static int nlua_schedule(lua_State *const lstate)
|| {6:lstate} != {6:lstate}) { |
{11:lua_pushliteral}(lstate, {5:"vim.schedule: expected function"}); |
{4:return} {11:lua_error}(lstate); |
*^/ |
{8:*^/} |
} |
|
{7:LuaRef} cb = {11:nlua_ref}(lstate, {5:1}); |