feat: Simplify wkSpec func

This commit is contained in:
punkfairie 2024-12-04 21:41:30 -08:00
parent df3282c64d
commit d8949f0cee
Signed by: punkfairie
GPG key ID: A509E8F77FB9D696
5 changed files with 37 additions and 47 deletions

View file

@ -21,44 +21,20 @@ end
---Generates a function that can be used to create which-key mappings. ---Generates a function that can be used to create which-key mappings.
---@param color string The color to use for the icon. ---@param color string The color to use for the icon.
---@param setRhs? boolean Whether to allow setting the rhs.
---@return function ---@return function
function M.wkSpec(color, setRhs) function M.wkSpec(color)
if setRhs then
---@param lhs string
---@param rhs string | fun()
---@param icon string | wk.Icon
---@param opts? wk.Spec
return function(lhs, rhs, icon, opts)
if type(icon) == 'string' then
icon = { icon = icon, color = color }
else
icon = vim.tbl_deep_extend('force', icon, { color = color })
end
return vim.tbl_deep_extend(
'force',
{ lhs, rhs, icon = icon },
(opts or {})
)
end
end
---@param lhs string ---@param lhs string
---@param rhs string | fun()
---@param icon string | wk.Icon ---@param icon string | wk.Icon
---@param opts? wk.Spec ---@param opts? wk.Spec
return function(lhs, icon, opts) return function(lhs, rhs, icon, opts)
if type(icon) == 'string' then if type(icon) == 'string' then
icon = { icon = icon, color = color } icon = { icon = icon, color = color }
else else
icon = vim.tbl_deep_extend('force', icon, { color = color }) icon = vim.tbl_deep_extend('force', icon, { color = color })
end end
return vim.tbl_deep_extend( return vim.tbl_deep_extend('force', { lhs, rhs, icon = icon }, (opts or {}))
'force',
{ lhs, icon = { icon = icon, color = color } },
(opts or {})
)
end end
end end

View file

@ -51,11 +51,11 @@ return {
local mkKey = MarleyVim.wkSpec(require('colors').search) local mkKey = MarleyVim.wkSpec(require('colors').search)
require('which-key').add({ require('which-key').add({
mkKey('s', '', { mode = { 'n', 'x', 'o' } }), mkKey('s', nil, '', { mode = { 'n', 'x', 'o' } }),
mkKey('S', '', { mode = { 'n', 'x', 'o' } }), mkKey('S', nil, '', { mode = { 'n', 'x', 'o' } }),
mkKey('r', '', { mode = { 'o' } }), mkKey('r', nil, '', { mode = { 'o' } }),
mkKey('r', '󰊕', { mode = { 'x', 'o' } }), mkKey('r', nil, '󰊕', { mode = { 'x', 'o' } }),
mkKey('<C-s>', '󰥖', { mode = { 'c' } }), mkKey('<C-s>', nil, '󰥖', { mode = { 'c' } }),
}) })
end, end,
} }

View file

@ -25,7 +25,7 @@ return {
local gitsigns = package.loaded.gitsigns local gitsigns = package.loaded.gitsigns
local icons = require('icons') local icons = require('icons')
local mkKey = MarleyVim.wkSpec(require('colors').git, true) local mkKey = MarleyVim.wkSpec(require('colors').git)
require('which-key').add({ require('which-key').add({
mkKey(']h', function() mkKey(']h', function()
if vim.wo.diff then if vim.wo.diff then
@ -34,6 +34,7 @@ return {
gitsigns.nav_hunk('next') gitsigns.nav_hunk('next')
end end
end, icons.next, { desc = 'next hunk' }), end, icons.next, { desc = 'next hunk' }),
mkKey('[h', function() mkKey('[h', function()
if vim.wo.diff then if vim.wo.diff then
vim.cmd.normal({ '[c', bang = true }) vim.cmd.normal({ '[c', bang = true })
@ -41,68 +42,81 @@ return {
gitsigns.nav_hunk('prev') gitsigns.nav_hunk('prev')
end end
end, icons.prev, { desc = 'previous hunk' }), end, icons.prev, { desc = 'previous hunk' }),
mkKey(']H', function() mkKey(']H', function()
gitsigns.nav_hunk('last') gitsigns.nav_hunk('last')
end, icons.last, { desc = 'last hunk' }), end, icons.last, { desc = 'last hunk' }),
mkKey('[H', function() mkKey('[H', function()
gitsigns.nav_hunk('first') gitsigns.nav_hunk('first')
end, icons.first, { desc = 'first hunk' }), end, icons.first, { desc = 'first hunk' }),
mkKey( mkKey(
'<LEADER>ghs', '<LEADER>ghs',
'<CMD>Gitsigns stage_hunk<CR>', '<CMD>Gitsigns stage_hunk<CR>',
icons.git.staged, icons.git.staged,
{ mode = { 'n', 'v' }, desc = 'stage hunk' } { mode = { 'n', 'v' }, desc = 'stage hunk' }
), ),
mkKey( mkKey(
'<LEADER>ghr', '<LEADER>ghr',
'<CMD>Gitsigns reset_hunk<CR>', '<CMD>Gitsigns reset_hunk<CR>',
icons.git.unstaged, icons.git.unstaged,
{ mode = { 'n', 'v' }, desc = 'reset hunk' } { mode = { 'n', 'v' }, desc = 'reset hunk' }
), ),
mkKey( mkKey(
'<LEADER>ghS', '<LEADER>ghS',
gitsigns.stage_buffer, gitsigns.stage_buffer,
icons.git.staged, icons.git.staged,
{ desc = 'stage buffer' } { desc = 'stage buffer' }
), ),
mkKey( mkKey(
'<LEADER>ghu', '<LEADER>ghu',
gitsigns.undo_stage_hunk, gitsigns.undo_stage_hunk,
icons.undo, icons.undo,
{ desc = 'undo stage hunk' } { desc = 'undo stage hunk' }
), ),
mkKey( mkKey(
'<LEADER>ghR', '<LEADER>ghR',
gitsigns.reset_buffer, gitsigns.reset_buffer,
icons.git.unstaged, icons.git.unstaged,
{ desc = 'reset buffer' } { desc = 'reset buffer' }
), ),
mkKey( mkKey(
'<LEADER>ghp', '<LEADER>ghp',
gitsigns.preview_hunk_inline, gitsigns.preview_hunk_inline,
'', '',
{ desc = 'preview hunk inline' } { desc = 'preview hunk inline' }
), ),
mkKey('<LEADER>ghb', function() mkKey('<LEADER>ghb', function()
gitsigns.blame_line({ full = true }) gitsigns.blame_line({ full = true })
end, { cat = 'filetype', name = 'git' }, { end, { cat = 'filetype', name = 'git' }, {
desc = 'blame line', desc = 'blame line',
}), }),
mkKey( mkKey(
'<LEADER>ghB', '<LEADER>ghB',
gitsigns.blame, gitsigns.blame,
{ cat = 'filetype', name = 'git' }, { cat = 'filetype', name = 'git' },
{ desc = 'blame buffer' } { desc = 'blame buffer' }
), ),
mkKey( mkKey(
'<LEADER>ghd', '<LEADER>ghd',
gitsigns.diffthis, gitsigns.diffthis,
icons.git.diff, icons.git.diff,
{ desc = 'diff file' } { desc = 'diff file' }
), ),
mkKey('<LEADER>ghD', function() mkKey('<LEADER>ghD', function()
gitsigns.diffthis('~') gitsigns.diffthis('~')
end, icons.git.diff, { desc = 'diff file from ~' }), end, icons.git.diff, { desc = 'diff file from ~' }),
mkKey( mkKey(
'ih', 'ih',
'<CMD><C-u>Gitsigns select_hunk<CR>', '<CMD><C-u>Gitsigns select_hunk<CR>',

View file

@ -94,16 +94,16 @@ return {
local mkKey = MarleyVim.wkSpec(colors.buffers) local mkKey = MarleyVim.wkSpec(colors.buffers)
require('which-key').add({ require('which-key').add({
mkKey('<LEADER>bp', ''), mkKey('<LEADER>bp', nil, ''),
mkKey('<LEADER>bP', '󰐄'), mkKey('<LEADER>bP', nil, '󰐄'),
mkKey('<LEADER>br', ''), mkKey('<LEADER>br', nil, ''),
mkKey('<LEADER>bl', ''), mkKey('<LEADER>bl', nil, ''),
mkKey('<S-H>', ''), mkKey('<S-H>', nil, ''),
mkKey('<S-L>', ''), mkKey('<S-L>', nil, ''),
mkKey('[b', ''), mkKey('[b', nil, ''),
mkKey(']b', ''), mkKey(']b', nil, ''),
mkKey('[B', ''), mkKey('[B', nil, ''),
mkKey(']B', ''), mkKey(']B', nil, ''),
}) })
end, end,
} }

View file

@ -105,9 +105,9 @@ return {
local mkKey = MarleyVim.wkSpec(require('colors').notifications) local mkKey = MarleyVim.wkSpec(require('colors').notifications)
require('which-key').add({ require('which-key').add({
mkKey('<S-ENTER>', '', { mode = 'c' }), mkKey('<S-ENTER>', nil, '', { mode = 'c' }),
mkKey('<C-f>', '', { mode = { 'i', 'n', 's' } }), mkKey('<C-f>', nil, '', { mode = { 'i', 'n', 's' } }),
mkKey('<C-b>', '', { mode = { 'i', 'n', 's' } }), mkKey('<C-b>', nil, '', { mode = { 'i', 'n', 's' } }),
}) })
end, end,
} }