treesitter: propagate on_detach event properly

This commit is contained in:
Björn Linse
2021-02-06 10:17:40 +01:00
parent 230f2ff381
commit fa5f583981
3 changed files with 19 additions and 3 deletions

View File

@@ -44,10 +44,11 @@ function M._create_parser(bufnr, lang, opts)
self:_on_bytes(...)
end
local function detach_cb()
local function detach_cb(_, ...)
if parsers[bufnr] == self then
parsers[bufnr] = nil
end
self:_on_detach(...)
end
a.nvim_buf_attach(self:source(), false, {on_bytes=bytes_cb, on_detach=detach_cb, preview=true})

View File

@@ -113,8 +113,9 @@ function TSHighlighter.new(tree, opts)
opts = opts or {}
self.tree = tree
tree:register_cbs {
on_changedtree = function(...) self:on_changedtree(...) end,
on_bytes = function(...) self:on_bytes(...) end
on_changedtree = function(...) self:on_changedtree(...) end;
on_bytes = function(...) self:on_bytes(...) end;
on_detach = function(...) self:on_detach(...) end;
}
self.bufnr = tree:source()
@@ -176,6 +177,10 @@ function TSHighlighter:on_bytes(_, _, start_row, _, _, _, _, _, new_end)
a.nvim__buf_redraw_range(self.bufnr, start_row, start_row + new_end + 1)
end
function TSHighlighter:on_detach()
self:destroy()
end
function TSHighlighter:on_changedtree(changes)
for _, ch in ipairs(changes or {}) do
a.nvim__buf_redraw_range(self.bufnr, ch[1], ch[3]+1)

View File

@@ -35,6 +35,7 @@ function LanguageTree.new(source, lang, opts)
_callbacks = {
changedtree = {},
bytes = {},
detach = {},
child_added = {},
child_removed = {}
},
@@ -397,6 +398,11 @@ function LanguageTree:_on_bytes(bufnr, changed_tick,
new_row, new_col, new_byte)
end
function LanguageTree:_on_detach(...)
self:invalidate()
self:_do_callback('detach', ...)
end
--- Registers callbacks for the parser
-- @param cbs An `nvim_buf_attach`-like table argument with the following keys :
-- `on_bytes` : see `nvim_buf_attach`, but this will be called _after_ the parsers callback.
@@ -416,6 +422,10 @@ function LanguageTree:register_cbs(cbs)
table.insert(self._callbacks.bytes, cbs.on_bytes)
end
if cbs.on_detach then
table.insert(self._callbacks.detach, cbs.on_detach)
end
if cbs.on_child_added then
table.insert(self._callbacks.child_added, cbs.on_child_added)
end