From 7d4b90741638ad16f62a19cc85516f06f6a02973 Mon Sep 17 00:00:00 2001 From: punkfairie Date: Sun, 1 Dec 2024 17:10:02 -0800 Subject: [PATCH] feat: which-key icons for plugins --- nvim/lua/colors.lua | 13 ++++ nvim/lua/keymaps.lua | 86 +++++++++++----------- nvim/lua/lib/init.lua | 16 ++++ nvim/lua/plugins/editor/flash-nvim.lua | 24 ++++-- nvim/lua/plugins/editor/grug-far-nvim.lua | 2 +- nvim/lua/plugins/editor/neo-tree-nvim.lua | 19 ++++- nvim/lua/plugins/editor/which-key-nvim.lua | 60 +++++++++++++-- nvim/lua/plugins/ui/bufferline-nvim.lua | 42 ++++++++--- nvim/lua/plugins/ui/noice-nvim.lua | 27 ++++--- nvim/lua/snacks-nvim.lua | 16 ++-- 10 files changed, 219 insertions(+), 86 deletions(-) create mode 100644 nvim/lua/colors.lua diff --git a/nvim/lua/colors.lua b/nvim/lua/colors.lua new file mode 100644 index 0000000..5276978 --- /dev/null +++ b/nvim/lua/colors.lua @@ -0,0 +1,13 @@ +---@type {[string]:"azure" | "blue" | "cyan" | "green" | "grey" | "orange" | "purple" | "red" | "yellow"} +return { + buffers = 'cyan', + diagnostics = 'green', + surround = 'purple', + ui = 'green', + window = 'blue', + search = 'green', + git = 'orange', + notifications = 'orange', + sessions = 'azure', + explorer = 'yellow', +} diff --git a/nvim/lua/keymaps.lua b/nvim/lua/keymaps.lua index e73dc32..ab1cae5 100644 --- a/nvim/lua/keymaps.lua +++ b/nvim/lua/keymaps.lua @@ -22,12 +22,12 @@ end -- Resize windows -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- for key, dir in pairs({ - Up = 'Increase', - Down = 'Decrease', - Left = 'Decrease', - Right = 'Increase', + Up = 'increase', + Down = 'decrease', + Left = 'decrease', + Right = 'increase', }) do - local sign = (dir == 'Increase') and '+' or '-' + local sign = (dir == 'increase') and '+' or '-' set( { 'n' }, @@ -44,8 +44,8 @@ for key, dir in pairs({ ['[b'] = 'previous', [']b'] = 'next', }) do - -- previous -> Prev - local prettyDir = (dir:gsub('^%l', string.upper)):sub(1, 4) + -- previous -> prev + local prettyDir = dir:sub(1, 4) set( { 'n' }, @@ -55,21 +55,21 @@ for key, dir in pairs({ ) end -set({ 'n' }, 'bD', 'bd', { desc = 'Delete buffer and window' }) +set({ 'n' }, 'bD', 'bd', { desc = 'delete buffer and window' }) -- Clear search & refresh -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- set( { 'n', 'i' }, '', 'noh', - { desc = 'Escape and clear hlsearch' } + { desc = 'escape and clear hlsearch' } ) set( { 'n' }, 'ur', 'nohlsearchdiffupdatenormal! ', - { desc = 'Redraw / clear hlsearch / diff update' } + { desc = 'redraw / clear hlsearch / diff update' } ) -- Better n & N -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- @@ -80,14 +80,14 @@ for _, mode in ipairs({ 'n', 'x', 'o' }) do { mode }, 'n', f("'Nn'[v:searchforward]%s", zv), - { desc = 'Next search result', expr = true } + { desc = 'next search result', expr = true } ) set( { mode }, 'N', f("'nN'[v:searchforward]%s", zv), - { desc = 'Previous search result', expr = true } + { desc = 'previous search result', expr = true } ) end @@ -102,34 +102,36 @@ set( { 'n' }, 'K', 'norm! K', - { desc = 'Search for word' } + { desc = 'search for word' } ) -- Better indenting -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- for _, char in ipairs({ '<', '>' }) do - set({ 'v' }, char, f('%sgv', char)) + local desc = 'indent ' .. (char == '<' and 'left' or 'right') + + set({ 'v' }, char, f('%sgv', char), { desc = desc }) end -- Commenting -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -for key, dir in pairs({ o = 'Below', O = 'Above' }) do +for key, dir in pairs({ o = 'below', O = 'above' }) do set( { 'n' }, f('gc%s', key), f('%sVcxnormal gccfxa', key), - { desc = f('Add comment %s', dir) } + { desc = f('add comment %s', dir) } ) end -- New files -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -set({ 'n' }, 'fn', 'enew', { desc = 'New file' }) +set({ 'n' }, 'fn', 'enew', { desc = 'new file' }) -- Locations/quickfixes -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -set({ 'n' }, 'xl', 'lopen', { desc = 'Location list' }) -set({ 'n' }, 'xq', 'copen', { desc = 'Quickfix list' }) +set({ 'n' }, 'xl', 'lopen', { desc = 'location list' }) +set({ 'n' }, 'xq', 'copen', { desc = 'quickfix list' }) -for key, dir in pairs({ ['['] = 'Previous', [']'] = 'Next' }) do - -- Previous -> prev - local cmd = (dir:gsub('^%l', string.lower)):sub(1, 4) +for key, dir in pairs({ ['['] = 'previous', [']'] = 'next' }) do + -- previous -> prev + local cmd = dir:sub(1, 4) set( { 'n' }, @@ -142,7 +144,7 @@ end -- Diagnostics -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- set({ 'n' }, 'cd', function() vim.diagnostic.open_float() -end, { desc = 'Line diagnostics' }) +end, { desc = 'line diagnostics' }) local function goto_diagnostic(next, severity) local go = next and vim.diagnostic.goto_next or vim.diagnostic.goto_prev @@ -153,48 +155,48 @@ local function goto_diagnostic(next, severity) end for key, sev in pairs({ - d = { nil, 'Diagnostic' }, - e = { 'ERROR', 'Error' }, - w = { 'WARN', 'Warning' }, + d = { nil, 'diagnostic' }, + e = { 'ERROR', 'error' }, + w = { 'WARN', 'warning' }, }) do set( { 'n' }, f(']%s', key), goto_diagnostic(true, sev[1]), - { desc = f('Next %s', sev[2]) } + { desc = f('next %s', sev[2]) } ) set( { 'n' }, f('[%s', key), goto_diagnostic(false, sev[1]), - { desc = f('Previous %s', sev[2]) } + { desc = f('previous %s', sev[2]) } ) end -- Quit -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -set({ 'n' }, 'qq', 'qa', { desc = 'Quit all' }) +set({ 'n' }, 'qq', 'qa', { desc = 'quit all' }) -- Inspect -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -set({ 'n' }, 'ui', vim.show_pos, { desc = 'Inspect position' }) +set({ 'n' }, 'ui', vim.show_pos, { desc = 'inspect position' }) -- Window management -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -set({ 'n' }, 'w', '', { desc = 'Windows', remap = true }) -set({ 'n' }, '-', 's', { desc = 'Split below', remap = true }) -set({ 'n' }, '|', 'v', { desc = 'Split right', remap = true }) -set({ 'n' }, 'wd', 'c', { desc = 'Delete window', remap = true }) +set({ 'n' }, 'w', '', { desc = 'windows', remap = true }) +set({ 'n' }, '-', 's', { desc = 'split below', remap = true }) +set({ 'n' }, '|', 'v', { desc = 'split right', remap = true }) +set({ 'n' }, 'wd', 'c', { desc = 'delete window', remap = true }) -- Tab management -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- for key, act in pairs({ - l = 'Last', - o = 'Only', - f = 'First', - [''] = 'New', - [']'] = 'Next', - d = 'Close', - ['['] = 'Previous', + l = 'last', + o = 'only', + f = 'first', + [''] = 'new', + [']'] = 'next', + d = 'close', + ['['] = 'previous', }) do - local desc = (act == 'Only') and 'Close other tabs' or f('%s tab', act) + local desc = (act == 'only') and 'close other tabs' or f('%s tab', act) set( { 'n' }, diff --git a/nvim/lua/lib/init.lua b/nvim/lua/lib/init.lua index ca743bd..37545d7 100644 --- a/nvim/lua/lib/init.lua +++ b/nvim/lua/lib/init.lua @@ -19,4 +19,20 @@ function M.local_require(prefix) end end +---Generates a function that can be used to create which-key mappings. +---@param color string The color to use for the icon. +---@return function +function M.wkSpec(color) + ---@param lhs string + ---@param icon string + ---@param opts? wk.Spec + return function(lhs, icon, opts) + return vim.tbl_deep_extend( + 'force', + { lhs, icon = { icon = icon, color = color } }, + (opts or {}) + ) + end +end + return M diff --git a/nvim/lua/plugins/editor/flash-nvim.lua b/nvim/lua/plugins/editor/flash-nvim.lua index 9cce8d3..5e2d187 100644 --- a/nvim/lua/plugins/editor/flash-nvim.lua +++ b/nvim/lua/plugins/editor/flash-nvim.lua @@ -8,7 +8,7 @@ return { require('flash').jump() end, mode = { 'n', 'x', 'o' }, - desc = 'Flash', + desc = 'flash', }, { 'S', @@ -16,14 +16,15 @@ return { require('flash').treesitter() end, mode = { 'n', 'x', 'o' }, - desc = 'Flash (treesitter)', + desc = 'flash (treesitter)', }, { 'r', function() require('flash').remote() end, - desc = 'Remote flash', + mode = { 'o' }, + desc = 'remote flash', }, { 'R', @@ -31,17 +32,30 @@ return { require('flash').treesitter_search() end, mode = { 'x', 'o' }, - desc = 'Treesitter search', + desc = 'treesitter search', }, { '', function() require('flash').toggle() end, - 'Toggle flash search', + mode = { 'c' }, + desc = 'toggle flash search', }, }, + before = function() + require('lz.n').trigger_load('which-key.nvim') + end, after = function() require('flash').setup({}) + + 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' } }), + }) end, } diff --git a/nvim/lua/plugins/editor/grug-far-nvim.lua b/nvim/lua/plugins/editor/grug-far-nvim.lua index 4d4da38..c142e9c 100644 --- a/nvim/lua/plugins/editor/grug-far-nvim.lua +++ b/nvim/lua/plugins/editor/grug-far-nvim.lua @@ -16,7 +16,7 @@ return { }) end, mode = { 'n', 'v' }, - desc = 'Search and replace', + desc = 'search and replace', }, }, after = function() diff --git a/nvim/lua/plugins/editor/neo-tree-nvim.lua b/nvim/lua/plugins/editor/neo-tree-nvim.lua index 3a1cc25..385f0cf 100644 --- a/nvim/lua/plugins/editor/neo-tree-nvim.lua +++ b/nvim/lua/plugins/editor/neo-tree-nvim.lua @@ -15,7 +15,7 @@ local function make_toggle_mapping(root, grouped) dir = root and (MarleyVim.root()) or (vim.uv.cwd()), }) end, - desc = 'Explorer (' .. (root and 'root' or 'cwd') .. ')', + desc = 'explorer (' .. (root and 'root' or 'cwd') .. ')', } end @@ -35,7 +35,7 @@ return { toggle = true, }) end, - desc = 'Git explorer', + desc = 'git explorer', }, { 'be', @@ -45,11 +45,16 @@ return { toggle = true, }) end, - desc = 'Buffer explorer', + desc = 'buffer explorer', }, }, before = function() - require('lz.n').trigger_load({ 'plenary.nvim', 'mini.icons', 'nui.nvim' }) + require('lz.n').trigger_load({ + 'plenary.nvim', + 'mini.icons', + 'nui.nvim', + 'which-key.nvim', + }) end, after = function() local icons = require('icons') @@ -122,5 +127,11 @@ return { { event = events.FILE_RENAMED, handler = on_move }, }, }) + + local colors = require('colors') + require('which-key').add({ + { 'ge', icon = { icon = '', color = colors.git } }, + { 'be', icon = { icon = '', color = colors.buffer } }, + }) end, } diff --git a/nvim/lua/plugins/editor/which-key-nvim.lua b/nvim/lua/plugins/editor/which-key-nvim.lua index 2322155..4db3ddb 100644 --- a/nvim/lua/plugins/editor/which-key-nvim.lua +++ b/nvim/lua/plugins/editor/which-key-nvim.lua @@ -21,6 +21,9 @@ return { require('lz.n').trigger_load('mini.icons') end, after = function() + local icons = require('icons') + local colors = require('colors') + require('which-key').setup({ preset = 'modern', spec = { @@ -37,12 +40,12 @@ return { { 'u', group = 'ui', - icon = { icon = '󰙵 ', color = 'cyan' }, + icon = { icon = '󰙵 ', color = colors.ui }, }, { 'x', group = 'diagnostics/quickfix', - icon = { icon = '󱖫 ', color = 'green' }, + icon = { icon = '󱖫 ', color = colors.diagnostics }, }, { '[', group = 'previous' }, { ']', group = 'next' }, @@ -50,7 +53,7 @@ return { { 'gs', group = 'surround', - icon = { icon = '󰅲', color = 'purple' }, + icon = { icon = '󰅲', color = colors.surround }, }, { 'z', group = 'fold' }, { @@ -67,12 +70,59 @@ return { return require('which-key.extras').expand.win() end, }, - { 'gx', desc = 'Open with system app' }, + { 'gx', desc = 'open with system app' }, + { 'ur', icon = { icon = '', color = colors.ui } }, + { '|', icon = { icon = '' } }, + { '-', icon = { icon = '' } }, + }, + { + mode = { 'n', 'i' }, + hidden = true, + { '' }, + { '' }, + { '' }, + { '' }, + }, + { + mode = { 'n', 'x' }, + hidden = true, + { 'j' }, + { 'k' }, + }, + { + mode = { 'v' }, + { '<', icon = { icon = '󰉵' } }, + { '>', icon = { icon = '󰉶' } }, }, }, icons = { rules = { - { pattern = 'explorer', icon = '󰙅' }, + { pattern = 'explorer', icon = '󰙅', color = colors.explorer }, + { pattern = 'lazygit', icon = '' }, + { pattern = 'comment', icon = '' }, + { pattern = 'buffer', color = colors.buffer }, + { + pattern = 'notification', + icon = '󰈸', + color = colors.notifications, + }, + { pattern = 'message', icon = '󰈸', color = colors.notifications }, + { pattern = 'session', icon = '', color = colors.sessions }, + { + pattern = 'diagnostic', + icon = icons.diagnostics.Info, + hl = 'DiagnosticInfo', + }, + { + pattern = 'error', + icon = icons.diagnostics.Error, + hl = 'DiagnosticError', + }, + { + pattern = ' warning', + icon = icons.diagnostics.Warning, + hl = 'DiagnosticWarn', + }, }, }, }) diff --git a/nvim/lua/plugins/ui/bufferline-nvim.lua b/nvim/lua/plugins/ui/bufferline-nvim.lua index 3d0e617..920508e 100644 --- a/nvim/lua/plugins/ui/bufferline-nvim.lua +++ b/nvim/lua/plugins/ui/bufferline-nvim.lua @@ -2,31 +2,35 @@ return { 'bufferline.nvim', event = 'DeferredUIEnter', keys = { - { 'bp', 'BufferLineTogglePin', desc = 'Toggle pin' }, + { + 'bp', + 'BufferLineTogglePin', + desc = 'toggle buffer pin', + }, { 'bP', 'BufferLineGroupClose ungrouped', - desc = 'Delete non-pinned buffers', + desc = 'delete non-pinned buffers', }, { 'br', 'BufferLineCloseRight', - desc = 'Delete buffers to the right', + desc = 'delete buffers to the right', }, { 'bl', 'BufferLineCloseLeft', - desc = 'Delete buffers to the left', + desc = 'delete buffers to the left', }, - { '', 'BufferLineCyclePrev', desc = 'Previous buffer' }, - { '', 'BufferLineCycleNext', desc = 'Next buffer' }, - { '[b', 'BufferLineCyclePrev', desc = 'Previous buffer' }, - { ']b', 'BufferLineCycleNext', desc = 'Next buffer' }, - { '[B', 'BufferLineMovePrev', desc = 'Move buffer left' }, - { ']B', 'BufferLineMoveNext', desc = 'Move buffer right' }, + { '', 'BufferLineCyclePrev', desc = 'previous buffer' }, + { '', 'BufferLineCycleNext', desc = 'next buffer' }, + { '[b', 'BufferLineCyclePrev', desc = 'previous buffer' }, + { ']b', 'BufferLineCycleNext', desc = 'next buffer' }, + { '[B', 'BufferLineMovePrev', desc = 'move buffer left' }, + { ']B', 'BufferLineMoveNext', desc = 'move buffer right' }, }, before = function() - require('lz.n').trigger_load('mini.icons') + require('lz.n').trigger_load({ 'mini.icons', 'which-key.nvim' }) if vim.g.colors_name == 'rose-pine' then require('lz.n').trigger_load('rose-pine') @@ -85,5 +89,21 @@ return { end) end, }) + + local colors = require('colors') + 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', ''), + }) end, } diff --git a/nvim/lua/plugins/ui/noice-nvim.lua b/nvim/lua/plugins/ui/noice-nvim.lua index 8167523..03a22ac 100644 --- a/nvim/lua/plugins/ui/noice-nvim.lua +++ b/nvim/lua/plugins/ui/noice-nvim.lua @@ -2,49 +2,49 @@ return { 'noice.nvim', event = 'DeferredUIEnter', keys = { - { 'nn', '', desc = '+notifications' }, + { 'n', '', desc = '+notifications' }, { '', function() require('noice').redirect(vim.fn.getcmdline()) end, mode = 'c', - desc = 'Redirect cmdline', + desc = 'redirect cmdline', }, { 'nl', function() require('noice').cmd('last') end, - desc = 'Last message', + desc = 'last message', }, { 'nh', function() require('noice').cmd('history') end, - desc = 'Message history', + desc = 'message history', }, { 'na', function() require('noice').cmd('all') end, - desc = 'All messages', + desc = 'all messages', }, { 'nd', function() require('noice').cmd('dismiss') end, - desc = 'Dismiss all', + desc = 'dismiss all', }, { 'nt', function() require('noice').cmd('pick') end, - desc = 'Message picker', + desc = 'message picker', }, { '', @@ -56,7 +56,7 @@ return { mode = { 'i', 'n', 's' }, silent = true, expr = true, - desc = 'Scroll forward', + desc = 'scroll forward', }, { '', @@ -68,11 +68,11 @@ return { mode = { 'i', 'n', 's' }, silent = true, expr = true, - desc = 'Scroll backward', + desc = 'scroll backward', }, }, before = function() - require('lz.n').trigger_load('nui.nvim') + require('lz.n').trigger_load({ 'nui.nvim', 'which-key.nvim' }) end, after = function() require('noice').setup({ @@ -102,5 +102,12 @@ return { lsp_doc_border = true, }, }) + + local mkKey = MarleyVim.wkSpec(require('colors').notifications) + require('which-key').add({ + mkKey('', '', { mode = 'c' }), + mkKey('', '', { mode = { 'i', 'n', 's' } }), + mkKey('', '', { mode = { 'i', 'n', 's' } }), + }) end, } diff --git a/nvim/lua/snacks-nvim.lua b/nvim/lua/snacks-nvim.lua index d7bc2ca..c738c90 100644 --- a/nvim/lua/snacks-nvim.lua +++ b/nvim/lua/snacks-nvim.lua @@ -26,16 +26,16 @@ vim.print = _G.dd set({ 'n' }, 'un', function() Snacks.notifier.hide() -end, { desc = 'Dismiss all notifications' }) +end, { desc = 'dismiss all notifications' }) -- Buffers -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- set({ 'n' }, 'bd', function() Snacks.bufdelete() -end, { desc = 'Delete buffer' }) +end, { desc = 'delete buffer' }) set({ 'n' }, 'bo', function() Snacks.bufdelete.other() -end, { desc = 'Delete other buffers' }) +end, { desc = 'delete other buffers' }) -- Toggles -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- @@ -69,11 +69,11 @@ end set({ 'n' }, 'gb', function() Snacks.git.blame_line() -end, { desc = 'Git blame line' }) +end, { desc = 'git blame line' }) set({ 'n' }, 'gB', function() Snacks.gitbrowse() -end, { desc = 'Git browse (open)' }) +end, { desc = 'git browse (open)' }) set({ 'n' }, 'gY', function() Snacks.gitbrowse({ @@ -81,14 +81,14 @@ set({ 'n' }, 'gY', function() vim.fn.setreg('+', url) end, }) -end, { desc = 'Git browse (copy)' }) +end, { desc = 'git browse (copy)' }) -- LazyGit -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- if vim.fn.executable('lazygit') == 1 then set({ 'n' }, 'gg', function() Snacks.lazygit({ cwd = MarleyVim.root() }) - end, { desc = 'Lazygit (root dir)' }) + end, { desc = 'lazygit (root dir)' }) -- set({ 'n' }, 'gG', function() -- Snacks.lazygit() @@ -96,7 +96,7 @@ if vim.fn.executable('lazygit') == 1 then set({ 'n' }, 'gf', function() Snacks.lazygit.log_file() - end, { desc = 'Lazygit current file history' }) + end, { desc = 'lazygit current file history' }) set({ 'n' }, 'gl', function() Snacks.lazygit.log({ cwd = MarleyVim.root() })