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]efinitions", 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("hh", vim.lsp.buf.signature_help, "Signature [hh]elp", opts) M.nmapkey("wa", vim.lsp.buf.add_workspace_folder, "[w]orkspace [a]dd folder", opts) M.nmapkey("wr", vim.lsp.buf.remove_workspace_folder, "[w]orkspace [r]emove folder", opts) M.nmapkey("wl", vim.lsp.buf.list_workspace_folders, "[w]orkspace [l]ist folder", opts) M.nvmapkey("ca", vim.lsp.buf.code_action, "See available [c]ode [a]ctions", opts) M.nmapkey("rn", vim.lsp.buf.rename, "Smart [r]e[n]ame", opts) M.nmapkey("d", vim.diagnostic.open_float, "Show line [d]iagnostics", opts) M.nmapkey("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("q", vim.diagnostic.setloclist, "Add buffer diagnostics to location list", opts) M.nmapkey("rs", "LspRestart", "Restart LSP", opts) end local function any_file_exists_at_root(names) local cwd = vim.fn.getcwd() for _, name in ipairs(names) do local path = cwd .. "/" .. name if vim.loop.fs_stat(path) then return true end end return false end function M.cwd_is_biome() return any_file_exists_at_root({ "biome.json", "biome.jsonc" }) end function M.cwd_is_prettier() return any_file_exists_at_root({ ".prettierrc", "prettierrc.json", "prettierrc.jsonc", ".prettierrc.js" }) end function M.cwd_is_eslint() return any_file_exists_at_root({ ".eslintrc", ".eslintrc.js", ".eslintrc.json" }) end function M.buf_is_obsidian(bufnr) local clients = vim.lsp.get_clients({ bufnr = bufnr }) for _, client in ipairs(clients) do if client.name == "obsidian-ls" then return true end end return false end return M