From 25ea356161588b60df5c72922734dbeea9cb652c Mon Sep 17 00:00:00 2001 From: newbieQQ Date: Wed, 17 Jun 2026 15:44:16 +0800 Subject: [PATCH] refactor: dynamic keymap registration from config.commands Replace hardcoded reasonix/lazygit keymap blocks with a single loop over config.commands. Adding a new tool (e.g. codex) now only requires two lines in user config: commands = { codex = 'codex' }, keymaps = { codex = { 'n', 'cx' } }, Commands can be strings or functions for lazy evaluation. --- lua/QQdock/init.lua | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lua/QQdock/init.lua b/lua/QQdock/init.lua index 8d873af..362f262 100644 --- a/lua/QQdock/init.lua +++ b/lua/QQdock/init.lua @@ -8,11 +8,17 @@ -- horizontal = 10, -- 竖屏下方终端高度(行数) -- vertical = 40, -- 横屏右侧终端宽度(列数) -- }, +-- commands = { +-- reasonix = 'reasonix', -- 任何终端命令,key = 工具名 +-- lazygit = 'lazygit', +-- codex = 'codex', -- 新增工具只需加一行 +-- }, -- keymaps = { -- shell = { 'n', '' }, -- 普通终端 -- shell_i = { 'i', '' }, -- 插入模式也开终端 --- reasonix = { 'n', '' }, -- Reasonix --- lazygit = { 'n', 'gg' }, -- lazygit +-- reasonix = { 'n', '' }, -- 键名必须匹配 commands 里的 key +-- lazygit = { 'n', 'gg' }, +-- codex = { 'n', 'cx' }, -- 新增快捷键只需加一行 -- }, -- }) -- @@ -88,17 +94,13 @@ function M.setup(opts) if km.shell_i then safe_map(km.shell_i[1], km.shell_i[2], M.shell) end - if km.reasonix then - safe_map(km.reasonix[1], km.reasonix[2], function() - local cmd = config.commands.reasonix - M.open(type(cmd) == 'function' and cmd() or cmd) - end) - end - if km.lazygit then - safe_map(km.lazygit[1], km.lazygit[2], function() - local cmd = config.commands.lazygit - M.open(type(cmd) == 'function' and cmd() or cmd) - end) + -- 注册工具命令键位(动态遍历 config.commands,添加新工具只需改 config) + for name, cmd in pairs(config.commands) do + if km[name] then + safe_map(km[name][1], km[name][2], function() + M.open(type(cmd) == 'function' and cmd() or cmd) + end) + end end end