summaryrefslogtreecommitdiffstats
path: root/plugin/50-fold.lua
blob: 81c1960229ca05aa096f3c1910e1837e1543f301 (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
29
30
--
-- 50-fold.lua
--
-- * Creates an autocommand to 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