feat(options): don't overwrite indentexpr/foldexpr/foldmethod when set by plugins. Fixes #6464

This commit is contained in:
Folke Lemaitre
2025-09-18 22:19:03 +02:00
parent 3a743f7f85
commit ccbaf55c2f
3 changed files with 54 additions and 6 deletions

View File

@@ -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

View File

@@ -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,
})

View File

@@ -304,4 +304,51 @@ function M.statuscolumn()
return package.loaded.snacks and require("snacks.statuscolumn").get() or ""
end
local _defaults = {} ---@type table<string, boolean>
-- 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