瀏覽代碼

feat: reload and lir color update (#3123)

Christian Chiarulli 2 年之前
父節點
當前提交
1c03ac8052

+ 1 - 0
.luacheckrc

@@ -3,6 +3,7 @@
 stds.nvim = {
   globals = {
     "lvim",
+    "reload",
     vim = { fields = { "g" } },
     "TERMINAL",
     "USER",

+ 10 - 7
init.lua

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

+ 2 - 1
lua/lvim/core/builtins/init.lua

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

+ 1 - 1
lua/lvim/core/indentlines.lua

@@ -27,7 +27,7 @@ M.config = function()
 end
 
 M.setup = function()
-  local status_ok, indent_blankline = pcall(require, "indent_blankline")
+  local status_ok, indent_blankline = pcall(reload, "indent_blankline")
   if not status_ok then
     return
   end

+ 27 - 14
lua/lvim/core/lir.lua

@@ -1,21 +1,20 @@
 local M = {}
 
--- local Log = require "lvim.core.log"
-
 M.config = function()
   lvim.builtin.lir = {
     active = true,
     on_config_done = nil,
+    icon = "",
   }
 
-  local status_ok, lir = pcall(require, "lir")
+  local status_ok, lir = pcall(reload, "lir")
   if not status_ok then
     return
   end
 
-  local actions = require "lir.actions"
-  local mark_actions = require "lir.mark.actions"
-  local clipboard_actions = require "lir.clipboard.actions"
+  local actions = reload "lir.actions"
+  local mark_actions = reload "lir.mark.actions"
+  local clipboard_actions = reload "lir.clipboard.actions"
 
   lir.setup {
     show_hidden_files = false,
@@ -84,31 +83,45 @@ M.config = function()
   }
 
   -- custom folder icon
-  require("nvim-web-devicons").set_icon {
+  reload("nvim-web-devicons").set_icon {
     lir_folder_icon = {
       icon = "",
-      -- color = "#7ebae4",
-      -- color = "#569CD6",
       color = "#42A5F5",
       name = "LirFolderNode",
     },
   }
 end
 
+function M.icon_setup()
+  local function get_hl_by_name(name)
+    local ret = vim.api.nvim_get_hl_by_name(name.group, true)
+    return string.format("#%06x", ret[name.property])
+  end
+
+  local found, icon_hl = pcall(get_hl_by_name, { group = "NvimTreeFolderIcon", property = "foreground" })
+  if not found then
+    icon_hl = "#42A5F5"
+  end
+
+  reload("nvim-web-devicons").set_icon {
+    lir_folder_icon = {
+      icon = lvim.builtin.lir.icon,
+      color = icon_hl,
+      name = "LirFolderNode",
+    },
+  }
+end
+
 function M.setup()
   if lvim.builtin.nvimtree.active then
-    -- Log:warn "Unable to configure lir while nvimtree is active! Please set 'lvim.builtin.nvimtree.active=false'"
     return
   end
 
-  local status_ok, lir = pcall(require, "lir")
+  local status_ok, lir = pcall(reload, "lir")
   if not status_ok then
     return
   end
 
-  lir.setup(lvim.builtin.lir.setup)
-  require("nvim-web-devicons").set_icon(lvim.builtin.lir.icons)
-
   if lvim.builtin.lir.on_config_done then
     lvim.builtin.lir.on_config_done(lir)
   end

+ 1 - 1
lua/lvim/core/nvimtree.lua

@@ -11,7 +11,7 @@ function M.config()
         "dashboard",
         "alpha",
       },
-      auto_reload_on_write = true,
+      auto_reload_on_write = false,
       hijack_directories = {
         enable = false,
       },

+ 7 - 1
lua/lvim/lsp/providers/sumneko_lua.lua

@@ -44,8 +44,14 @@ local opts = {
   settings = {
     Lua = {
       telemetry = { enable = false },
+      runtime = {
+        version = "LuaJIT",
+        special = {
+          reload = "require",
+        },
+      },
       diagnostics = {
-        globals = { "vim", "lvim", "packer_plugins" },
+        globals = { "vim", "lvim", "packer_plugins", "reload" },
       },
       workspace = default_workspace,
     },

+ 28 - 27
lua/lvim/plugins.lua

@@ -1,3 +1,4 @@
+local reload = require("lvim.utils.reload").reload
 local core_plugins = {
   -- Packer can manage itself as an optional plugin
   { "wbthomason/packer.nvim" },
@@ -10,20 +11,20 @@ local core_plugins = {
   {
     "williamboman/mason.nvim",
     config = function()
-      require("lvim.core.mason").setup()
+      reload("lvim.core.mason").setup()
     end,
   },
   {
     "folke/tokyonight.nvim",
     config = function()
-      require("lvim.core.theme").setup()
+      reload("lvim.core.theme").setup()
     end,
     -- disable = not vim.startswith(lvim.colorscheme, "tokyonight"),
   },
   {
     "rcarriga/nvim-notify",
     config = function()
-      require("lvim.core.notify").setup()
+      reload("lvim.core.notify").setup()
     end,
     requires = { "nvim-telescope/telescope.nvim" },
     disable = not lvim.builtin.notify.active or not lvim.builtin.telescope.active,
@@ -37,7 +38,7 @@ local core_plugins = {
     "nvim-telescope/telescope.nvim",
     branch = "0.1.x",
     config = function()
-      require("lvim.core.telescope").setup()
+      reload("lvim.core.telescope").setup()
     end,
     disable = not lvim.builtin.telescope.active,
   },
@@ -52,7 +53,7 @@ local core_plugins = {
     "hrsh7th/nvim-cmp",
     config = function()
       if lvim.builtin.cmp then
-        require("lvim.core.cmp").setup()
+        reload("lvim.core.cmp").setup()
       end
     end,
     requires = {
@@ -66,7 +67,7 @@ local core_plugins = {
   {
     "L3MON4D3/LuaSnip",
     config = function()
-      local utils = require "lvim.utils"
+      local utils = reload "lvim.utils"
       local paths = {}
       if lvim.builtin.luasnip.sources.friendly_snippets then
         paths[#paths + 1] = utils.join_paths(get_runtime_dir(), "site", "pack", "packer", "start", "friendly-snippets")
@@ -75,11 +76,11 @@ local core_plugins = {
       if utils.is_directory(user_snippets) then
         paths[#paths + 1] = user_snippets
       end
-      require("luasnip.loaders.from_lua").lazy_load()
-      require("luasnip.loaders.from_vscode").lazy_load {
+      reload("luasnip.loaders.from_lua").lazy_load()
+      reload("luasnip.loaders.from_vscode").lazy_load {
         paths = paths,
       }
-      require("luasnip.loaders.from_snipmate").lazy_load()
+      reload("luasnip.loaders.from_snipmate").lazy_load()
     end,
   },
   {
@@ -104,7 +105,7 @@ local core_plugins = {
     "windwp/nvim-autopairs",
     -- event = "InsertEnter",
     config = function()
-      require("lvim.core.autopairs").setup()
+      reload("lvim.core.autopairs").setup()
     end,
     disable = not lvim.builtin.autopairs.active,
   },
@@ -114,7 +115,7 @@ local core_plugins = {
     "nvim-treesitter/nvim-treesitter",
     -- run = ":TSUpdate",
     config = function()
-      require("lvim.core.treesitter").setup()
+      reload("lvim.core.treesitter").setup()
     end,
   },
   {
@@ -128,7 +129,7 @@ local core_plugins = {
     -- event = "BufWinOpen",
     -- cmd = "NvimTreeToggle",
     config = function()
-      require("lvim.core.nvimtree").setup()
+      reload("lvim.core.nvimtree").setup()
     end,
     disable = not lvim.builtin.nvimtree.active,
   },
@@ -136,7 +137,7 @@ local core_plugins = {
   {
     "christianchiarulli/lir.nvim",
     config = function()
-      require("lvim.core.lir").setup()
+      reload("lvim.core.lir").setup()
     end,
     disable = not lvim.builtin.lir.active,
   },
@@ -144,7 +145,7 @@ local core_plugins = {
     "lewis6991/gitsigns.nvim",
 
     config = function()
-      require("lvim.core.gitsigns").setup()
+      reload("lvim.core.gitsigns").setup()
     end,
     event = "BufRead",
     disable = not lvim.builtin.gitsigns.active,
@@ -154,7 +155,7 @@ local core_plugins = {
   {
     "folke/which-key.nvim",
     config = function()
-      require("lvim.core.which-key").setup()
+      reload("lvim.core.which-key").setup()
     end,
     event = "BufWinEnter",
     disable = not lvim.builtin.which_key.active,
@@ -165,7 +166,7 @@ local core_plugins = {
     "numToStr/Comment.nvim",
     event = "BufRead",
     config = function()
-      require("lvim.core.comment").setup()
+      reload("lvim.core.comment").setup()
     end,
     disable = not lvim.builtin.comment.active,
   },
@@ -174,7 +175,7 @@ local core_plugins = {
   {
     "ahmedkhalf/project.nvim",
     config = function()
-      require("lvim.core.project").setup()
+      reload("lvim.core.project").setup()
     end,
     disable = not lvim.builtin.project.active,
   },
@@ -191,7 +192,7 @@ local core_plugins = {
     "nvim-lualine/lualine.nvim",
     -- "Lunarvim/lualine.nvim",
     config = function()
-      require("lvim.core.lualine").setup()
+      reload("lvim.core.lualine").setup()
     end,
     disable = not lvim.builtin.lualine.active,
   },
@@ -200,7 +201,7 @@ local core_plugins = {
   {
     "SmiteshP/nvim-navic",
     config = function()
-      require("lvim.core.breadcrumbs").setup()
+      reload("lvim.core.breadcrumbs").setup()
     end,
     disable = not lvim.builtin.breadcrumbs.active,
   },
@@ -208,7 +209,7 @@ local core_plugins = {
   {
     "akinsho/bufferline.nvim",
     config = function()
-      require("lvim.core.bufferline").setup()
+      reload("lvim.core.bufferline").setup()
     end,
     branch = "main",
     event = "BufWinEnter",
@@ -220,7 +221,7 @@ local core_plugins = {
     "mfussenegger/nvim-dap",
     -- event = "BufWinEnter",
     config = function()
-      require("lvim.core.dap").setup()
+      reload("lvim.core.dap").setup()
     end,
     disable = not lvim.builtin.dap.active,
   },
@@ -238,7 +239,7 @@ local core_plugins = {
   {
     "goolord/alpha-nvim",
     config = function()
-      require("lvim.core.alpha").setup()
+      reload("lvim.core.alpha").setup()
     end,
     disable = not lvim.builtin.alpha.active,
   },
@@ -249,7 +250,7 @@ local core_plugins = {
     event = "BufWinEnter",
     branch = "main",
     config = function()
-      require("lvim.core.terminal").setup()
+      reload("lvim.core.terminal").setup()
     end,
     disable = not lvim.builtin.terminal.active,
   },
@@ -262,7 +263,7 @@ local core_plugins = {
   {
     "RRethy/vim-illuminate",
     config = function()
-      require("lvim.core.illuminate").setup()
+      reload("lvim.core.illuminate").setup()
     end,
     disable = not lvim.builtin.illuminate.active,
   },
@@ -270,7 +271,7 @@ local core_plugins = {
   {
     "lukas-reineke/indent-blankline.nvim",
     config = function()
-      require("lvim.core.indentlines").setup()
+      reload("lvim.core.indentlines").setup()
     end,
     disable = not lvim.builtin.indentlines.active,
   },
@@ -281,7 +282,7 @@ local core_plugins = {
     config = function()
       pcall(function()
         if lvim and lvim.colorscheme == "onedarker" then
-          require("onedarker").setup()
+          reload("onedarker").setup()
           lvim.builtin.lualine.options.theme = "onedarker"
         end
       end)
@@ -295,7 +296,7 @@ local content = vim.fn.readfile(default_snapshot_path)
 local default_sha1 = vim.fn.json_decode(content)
 
 local get_default_sha1 = function(spec)
-  local short_name, _ = require("packer.util").get_plugin_short_name(spec)
+  local short_name, _ = reload("packer.util").get_plugin_short_name(spec)
   return default_sha1[short_name] and default_sha1[short_name].commit
 end
 

+ 80 - 0
lua/lvim/utils/reload.lua

@@ -0,0 +1,80 @@
+local M = {}
+
+-- revisit this
+-- function prequire(package)
+--   local status, lib = pcall(require, package)
+--   if status then
+--     return lib
+--   else
+--     vim.notify("Failed to require '" .. package .. "' from " .. debug.getinfo(2).source)
+--     return nil
+--   end
+-- end
+
+local function _assign(old, new, k)
+  local otype = type(old[k])
+  local ntype = type(new[k])
+  -- print("hi")
+  if (otype == "thread" or otype == "userdata") or (ntype == "thread" or ntype == "userdata") then
+    vim.notify(string.format("warning: old or new attr %s type be thread or userdata", k))
+  end
+  old[k] = new[k]
+end
+
+local function _replace(old, new, repeat_tbl)
+  if repeat_tbl[old] then
+    return
+  end
+  repeat_tbl[old] = true
+
+  local dellist = {}
+  for k, _ in pairs(old) do
+    if not new[k] then
+      table.insert(dellist, k)
+    end
+  end
+  for _, v in ipairs(dellist) do
+    old[v] = nil
+  end
+
+  for k, _ in pairs(new) do
+    if not old[k] then
+      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))
+        _assign(old, new, k)
+      else
+        if type(old[k]) == "table" then
+          _replace(old[k], new[k], repeat_tbl)
+        else
+          _assign(old, new, k)
+        end
+      end
+    end
+  end
+end
+
+M.reload = function(mod)
+  if not package.loaded[mod] then
+    local m = require(mod)
+    return m
+  end
+  -- vim.notify "begin reload!!!"
+
+  local old = package.loaded[mod]
+  package.loaded[mod] = nil
+  local new = require(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
+
+return M