From bc1018a8d3eeeade9b3fad147a9d9a819985d69d Mon Sep 17 00:00:00 2001 From: Luuk van Baal Date: Sun, 16 Feb 2025 00:07:08 +0100 Subject: 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`. --- test/functional/treesitter/fold_spec.lua | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'test/functional/treesitter') diff --git a/test/functional/treesitter/fold_spec.lua b/test/functional/treesitter/fold_spec.lua index ac58df4bba..1212212d62 100644 --- a/test/functional/treesitter/fold_spec.lua +++ b/test/functional/treesitter/fold_spec.lua @@ -811,17 +811,19 @@ t2]]) ]] -- foldexpr will return '0' for all lines - local levels = get_fold_levels() ---@type integer[] - eq(19, #levels) - for lnum, level in ipairs(levels) do - eq('0', level, string.format("foldlevel[%d] == %s; expected '0'", lnum, level)) + local function expect_no_folds() + local levels = get_fold_levels() ---@type integer[] + eq(19, #levels) + for lnum, level in ipairs(levels) do + eq('0', level, string.format("foldlevel[%d] == %s; expected '0'", lnum, level)) + end end + expect_no_folds() -- reload buffer as c filetype to simulate new parser being found feed('GA// vim: ft=c') command([[write | edit]]) - - eq({ + local foldlevels = { [1] = '>1', [2] = '1', [3] = '1', @@ -841,6 +843,14 @@ t2]]) [17] = '3', [18] = '2', [19] = '1', - }, get_fold_levels()) + } + eq(foldlevels, get_fold_levels()) + + -- only changing filetype should change the parser again + command('set ft=some_filetype_without_treesitter_parser') + expect_no_folds() + + command('set ft=c') + eq(foldlevels, get_fold_levels()) end) end) -- cgit