fix(treesitter): avoid computing fold levels for empty buffer

Problem:  Computing fold levels for an empty buffer (somehow) breaks the
          parser state, resulting in a broken highlighter and foldexpr.
          Cached foldexpr parser is invalid after filetype has changed.
Solution: Avoid computing fold levels for empty buffer.
          Clear cached foldinfos upon `FileType`.
This commit is contained in:
Luuk van Baal
2025-02-16 00:07:08 +01:00
committed by Christian Clason
parent a0b52e7cb3
commit bc1018a8d3
3 changed files with 79 additions and 9 deletions

View File

@@ -75,7 +75,15 @@ local function compute_folds_levels(bufnr, info, srow, erow, callback)
erow = erow or api.nvim_buf_line_count(bufnr)
local parser = info.parser
if not parser then
if
not parser
-- Parsing an empty buffer results in problems with the parsing state,
-- resulting in both a broken highlighter and foldexpr.
or api.nvim_buf_line_count(bufnr) == 1
and api.nvim_buf_call(bufnr, function()
return vim.fn.line2byte(1) <= 0
end)
then
return
end
@@ -380,7 +388,7 @@ function M.foldexpr(lnum)
if not foldinfos[bufnr] then
foldinfos[bufnr] = FoldInfo.new(bufnr)
api.nvim_create_autocmd({ 'BufUnload', 'VimEnter' }, {
api.nvim_create_autocmd({ 'BufUnload', 'VimEnter', 'FileType' }, {
buffer = bufnr,
once = true,
callback = function()