aboutsummaryrefslogtreecommitdiffstats
path: root/plugin/50-fold.lua
blob: 41cf09bb6b1302672a9d533e80fb683056e36e8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
--[[ 50-fold.lua — fold with treesitter automatically and close all folds on 'FileType'.

Sets 'foldlevel' to 99 so that new text will not fold while editing.
]]

local function fold_with_treesitter()
	if vim.api.nvim_win_get_config(0).relative ~= "" or vim.treesitter.get_parser(0) == nil then
		return
	end
	vim.opt_local.foldexpr = "v:lua.vim.treesitter.foldexpr()"
	vim.opt_local.foldmethod = "expr"
	if vim.wo.foldlevel >= 99 then
		vim.cmd("silent! %foldclose!") -- no custom level: close everything on open
	else
		vim.cmd("silent! normal! zx") -- honor the ftplugin's 'foldlevel' (e.g. markdown keeps its top heading open)
	end
end

----------------------------------------------------------------------------------------------------

vim.api.nvim_create_autocmd("FileType", {
	desc = "Initialize folds",
	group = vim.g.dotfiles_augroup,
	callback = fold_with_treesitter,
})

vim.opt.foldenable = true
vim.opt.foldlevel = 99