Initial Commit - Add nvim config
Signed-off-by: Daniel Henry <iamdanhenry@gmail.com>
This commit is contained in:
59
.config/nvim/lua/c/keybindings.lua
Normal file
59
.config/nvim/lua/c/keybindings.lua
Normal file
@@ -0,0 +1,59 @@
|
||||
-- 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", "<leader>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({ { "<leader>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", "<leader>rn", vim.lsp.buf.rename, "LSP: rename symbol")
|
||||
buf_map(buf, "n", "<leader>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", "<leader>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", "<leader>mm", "<cmd>make<CR>", "Make: build")
|
||||
buf_map(buf, "n", "<leader>mb", "<cmd>make bear<CR>", "Make: build")
|
||||
buf_map(buf, "n", "<leader>mo", "<cmd>copen<CR>", "Quickfix: open")
|
||||
buf_map(buf, "n", "<leader>mn", "<cmd>cnext<CR>", "Quickfix: next")
|
||||
buf_map(buf, "n", "<leader>mp", "<cmd>cprev<CR>", "Quickfix: prev")
|
||||
|
||||
-- which-key: label these groups for *this buffer only*
|
||||
local ok, wk = pcall(require, "which-key")
|
||||
if ok then
|
||||
wk.add({
|
||||
{ "<leader>c", group = "C / LSP" },
|
||||
{ "<leader>m", group = "Make / Quickfix" },
|
||||
}, { buffer = buf })
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user