From e5bcf01c759e7c833d8a5f1fcf665b6ea32a7c16 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Tue, 4 Oct 2022 19:23:52 +0200 Subject: [PATCH] refactor: more deliberate reload (#3133) --- after/plugin/highlights.lua | 1 - ftplugin/lua.lua | 2 ++ init.lua | 16 +++++------ lua/lvim/bootstrap.lua | 18 ++++-------- lua/lvim/config/init.lua | 15 +++++----- lua/lvim/core/builtins/init.lua | 2 +- lua/lvim/core/lir.lua | 26 +++++------------- lua/lvim/core/lualine/init.lua | 7 ++--- lua/lvim/core/lualine/styles.lua | 8 ++++++ lua/lvim/core/theme.lua | 12 +++++++- lua/lvim/plugins.lua | 5 +--- lua/lvim/utils/hooks.lua | 2 +- lua/lvim/utils/{reload.lua => modules.lua} | 32 +++++++++++++++++----- 13 files changed, 80 insertions(+), 66 deletions(-) delete mode 100644 after/plugin/highlights.lua rename lua/lvim/utils/{reload.lua => modules.lua} (66%) diff --git a/after/plugin/highlights.lua b/after/plugin/highlights.lua deleted file mode 100644 index 4c343e80..00000000 --- a/after/plugin/highlights.lua +++ /dev/null @@ -1 +0,0 @@ -reload("lvim.core.lir").icon_setup() diff --git a/ftplugin/lua.lua b/ftplugin/lua.lua index d6b9ced7..138332e9 100644 --- a/ftplugin/lua.lua +++ b/ftplugin/lua.lua @@ -1,4 +1,6 @@ local fmt = string.format +-- luacheck: ignore +-- TODO: fix lint violations -- Iterator that splits a string o a given delimiter local function split(str, delim) diff --git a/init.lua b/init.lua index 6f78b021..a171dd53 100644 --- a/init.lua +++ b/init.lua @@ -5,20 +5,20 @@ if not vim.tbl_contains(vim.opt.rtp:get(), base_dir) then vim.opt.rtp:append(base_dir) 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" -local commands = reload "lvim.core.commands" +local commands = require "lvim.core.commands" commands.load(commands.defaults) -reload("lvim.lsp").setup() +require("lvim.lsp").setup() diff --git a/lua/lvim/bootstrap.lua b/lua/lvim/bootstrap.lua index e0b781d7..b803cfa6 100644 --- a/lua/lvim/bootstrap.lua +++ b/lua/lvim/bootstrap.lua @@ -19,15 +19,9 @@ function _G.join_paths(...) return result end ----Require a module in protected mode without relying on its cached value ----@param module string ----@return any -function _G.require_clean(module) - package.loaded[module] = nil - _G[module] = nil - local _, requested = pcall(require, module) - return requested -end +_G.require_clean = require("lvim.utils.modules").require_clean +_G.require_safe = require("lvim.utils.modules").require_safe +_G.reload = require("lvim.utils.modules").reload ---Get the full path to `$LUNARVIM_RUNTIME_DIR` ---@return string @@ -121,10 +115,10 @@ end ---Update LunarVim ---pulls the latest changes from github and, resets the startup cache function M:update() - require_clean("lvim.utils.hooks").run_pre_update() - local ret = require_clean("lvim.utils.git").update_base_lvim() + reload("lvim.utils.hooks").run_pre_update() + local ret = reload("lvim.utils.git").update_base_lvim() if ret then - require_clean("lvim.utils.hooks").run_post_update() + reload("lvim.utils.hooks").run_post_update() end end diff --git a/lua/lvim/config/init.lua b/lua/lvim/config/init.lua index c6765f56..483af50d 100644 --- a/lua/lvim/config/init.lua +++ b/lua/lvim/config/init.lua @@ -111,7 +111,7 @@ end --- Override the configuration with a user provided one -- @param config_path The path to the configuration overrides 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() local ok, err = pcall(dofile, config_path) if not ok then @@ -128,7 +128,7 @@ function M:load(config_path) 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 autocmds.enable_transparent_mode() @@ -139,17 +139,18 @@ end -- @param config_path The path to the configuration overrides function M:reload() vim.schedule(function() - require_clean("lvim.utils.hooks").run_pre_reload() + reload("lvim.utils.hooks").run_pre_reload() M:load() - require("lvim.core.autocmds").configure_format_on_save() + reload("lvim.core.autocmds").configure_format_on_save() - local plugins = require "lvim.plugins" - local plugin_loader = require "lvim.plugin-loader" + local plugins = reload "lvim.plugins" + local plugin_loader = reload "lvim.plugin-loader" 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 diff --git a/lua/lvim/core/builtins/init.lua b/lua/lvim/core/builtins/init.lua index 84e37655..0060c460 100644 --- a/lua/lvim/core/builtins/init.lua +++ b/lua/lvim/core/builtins/init.lua @@ -26,7 +26,7 @@ local builtins = { function M.config(config) for _, builtin_path in ipairs(builtins) do - local builtin = require("lvim.utils.reload").reload(builtin_path) + local builtin = reload(builtin_path) builtin.config(config) end diff --git a/lua/lvim/core/lir.lua b/lua/lvim/core/lir.lua index af9eb549..68445664 100644 --- a/lua/lvim/core/lir.lua +++ b/lua/lvim/core/lir.lua @@ -7,16 +7,16 @@ M.config = function() icon = "", } - local status_ok, lir = pcall(reload, "lir") + local status_ok, _ = pcall(require, "lir") if not status_ok then return end - local actions = reload "lir.actions" - local mark_actions = reload "lir.mark.actions" - local clipboard_actions = reload "lir.clipboard.actions" + local actions = require "lir.actions" + local mark_actions = require "lir.mark.actions" + local clipboard_actions = require "lir.clipboard.actions" - lir.setup { + lvim.builtin.lir = vim.tbl_extend("force", lvim.builtin.lir, { show_hidden_files = false, devicons_enable = true, mappings = { @@ -80,16 +80,7 @@ M.config = function() -- echo cwd -- vim.api.nvim_echo({ { vim.fn.expand "%:p", "Normal" } }, false, {}) end, - } - - -- custom folder icon - reload("nvim-web-devicons").set_icon { - lir_folder_icon = { - icon = lvim.icons.ui.Folder, - color = "#42A5F5", - name = "LirFolderNode", - }, - } + }) end function M.icon_setup() @@ -113,14 +104,11 @@ function M.icon_setup() end function M.setup() - if lvim.builtin.nvimtree.active then - return - end - local status_ok, lir = pcall(reload, "lir") if not status_ok then return end + lir.setup(lvim.builtin.lir) if lvim.builtin.lir.on_config_done then lvim.builtin.lir.on_config_done(lir) diff --git a/lua/lvim/core/lualine/init.lua b/lua/lvim/core/lualine/init.lua index e041e8a8..cd6237bf 100644 --- a/lua/lvim/core/lualine/init.lua +++ b/lua/lvim/core/lualine/init.lua @@ -34,16 +34,13 @@ M.config = function() end 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" + local status_ok, lualine = pcall(require, "lualine") + if not status_ok then return end require("lvim.core.lualine.styles").update() - local lualine = require "lualine" lualine.setup(lvim.builtin.lualine) if lvim.builtin.lualine.on_config_done then diff --git a/lua/lvim/core/lualine/styles.lua b/lua/lvim/core/lualine/styles.lua index 8cde37c4..81dbbabb 100644 --- a/lua/lvim/core/lualine/styles.lua +++ b/lua/lvim/core/lualine/styles.lua @@ -152,6 +152,14 @@ function M.update() local style = M.get_style(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 return M diff --git a/lua/lvim/core/theme.lua b/lua/lvim/core/theme.lua index 43ba3a07..394963a0 100644 --- a/lua/lvim/core/theme.lua +++ b/lua/lvim/core/theme.lua @@ -83,13 +83,23 @@ M.config = function() end 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") if not status_ok then return end theme.setup(lvim.builtin.theme.options) - lvim.builtin.lualine.options.theme = "tokyonight" + + require("lvim.core.lualine").setup() + + require("lvim.core.lir").icon_setup() end return M diff --git a/lua/lvim/plugins.lua b/lua/lvim/plugins.lua index c315cdd9..0b5e1c72 100644 --- a/lua/lvim/plugins.lua +++ b/lua/lvim/plugins.lua @@ -16,10 +16,6 @@ local core_plugins = { }, { "folke/tokyonight.nvim", - config = function() - require("lvim.core.theme").setup() - end, - -- disable = not vim.startswith(lvim.colorscheme, "tokyonight"), }, { "rcarriga/nvim-notify", @@ -139,6 +135,7 @@ local core_plugins = { config = function() require("lvim.core.lir").setup() end, + requires = { "kyazdani42/nvim-web-devicons" }, disable = not lvim.builtin.lir.active, }, { diff --git a/lua/lvim/utils/hooks.lua b/lua/lvim/utils/hooks.lua index fd78ef19..4101c573 100644 --- a/lua/lvim/utils/hooks.lua +++ b/lua/lvim/utils/hooks.lua @@ -61,7 +61,7 @@ function M.run_post_update() vim.wait(1000, function() return false 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 vim.notify("Reverted to the last known compatible version: " .. compat_tag, vim.log.levels.WARN) end diff --git a/lua/lvim/utils/reload.lua b/lua/lvim/utils/modules.lua similarity index 66% rename from lua/lvim/utils/reload.lua rename to lua/lvim/utils/modules.lua index 46392349..d5674483 100644 --- a/lua/lvim/utils/reload.lua +++ b/lua/lvim/utils/modules.lua @@ -1,5 +1,6 @@ local M = {} +local Log = require "lvim.core.log" -- revisit this -- function prequire(package) -- local status, lib = pcall(require, package) @@ -42,7 +43,9 @@ local function _replace(old, new, repeat_tbl) old[k] = new[k] else 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) else if type(old[k]) == "table" then @@ -55,25 +58,40 @@ local function _replace(old, new, repeat_tbl) 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) if not package.loaded[mod] then - local m = require(mod) - return m + return M.require_safe(mod) end - -- vim.notify "begin reload!!!" local old = package.loaded[mod] package.loaded[mod] = nil - local new = require(mod) + local new = M.require_safe(mod) if type(old) == "table" and type(new) == "table" then - -- vim.notify "pick object in new module to old module!!!" local repeat_tbl = {} _replace(old, new, repeat_tbl) end package.loaded[mod] = old - -- vim.notify "finish reload!!!" return old end