mirror of
https://github.com/LazyVim/LazyVim.git
synced 2025-12-25 12:14:19 +01:00
feat(options): don't overwrite indentexpr/foldexpr/foldmethod when set by plugins. Fixes #6464
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user