From d8949f0cee4cb69f8216d838d2459978ad5d2edc Mon Sep 17 00:00:00 2001 From: punkfairie Date: Wed, 4 Dec 2024 21:41:30 -0800 Subject: [PATCH] feat: Simplify wkSpec func --- nvim/lua/lib/init.lua | 32 +++-------------------- nvim/lua/plugins/editor/flash-nvim.lua | 10 +++---- nvim/lua/plugins/editor/gitsigns-nvim.lua | 16 +++++++++++- nvim/lua/plugins/ui/bufferline-nvim.lua | 20 +++++++------- nvim/lua/plugins/ui/noice-nvim.lua | 6 ++--- 5 files changed, 37 insertions(+), 47 deletions(-) diff --git a/nvim/lua/lib/init.lua b/nvim/lua/lib/init.lua index 8d3d8ef..ec89d3b 100644 --- a/nvim/lua/lib/init.lua +++ b/nvim/lua/lib/init.lua @@ -21,44 +21,20 @@ end ---Generates a function that can be used to create which-key mappings. ---@param color string The color to use for the icon. ----@param setRhs? boolean Whether to allow setting the rhs. ---@return function -function M.wkSpec(color, setRhs) - 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 - +function M.wkSpec(color) ---@param lhs string + ---@param rhs string | fun() ---@param icon string | wk.Icon ---@param opts? wk.Spec - return function(lhs, icon, opts) + 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, icon = { icon = icon, color = color } }, - (opts or {}) - ) + return vim.tbl_deep_extend('force', { lhs, rhs, icon = icon }, (opts or {})) end end diff --git a/nvim/lua/plugins/editor/flash-nvim.lua b/nvim/lua/plugins/editor/flash-nvim.lua index 5e2d187..f9d9775 100644 --- a/nvim/lua/plugins/editor/flash-nvim.lua +++ b/nvim/lua/plugins/editor/flash-nvim.lua @@ -51,11 +51,11 @@ return { local mkKey = MarleyVim.wkSpec(require('colors').search) require('which-key').add({ - mkKey('s', '', { mode = { 'n', 'x', 'o' } }), - mkKey('S', '', { mode = { 'n', 'x', 'o' } }), - mkKey('r', '', { mode = { 'o' } }), - mkKey('r', '󰊕', { mode = { 'x', 'o' } }), - mkKey('', '󰥖', { mode = { 'c' } }), + mkKey('s', nil, '', { mode = { 'n', 'x', 'o' } }), + mkKey('S', nil, '', { mode = { 'n', 'x', 'o' } }), + mkKey('r', nil, '', { mode = { 'o' } }), + mkKey('r', nil, '󰊕', { mode = { 'x', 'o' } }), + mkKey('', nil, '󰥖', { mode = { 'c' } }), }) end, } diff --git a/nvim/lua/plugins/editor/gitsigns-nvim.lua b/nvim/lua/plugins/editor/gitsigns-nvim.lua index b821cde..b972bf1 100644 --- a/nvim/lua/plugins/editor/gitsigns-nvim.lua +++ b/nvim/lua/plugins/editor/gitsigns-nvim.lua @@ -25,7 +25,7 @@ return { local gitsigns = package.loaded.gitsigns local icons = require('icons') - local mkKey = MarleyVim.wkSpec(require('colors').git, true) + local mkKey = MarleyVim.wkSpec(require('colors').git) require('which-key').add({ mkKey(']h', function() if vim.wo.diff then @@ -34,6 +34,7 @@ return { gitsigns.nav_hunk('next') end end, icons.next, { desc = 'next hunk' }), + mkKey('[h', function() if vim.wo.diff then vim.cmd.normal({ '[c', bang = true }) @@ -41,68 +42,81 @@ return { gitsigns.nav_hunk('prev') end end, icons.prev, { desc = 'previous hunk' }), + mkKey(']H', function() gitsigns.nav_hunk('last') end, icons.last, { desc = 'last hunk' }), + mkKey('[H', function() gitsigns.nav_hunk('first') end, icons.first, { desc = 'first hunk' }), + mkKey( 'ghs', 'Gitsigns stage_hunk', icons.git.staged, { mode = { 'n', 'v' }, desc = 'stage hunk' } ), + mkKey( 'ghr', 'Gitsigns reset_hunk', icons.git.unstaged, { mode = { 'n', 'v' }, desc = 'reset hunk' } ), + mkKey( 'ghS', gitsigns.stage_buffer, icons.git.staged, { desc = 'stage buffer' } ), + mkKey( 'ghu', gitsigns.undo_stage_hunk, icons.undo, { desc = 'undo stage hunk' } ), + mkKey( 'ghR', gitsigns.reset_buffer, icons.git.unstaged, { desc = 'reset buffer' } ), + mkKey( 'ghp', gitsigns.preview_hunk_inline, '', { desc = 'preview hunk inline' } ), + mkKey('ghb', function() gitsigns.blame_line({ full = true }) end, { cat = 'filetype', name = 'git' }, { desc = 'blame line', }), + mkKey( 'ghB', gitsigns.blame, { cat = 'filetype', name = 'git' }, { desc = 'blame buffer' } ), + mkKey( 'ghd', gitsigns.diffthis, icons.git.diff, { desc = 'diff file' } ), + mkKey('ghD', function() gitsigns.diffthis('~') end, icons.git.diff, { desc = 'diff file from ~' }), + mkKey( 'ih', 'Gitsigns select_hunk', diff --git a/nvim/lua/plugins/ui/bufferline-nvim.lua b/nvim/lua/plugins/ui/bufferline-nvim.lua index 920508e..91526b4 100644 --- a/nvim/lua/plugins/ui/bufferline-nvim.lua +++ b/nvim/lua/plugins/ui/bufferline-nvim.lua @@ -94,16 +94,16 @@ return { local mkKey = MarleyVim.wkSpec(colors.buffers) require('which-key').add({ - mkKey('bp', ''), - mkKey('bP', '󰐄'), - mkKey('br', ''), - mkKey('bl', ''), - mkKey('', ''), - mkKey('', ''), - mkKey('[b', ''), - mkKey(']b', ''), - mkKey('[B', ''), - mkKey(']B', ''), + mkKey('bp', nil, ''), + mkKey('bP', nil, '󰐄'), + mkKey('br', nil, ''), + mkKey('bl', nil, ''), + mkKey('', nil, ''), + mkKey('', nil, ''), + mkKey('[b', nil, ''), + mkKey(']b', nil, ''), + mkKey('[B', nil, ''), + mkKey(']B', nil, ''), }) end, } diff --git a/nvim/lua/plugins/ui/noice-nvim.lua b/nvim/lua/plugins/ui/noice-nvim.lua index 03a22ac..6f621d4 100644 --- a/nvim/lua/plugins/ui/noice-nvim.lua +++ b/nvim/lua/plugins/ui/noice-nvim.lua @@ -105,9 +105,9 @@ return { local mkKey = MarleyVim.wkSpec(require('colors').notifications) require('which-key').add({ - mkKey('', '', { mode = 'c' }), - mkKey('', '', { mode = { 'i', 'n', 's' } }), - mkKey('', '', { mode = { 'i', 'n', 's' } }), + mkKey('', nil, '', { mode = 'c' }), + mkKey('', nil, '', { mode = { 'i', 'n', 's' } }), + mkKey('', nil, '', { mode = { 'i', 'n', 's' } }), }) end, }