From 8b714fabfad03d9d33ae07138bbd05db075b856e Mon Sep 17 00:00:00 2001 From: Fabian Schmidt Date: Tue, 17 Sep 2024 14:49:38 +0200 Subject: [PATCH] Add which-key plugin and descriptions for all custom keymaps --- init.lua | 3 +- lazy-lock.json | 3 +- lua/maps.lua | 55 ---------------------------------- lua/plugins/dap.lua | 21 +++++++++++-- lua/plugins/lsp.lua | 31 +++++++++----------- lua/plugins/navigation.lua | 5 ++++ lua/plugins/wk.lua | 60 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 102 insertions(+), 76 deletions(-) delete mode 100644 lua/maps.lua create mode 100644 lua/plugins/wk.lua diff --git a/init.lua b/init.lua index 80fa602..0460980 100644 --- a/init.lua +++ b/init.lua @@ -13,7 +13,6 @@ end vim.opt.rtp:prepend(lazypath) vim.g.mapleader = " " require("lazy").setup("plugins") -require("maps").init() require("commands") vim.opt.backup = false @@ -43,3 +42,5 @@ vim.opt.expandtab = false vim.opt.autoindent = true vim.opt.smartindent = true vim.opt.completeopt = { "menu", "menuone", "noselect" } + +-- TODO replace config with opts in every plugin diff --git a/lazy-lock.json b/lazy-lock.json index 3a106fd..b5bac54 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -36,5 +36,6 @@ "telescope-ui-select.nvim": { "branch": "master", "commit": "6e51d7da30bd139a6950adf2a47fda6df9fa06d2" }, "telescope.nvim": { "branch": "master", "commit": "a0bbec21143c7bc5f8bb02e0005fa0b982edc026" }, "trouble.nvim": { "branch": "main", "commit": "6efc446226679fda0547c0fd6a7892fd5f5b15d8" }, - "vimwiki": { "branch": "dev", "commit": "705ad1e0dded0e3b7ff5fac78547ab67c9d39bdf" } + "vimwiki": { "branch": "dev", "commit": "705ad1e0dded0e3b7ff5fac78547ab67c9d39bdf" }, + "which-key.nvim": { "branch": "main", "commit": "bfec3d6bc0a9b0b2cb11644642f78c2c3915eef0" } } diff --git a/lua/maps.lua b/lua/maps.lua deleted file mode 100644 index 7aec44a..0000000 --- a/lua/maps.lua +++ /dev/null @@ -1,55 +0,0 @@ --- maps.lua - -local maps = {} - -local function map(mode, lhs, rhs, opts) - local options = { noremap = true } - if opts then options = vim.tbl_extend('force', options, opts) end - vim.api.nvim_set_keymap(mode, lhs, rhs, options) -end - -function maps.term() - -- Open and move to resized terminal - map('n', 't', 'sj :terminal:res 5i', { noremap = false }) - -- Enter normal mode in terminal - map('t', 'ß', '') -end - -function maps.telescope() - map('n', 'ff', 'lua require(\'telescope.builtin\').git_files()') - map('n', 'fg', 'lua require(\'telescope.builtin\').live_grep()') - map('n', 'fb', 'lua require(\'telescope.builtin\').buffers()') - map('n', 'fh', 'lua require(\'telescope.builtin\').help_tags()') -end - -function maps.fixIndent() - map('v', '<', '', '>gv') -end - -function maps.moveByRow() - -- When text is wrapped, move by terminal rows, not lines, unles a count is provided - map('n', 'j', 'v:count == 0 ? \'gj\' : \'j\'', { silent = true, expr = true }) - map('n', 'k', 'v:count == 0 ? \'gk\' : \'k\'', { silent = true, expr = true }) -end - -function maps.centeredSearch() - map('n', 'n', 'nzzzv') - map('n', 'N', 'Nzzzv') -end - -function maps.init() - vim.g.mapleader = " " - - -- map('n', 'b', 'NvimTreeToggle') - map('n', 'b', 'Lexplore | vert res 30') - map('n', 'cc', 'Telescope colorscheme') - - maps.term() - maps.telescope() - maps.fixIndent() - maps.moveByRow() - maps.centeredSearch() -end - -return maps diff --git a/lua/plugins/dap.lua b/lua/plugins/dap.lua index 7ffa650..2446521 100644 --- a/lua/plugins/dap.lua +++ b/lua/plugins/dap.lua @@ -1,12 +1,29 @@ return { { 'mfussenegger/nvim-dap', - dependencies = { 'nvim-lua/plenary.nvim' } + dependencies = { 'nvim-lua/plenary.nvim' }, + config = function() + local wk = require("which-key") + local dap = require("dap") + wk.add({ + { 'd', group = "dap" }, + { 'db', dap.toggle_breakpoint, desc = "Toggle breakpoint" }, + { 'dc', dap.continue, desc = "Launch/resume debug session" }, + { 'do', dap.step_over, desc = "Step over" }, + { 'di', dap.step_into, desc = "Step into" }, + { 'du', dap.step_out, desc = "Step out" }, + }) + end }, { 'rcarriga/nvim-dap-ui', config = function() - require("dapui").setup() + local dapui = require("dapui") + dapui.setup() + local wk = require("which-key") + wk.add({ + { 'dd', dapui.toggle, desc = "Toggle dap ui" } + }) end, dependencies = { 'mfussenegger/nvim-dap', 'nvim-neotest/nvim-nio' } }, diff --git a/lua/plugins/lsp.lua b/lua/plugins/lsp.lua index 2eb3c1b..7e8102c 100644 --- a/lua/plugins/lsp.lua +++ b/lua/plugins/lsp.lua @@ -5,26 +5,23 @@ local lsp_attach = function(client, buf) vim.lsp.inlay_hint.enable(true, nil) - local opts = { noremap = true, silent = true } - vim.keymap.set('n', '', require("telescope.builtin").diagnostics, opts) - - local bufopts = { noremap = true, silent = true, buffer = buf } + local wk = require("which-key") - vim.keymap.set('n', 'e', vim.diagnostic.open_float, bufopts) - vim.keymap.set('n', '', require("telescope.builtin").diagnostics, bufopts) - vim.keymap.set('n', 'd', vim.diagnostic.goto_prev, bufopts) - vim.keymap.set('n', 'd', vim.diagnostic.goto_next, bufopts) + wk.add({ + { 'e', vim.diagnostic.open_float, bufopts, desc = "Show diagnostics in buffer" }, + { '', require("telescope.builtin").diagnostics, bufopts, desc = "Show diagnostics under cursor" }, + { 'd', vim.diagnostic.goto_prev, bufopts }, + { 'd', vim.diagnostic.goto_next, bufopts }, - vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, bufopts) - vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts) - vim.keymap.set('n', 'K', vim.lsp.buf.hover, bufopts) - vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, bufopts) - vim.keymap.set('n', '', vim.lsp.buf.signature_help, bufopts) - vim.keymap.set('n', 'D', vim.lsp.buf.type_definition, bufopts) - vim.keymap.set('n', 'rn', vim.lsp.buf.rename, bufopts) - vim.keymap.set('n', 'ca', vim.lsp.buf.code_action, bufopts) - vim.keymap.set('n', 'gr', require("telescope.builtin").lsp_references, bufopts) + { 'gD', vim.lsp.buf.declaration, bufopts, desc = "Go to declaration" }, + { 'gd', vim.lsp.buf.definition, bufopts, desc = "Go to definition" }, + { 'gi', vim.lsp.buf.implementation, bufopts, desc = "Go to implementation" }, + { '', vim.lsp.buf.signature_help, bufopts, desc = "Show signature" }, + { 'rn', vim.lsp.buf.rename, bufopts, desc = "Rename" }, + { 'ca', vim.lsp.buf.code_action, bufopts, desc = "Show code actions" }, + { 'gr', require("telescope.builtin").lsp_references, bufopts, desc = "Shwo references" }, + }) end local mason_path = vim.fn.glob(vim.fn.stdpath "data" .. "/mason/packages/codelldb/extension/") diff --git a/lua/plugins/navigation.lua b/lua/plugins/navigation.lua index dd52c4a..b41d3a0 100644 --- a/lua/plugins/navigation.lua +++ b/lua/plugins/navigation.lua @@ -8,6 +8,11 @@ return { vim.keymap.set("n", "gh", require("telescope.builtin").git_bcommits, opts) vim.keymap.set("n", "gb", require("telescope.builtin").git_branches, opts) vim.keymap.set("n", "gs", require("telescope.builtin").git_status, opts) + vim.keymap.set('n', 'ff', 'lua require(\'telescope.builtin\').git_files()', opts) + vim.keymap.set('n', 'fg', 'lua require(\'telescope.builtin\').live_grep()', opts) + vim.keymap.set('n', 'fb', 'lua require(\'telescope.builtin\').buffers()', opts) + vim.keymap.set('n', 'fh', 'lua require(\'telescope.builtin\').help_tags()', opts) + vim.keymap.set('n', 'cc', 'Telescope colorscheme') end }, { diff --git a/lua/plugins/wk.lua b/lua/plugins/wk.lua new file mode 100644 index 0000000..8408b7e --- /dev/null +++ b/lua/plugins/wk.lua @@ -0,0 +1,60 @@ +-- maps.lua + +local maps = {} + +function maps.term() + vim.api.nvim_set_keymap('n', 't', 'sj :terminal:res 5i', + { noremap = false, desc = "Open and move to terminal" }) + vim.api.nvim_set_keymap('t', 'ß', '', { noremap = true, desc = "Enter normal mode in terminal" }) +end + +function maps.fixIndent() + vim.api.nvim_set_keymap('v', '<', '', '>gv', { noremap = true, desc = "Add identation level" }) +end + +function maps.moveByRow() + -- When text is wrapped, move by terminal rows, not lines, unles a count is provided + vim.api.nvim_set_keymap('n', 'j', 'v:count == 0 ? \'gj\' : \'j\'', + { silent = true, expr = true, desc = "Go down 1 line" }) + vim.api.nvim_set_keymap('n', 'k', 'v:count == 0 ? \'gk\' : \'k\'', + { silent = true, expr = true, desc = "Go up 1 line" }) +end + +function maps.centeredSearch() + vim.api.nvim_set_keymap('n', 'n', 'nzzzv', { noremap = true, desc = "Search next" }) + vim.api.nvim_set_keymap('n', 'N', 'Nzzzv', { noremap = true, desc = "Search previous" }) +end + +function maps.init() + vim.g.mapleader = " " + + vim.api.nvim_set_keymap('n', 'b', 'Lexplore | vert res 30', + { noremap = true, desc = "Open file explorer" }) + + maps.term() + maps.fixIndent() + maps.moveByRow() + maps.centeredSearch() +end + +return { + "folke/which-key.nvim", + event = "VeryLazy", + keys = { + { + "?", + function() + require("which-key").show({ global = false }) + end, + desc = "Buffer Local Keymaps (which-key)", + }, + }, + config = function() + require("which-key").setup({ + preset = "helix" + }) + local wk = require("which-key") + maps.init() + end +}