fix(options): track some initial options right after loading options.lua. See #6463

This commit is contained in:
Folke Lemaitre
2025-09-19 20:26:36 +02:00
parent 5ce7cd650a
commit 9c611b0c57
2 changed files with 19 additions and 3 deletions

View File

@@ -304,6 +304,8 @@ function M.load(name)
end
M.did_init = false
M._options = {} ---@type vim.wo|vim.bo
function M.init()
if M.did_init then
return
@@ -326,6 +328,12 @@ function M.init()
-- this is needed to make sure options will be correctly applied
-- after installing missing plugins
M.load("options")
-- save some options to track defaults
M._options.indentexpr = vim.o.indentexpr
M._options.foldmethod = vim.o.foldmethod
M._options.foldexpr = vim.o.foldexpr
-- defer built-in clipboard handling: "xsel" and "pbcopy" can be slow
lazy_clipboard = vim.opt.clipboard
vim.opt.clipboard = ""

View File

@@ -317,11 +317,12 @@ local _defaults = {} ---@type table<string, boolean>
---@return boolean was_set
function M.set_default(option, value)
local l = vim.api.nvim_get_option_value(option, { scope = "local" })
local g = vim.api.nvim_get_option_value(option, { scope = "global" })
local g = LazyVim.config._options[option] or vim.api.nvim_get_option_value(option, { scope = "global" })
_defaults[("%s=%s"):format(option, value)] = true
local key = ("%s=%s"):format(option, l)
local source = ""
if l ~= g and not _defaults[key] then
-- Option does not match global and is not a default value
-- Check if it was set by a script in $VIMRUNTIME
@@ -330,11 +331,12 @@ function M.set_default(option, value)
local scriptinfo = vim.tbl_filter(function(e)
return e.sid == info.last_set_sid
end, vim.fn.getscriptinfo())
source = scriptinfo[1] and scriptinfo[1].name or ""
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),
("Not setting option `%s` to `%q` because it was changed by a plugin."):format(option, value),
{ title = "LazyVim", once = true }
)
end
@@ -343,7 +345,13 @@ function M.set_default(option, value)
end
if vim.g.lazyvim_debug_set_default then
LazyVim.info(("Setting option `%s` to `%s`"):format(option, value), { title = "LazyVim", once = true })
LazyVim.info({
("Setting option `%s` to `%q`"):format(option, value),
("Was: %q"):format(l),
("Global: %q"):format(g),
source ~= "" and ("Last set by: %s"):format(source) or "",
"buf: " .. vim.api.nvim_buf_get_name(0),
}, { title = "LazyVim", once = true })
end
vim.api.nvim_set_option_value(option, value, { scope = "local" })