From ccbaf55c2f8bc691f8f64fe40ce00a1abda4fbac Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Thu, 18 Sep 2025 22:19:03 +0200 Subject: [PATCH] feat(options): don't overwrite indentexpr/foldexpr/foldmethod when set by plugins. Fixes #6464 --- lua/lazyvim/plugins/lsp/init.lua | 6 ++-- lua/lazyvim/plugins/treesitter.lua | 7 +++-- lua/lazyvim/util/init.lua | 47 ++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/lua/lazyvim/plugins/lsp/init.lua b/lua/lazyvim/plugins/lsp/init.lua index 95b7385c..bc6a7f46 100644 --- a/lua/lazyvim/plugins/lsp/init.lua +++ b/lua/lazyvim/plugins/lsp/init.lua @@ -146,9 +146,9 @@ return { -- folds if opts.folds.enabled then LazyVim.lsp.on_supports_method("textDocument/foldingRange", function(client, buffer) - local win = vim.api.nvim_get_current_win() - vim.wo[win][0].foldexpr = "v:lua.vim.lsp.foldexpr()" - vim.wo[win][0].foldmethod = "expr" + if LazyVim.set_default("foldmethod", "expr") then + LazyVim.set_default("foldexpr", "v:lua.vim.lsp.foldexpr()") + end end) end diff --git a/lua/lazyvim/plugins/treesitter.lua b/lua/lazyvim/plugins/treesitter.lua index a7ef7ed8..751a72f3 100644 --- a/lua/lazyvim/plugins/treesitter.lua +++ b/lua/lazyvim/plugins/treesitter.lua @@ -95,13 +95,14 @@ return { -- indents if vim.tbl_get(opts, "indent", "enable") ~= false then - vim.bo[ev.buf].indentexpr = "v:lua.LazyVim.treesitter.indentexpr()" + LazyVim.set_default("indentexpr", "v:lua.LazyVim.treesitter.indentexpr()") end -- folds if vim.tbl_get(opts, "folds", "enable") ~= false then - vim.wo.foldmethod = "expr" - vim.wo.foldexpr = "v:lua.LazyVim.treesitter.foldexpr()" + if LazyVim.set_default("foldmethod", "expr") then + LazyVim.set_default("foldexpr", "v:lua.LazyVim.treesitter.foldexpr()") + end end end, }) diff --git a/lua/lazyvim/util/init.lua b/lua/lazyvim/util/init.lua index 44a135ad..a3349c8a 100644 --- a/lua/lazyvim/util/init.lua +++ b/lua/lazyvim/util/init.lua @@ -304,4 +304,51 @@ function M.statuscolumn() return package.loaded.snacks and require("snacks.statuscolumn").get() or "" end +local _defaults = {} ---@type table + +-- Determines whether it's safe to set an option to a default value. +-- +-- It will only set the option if: +-- * it is the same as the global value +-- * it is the same as a previously set default value +-- * it was last set by a script in $VIMRUNTIME +---@param option string +---@param value string|number|boolean +---@return boolean was_set +function M.set_default(option, value) + local key = ("%s=%s"):format(option, value) + _defaults[key] = true + + if not _defaults[key] then + local l = vim.api.nvim_get_option_value(option, { scope = "local" }) + local g = vim.api.nvim_get_option_value(option, { scope = "global" }) + + if l ~= g then + -- Option changed, so check if it was set by an ft plugin + local info = vim.api.nvim_get_option_info2(option, { scope = "local" }) + ---@param e vim.fn.getscriptinfo.ret + local scriptinfo = vim.tbl_filter(function(e) + return e.sid == info.last_set_sid + end, vim.fn.getscriptinfo()) + local by_rtp = #scriptinfo == 1 and vim.startswith(scriptinfo[1].name, vim.fn.expand("$VIMRUNTIME")) + if not by_rtp then + if vim.g.lazyvim_debug_set_default then + LazyVim.warn( + ("Not setting option `%s` to `%s` because it was changed by a filetype plugin."):format(option, value), + { title = "LazyVim", once = true } + ) + end + return false + end + end + end + + if vim.g.lazyvim_debug_set_default then + LazyVim.info(("Setting option `%s` to `%s`"):format(option, value), { title = "LazyVim", once = true }) + end + + vim.api.nvim_set_option_value(option, value, { scope = "local" }) + return true +end + return M