refactor: more deliberate reload (#3133)

This commit is contained in:
kylo252 2022-10-04 19:23:52 +02:00 committed by GitHub
parent 560ee4d7cf
commit e5bcf01c75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 80 additions and 66 deletions

View file

@ -1 +0,0 @@
reload("lvim.core.lir").icon_setup()

View file

@ -1,4 +1,6 @@
local fmt = string.format local fmt = string.format
-- luacheck: ignore
-- TODO: fix lint violations
-- Iterator that splits a string o a given delimiter -- Iterator that splits a string o a given delimiter
local function split(str, delim) local function split(str, delim)

View file

@ -5,20 +5,20 @@ if not vim.tbl_contains(vim.opt.rtp:get(), base_dir) then
vim.opt.rtp:append(base_dir) vim.opt.rtp:append(base_dir)
end end
reload = require("lvim.utils.reload").reload require("lvim.bootstrap"):init(base_dir)
reload("lvim.bootstrap"):init(base_dir) require("lvim.config"):load()
reload("lvim.config"):load() local plugins = require "lvim.plugins"
local plugins = reload "lvim.plugins" require("lvim.plugin-loader").load { plugins, lvim.plugins }
reload("lvim.plugin-loader").load { plugins, lvim.plugins } require("lvim.core.theme").setup()
local Log = reload "lvim.core.log" local Log = require "lvim.core.log"
Log:debug "Starting LunarVim" Log:debug "Starting LunarVim"
local commands = reload "lvim.core.commands" local commands = require "lvim.core.commands"
commands.load(commands.defaults) commands.load(commands.defaults)
reload("lvim.lsp").setup() require("lvim.lsp").setup()

View file

@ -19,15 +19,9 @@ function _G.join_paths(...)
return result return result
end end
---Require a module in protected mode without relying on its cached value _G.require_clean = require("lvim.utils.modules").require_clean
---@param module string _G.require_safe = require("lvim.utils.modules").require_safe
---@return any _G.reload = require("lvim.utils.modules").reload
function _G.require_clean(module)
package.loaded[module] = nil
_G[module] = nil
local _, requested = pcall(require, module)
return requested
end
---Get the full path to `$LUNARVIM_RUNTIME_DIR` ---Get the full path to `$LUNARVIM_RUNTIME_DIR`
---@return string ---@return string
@ -121,10 +115,10 @@ end
---Update LunarVim ---Update LunarVim
---pulls the latest changes from github and, resets the startup cache ---pulls the latest changes from github and, resets the startup cache
function M:update() function M:update()
require_clean("lvim.utils.hooks").run_pre_update() reload("lvim.utils.hooks").run_pre_update()
local ret = require_clean("lvim.utils.git").update_base_lvim() local ret = reload("lvim.utils.git").update_base_lvim()
if ret then if ret then
require_clean("lvim.utils.hooks").run_post_update() reload("lvim.utils.hooks").run_post_update()
end end
end end

View file

@ -111,7 +111,7 @@ end
--- Override the configuration with a user provided one --- Override the configuration with a user provided one
-- @param config_path The path to the configuration overrides -- @param config_path The path to the configuration overrides
function M:load(config_path) function M:load(config_path)
local autocmds = require "lvim.core.autocmds" local autocmds = reload "lvim.core.autocmds"
config_path = config_path or self:get_user_config_path() config_path = config_path or self:get_user_config_path()
local ok, err = pcall(dofile, config_path) local ok, err = pcall(dofile, config_path)
if not ok then if not ok then
@ -128,7 +128,7 @@ function M:load(config_path)
vim.g.mapleader = (lvim.leader == "space" and " ") or lvim.leader vim.g.mapleader = (lvim.leader == "space" and " ") or lvim.leader
require("lvim.keymappings").load(lvim.keys) reload("lvim.keymappings").load(lvim.keys)
if lvim.transparent_window then if lvim.transparent_window then
autocmds.enable_transparent_mode() autocmds.enable_transparent_mode()
@ -139,17 +139,18 @@ end
-- @param config_path The path to the configuration overrides -- @param config_path The path to the configuration overrides
function M:reload() function M:reload()
vim.schedule(function() vim.schedule(function()
require_clean("lvim.utils.hooks").run_pre_reload() reload("lvim.utils.hooks").run_pre_reload()
M:load() M:load()
require("lvim.core.autocmds").configure_format_on_save() reload("lvim.core.autocmds").configure_format_on_save()
local plugins = require "lvim.plugins" local plugins = reload "lvim.plugins"
local plugin_loader = require "lvim.plugin-loader" local plugin_loader = reload "lvim.plugin-loader"
plugin_loader.reload { plugins, lvim.plugins } plugin_loader.reload { plugins, lvim.plugins }
require_clean("lvim.utils.hooks").run_post_reload() reload("lvim.core.theme").setup()
reload("lvim.utils.hooks").run_post_reload()
end) end)
end end

View file

@ -26,7 +26,7 @@ local builtins = {
function M.config(config) function M.config(config)
for _, builtin_path in ipairs(builtins) do for _, builtin_path in ipairs(builtins) do
local builtin = require("lvim.utils.reload").reload(builtin_path) local builtin = reload(builtin_path)
builtin.config(config) builtin.config(config)
end end

View file

@ -7,16 +7,16 @@ M.config = function()
icon = "", icon = "",
} }
local status_ok, lir = pcall(reload, "lir") local status_ok, _ = pcall(require, "lir")
if not status_ok then if not status_ok then
return return
end end
local actions = reload "lir.actions" local actions = require "lir.actions"
local mark_actions = reload "lir.mark.actions" local mark_actions = require "lir.mark.actions"
local clipboard_actions = reload "lir.clipboard.actions" local clipboard_actions = require "lir.clipboard.actions"
lir.setup { lvim.builtin.lir = vim.tbl_extend("force", lvim.builtin.lir, {
show_hidden_files = false, show_hidden_files = false,
devicons_enable = true, devicons_enable = true,
mappings = { mappings = {
@ -80,16 +80,7 @@ M.config = function()
-- echo cwd -- echo cwd
-- vim.api.nvim_echo({ { vim.fn.expand "%:p", "Normal" } }, false, {}) -- vim.api.nvim_echo({ { vim.fn.expand "%:p", "Normal" } }, false, {})
end, end,
} })
-- custom folder icon
reload("nvim-web-devicons").set_icon {
lir_folder_icon = {
icon = lvim.icons.ui.Folder,
color = "#42A5F5",
name = "LirFolderNode",
},
}
end end
function M.icon_setup() function M.icon_setup()
@ -113,14 +104,11 @@ function M.icon_setup()
end end
function M.setup() function M.setup()
if lvim.builtin.nvimtree.active then
return
end
local status_ok, lir = pcall(reload, "lir") local status_ok, lir = pcall(reload, "lir")
if not status_ok then if not status_ok then
return return
end end
lir.setup(lvim.builtin.lir)
if lvim.builtin.lir.on_config_done then if lvim.builtin.lir.on_config_done then
lvim.builtin.lir.on_config_done(lir) lvim.builtin.lir.on_config_done(lir)

View file

@ -34,16 +34,13 @@ M.config = function()
end end
M.setup = function() M.setup = function()
-- avoid running in headless mode since it's harder to detect failures local status_ok, lualine = pcall(require, "lualine")
if #vim.api.nvim_list_uis() == 0 then if not status_ok then
local Log = require "lvim.core.log"
Log:debug "headless mode detected, skipping running setup for lualine"
return return
end end
require("lvim.core.lualine.styles").update() require("lvim.core.lualine.styles").update()
local lualine = require "lualine"
lualine.setup(lvim.builtin.lualine) lualine.setup(lvim.builtin.lualine)
if lvim.builtin.lualine.on_config_done then if lvim.builtin.lualine.on_config_done then

View file

@ -152,6 +152,14 @@ function M.update()
local style = M.get_style(lvim.builtin.lualine.style) local style = M.get_style(lvim.builtin.lualine.style)
lvim.builtin.lualine = vim.tbl_deep_extend("keep", lvim.builtin.lualine, style) lvim.builtin.lualine = vim.tbl_deep_extend("keep", lvim.builtin.lualine, style)
local color_template = vim.g.colors_name or lvim.colorscheme
local theme_supported, template = pcall(function()
require("lualine.utils.loader").load_theme(color_template)
end)
if theme_supported and template then
lvim.builtin.lualine.options.theme = color_template
end
end end
return M return M

View file

@ -83,13 +83,23 @@ M.config = function()
end end
M.setup = function() M.setup = function()
-- avoid running in headless mode since it's harder to detect failures
if #vim.api.nvim_list_uis() == 0 then
local Log = require "lvim.core.log"
Log:debug "headless mode detected, skipping running setup for lualine"
return
end
local status_ok, theme = pcall(require, "tokyonight") local status_ok, theme = pcall(require, "tokyonight")
if not status_ok then if not status_ok then
return return
end end
theme.setup(lvim.builtin.theme.options) theme.setup(lvim.builtin.theme.options)
lvim.builtin.lualine.options.theme = "tokyonight"
require("lvim.core.lualine").setup()
require("lvim.core.lir").icon_setup()
end end
return M return M

View file

@ -16,10 +16,6 @@ local core_plugins = {
}, },
{ {
"folke/tokyonight.nvim", "folke/tokyonight.nvim",
config = function()
require("lvim.core.theme").setup()
end,
-- disable = not vim.startswith(lvim.colorscheme, "tokyonight"),
}, },
{ {
"rcarriga/nvim-notify", "rcarriga/nvim-notify",
@ -139,6 +135,7 @@ local core_plugins = {
config = function() config = function()
require("lvim.core.lir").setup() require("lvim.core.lir").setup()
end, end,
requires = { "kyazdani42/nvim-web-devicons" },
disable = not lvim.builtin.lir.active, disable = not lvim.builtin.lir.active,
}, },
{ {

View file

@ -61,7 +61,7 @@ function M.run_post_update()
vim.wait(1000, function() vim.wait(1000, function()
return false return false
end) end)
local ret = require_clean("lvim.utils.git").switch_lvim_branch(compat_tag) local ret = reload("lvim.utils.git").switch_lvim_branch(compat_tag)
if ret then if ret then
vim.notify("Reverted to the last known compatible version: " .. compat_tag, vim.log.levels.WARN) vim.notify("Reverted to the last known compatible version: " .. compat_tag, vim.log.levels.WARN)
end end

View file

@ -1,5 +1,6 @@
local M = {} local M = {}
local Log = require "lvim.core.log"
-- revisit this -- revisit this
-- function prequire(package) -- function prequire(package)
-- local status, lib = pcall(require, package) -- local status, lib = pcall(require, package)
@ -42,7 +43,9 @@ local function _replace(old, new, repeat_tbl)
old[k] = new[k] old[k] = new[k]
else else
if type(old[k]) ~= type(new[k]) then if type(old[k]) ~= type(new[k]) then
vim.notify(string.format("warning: attr %s old type no equal new type!!!", k)) Log:debug(
string.format("Reloader: mismatch between old [%s] and new [%s] type for [%s]", type(old[k]), type(new[k]), k)
)
_assign(old, new, k) _assign(old, new, k)
else else
if type(old[k]) == "table" then if type(old[k]) == "table" then
@ -55,25 +58,40 @@ local function _replace(old, new, repeat_tbl)
end end
end end
M.require_clean = function(m)
package.loaded[m] = nil
_G[m] = nil
local _, module = pcall(require, m)
return module
end
M.require_safe = function(mod)
local status_ok, module = pcall(require, mod)
if not status_ok then
local trace = debug.getinfo(2, "SL")
local shorter_src = trace.short_src
local lineinfo = shorter_src .. ":" .. (trace.currentline or trace.linedefined)
local msg = string.format("%s : skipped loading [%s]", lineinfo, mod)
Log:debug(msg)
end
return module
end
M.reload = function(mod) M.reload = function(mod)
if not package.loaded[mod] then if not package.loaded[mod] then
local m = require(mod) return M.require_safe(mod)
return m
end end
-- vim.notify "begin reload!!!"
local old = package.loaded[mod] local old = package.loaded[mod]
package.loaded[mod] = nil package.loaded[mod] = nil
local new = require(mod) local new = M.require_safe(mod)
if type(old) == "table" and type(new) == "table" then if type(old) == "table" and type(new) == "table" then
-- vim.notify "pick object in new module to old module!!!"
local repeat_tbl = {} local repeat_tbl = {}
_replace(old, new, repeat_tbl) _replace(old, new, repeat_tbl)
end end
package.loaded[mod] = old package.loaded[mod] = old
-- vim.notify "finish reload!!!"
return old return old
end end