]> git.rmz.io Git - dotfiles.git/blobdiff - nvim/lua/rmz/util/init.lua
lazyvim: foldexpr using treesitter if available
[dotfiles.git] / nvim / lua / rmz / util / init.lua
index 54d8f6b7dec0ccf867d4333d175b6cd8dd83f76c..abf0c2130696a451af9a400b1e93e07d88e61089 100644 (file)
@@ -21,4 +21,21 @@ function M.dedup(list)
   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