From ae765f74382f470383180d5379d82956c4754735 Mon Sep 17 00:00:00 2001 From: xfy Date: Thu, 28 May 2026 15:47:02 +0800 Subject: [PATCH] Add lazy.track() for accurate module loading statistics in starter dashboard - Introduce lazy.track(name) to mark directly-loaded modules as loaded without invoking their setup function again. - Replace plugin-package counting with a fixed module list (15 functional modules) so the starter footer shows consistent X/Y progress. - Call lazy.track() for starter, notify, cmdline, lspconfig, and mason. Co-Authored-By: Claude Opus 4.7 (1M context) --- lua/lazy.lua | 5 +++++ lua/lsp.lua | 3 +++ lua/pack.lua | 37 ++++++++++++++++++++----------------- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/lua/lazy.lua b/lua/lazy.lua index 878469a..2cfbbe3 100644 --- a/lua/lazy.lua +++ b/lua/lazy.lua @@ -43,6 +43,11 @@ M.load = function(name, fn) end end +-- 仅标记模块为已加载,不执行 setup(用于直接加载的插件)。 +M.track = function(name) + M._loaded[name] = true +end + -- --------------------------------------------------------------------------- -- 事件触发懒加载 -- --------------------------------------------------------------------------- diff --git a/lua/lsp.lua b/lua/lsp.lua index 3118c35..a03fe6f 100644 --- a/lua/lsp.lua +++ b/lua/lsp.lua @@ -83,7 +83,10 @@ end) -- nvim-lspconfig 提供常见 LSP 服务器的预设配置。 vim.cmd.packadd("nvim-lspconfig") +lazy.track("lspconfig") + require("mason").setup() +lazy.track("mason") -- 构建 LSP 客户端 capabilities(能力声明),告知服务器客户端支持的功能。 -- 先获取 Neovim 默认 capabilities,再与 mini.completion 的 LSP 补全能力合并。 diff --git a/lua/pack.lua b/lua/pack.lua index 351fc2d..c6b2395 100644 --- a/lua/pack.lua +++ b/lua/pack.lua @@ -57,19 +57,18 @@ vim.pack.add({ local starter = require("mini.starter") --- 统计已安装插件数量(用于页脚显示 "Loaded X/Y plugins") --- 遍历 stdpath("data")/site/pack/core/opt/ 目录下的所有子目录 -local function get_total_plugins() - local pack_dir = vim.fn.stdpath("data") .. "/site/pack/core/opt" - local paths = vim.fn.glob(pack_dir .. "/*", false, true) - local count = 0 - for _, path in ipairs(paths) do - if vim.fn.isdirectory(path) == 1 then - count = count + 1 - end - end - return count -end +-- 所有功能模块列表(用于页脚 "X/Y modules" 统计)。 +-- 口径与 lazy._loaded 一致:每个独立初始化的功能模块算一个, +-- 不再按 vim.pack 的插件包统计(如 mini.nvim 包含多个模块)。 +local all_modules = { + "starter", "notify", "cmdline", "icons", + "pick", "files", + "diff", "surround", + "completion", "snippets", + "treesitter", "lsp", + "conform", "lspconfig", "mason", + "fugitive", "grugfar", +} -- Neovim ASCII Art Logo(使用 Unicode 方块字符绘制) local header_lines = { @@ -95,13 +94,13 @@ starter.setup({ items = { { name = " ", action = "", section = "" } }, -- 空选项(仅展示页眉页脚) header = table.concat(header_lines, "\n"), -- Logo 文本 - -- 页脚函数:显示启动耗时和插件加载统计 + -- 页脚函数:显示启动耗时和模块加载统计 footer = function() - -- 已加载的懒加载模块数(动态统计) + -- 已加载的模块数(动态统计,包含直接加载和懒加载) local loaded = vim.tbl_count(require("lazy")._loaded) - local total = get_total_plugins() + local total = #all_modules -- startup_ms 在 pack.lua 加载时一次性计算,避免后续渲染时数值变化 - local text = string.format(" Loaded %d/%d plugins in %.0f ms", loaded, total, startup_ms) + local text = string.format(" %d/%d modules | %.0f ms", loaded, total, startup_ms) local text_width = vim.fn.strdisplaywidth(text) -- 计算左填充空格数以实现居中 local pad = math.floor((max_header_width - text_width) / 2) @@ -114,6 +113,8 @@ starter.setup({ }, }) +lazy.track("starter") + -- --------------------------------------------------------------------------- -- mini.files — 文件浏览器(按键触发懒加载) -- --------------------------------------------------------------------------- @@ -193,6 +194,7 @@ require("mini.notify").setup({ }) -- 将 vim.notify 重定向到 mini.notify vim.notify = require("mini.notify").make_notify() +lazy.track("notify") -- --------------------------------------------------------------------------- -- mini.cmdline — 增强命令行 @@ -206,6 +208,7 @@ vim.keymap.set("n", ":", function() require("mini.cmdline").setup({ autocorrect = { enable = false }, -- 禁用自动纠正 }) + lazy.track("cmdline") return ":" end, { expr = true, noremap = true })