From a6b38de763addf2d22e8a43a1e679cc855dc376d Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Wed, 17 Sep 2025 22:46:14 +0200 Subject: [PATCH] fix(treesitter): indentexpr/foldexpr now work as intended and override ftplugin settings. Fixes #6447 --- lua/lazyvim/config/options.lua | 2 +- lua/lazyvim/plugins/treesitter.lua | 9 +++++++++ lua/lazyvim/util/treesitter.lua | 4 ++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lua/lazyvim/config/options.lua b/lua/lazyvim/config/options.lua index f71f96df..2872d2a4 100644 --- a/lua/lazyvim/config/options.lua +++ b/lua/lazyvim/config/options.lua @@ -72,7 +72,7 @@ opt.foldexpr = "v:lua.LazyVim.treesitter.foldexpr()" -- treesitter folds opt.foldlevel = 99 opt.foldmethod = "expr" opt.foldtext = "" -opt.formatexpr = "v:lua.require'lazyvim.util'.format.formatexpr()" +opt.formatexpr = "v:lua.LazyVim.format.formatexpr()" opt.formatoptions = "jcroqlnt" -- tcqj opt.grepformat = "%f:%l:%c:%m" opt.grepprg = "rg --vimgrep" diff --git a/lua/lazyvim/plugins/treesitter.lua b/lua/lazyvim/plugins/treesitter.lua index c9a6e094..327c13be 100644 --- a/lua/lazyvim/plugins/treesitter.lua +++ b/lua/lazyvim/plugins/treesitter.lua @@ -80,9 +80,18 @@ return { -- treesitter highlighting vim.api.nvim_create_autocmd("FileType", { + group = vim.api.nvim_create_augroup("lazyvim_treesitter", { clear = true }), callback = function(ev) if LazyVim.treesitter.have(ev.match) then pcall(vim.treesitter.start) + + -- check if ftplugins changed foldexpr/indentexpr + for _, option in ipairs({ "foldexpr", "indentexpr" }) do + local expr = "v:lua.LazyVim.treesitter." .. option .. "()" + if vim.opt_global[option]:get() == expr then + vim.opt_local[option] = expr + end + end end end, }) diff --git a/lua/lazyvim/util/treesitter.lua b/lua/lazyvim/util/treesitter.lua index 84e279ce..07e0e5eb 100644 --- a/lua/lazyvim/util/treesitter.lua +++ b/lua/lazyvim/util/treesitter.lua @@ -22,12 +22,12 @@ end function M.foldexpr() local buf = vim.api.nvim_get_current_buf() - return M.have(vim.b[buf].filetype) and vim.treesitter.foldexpr() or "0" + return M.have(vim.bo[buf].filetype) and vim.treesitter.foldexpr() or "0" end function M.indentexpr() local buf = vim.api.nvim_get_current_buf() - return M.have(vim.b[buf].filetype) and require("nvim-treesitter").indentexpr() or -1 + return M.have(vim.bo[buf].filetype) and require("nvim-treesitter").indentexpr() or -1 end return M