Bläddra i källkod

refactor: more deliberate reload (#3133)

kylo252 2 år sedan
förälder
incheckning
e5bcf01c75

+ 0 - 1
after/plugin/highlights.lua

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

+ 2 - 0
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)

+ 8 - 8
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()

+ 6 - 12
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
 

+ 8 - 7
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
 

+ 1 - 1
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

+ 7 - 19
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)

+ 2 - 5
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

+ 8 - 0
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

+ 11 - 1
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

+ 1 - 4
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,
   },
   {

+ 1 - 1
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

+ 25 - 7
lua/lvim/utils/reload.lua → 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