dots/.config/nvim/lua/utils.lua

72 lines
3.2 KiB
Lua

local M = {}
--- Sets a non-recursive keymap with optional description.
---@param mode string|table Mode(s) for the keymap.
---@param lhs string Left-hand side of the keymap.
---@param rhs string|function Right-hand side of the keymap.
---@param desc string|nil Description for the keymap.
---@param bufopts table|nil Additional options for the keymap.
M.mapkey = function(mode, lhs, rhs, desc, bufopts)
bufopts = bufopts or {}
if desc then
bufopts.desc = desc
end
vim.keymap.set(mode, lhs, rhs, bufopts)
end
--- Sets a non-recursive keymap for normal mode.
---@param lhs string Left-hand side of the keymap.
---@param rhs string|function Right-hand side of the keymap.
---@param desc string|nil Description for the keymap.
---@param bufopts table|nil Additional options for the keymap.
function M.nmapkey(lhs, rhs, desc, bufopts)
M.mapkey("n", lhs, rhs, desc, bufopts)
end
--- Sets a non-recursive keymap for visual mode.
---@param lhs string Left-hand side of the keymap.
---@param rhs string|function Right-hand side of the keymap.
---@param desc string|nil Description for the keymap.
---@param bufopts table|nil Additional options for the keymap.
function M.vmapkey(lhs, rhs, desc, bufopts)
M.mapkey("v", lhs, rhs, desc, bufopts)
end
--- Sets a non-recursive keymap for normal and visual mode.
---@param lhs string Left-hand side of the keymap.
---@param rhs string|function Right-hand side of the keymap.
---@param desc string|nil Description for the keymap.
---@param bufopts table|nil Additional options for the keymap.
function M.nvmapkey(lhs, rhs, desc, bufopts)
M.mapkey({ "n", "v" }, lhs, rhs, desc, bufopts)
end
function M.common_mappings(opts)
local telescope = require("telescope.builtin")
M.nmapkey("gr", telescope.lsp_references, "[g]et [r]eferences", opts)
M.nmapkey("gd", telescope.lsp_definitions, "[g]et [d]definitions", opts)
M.nmapkey("gi", telescope.lsp_implementations, "[g]et [i]mplementations", opts)
M.nmapkey("gt", telescope.lsp_type_definitions, "[g]et [t]ype definitions", opts)
M.nmapkey("gD", vim.lsp.buf.declaration, "[g]o to [D]eclaration", opts)
M.nmapkey("K", vim.lsp.buf.hover, "Show do[K]umentation for what is under cursor", opts)
M.nmapkey("<leader>hh", vim.lsp.buf.signature_help, "Signature [hh]elp", opts)
M.nmapkey("<leader>wa", vim.lsp.buf.add_workspace_folder, "[w]orkspace [a]dd folder", opts)
M.nmapkey("<leader>wr", vim.lsp.buf.remove_workspace_folder, "[w]orkspace [r]emove folder", opts)
M.nmapkey("<leader>wl", vim.lsp.buf.list_workspace_folders, "[w]orkspace [l]ist folder", opts)
M.nvmapkey("<leader>ca", vim.lsp.buf.code_action, "See available [c]ode [a]ctions", opts)
M.nmapkey("<leader>rn", vim.lsp.buf.rename, "Smart [r]e[n]ame", opts)
M.nmapkey("<leader>d", vim.diagnostic.open_float, "Show line [d]iagnostics", opts)
M.nmapkey("<leader>D", telescope.diagnostics, "Show buffer [D]iagnostics", opts)
M.nmapkey("[d", function()
vim.diagnostic.jump({ count = -1, float = true })
end, "Go to previous diagnostic", opts)
M.nmapkey("]d", function()
vim.diagnostic.jump({ count = 1, float = true })
end, "Go to next diagnostic", opts)
M.nmapkey("<leader>q", vim.diagnostic.setloclist, "Add buffer diagnostics to location list", opts)
M.nmapkey("<leader>rs", "<cmd>LspRestart<cr>", "Restart LSP", opts)
end
return M