From 1cb58434495e4f88b9af7abbfa8c81d77a27916b Mon Sep 17 00:00:00 2001 From: captain Date: Thu, 7 May 2026 17:04:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=87=92=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/pack/configs/autopairs.lua | 1 + lua/pack/configs/gitsigns.lua | 1 + lua/pack/configs/indentblankline.lua | 1 + lua/pack/configs/lspconfig.lua | 1 + lua/pack/configs/lualine.lua | 95 +++++++++++++++----------- lua/pack/configs/mini.lua | 1 + lua/pack/configs/peek.lua | 50 ++++++++------ lua/pack/configs/rainbowdelimiters.lua | 3 +- lua/pack/configs/stickscroll.lua | 1 + lua/pack/configs/ufo.lua | 1 + snippets/lua.json | 2 + 11 files changed, 94 insertions(+), 63 deletions(-) diff --git a/lua/pack/configs/autopairs.lua b/lua/pack/configs/autopairs.lua index 4714ac5..3d9ba95 100644 --- a/lua/pack/configs/autopairs.lua +++ b/lua/pack/configs/autopairs.lua @@ -9,6 +9,7 @@ local P = { vim.api.nvim_create_autocmd({ "InsertEnter" }, { + once = true, callback = function() PackUtils.load(P, function() require('nvim-autopairs').setup({ diff --git a/lua/pack/configs/gitsigns.lua b/lua/pack/configs/gitsigns.lua index 8183059..76ddbef 100644 --- a/lua/pack/configs/gitsigns.lua +++ b/lua/pack/configs/gitsigns.lua @@ -13,6 +13,7 @@ PackUtils.setup_listener(P.name, P.build_cmd) vim.api.nvim_create_autocmd({ "UIEnter", -- vim.schedule(function() }, { + once = true, callback = function() vim.schedule(function() PackUtils.load(P, function() diff --git a/lua/pack/configs/indentblankline.lua b/lua/pack/configs/indentblankline.lua index 8d9cd0e..4035abd 100644 --- a/lua/pack/configs/indentblankline.lua +++ b/lua/pack/configs/indentblankline.lua @@ -8,6 +8,7 @@ local P = { vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" -- 处理普通的文本文件时也能显示基础线条 }, { + once = true, callback = function() PackUtils.load(P, function() local highlight = { diff --git a/lua/pack/configs/lspconfig.lua b/lua/pack/configs/lspconfig.lua index 20ed12e..5ff601d 100644 --- a/lua/pack/configs/lspconfig.lua +++ b/lua/pack/configs/lspconfig.lua @@ -28,6 +28,7 @@ vim.keymap.set("n", "=", vim.diagnostic.goto_next, opts) -- +跳 -- 懒加载触发器:当打开文件时触发 vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, { + once = true, callback = function() PackUtils.load(P, function() -- === 基础依赖初始化 (Mason) === diff --git a/lua/pack/configs/lualine.lua b/lua/pack/configs/lualine.lua index c7dd1c8..12acb22 100644 --- a/lua/pack/configs/lualine.lua +++ b/lua/pack/configs/lualine.lua @@ -8,50 +8,63 @@ local P = { }, } --- 懒加载触发器 +-- 将 Lualine 的配置逻辑封装成一个独立函数,方便重复调用 +local function setup_lualine_with_blend() + -- 确保 lualine 已经加载,如果没有加载则不执行(防止 ColorScheme 先于插件加载触发报错) + local has_lualine, lualine = pcall(require, "lualine") + if not has_lualine then return end + + -- ==================== 【动态融合逻辑】 ==================== + local normal_hl = vim.api.nvim_get_hl(0, { name = "Normal" }) + local editor_bg = normal_hl.bg and string.format("#%06x", normal_hl.bg) or "NONE" + + -- 每次需要重新 require('lualine.themes.auto') 来获取基于新主题生成的颜色 + package.loaded["lualine.themes.auto"] = nil + local blended_theme = require("lualine.themes.auto") + + for _, mode in pairs(blended_theme) do + for _, section in pairs(mode) do + if type(section) == "table" then + section.bg = editor_bg + end + end + end + + lualine.setup({ + options = { + theme = blended_theme, + globalstatus = true, + always_divide_middle = false, + component_separators = { left = "", right = "" }, + section_separators = { left = "", right = "" }, + }, + sections = { + lualine_a = {}, + lualine_b = { "branch", "diff", "diagnostics" }, + lualine_c = { { "lsp_status", color = { fg = "#6C6E81" } } }, + lualine_x = {}, + lualine_y = {}, + lualine_z = { "progress" }, + }, + }) +end + +-- 懒加载触发器 (只负责初次加载) vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, { + once = true, -- 只需要触发一次来加载插件 callback = function() PackUtils.load(P, function() - -- ==================== 【魔法时刻:动态融合主题】 ==================== - -- 获取当前 Neovim 的真实背景色 (Normal 高亮组) - local normal_hl = vim.api.nvim_get_hl(0, { name = "Normal" }) - -- 如果是透明背景,bg 返回 nil,我们赋值为 "NONE";否则转换为 HEX 颜色码 - local editor_bg = normal_hl.bg and string.format("#%06x", normal_hl.bg) or "NONE" - -- 加载 Lualine 的自动主题,它会自动为你提取当前主题的前景色(文字颜色) - local blended_theme = require("lualine.themes.auto") - -- 遍历主题的所有的模式 (normal, insert, visual...) 和区块 (a, b, c...) - for _, mode in pairs(blended_theme) do - for _, section in pairs(mode) do - if type(section) == "table" then - -- 强行将它们的背景色全部涂成和编辑器完全一样的颜色 - section.bg = editor_bg - end - end - end - -- ==================================================================== - require("lualine").setup({ - options = { - -- 挂载我们刚刚特制的融合主题 - theme = blended_theme, - -- 全局唯一状态栏 (不仅底部更干净,而且背景融合得更彻底) - globalstatus = true, - - always_divide_middle = false, - component_separators = { left = "", right = "" }, - section_separators = { left = "", right = "" }, - }, - sections = { - lualine_a = {}, - lualine_b = { "branch", "diff", "diagnostics" }, - lualine_c = { { - "lsp_status", - color = { fg = "#6C6E81" }, - }, }, - lualine_x = {}, - lualine_y = {}, - lualine_z = { "progress" }, - }, - }) + setup_lualine_with_blend() end) end }) + +-- 主题切换触发器 (负责在主题改变时刷新) +vim.api.nvim_create_autocmd("ColorScheme", { + -- 防止配置堆叠 + group = vim.api.nvim_create_augroup("LualineSync", { clear = true }), + callback = function() + -- 如果插件已经加载了,就重新应用配置 + setup_lualine_with_blend() + end +}) diff --git a/lua/pack/configs/mini.lua b/lua/pack/configs/mini.lua index 64bf7f5..bc179cd 100644 --- a/lua/pack/configs/mini.lua +++ b/lua/pack/configs/mini.lua @@ -7,6 +7,7 @@ local P = { vim.api.nvim_create_autocmd({ "UIEnter", -- vim.schedule(function() }, { + once = true, callback = function() vim.schedule(function() PackUtils.load(P, function() diff --git a/lua/pack/configs/peek.lua b/lua/pack/configs/peek.lua index e79e4f7..6bf6bc6 100644 --- a/lua/pack/configs/peek.lua +++ b/lua/pack/configs/peek.lua @@ -8,30 +8,38 @@ local P = { PackUtils.setup_listener(P.name, P.build_cmd) +-- app = { "zen", "-private-window" }, +-- app = { "firefox-esr", "-private-window" }, + -- 在插件未加载时,这些命令就存在了。一旦被调用,它们会先加载插件,再执行真正的功能。 vim.api.nvim_create_user_command("PeekToggle", function() - local peek = require("peek") - if not peek.is_open() and vim.bo[vim.api.nvim_get_current_buf()].filetype == 'markdown' then - PackUtils.load(P, function() - local app = { "chromium", "--app=http://localhost:9000/?theme=dark", "--incognito" } - if IS_ARM then - app = { "chromium", "--no-sandbox", "--app=http://localhost:9000/?theme=dark", "--incognito", "--test-type", - "--force-device-scale-factor=1.75" } - end - if vim.fn.has("wsl") == 1 then - app = { "/mnt/d/my_program/chrome-win/chrome.exe", "--no-sandbox", "--app=http://localhost:9000/?theme=dark", "--incognito", "--test-type" } - end - require("peek").setup({ - port = 9000, - -- app = { "zen", "-private-window" }, - -- app = { "firefox-esr", "-private-window" }, - app = app - }) - end) - peek.open() - else -- 只有加载了插件的情况下执行关闭,这样可以跳过非markdown文件 - if PackUtils.plugin_loaded[P.name] then + local is_markdown = vim.bo[vim.api.nvim_get_current_buf()].filetype == 'markdown' + if not PackUtils.plugin_loaded[P.name] then + if is_markdown then + PackUtils.load(P, function() + -- 动态决定浏览器配置 + local app = { "chromium", "--app=http://localhost:9000/?theme=dark", "--incognito" } + if IS_ARM then + app = { "chromium", "--no-sandbox", "--app=http://localhost:9000/?theme=dark", "--incognito", "--test-type", + "--force-device-scale-factor=1.75" } + elseif vim.fn.has("wsl") == 1 then + app = { "/mnt/d/my_program/chrome-win/chrome.exe", "--no-sandbox", "--app=http://localhost:9000/?theme=dark", + "--incognito", "--test-type" } + end + local peek = require("peek") + peek.setup({ + port = 9000, + app = app + }) + peek.open() + end) + end + else + local peek = require("peek") + if peek.is_open() then peek.close() + elseif is_markdown then + peek.open() end end end, { desc = "Lazy load and open Peek" }) diff --git a/lua/pack/configs/rainbowdelimiters.lua b/lua/pack/configs/rainbowdelimiters.lua index 17481a6..3842388 100644 --- a/lua/pack/configs/rainbowdelimiters.lua +++ b/lua/pack/configs/rainbowdelimiters.lua @@ -2,11 +2,12 @@ local P = { name = "rainbow-delimiters.nvim", } --- + -- 懒加载触发器 vim.api.nvim_create_autocmd({ "FileType", }, { + once = true, callback = function() PackUtils.load(P, function() vim.api.nvim_set_hl(0, "RainbowDelimiterRed", { fg = "#E06C75" }) diff --git a/lua/pack/configs/stickscroll.lua b/lua/pack/configs/stickscroll.lua index 9abe4a7..d1d953a 100644 --- a/lua/pack/configs/stickscroll.lua +++ b/lua/pack/configs/stickscroll.lua @@ -9,6 +9,7 @@ local P = { vim.api.nvim_create_autocmd({ "FileType" }, { + once = true, callback = function() PackUtils.load(P, function() -- 前往上下文开始处 diff --git a/lua/pack/configs/ufo.lua b/lua/pack/configs/ufo.lua index 34c8327..700e168 100644 --- a/lua/pack/configs/ufo.lua +++ b/lua/pack/configs/ufo.lua @@ -16,6 +16,7 @@ local P = { vim.api.nvim_create_autocmd({ "UIEnter", -- vim.schedule(function() }, { + once = true, callback = function() vim.schedule(function() PackUtils.load(P, function() diff --git a/snippets/lua.json b/snippets/lua.json index f7841ab..fe4ce96 100644 --- a/snippets/lua.json +++ b/snippets/lua.json @@ -39,6 +39,8 @@ "\t-- \"VimEnter\", -- 核心初始化完成", "\t-- \"UIEnter\", -- vim.schedule(function()", "}, {", + "\tonce = true,", + "\t-- group = vim.api.nvim_create_augroup(\"<++>\", { clear = true }),", "\tcallback = function()", "\t\tPackUtils.load(P, function()", "\t\t\t$7",