diff --git a/README.md b/README.md index 903a2c23..b68174b9 100644 --- a/README.md +++ b/README.md @@ -78,8 +78,26 @@ lvim.colorscheme = "spacegray" lvim.builtin.compe.autocomplete = true --- keymappings +-- keymappings [view all the defaults by pressing Lk] lvim.leader = "space" +-- add your own keymapping +lvim.keys.normal_mode[""] = ":w" +-- unmap a default keymapping +-- lvim.keys.normal_mode[""] = "" +-- edit a default keymapping +-- lvim.keys.normal_mode[""] = ":q" + +-- Use which-key to add extra bindings with the leader-key prefix +-- lvim.builtin.which_key.mappings["P"] = { "lua require'telescope'.extensions.project.project{}", "Projects" } +-- lvim.builtin.which_key.mappings["t"] = { +-- name = "+Trouble", +-- r = { "Trouble lsp_references", "References" }, +-- f = { "Trouble lsp_definitions", "Definitions" }, +-- d = { "Trouble lsp_document_diagnostics", "Diagnosticss" }, +-- q = { "Trouble quickfix", "QuickFix" }, +-- l = { "Trouble loclist", "LocationList" }, +-- w = { "Trouble lsp_workspace_diagnostics", "Diagnosticss" }, +-- } -- After changing plugin config exit and reopen LunarVim, Run :PackerInstall :PackerCompile lvim.builtin.dashboard.active = true @@ -123,17 +141,6 @@ lvim.plugins = { -- { "BufWinEnter", "*.lua", "setlocal ts=8 sw=8" }, -- } --- Additional Leader bindings for WhichKey --- lvim.builtin.which_key.mappings["P"] = { "lua require'telescope'.extensions.project.project{}", "Projects" } --- lvim.builtin.which_key.mappings["t"] = { --- name = "+Trouble", --- r = { "Trouble lsp_references", "References" }, --- f = { "Trouble lsp_definitions", "Definitions" }, --- d = { "Trouble lsp_document_diagnostics", "Diagnosticss" }, --- q = { "Trouble quickfix", "QuickFix" }, --- l = { "Trouble loclist", "LocationList" }, --- w = { "Trouble lsp_workspace_diagnostics", "Diagnosticss" }, --- } ``` diff --git a/init.lua b/init.lua index a69fde44..0224a383 100644 --- a/init.lua +++ b/init.lua @@ -42,11 +42,6 @@ end require("settings").load_commands() autocmds.define_augroups(lvim.autocommands) -local keymap = require "utils.keymap" -local default_keymaps = require "keymappings" -keymap.load(default_keymaps.keymaps, default_keymaps.opts) -keymap.load(lvim.keys, default_keymaps.opts) - local plugins = require "plugins" local plugin_loader = require("plugin-loader").init() plugin_loader:load { plugins, lvim.plugins } @@ -73,6 +68,8 @@ if lsp_settings_status_ok then } end +require("keymappings").setup() + -- TODO: these guys need to be in language files -- if lvim.lang.emmet.active then -- require "lsp.emmet-ls" diff --git a/lua/core/bufferline.lua b/lua/core/bufferline.lua index 35831d03..68030c81 100644 --- a/lua/core/bufferline.lua +++ b/lua/core/bufferline.lua @@ -1,16 +1,11 @@ lvim.builtin.bufferline = { keymap = { - values = { - normal_mode = { - [""] = { ":BufferNext" }, - [""] = { ":BufferPrevious" }, - }, - }, - opts = { - normal_mode = { noremap = true, silent = true }, + normal_mode = { + [""] = ":BufferNext", + [""] = ":BufferPrevious", }, }, } -local keymap = require "utils.keymap" -keymap.load(lvim.builtin.bufferline.keymap.values, lvim.builtin.bufferline.keymap.opts) +local keymap = require "keymappings" +keymap.append_to_defaults(lvim.builtin.bufferline.keymap) diff --git a/lua/core/compe.lua b/lua/core/compe.lua index 5f1632f9..2d183683 100644 --- a/lua/core/compe.lua +++ b/lua/core/compe.lua @@ -42,12 +42,12 @@ M.config = function() keymap = { values = { insert_mode = { - [""] = { 'pumvisible() ? "" : ""' }, - [""] = { 'pumvisible() ? "" : ""' }, - [""] = { "compe#complete()" }, - [""] = { "compe#close('')" }, - [""] = { "compe#scroll({ 'delta': +4 })" }, - [""] = { "compe#scroll({ 'delta': -4 })" }, + [""] = 'pumvisible() ? "" : ""', + [""] = 'pumvisible() ? "" : ""', + [""] = "compe#complete()", + [""] = "compe#close('')", + [""] = "compe#scroll({ 'delta': +4 })", + [""] = "compe#scroll({ 'delta': -4 })", }, }, opts = { @@ -105,7 +105,7 @@ M.setup = function() end end - local keymap = require "utils.keymap" + local keymap = require "keymappings" keymap.load(lvim.builtin.compe.keymap.values, lvim.builtin.compe.keymap.opts) end diff --git a/lua/core/which-key.lua b/lua/core/which-key.lua index 17995e87..eab9266a 100644 --- a/lua/core/which-key.lua +++ b/lua/core/which-key.lua @@ -166,6 +166,10 @@ M.config = function() "Workspace Symbols", }, }, + L = { + name = "+LunarVim", + k = { "lua require('keymappings').print()", "View LunarVim's default keymappings" }, + }, s = { name = "Search", diff --git a/lua/default-config.lua b/lua/default-config.lua index e7d0bfa7..7563d36d 100644 --- a/lua/default-config.lua +++ b/lua/default-config.lua @@ -1237,6 +1237,7 @@ lvim.lang = { }, } +require("keymappings").config() require("core.which-key").config() require "core.status_colors" require("core.gitsigns").config() diff --git a/lua/keymappings.lua b/lua/keymappings.lua index 9ef37a39..038ebed4 100644 --- a/lua/keymappings.lua +++ b/lua/keymappings.lua @@ -1,109 +1,157 @@ -local opts = { - insert_mode = { noremap = true, silent = true }, - normal_mode = { noremap = true, silent = true }, - visual_mode = { noremap = true, silent = true }, - visual_block_mode = { noremap = true, silent = true }, - term_mode = { silent = true }, +local M = {} + +local generic_opts_any = { noremap = true, silent = true } + +local mode_adapters = { + insert_mode = "i", + normal_mode = "n", + term_mode = "t", + visual_mode = "v", + visual_block_mode = "x", } -local keymaps = { - insert_mode = { - -- I hate escape - ["jk"] = { "" }, - ["kj"] = { "" }, - ["jj"] = { "" }, - -- Move current line / block with Alt-j/k ala vscode. - [""] = { ":m .+1==gi" }, - [""] = { ":m .-2==gi" }, - -- navigation - [""] = { "k" }, - [""] = { "j" }, - [""] = { "h" }, - [""] = { "l" }, - }, - - normal_mode = { - -- Better window movement - [""] = { "h" }, - [""] = { "j" }, - [""] = { "k" }, - [""] = { "l" }, - - -- Resize with arrows - [""] = { ":resize -2" }, - [""] = { ":resize +2" }, - [""] = { ":vertical resize -2" }, - [""] = { ":vertical resize +2" }, - - -- Tab switch buffer - -- { "", ":bnext" }, - -- { "", ":bprevious" }, - - -- Move current line / block with Alt-j/k a la vscode. - [""] = { ":m .+1==" }, - [""] = { ":m .-2==" }, - - -- QuickFix - ["]q"] = { ":cnext" }, - ["[q"] = { ":cprev" }, - [""] = { ":call QuickFixToggle()" }, - - -- {'', 'compe#complete()', {noremap = true, silent = true, expr = true}}, - - -- LSP - ["gd"] = { "lua vim.lsp.buf.definition()" }, - ["gD"] = { "lua vim.lsp.buf.declaration()" }, - ["gr"] = { "lua vim.lsp.buf.references()" }, - ["gi"] = { "lua vim.lsp.buf.implementation()" }, - ["gl"] = { "lua vim.lsp.diagnostic.show_line_diagnostics({ show_header = false, border = 'single' })" }, - ["gs"] = { "lua vim.lsp.buf.signature_help()" }, - ["gp"] = { "lua require'lsp.peek'.Peek('definition')" }, - ["K"] = { "lua vim.lsp.buf.hover()" }, - [""] = { "lua vim.lsp.diagnostic.goto_prev({popup_opts = {border = lvim.lsp.popup_border}})" }, - [""] = { "lua vim.lsp.diagnostic.goto_next({popup_opts = {border = lvim.lsp.popup_border}})" }, - }, - - term_mode = { - -- Terminal window navigation - [""] = { "h" }, - [""] = { "j" }, - [""] = { "k" }, - [""] = { "l" }, - }, - - visual_mode = { - -- Better indenting - ["<"] = { ""] = { ">gv" }, - - -- { "p", '"0p', { silent = true } }, - -- { "P", '"0P', { silent = true } }, - }, - - visual_block_mode = { - -- Move selected line / block of text in visual mode - ["K"] = { ":move '<-2gv-gv" }, - ["J"] = { ":move '>+1gv-gv" }, - - -- Move current line / block with Alt-j/k ala vscode. - [""] = { ":m '>+1gv-gv" }, - [""] = { ":m '<-2gv-gv" }, - }, -} - -if vim.fn.has "mac" == 1 then - -- TODO: fix this - keymaps.normal_mode[""] = keymaps.normal_mode[""] - keymaps.normal_mode[""] = keymaps.normal_mode[""] - keymaps.normal_mode[""] = keymaps.normal_mode[""] - keymaps.normal_mode[""] = keymaps.normal_mode[""] +-- Append key mappings to lunarvim's defaults for a given mode +-- @param keymaps The table of key mappings containing a list per mode (normal_mode, insert_mode, ..) +function M.append_to_defaults(keymaps) + for mode, mappings in pairs(keymaps) do + for k, v in ipairs(mappings) do + lvim.keys[mode][k] = v + end + end end -vim.g.mapleader = (lvim.leader == "space" and " ") or lvim.leader +-- Load key mappings for a given mode +-- @param mode The keymap mode, can be one of the keys of mode_adapters +-- @param keymaps The list of key mappings +-- @param opts The mapping options +function M.load_mode(mode, keymaps, opts) + mode = mode_adapters[mode] and mode_adapters[mode] or mode + for k, v in pairs(keymaps) do + vim.api.nvim_set_keymap(mode, k, v, opts) + end +end --- navigate tab completion with and --- runs conditionally -vim.cmd 'inoremap pumvisible() ? "\\" : "\\"' -vim.cmd 'inoremap pumvisible() ? "\\" : "\\"' +-- Load key mappings for all provided modes +-- @param keymaps A list of key mappings for each mode +-- @param opts The mapping options for each mode +function M.load(keymaps, opts) + for mode, mapping in pairs(keymaps) do + M.load_mode(mode, mapping, opts[mode]) + end +end -return { keymaps = keymaps, opts = opts } +function M.config() + lvim.keys = { + ---@usage change or add keymappings for insert mode + insert_mode = { + -- 'jk' for quitting insert mode + ["jk"] = "", + -- 'kj' for quitting insert mode + ["kj"] = "", + -- 'jj' for quitting insert mode + ["jj"] = "", + -- Move current line / block with Alt-j/k ala vscode. + [""] = ":m .+1==gi", + -- Move current line / block with Alt-j/k ala vscode. + [""] = ":m .-2==gi", + -- navigation + [""] = "k", + [""] = "j", + [""] = "h", + [""] = "l", + }, + + ---@usage change or add keymappings for normal mode + normal_mode = { + -- Better window movement + [""] = "h", + [""] = "j", + [""] = "k", + [""] = "l", + + -- Resize with arrows + [""] = ":resize -2", + [""] = ":resize +2", + [""] = ":vertical resize -2", + [""] = ":vertical resize +2", + + -- Tab switch buffer + [""] = ":BufferNext", + [""] = ":BufferPrevious", + + -- Move current line / block with Alt-j/k a la vscode. + [""] = ":m .+1==", + [""] = ":m .-2==", + + -- QuickFix + ["]q"] = ":cnext", + ["[q"] = ":cprev", + [""] = ":call QuickFixToggle()", + }, + + ---@usage change or add keymappings for terminal mode + term_mode = { + -- Terminal window navigation + [""] = "h", + [""] = "j", + [""] = "k", + [""] = "l", + }, + + ---@usage change or add keymappings for visual mode + visual_mode = { + -- Better indenting + ["<"] = ""] = ">gv", + + -- ["p"] = '"0p', + -- ["P"] = '"0P', + }, + + ---@usage change or add keymappings for visual block mode + visual_block_mode = { + -- Move selected line / block of text in visual mode + ["K"] = ":move '<-2gv-gv", + ["J"] = ":move '>+1gv-gv", + + -- Move current line / block with Alt-j/k ala vscode. + [""] = ":m '>+1gv-gv", + [""] = ":m '<-2gv-gv", + }, + } + + if vim.fn.has "mac" == 1 then + lvim.keys.normal_mode[""] = lvim.keys.normal_mode[""] + lvim.keys.normal_mode[""] = lvim.keys.normal_mode[""] + lvim.keys.normal_mode[""] = lvim.keys.normal_mode[""] + lvim.keys.normal_mode[""] = lvim.keys.normal_mode[""] + end +end + +function M.print(mode) + print "List of LunarVim's default keymappings (not including which-key)" + if mode then + print(vim.inspect(lvim.keys[mode])) + else + print(vim.inspect(lvim.keys)) + end +end + +function M.setup() + -- navigate tab completion with and + -- runs conditionally + vim.cmd 'inoremap pumvisible() ? "\\" : "\\"' + vim.cmd 'inoremap pumvisible() ? "\\" : "\\"' + local generic_opts = { + insert_mode = generic_opts_any, + normal_mode = generic_opts_any, + visual_mode = generic_opts_any, + visual_block_mode = generic_opts_any, + term_mode = { silent = true }, + } + + vim.g.mapleader = (lvim.leader == "space" and " ") or lvim.leader + M.load(lvim.keys, generic_opts) +end + +return M diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index 3373ac46..b85dfcd2 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -33,6 +33,24 @@ local function lsp_highlight_document(client) end end +local function add_lsp_buffer_keybindings(bufnr) + local wk = require "which-key" + local keys = { + ["K"] = { "lua vim.lsp.buf.hover()", "Show hover" }, + ["gd"] = { "lua vim.lsp.buf.definition()", "Goto Definition" }, + ["gD"] = { "lua vim.lsp.buf.declaration()", "Goto declaration" }, + ["gr"] = { "lua vim.lsp.buf.references()", "Goto references" }, + ["gi"] = { "lua vim.lsp.buf.implementation()", "Goto implementation" }, + ["gs"] = { "lua vim.lsp.buf.signature_help()", "show signature help" }, + ["gp"] = { "lua require'lsp.peek'.Peek('definition')", "Peek definition" }, + ["gl"] = { + "lua vim.lsp.diagnostic.show_line_diagnostics({ show_header = false, border = 'single' })", + "Show line diagnostics", + }, + } + wk.register(keys, { mode = "n", buffer = bufnr }) +end + function M.common_capabilities() local capabilities = vim.lsp.protocol.make_client_capabilities() capabilities.textDocument.completion.completionItem.snippetSupport = true @@ -64,6 +82,7 @@ function M.common_on_attach(client, bufnr) lvim.lsp.on_attach_callback(client, bufnr) end lsp_highlight_document(client) + add_lsp_buffer_keybindings(bufnr) require("lsp.null-ls").setup(vim.bo.filetype) end diff --git a/lua/utils/init.lua b/lua/utils/init.lua index 9f0064e1..9c9b8523 100644 --- a/lua/utils/init.lua +++ b/lua/utils/init.lua @@ -98,6 +98,7 @@ function utils.reload_lv_config() plugin_loader:load { plugins, lvim.plugins } vim.cmd ":PackerCompile" vim.cmd ":PackerInstall" + require("keymappings").setup() -- vim.cmd ":PackerClean" end diff --git a/utils/installer/config.example-no-ts.lua b/utils/installer/config.example-no-ts.lua index c74548dc..4303d264 100644 --- a/utils/installer/config.example-no-ts.lua +++ b/utils/installer/config.example-no-ts.lua @@ -5,17 +5,25 @@ lvim.format_on_save = true lvim.lint_on_save = true lvim.colorscheme = "spacegray" --- keymappings +-- keymappings [view all the defaults by pressing Lk] lvim.leader = "space" --- overwrite/augment the key-mappings provided by LunarVim for any mode, or leave empty to keep the defaults. --- lvim.keys.normal_mode = { --- -- Page down/up --- ["[d"] = { "" }, --- ["]d"] = { "" }, --- --- -- Navigate buffers --- [""] = { ":bnext" }, --- [""] = { ":bprevious" }, +-- add your own keymapping +lvim.keys.normal_mode[""] = ":w" +-- unmap a default keymapping +-- lvim.keys.normal_mode[""] = "" +-- edit a default keymapping +-- lvim.keys.normal_mode[""] = ":q" + +-- Use which-key to add extra bindings with the leader-key prefix +-- lvim.builtin.which_key.mappings["P"] = { "lua require'telescope'.extensions.project.project{}", "Projects" } +-- lvim.builtin.which_key.mappings["t"] = { +-- name = "+Trouble", +-- r = { "Trouble lsp_references", "References" }, +-- f = { "Trouble lsp_definitions", "Definitions" }, +-- d = { "Trouble lsp_document_diagnostics", "Diagnosticss" }, +-- q = { "Trouble quickfix", "QuickFix" }, +-- l = { "Trouble loclist", "LocationList" }, +-- w = { "Trouble lsp_workspace_diagnostics", "Diagnosticss" }, -- } -- TODO: User Config for predefined plugins @@ -69,5 +77,3 @@ lvim.builtin.treesitter.highlight.enabled = true -- lvim.autocommands.custom_groups = { -- { "BufWinEnter", "*.lua", "setlocal ts=8 sw=8" }, -- } - --- Additional Leader bindings for WhichKey diff --git a/utils/installer/config.example.lua b/utils/installer/config.example.lua index a7a55874..90bf6c92 100644 --- a/utils/installer/config.example.lua +++ b/utils/installer/config.example.lua @@ -13,17 +13,26 @@ an executable lvim.format_on_save = true lvim.lint_on_save = true lvim.colorscheme = "spacegray" --- keymappings + +-- keymappings [view all the defaults by pressing Lk] lvim.leader = "space" --- overwrite/augment the key-mappings provided by LunarVim for any mode, or leave empty to keep the defaults. --- lvim.keys.normal_mode = { --- -- Page down/up --- ["[d"] = { "" }, --- ["]d"] = { "" }, --- --- -- Navigate buffers --- [""] = { ":bnext" }, --- [""] = { ":bprevious" }, +-- add your own keymapping +lvim.keys.normal_mode[""] = ":w" +-- unmap a default keymapping +-- lvim.keys.normal_mode[""] = "" +-- edit a default keymapping +-- lvim.keys.normal_mode[""] = ":q" + +-- Use which-key to add extra bindings with the leader-key prefix +-- lvim.builtin.which_key.mappings["P"] = { "lua require'telescope'.extensions.project.project{}", "Projects" } +-- lvim.builtin.which_key.mappings["t"] = { +-- name = "+Trouble", +-- r = { "Trouble lsp_references", "References" }, +-- f = { "Trouble lsp_definitions", "Definitions" }, +-- d = { "Trouble lsp_document_diagnostics", "Diagnosticss" }, +-- q = { "Trouble quickfix", "QuickFix" }, +-- l = { "Trouble loclist", "LocationList" }, +-- w = { "Trouble lsp_workspace_diagnostics", "Diagnosticss" }, -- } -- TODO: User Config for predefined plugins @@ -77,15 +86,3 @@ lvim.builtin.treesitter.highlight.enabled = true -- lvim.autocommands.custom_groups = { -- { "BufWinEnter", "*.lua", "setlocal ts=8 sw=8" }, -- } - --- Additional Leader bindings for WhichKey --- lvim.builtin.which_key.mappings["P"] = { "lua require'telescope'.extensions.project.project{}", "Projects" } --- lvim.builtin.which_key.mappings["t"] = { --- name = "+Trouble", --- r = { "Trouble lsp_references", "References" }, --- f = { "Trouble lsp_definitions", "Definitions" }, --- d = { "Trouble lsp_document_diagnostics", "Diagnosticss" }, --- q = { "Trouble quickfix", "QuickFix" }, --- l = { "Trouble loclist", "LocationList" }, --- w = { "Trouble lsp_workspace_diagnostics", "Diagnosticss" }, --- }