Skip to content

Commit 6f7defb

Browse files
committed
refactor(config)!: breaking change - update shortcut registration and config formatting
1 parent 17e956a commit 6f7defb

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

lua/simplegpt/conf.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ local M = {
311311
-- customized shortcuts
312312
custom_shortcuts = {
313313
-- An exmaple of shorcuts
314-
-- ["<LocalLeader>sQ"] = {
314+
-- {
315+
-- key = "<LocalLeader>sQ"
315316
-- mode = { "n", "v" },
316317
-- tpl = "question.json",
317318
-- target = "chat",

lua/simplegpt/shortcut.lua

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ function M.build_func(target)
5050
end
5151
end
5252

53-
local function register_shortcut_dict(shortcut_dict)
54-
for key, s in pairs(shortcut_dict) do
55-
vim.keymap.set(s.mode, key, function()
53+
-- Make sure we have `key`attribute in each element of `shortcut_list`
54+
local function register_shortcut_list(shortcut_list)
55+
for _, s in ipairs(shortcut_list) do
56+
vim.keymap.set(s.mode, s.key, function()
5657
loader.load_reg(s.tpl)
5758

5859
-- Support setting extra reg when loading template
@@ -77,16 +78,12 @@ end
7778
function M.register_shortcuts()
7879
local keymaps = require"simplegpt.conf".options.keymaps
7980
local shortcuts = keymaps.shortcuts
80-
local shortcut_dict = {}
8181
for _, s in ipairs(shortcuts.list) do
82-
local key = s.key or (shortcuts.prefix and shortcuts.prefix .. s.suffix)
83-
if key then
84-
shortcut_dict[key] = s
85-
end
82+
s.key = s.key or (shortcuts.prefix and shortcuts.prefix .. s.suffix)
8683
end
87-
register_shortcut_dict(shortcut_dict)
84+
register_shortcut_list(shortcuts.list)
8885

89-
register_shortcut_dict(keymaps.custom_shortcuts)
86+
register_shortcut_list(keymaps.custom_shortcuts)
9087
end
9188

9289

tests/demo/custom_tpl.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
opts = {
88
keymaps = {
99
custom_shortcuts = {
10-
["<localleader>st"] = {
10+
{
1111
mode = { "v" },
12+
key = "<localleader>st",
1213
tpl = "converter.json",
1314
target = "diff",
1415
opts = { noremap = true, silent = true, desc = "Convert between YAML & JSON" },

0 commit comments

Comments
 (0)