From cc4945649badd9497b40cbebcbc9d69281737a3a Mon Sep 17 00:00:00 2001 From: Job Jobse Date: Wed, 23 Sep 2026 20:29:53 +0200 Subject: [PATCH] refactor: merge leader functions --- plugin/20_keymaps.lua | 124 ++++++++++++++++++++---------------------- 1 file changed, 59 insertions(+), 65 deletions(-) diff --git a/plugin/20_keymaps.lua b/plugin/20_keymaps.lua index d4494df..fdcee1c 100644 --- a/plugin/20_keymaps.lua +++ b/plugin/20_keymaps.lua @@ -14,12 +14,8 @@ Config.leader_group_clues = { } -- Keymap Helpers -local nmap_leader = function(suffix, rhs, desc) - vim.keymap.set('n', '' .. suffix, rhs, { desc = desc, silent = true }) -end - -local xmap_leader = function(suffix, rhs, desc) - vim.keymap.set('x', '' .. suffix, rhs, { desc = desc, silent = true }) +local leader = function(suffix, rhs, desc, mode) + vim.keymap.set(mode or 'n', '' .. suffix, rhs, { desc = desc, silent = true }) end -- Buffers @@ -34,19 +30,19 @@ local close_invisible_buffers = function() :each(function(buf) pcall(vim.api.nvim_buf_delete, buf, { force = false }) end) end -nmap_leader('bd', vim.cmd.bdelete, 'Delete') -nmap_leader('bs', new_scratch_buffer, 'Scratch') -nmap_leader('bw', vim.cmd.bwipeout, 'Wipeout') -nmap_leader('bo', close_invisible_buffers, 'Close invisible buffers') +leader('bd', vim.cmd.bdelete, 'Delete') +leader('bs', new_scratch_buffer, 'Scratch') +leader('bw', vim.cmd.bwipeout, 'Wipeout') +leader('bo', close_invisible_buffers, 'Close invisible buffers') -- Debug -nmap_leader('db', function() require('dap').toggle_breakpoint() end, 'Toggle Breakpoint') -nmap_leader('dl', function() require('dap').set_breakpoint(nil, nil, vim.fn.input('Log point: ')) end, 'Log point') -nmap_leader('dc', function() require('dap').continue() end, 'Run/Continue') -nmap_leader('dC', function() require('dap').run_to_cursor() end, 'Run to Cursor') -nmap_leader('de', function() require('dap.ui.widgets').hover() end, 'Inspect') -nmap_leader('dr', function() require('dap').repl.open() end, 'Open REPL') -nmap_leader('dt', function() require('dap').terminate() end, 'Terminate') +leader('db', function() require('dap').toggle_breakpoint() end, 'Toggle Breakpoint') +leader('dl', function() require('dap').set_breakpoint(nil, nil, vim.fn.input('Log point: ')) end, 'Log point') +leader('dc', function() require('dap').continue() end, 'Run/Continue') +leader('dC', function() require('dap').run_to_cursor() end, 'Run to Cursor') +leader('de', function() require('dap.ui.widgets').hover() end, 'Inspect') +leader('dr', function() require('dap').repl.open() end, 'Open REPL') +leader('dt', function() require('dap').terminate() end, 'Terminate') -- Explore local explore_at_file = function() @@ -54,30 +50,30 @@ local explore_at_file = function() MiniFiles.open(vim.uv.fs_stat(file) and file or nil) end -nmap_leader('ed', function() MiniFiles.open() end, 'Directory') -nmap_leader('ef', explore_at_file, 'File directory') -nmap_leader('ey', function() vim.fn.setreg('+', vim.fn.fnamemodify(vim.api.nvim_buf_get_name(0), ':.')) end, 'Yank relative path') -nmap_leader('eY', function() vim.fn.setreg('+', vim.api.nvim_buf_get_name(0)) end, 'Yank absolute path') -nmap_leader('en', vim.cmd.messages, 'Notifications') +leader('ed', function() MiniFiles.open() end, 'Directory') +leader('ef', explore_at_file, 'File directory') +leader('ey', function() vim.fn.setreg('+', vim.fn.fnamemodify(vim.api.nvim_buf_get_name(0), ':.')) end, 'Yank relative path') +leader('eY', function() vim.fn.setreg('+', vim.api.nvim_buf_get_name(0)) end, 'Yank absolute path') +leader('en', vim.cmd.messages, 'Notifications') -- Find -nmap_leader('fb', function() MiniPick.builtin.buffers() end, 'Buffers') -nmap_leader('fc', function() MiniExtra.pickers.git_commits() end, 'Commits (all)') -nmap_leader('fC', function() MiniExtra.pickers.git_commits({ path = vim.fn.expand('%') }) end, 'Commits (buf)') -nmap_leader('fd', function() MiniExtra.pickers.diagnostic({ scope = 'all' }) end, 'Diagnostic workspace') -nmap_leader('fD', function() MiniExtra.pickers.diagnostic({ scope = 'current' }) end, 'Diagnostic buffer') -nmap_leader('ff', function() MiniPick.builtin.files() end, 'Files') -nmap_leader('fg', function() MiniPick.builtin.grep_live() end, 'Grep live') -nmap_leader('fG', function() MiniPick.builtin.grep({ pattern = vim.fn.expand('') }) end, 'Grep word') -nmap_leader('fh', function() MiniPick.builtin.help() end, 'Help tags') -nmap_leader('fl', function() MiniExtra.pickers.buf_lines({ scope = 'all' }) end, 'Lines (all)') -nmap_leader('fL', function() MiniExtra.pickers.buf_lines({ scope = 'current' }) end, 'Lines (buf)') -nmap_leader('fm', function() require('git').status() end, 'Modified / changed files') -nmap_leader('fo', function() MiniExtra.pickers.oldfiles() end, 'Old / recent files') -nmap_leader('fr', function() MiniPick.builtin.resume() end, 'Resume') -nmap_leader('fs', function() MiniExtra.pickers.lsp({ scope = 'workspace_symbol_live' }) end, 'Symbols workspace') -nmap_leader('fS', function() MiniExtra.pickers.lsp({ scope = 'document_symbol' }) end, 'Symbols document') -nmap_leader('f:', function() MiniExtra.pickers.history({ scope = ':' }) end, 'Command history') +leader('fb', function() MiniPick.builtin.buffers() end, 'Buffers') +leader('fc', function() MiniExtra.pickers.git_commits() end, 'Commits (all)') +leader('fC', function() MiniExtra.pickers.git_commits({ path = vim.fn.expand('%') }) end, 'Commits (buf)') +leader('fd', function() MiniExtra.pickers.diagnostic({ scope = 'all' }) end, 'Diagnostic workspace') +leader('fD', function() MiniExtra.pickers.diagnostic({ scope = 'current' }) end, 'Diagnostic buffer') +leader('ff', function() MiniPick.builtin.files() end, 'Files') +leader('fg', function() MiniPick.builtin.grep_live() end, 'Grep live') +leader('fG', function() MiniPick.builtin.grep({ pattern = vim.fn.expand('') }) end, 'Grep word') +leader('fh', function() MiniPick.builtin.help() end, 'Help tags') +leader('fl', function() MiniExtra.pickers.buf_lines({ scope = 'all' }) end, 'Lines (all)') +leader('fL', function() MiniExtra.pickers.buf_lines({ scope = 'current' }) end, 'Lines (buf)') +leader('fm', function() require('git').status() end, 'Modified / changed files') +leader('fo', function() MiniExtra.pickers.oldfiles() end, 'Old / recent files') +leader('fr', function() MiniPick.builtin.resume() end, 'Resume') +leader('fs', function() MiniExtra.pickers.lsp({ scope = 'workspace_symbol_live' }) end, 'Symbols workspace') +leader('fS', function() MiniExtra.pickers.lsp({ scope = 'document_symbol' }) end, 'Symbols document') +leader('f:', function() MiniExtra.pickers.history({ scope = ':' }) end, 'Command history') -- Git local diff_unstaged = function() @@ -86,44 +82,42 @@ local diff_unstaged = function() vim.cmd.diffthis() end -nmap_leader('ga', function() vim.cmd('Git add %') end, 'Add current file') -nmap_leader('gd', diff_unstaged, 'Diff unstaged') -nmap_leader('gs', function() MiniGit.show_at_cursor() end, 'Show at cursor') -xmap_leader('gs', function() MiniGit.show_at_cursor() end, 'Show at selection') +leader('ga', function() vim.cmd('Git add %') end, 'Add current file') +leader('gd', diff_unstaged, 'Diff unstaged') +leader('gs', function() MiniGit.show_at_cursor() end, 'Show at cursor') +leader('gs', function() MiniGit.show_at_cursor() end, 'Show at selection', 'x') -- Language -nmap_leader('la', function() vim.lsp.buf.code_action() end, 'Actions') -nmap_leader('ld', function() vim.diagnostic.open_float() end, 'Diagnostic popup') -nmap_leader('lf', function() require('conform').format({ async = true }) end, 'Format') -xmap_leader('lf', function() require('conform').format({ async = true }) end, 'Format selection') -nmap_leader('li', function() vim.lsp.buf.implementation() end, 'Implementation') -nmap_leader('lh', function() vim.lsp.buf.hover() end, 'Hover') -nmap_leader('ll', function() vim.lsp.codelens.run() end, 'Lens') -nmap_leader('lr', function() vim.lsp.buf.rename() end, 'Rename') -nmap_leader('lR', function() vim.lsp.buf.references() end, 'References') -nmap_leader('ls', function() vim.lsp.buf.definition() end, 'Source definition') -nmap_leader('lt', function() vim.lsp.buf.type_definition() end, 'Type definition') +leader('la', function() vim.lsp.buf.code_action() end, 'Actions') +leader('lf', function() require('conform').format({ async = true }) end, 'Format') +leader('lf', function() require('conform').format({ async = true }) end, 'Format selection', 'x') +leader('li', function() vim.lsp.buf.implementation() end, 'Implementation') +leader('ll', function() vim.lsp.codelens.run() end, 'Lens') +leader('lr', function() vim.lsp.buf.rename() end, 'Rename') +leader('lR', function() vim.lsp.buf.references() end, 'References') +leader('ls', function() vim.lsp.buf.definition() end, 'Source definition') +leader('lt', function() vim.lsp.buf.type_definition() end, 'Type definition') -- Request -nmap_leader('rs', function() require('kulala').run() end, 'Send HTTP request') +leader('rs', function() require('kulala').run() end, 'Send HTTP request') -- Terminal local open_ai = function() vim.cmd('vertical term ' .. (vim.fn.executable('pi') == 1 and 'pi' or 'claude')) end -nmap_leader('tT', function() vim.cmd('horizontal term') end, 'Terminal (horizontal)') -nmap_leader('tt', function() vim.cmd('vertical term') end, 'Terminal (vertical)') -nmap_leader('ta', open_ai, 'AI') -nmap_leader('tf', function() require('terminal').open(vim.o.shell) end, 'Terminal (popup)') -nmap_leader('tg', function() require('terminal').open('lazygit') end, 'LazyGit') -nmap_leader('ts', function() require('terminal').open('lazysql') end, 'LazySQL') +leader('tT', function() vim.cmd('horizontal term') end, 'Terminal (horizontal)') +leader('tt', function() vim.cmd('vertical term') end, 'Terminal (vertical)') +leader('ta', open_ai, 'AI') +leader('tf', function() require('terminal').open(vim.o.shell) end, 'Terminal (popup)') +leader('tg', function() require('terminal').open('lazygit') end, 'LazyGit') +leader('ts', function() require('terminal').open('lazysql') end, 'LazySQL') -- Tabs -nmap_leader('', vim.cmd.tabnew, 'New tab') -nmap_leader('d', vim.cmd.tabclose, 'Close tab') -nmap_leader(']', vim.cmd.tabnext, 'Next tab') -nmap_leader('[', vim.cmd.tabprevious, 'Previous tab') +leader('', vim.cmd.tabnew, 'New tab') +leader('d', vim.cmd.tabclose, 'Close tab') +leader(']', vim.cmd.tabnext, 'Next tab') +leader('[', vim.cmd.tabprevious, 'Previous tab') -- Overwrites vim.keymap.set('n', '', vim.cmd.nohlsearch, { desc = 'Clear highlights', silent = true })