irongit

A readme explaining how to make neovim work fast

hygo-vim/lua/plugins/folding.lua
58 lines1.6 KBLua
1local general = vim.api.nvim_create_augroup("Folding", { clear = true })
2
3-- use comments for fold expression
4-- use comments for fold expression
5-- use comments for fold expression
6-- use comments for fold expression
7_G.CommentFoldExpr = function()
8 local line = vim.fn.getline(vim.v.lnum)
9 local ft = vim.bo.filetype
10 local cs = vim.bo.commentstring:gsub("%%s", ""):gsub(" ", ""):gsub("([^%w])", "%%%1")
11 local should_fold = line:match("^%s*" .. cs) or _G.in_docstring
12 -- python docstrings
13 if ft == "python" then
14 if line:match('""".*"""') then
15 return "0"
16 end
17 if line:match('"""') then
18 if _G.in_docstring then
19 _G.in_docstring = false
20 else
21 _G.in_docstring = true
22 should_fold = true
23 end
24 end
25 end
26 -- lua docstrings
27 if ft == "lua" then
28 if line:match("--%[%[") then
29 _G.in_docstring = true
30 -- should_fold = true
31 end
32 if line:match("--%]%]") and _G.in_docstring then
33 _G.in_docstring = false
34 end
35 end
36 if should_fold then
37 return 1
38 end
39 return 0
40end
41local foldexpr = "v:lua.CommentFoldExpr()"
42vim.api.nvim_create_user_command("UseFoldTreesitter", function()
43 vim.opt_local.foldexpr = foldexpr
44end, { desc = "Set fold expression to treesitter" })
45vim.api.nvim_create_user_command("UseFoldComment", function()
46 foldexpr = "v:lua.CommentFoldExpr()"
47 vim.opt_local.foldexpr = foldexpr
48end, { desc = "Set foldexpr to comments" })
49vim.api.nvim_create_autocmd({ "BufReadPost", "FileType" }, {
50 group = general,
51 pattern = "*",
52 callback = function()
53 vim.opt_local.foldmethod = "expr"
54 vim.opt_local.foldexpr = foldexpr
55 end,
56})
57
58return {}