aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2024-07-03 07:40:42 +0800
committerGitHub <noreply@github.com>2024-07-03 07:40:42 +0800
commitd413038b4fd71b7a335b6653aa64d2cb6daeac7b (patch)
tree6f6cc9e09bb6a3b4aecdb64d761bc93e5730dd88
parentaec7f1979ada1b34cfb3d8fd33769232d0323ea8 (diff)
downloadrneovim-d413038b4fd71b7a335b6653aa64d2cb6daeac7b.tar.gz
rneovim-d413038b4fd71b7a335b6653aa64d2cb6daeac7b.tar.bz2
rneovim-d413038b4fd71b7a335b6653aa64d2cb6daeac7b.zip
fix(treesitter): ensure syntaxset augroup exists (#29542)
Problem: Error when calling vim.treesitter.start() and vim.treesitter.stop() in init.lua. Solution: Ensure syntaxset augroup exists after loading synload.vim.
-rw-r--r--runtime/lua/vim/treesitter/highlighter.lua3
-rw-r--r--test/functional/treesitter/highlight_spec.lua23
2 files changed, 24 insertions, 2 deletions
diff --git a/runtime/lua/vim/treesitter/highlighter.lua b/runtime/lua/vim/treesitter/highlighter.lua
index 052b839609..a94c408f4e 100644
--- a/runtime/lua/vim/treesitter/highlighter.lua
+++ b/runtime/lua/vim/treesitter/highlighter.lua
@@ -139,8 +139,11 @@ function TSHighlighter.new(tree, opts)
-- 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.
+ -- Still need to ensure that syntaxset augroup exists, so that calling :destroy()
+ -- immediately afterwards will not error.
if vim.g.syntax_on ~= 1 then
vim.cmd.runtime({ 'syntax/synload.vim', bang = true })
+ vim.api.nvim_create_augroup('syntaxset', { clear = false })
end
vim._with({ buf = self.bufnr }, function()
diff --git a/test/functional/treesitter/highlight_spec.lua b/test/functional/treesitter/highlight_spec.lua
index 05c0cdc01e..6eadb3110f 100644
--- a/test/functional/treesitter/highlight_spec.lua
+++ b/test/functional/treesitter/highlight_spec.lua
@@ -10,8 +10,6 @@ local command = n.command
local api = n.api
local eq = t.eq
-before_each(clear)
-
local hl_query_c = [[
(ERROR) @error
@@ -120,6 +118,7 @@ describe('treesitter highlighting (C)', function()
local screen
before_each(function()
+ clear()
screen = Screen.new(65, 18)
screen:attach()
screen:set_default_attr_ids {
@@ -800,6 +799,7 @@ describe('treesitter highlighting (lua)', function()
local screen
before_each(function()
+ clear()
screen = Screen.new(65, 18)
screen:attach()
screen:set_default_attr_ids {
@@ -838,6 +838,7 @@ describe('treesitter highlighting (help)', function()
local screen
before_each(function()
+ clear()
screen = Screen.new(40, 6)
screen:attach()
screen:set_default_attr_ids {
@@ -939,6 +940,7 @@ describe('treesitter highlighting (nested injections)', function()
local screen
before_each(function()
+ clear()
screen = Screen.new(80, 7)
screen:attach()
screen:set_default_attr_ids {
@@ -1006,6 +1008,7 @@ describe('treesitter highlighting (markdown)', function()
local screen
before_each(function()
+ clear()
screen = Screen.new(40, 6)
screen:attach()
exec_lua([[
@@ -1053,3 +1056,19 @@ printf('Hello World!');
})
end)
end)
+
+it('starting and stopping treesitter highlight in init.lua works #29541', function()
+ t.write_file(
+ 'Xinit.lua',
+ [[
+ vim.bo.ft = 'c'
+ vim.treesitter.start()
+ vim.treesitter.stop()
+ ]]
+ )
+ finally(function()
+ os.remove('Xinit.lua')
+ end)
+ clear({ args = { '-u', 'Xinit.lua' } })
+ eq('', api.nvim_get_vvar('errmsg'))
+end)