treesitter: add parser on_lines callbacks

This commit is contained in:
Thomas Vigouroux 2020-07-08 22:47:57 +02:00
parent 0f7eaa3555
commit 341e139992
2 changed files with 33 additions and 13 deletions

View File

@ -15,19 +15,27 @@ function Parser:parse()
local changes local changes
self.tree, changes = self._parser:parse_buf(self.bufnr) self.tree, changes = self._parser:parse_buf(self.bufnr)
self.valid = true self.valid = true
for _, cb in ipairs(self.change_cbs) do
cb(changes) if not vim.tbl_isempty(changes) then
for _, cb in ipairs(self.changedtree_cbs) do
cb(changes)
end
end end
return self.tree, changes return self.tree, changes
end end
function Parser:_on_lines(bufnr, _, start_row, old_stop_row, stop_row, old_byte_size) function Parser:_on_lines(bufnr, changed_tick, start_row, old_stop_row, stop_row, old_byte_size)
local start_byte = a.nvim_buf_get_offset(bufnr,start_row) local start_byte = a.nvim_buf_get_offset(bufnr,start_row)
local stop_byte = a.nvim_buf_get_offset(bufnr,stop_row) local stop_byte = a.nvim_buf_get_offset(bufnr,stop_row)
local old_stop_byte = start_byte + old_byte_size local old_stop_byte = start_byte + old_byte_size
self._parser:edit(start_byte,old_stop_byte,stop_byte, self._parser:edit(start_byte,old_stop_byte,stop_byte,
start_row,0,old_stop_row,0,stop_row,0) start_row,0,old_stop_row,0,stop_row,0)
self.valid = false self.valid = false
for _, cb in ipairs(self.lines_cbs) do
cb(bufnr, changed_tick, start_row, old_stop_row, stop_row, old_byte_size)
end
end end
function Parser:set_included_ranges(ranges) function Parser:set_included_ranges(ranges)
@ -80,7 +88,8 @@ function M.create_parser(bufnr, lang, id)
local self = setmetatable({bufnr=bufnr, lang=lang, valid=false}, Parser) local self = setmetatable({bufnr=bufnr, lang=lang, valid=false}, Parser)
self._parser = vim._create_ts_parser(lang) self._parser = vim._create_ts_parser(lang)
self.change_cbs = {} self.changedtree_cbs = {}
self.lines_cbs = {}
self:parse() self:parse()
-- TODO(bfredl): use weakref to self, so that the parser is free'd is no plugin is -- TODO(bfredl): use weakref to self, so that the parser is free'd is no plugin is
-- using it. -- using it.
@ -99,7 +108,7 @@ function M.create_parser(bufnr, lang, id)
return self return self
end end
function M.get_parser(bufnr, ft, cb) function M.get_parser(bufnr, ft, buf_attach_cbs)
if bufnr == nil or bufnr == 0 then if bufnr == nil or bufnr == 0 then
bufnr = a.nvim_get_current_buf() bufnr = a.nvim_get_current_buf()
end end
@ -111,9 +120,15 @@ function M.get_parser(bufnr, ft, cb)
if parsers[id] == nil then if parsers[id] == nil then
parsers[id] = M.create_parser(bufnr, ft, id) parsers[id] = M.create_parser(bufnr, ft, id)
end end
if cb ~= nil then
table.insert(parsers[id].change_cbs, cb) if buf_attach_cbs and buf_attach_cbs.on_changedtree then
table.insert(parsers[id].changedtree_cbs, buf_attach_cbs.on_changedtree)
end end
if buf_attach_cbs and buf_attach_cbs.on_lines then
table.insert(parsers[id].lines_cbs, buf_attach_cbs.on_lines)
end
return parsers[id] return parsers[id]
end end

View File

@ -25,7 +25,15 @@ TSHighlighter.hl_map = {
function TSHighlighter.new(query, bufnr, ft) function TSHighlighter.new(query, bufnr, ft)
local self = setmetatable({}, TSHighlighter) local self = setmetatable({}, TSHighlighter)
self.parser = vim.treesitter.get_parser(bufnr, ft, function(...) self:on_change(...) end) self.parser = vim.treesitter.get_parser(
bufnr,
ft,
{
on_changedtree = function(...) self:on_changedtree(...) end,
on_lines = function() self.root = self.parser:parse():root() end
}
)
self.buf = self.parser.bufnr self.buf = self.parser.bufnr
local tree = self.parser:parse() local tree = self.parser:parse()
@ -35,9 +43,6 @@ function TSHighlighter.new(query, bufnr, ft)
self.redraw_count = 0 self.redraw_count = 0
self.line_count = {} self.line_count = {}
a.nvim_buf_set_option(self.buf, "syntax", "") a.nvim_buf_set_option(self.buf, "syntax", "")
a.nvim_buf_attach(self.buf, false, {
on_lines=function(_) self.root = self.parser:parse():root() end
})
-- Tricky: if syntax hasn't been enabled, we need to reload color scheme -- Tricky: if syntax hasn't been enabled, we need to reload color scheme
-- but use synload.vim rather than syntax.vim to not enable -- but use synload.vim rather than syntax.vim to not enable
@ -82,10 +87,10 @@ function TSHighlighter:set_query(query)
end end
}) })
self:on_change({{self.root:range()}}) self:on_changedtree({{self.root:range()}})
end end
function TSHighlighter:on_change(changes) function TSHighlighter:on_changedtree(changes)
-- Get a fresh root -- Get a fresh root
self.root = self.parser.tree:root() self.root = self.parser.tree:root()