fix(treesitter): run FileType autocmds in the context of <abuf>

Problem: many FileType autocommands assume curbuf is the same as the target
buffer; this can cause &syntax to be restored for the wrong buffer in some cases
when TSHighlighter:destroy is called.

Solution: run nvim_exec_autocmds in the context of the target buffer via
nvim_buf_call.

(cherry picked from commit 3ec63cdab8)
This commit is contained in:
Sean Dewar
2025-08-19 16:14:10 +01:00
committed by github-actions[bot]
parent 3ab06d5188
commit fa64f2d09b
2 changed files with 16 additions and 4 deletions

View File

@@ -182,10 +182,13 @@ function TSHighlighter:destroy()
vim.b[self.bufnr].ts_highlight = nil
api.nvim_buf_clear_namespace(self.bufnr, ns, 0, -1)
if vim.g.syntax_on == 1 then
api.nvim_exec_autocmds(
'FileType',
{ group = 'syntaxset', buffer = self.bufnr, modeline = false }
)
-- FileType autocmds commonly assume curbuf is the target buffer, so nvim_buf_call.
api.nvim_buf_call(self.bufnr, function()
api.nvim_exec_autocmds(
'FileType',
{ group = 'syntaxset', buffer = self.bufnr, modeline = false }
)
end)
end
end
end