Merge pull request #32102 from tomtomjhj/cherrypick-ts-foldexpr

Backport treesitter foldexpr fixes
This commit is contained in:
Justin M. Keyes
2025-01-19 11:31:47 -08:00
committed by GitHub
2 changed files with 80 additions and 4 deletions

View File

@ -87,7 +87,7 @@ end
---@param srow integer
---@param erow integer 0-indexed, exclusive
function FoldInfo:add_range(srow, erow)
list_insert(self.levels, srow + 1, erow, '=')
list_insert(self.levels, srow + 1, erow, -1)
list_insert(self.levels0, srow + 1, erow, -1)
end
@ -268,6 +268,15 @@ end
---@package
function FoldInfo:do_foldupdate(bufnr)
-- InsertLeave is not executed when <C-C> is used for exiting the insert mode, leaving
-- do_foldupdate untouched. If another execution of foldupdate consumes foldupdate_range, the
-- InsertLeave do_foldupdate gets nil foldupdate_range. In that case, skip the update. This is
-- correct because the update that consumed the range must have incorporated the range that
-- InsertLeave meant to update.
if not self.foldupdate_range then
return
end
local srow, erow = self.foldupdate_range[1], self.foldupdate_range[2]
self.foldupdate_range = nil
for _, win in ipairs(vim.fn.win_findbuf(bufnr)) do
@ -421,9 +430,15 @@ api.nvim_create_autocmd('OptionSet', {
pattern = { 'foldminlines', 'foldnestmax' },
desc = 'Refresh treesitter folds',
callback = function()
for bufnr, _ in pairs(foldinfos) do
local buf = api.nvim_get_current_buf()
local bufs = vim.v.option_type == 'global' and vim.tbl_keys(foldinfos)
or foldinfos[buf] and { buf }
or {}
for _, bufnr in ipairs(bufs) do
foldinfos[bufnr] = FoldInfo.new()
compute_folds_levels(bufnr, foldinfos[bufnr])
api.nvim_buf_call(bufnr, function()
compute_folds_levels(bufnr, foldinfos[bufnr])
end)
foldinfos[bufnr]:foldupdate(bufnr, 0, api.nvim_buf_line_count(bufnr))
end
end,

View File

@ -646,6 +646,67 @@ t3]])
}
end)
it('does not extend closed fold with `o`/`O`', function()
local screen = Screen.new(60, 24)
screen:attach()
insert(test_text)
parse('c')
command([[set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldcolumn=1]])
feed('5ggzco')
screen:expect({
grid = [[
{7:-}void ui_refresh(void) |
{7:│}{ |
{7:│} int width = INT_MAX, height = INT_MAX; |
{7:│} bool ext_widgets[kUIExtCount]; |
{7:+}{13:+--- 3 lines: for (UIExtension i = 0; (int)i < kUIExtCount}|
{7:│}^ |
{7:│} |
{7:│} bool inclusive = ui_override(); |
{7:-} for (size_t i = 0; i < ui_count; i++) { |
{7:2} UI *ui = uis[i]; |
{7:2} width = MIN(ui->width, width); |
{7:2} height = MIN(ui->height, height); |
{7:2} foo = BAR(ui->bazaar, bazaar); |
{7:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
{7:3} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
{7:3} } |
{7:2} } |
{7:│}} |
{1:~ }|*5
{5:-- INSERT --} |
]],
})
feed('<Esc>O')
screen:expect({
grid = [[
{7:-}void ui_refresh(void) |
{7:│}{ |
{7:│} int width = INT_MAX, height = INT_MAX; |
{7:│} bool ext_widgets[kUIExtCount]; |
{7:+}{13:+--- 3 lines: for (UIExtension i = 0; (int)i < kUIExtCount}|
{7:│}^ |
{7:│} |*2
{7:│} bool inclusive = ui_override(); |
{7:-} for (size_t i = 0; i < ui_count; i++) { |
{7:2} UI *ui = uis[i]; |
{7:2} width = MIN(ui->width, width); |
{7:2} height = MIN(ui->height, height); |
{7:2} foo = BAR(ui->bazaar, bazaar); |
{7:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
{7:3} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
{7:3} } |
{7:2} } |
{7:│}} |
{1:~ }|*4
{5:-- INSERT --} |
]],
})
end)
it("doesn't open folds that are not touched", function()
local screen = Screen.new(40, 8)
screen:set_default_attr_ids({
@ -674,7 +735,7 @@ t2]])
grid = [[
{1:-}# h1 |
{1:│}t1 |
{1:}^ |
{1:-}^ |
{1:+}{2:+-- 2 lines: # h2·····················}|
{3:~ }|*3
{4:-- INSERT --} |