-- C language pack: keymaps -- We add LSP-powered maps *only when* clangd attaches, so they are buffer-local to C/C++. local function buf_map(buf, mode, lhs, rhs, desc) vim.keymap.set(mode, lhs, rhs, { buffer = buf, silent = true, noremap = true, desc = desc }) end -- Switch between header/source (create if missing) vim.keymap.set("n", "ah", function() require("c.utils").toggle_header_source() end, { desc = "C: alternate header/source" }) -- which-key label (buffer-local) if you like: local ok, wk = pcall(require, "which-key") if ok then wk.add({ { "a", group = "Alternate" } }) end -- When any LSP attaches, check if it's clangd on a C/C++ buffer and map keys. vim.api.nvim_create_autocmd("LspAttach", { callback = function(args) local client = vim.lsp.get_client_by_id(args.data.client_id) local buf = args.buf local ft = vim.bo[buf].filetype if not client or client.name ~= "clangd" then return end if ft ~= "c" and ft ~= "cpp" then return end -- === Navigation & refactoring (clangd) === buf_map(buf, "n", "gd", vim.lsp.buf.definition, "LSP: go to definition") buf_map(buf, "n", "gD", vim.lsp.buf.declaration, "LSP: go to definition") buf_map(buf, "n", "gr", vim.lsp.buf.references, "LSP: references") buf_map(buf, "n", "K", vim.lsp.buf.hover, "LSP: hover docs") buf_map(buf, "n", "rn", vim.lsp.buf.rename, "LSP: rename symbol") buf_map(buf, "n", "ca", vim.lsp.buf.code_action,"LSP: code action") buf_map(buf, "n", "]d", vim.diagnostic.goto_next, "Diag: next") buf_map(buf, "n", "[d", vim.diagnostic.goto_prev, "Diag: prev") -- Format now (uses Conform with clang-format or LSP fallback) buf_map(buf, "n", "cf", function() local ok, conform = pcall(require, "conform") if ok then conform.format({ bufnr = buf, lsp_fallback = true }) end end, "C: format buffer") -- === Build / Quickfix (make) === buf_map(buf, "n", "mm", "make", "Make: build") buf_map(buf, "n", "mb", "make bear", "Make: build") buf_map(buf, "n", "mo", "copen", "Quickfix: open") buf_map(buf, "n", "mn", "cnext", "Quickfix: next") buf_map(buf, "n", "mp", "cprev", "Quickfix: prev") -- which-key: label these groups for *this buffer only* local ok, wk = pcall(require, "which-key") if ok then wk.add({ { "c", group = "C / LSP" }, { "m", group = "Make / Quickfix" }, }, { buffer = buf }) end end, })