]> git.rmz.io Git - dotfiles.git/commitdiff
lazyvim: foldexpr using treesitter if available
authorSamir Benmendil <me@rmz.io>
Sun, 23 Feb 2025 14:19:23 +0000 (14:19 +0000)
committerSamir Benmendil <me@rmz.io>
Sun, 2 Mar 2025 16:05:28 +0000 (16:05 +0000)
nvim/lua/config/options.lua
nvim/lua/rmz/util/init.lua

index 5fce922343aa74f25701b3e3f270f621d502db62..81ff29e02db9ba47808933b49911f9d83ea4fa93 100644 (file)
@@ -20,5 +20,8 @@ if vim.fn.has("nvim-0.9.0") == 1 then
   opt.splitkeep = "screen"
   opt.shortmess:append({ C = true })
 end
   opt.splitkeep = "screen"
   opt.shortmess:append({ C = true })
 end
+opt.foldmethod = "expr"
+opt.foldexpr = "v:lua.require'rmz.util'.foldexpr()"
+
 -- do not let markdown plugin change indent
 vim.g.markdown_recommended_style = 0
 -- do not let markdown plugin change indent
 vim.g.markdown_recommended_style = 0
index 54d8f6b7dec0ccf867d4333d175b6cd8dd83f76c..abf0c2130696a451af9a400b1e93e07d88e61089 100644 (file)
@@ -21,4 +21,21 @@ function M.dedup(list)
   return ret
 end
 
   return ret
 end
 
+function M.foldexpr()
+  local buf = vim.api.nvim_get_current_buf()
+  if vim.b[buf].ts_folds == nil then
+    -- as long as we don't have a filetype, don't bother
+    -- checking if treesitter is available (it won't)
+    if vim.bo[buf].filetype == "" then
+      return "0"
+    end
+    if vim.bo[buf].filetype:find("dashboard") then
+      vim.b[buf].ts_folds = false
+    else
+      vim.b[buf].ts_folds = pcall(vim.treesitter.get_parser, buf)
+    end
+  end
+  return vim.b[buf].ts_folds and vim.treesitter.foldexpr() or "0"
+end
+
 return M
 return M