Initial Commit - Add nvim config
Signed-off-by: Daniel Henry <iamdanhenry@gmail.com>
This commit is contained in:
46
.config/nvim/lua/c/config.lua
Normal file
46
.config/nvim/lua/c/config.lua
Normal file
@@ -0,0 +1,46 @@
|
||||
-- C language pack: editor behavior for C/C++
|
||||
-- We keep this file "pure config" (no keymaps) to match your structure.
|
||||
|
||||
-- Use `make` for builds in C/C++ buffers.
|
||||
-- Quickfix will parse clang/gcc errors by default, so :make populates the list.
|
||||
vim.api.nvim_create_autocmd("FileType", {
|
||||
pattern = { "c", "cpp" },
|
||||
callback = function(args)
|
||||
-- Buffer-local so other languages can pick their own build tools later.
|
||||
vim.bo[args.buf].makeprg = "make -j"
|
||||
end,
|
||||
})
|
||||
|
||||
-- Friendly heads-up if compile_commands.json isn't present.
|
||||
-- clangd *can* work without it, but accuracy (includes/defines/flags) is much better with it.
|
||||
do
|
||||
local warned = false
|
||||
vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, {
|
||||
pattern = { "*.c", "*.h", "*.cpp", "*.hpp" },
|
||||
callback = function()
|
||||
if warned then return end
|
||||
local root = vim.fs.root(0, { "compile_commands.json", ".git", "Makefile", "makefile" })
|
||||
-- Look specifically for compile_commands.json upward from the file
|
||||
local found = vim.fs.find("compile_commands.json", { upward = true, path = vim.fs.dirname(vim.api.nvim_buf_get_name(0)) })[1]
|
||||
if not found and root then
|
||||
warned = true
|
||||
vim.schedule(function()
|
||||
vim.notify(
|
||||
"clangd: compile_commands.json not found.\n" ..
|
||||
"Run: bear -- make -j\n" ..
|
||||
"Tip: re-run after changing flags or adding files.",
|
||||
vim.log.levels.WARN,
|
||||
{ title = "C/clangd" }
|
||||
)
|
||||
end)
|
||||
end
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
-- Treat *.h as C (applies globally; easiest approach for a C-centric workflow)
|
||||
vim.filetype.add({
|
||||
extension = {
|
||||
h = "c",
|
||||
},
|
||||
})
|
||||
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,
|
||||
})
|
||||
|
||||
69
.config/nvim/lua/c/plugins.lua
Normal file
69
.config/nvim/lua/c/plugins.lua
Normal file
@@ -0,0 +1,69 @@
|
||||
-- C language pack: plugins that only load for C/C++
|
||||
-- - clangd (LSP) via nvim-lspconfig
|
||||
-- - clang-format on save via conform.nvim
|
||||
return {
|
||||
|
||||
-- LSP framework (just the client; the *server* is clangd you installed with Homebrew)
|
||||
{
|
||||
"neovim/nvim-lspconfig",
|
||||
ft = { "c", "cpp" }, -- load only when editing C/C++
|
||||
config = function()
|
||||
local lsp = require("lspconfig")
|
||||
|
||||
-- Minimal, sensible clangd setup.
|
||||
-- Tip: clangd finds your project root via compile_commands.json, compile_flags.txt, or .git
|
||||
lsp.clangd.setup({
|
||||
-- You can pass extra flags here later (e.g., to enable clang-tidy)
|
||||
-- cmd = { "clangd", "--header-insertion=never" },
|
||||
-- capabilities = ... (we'll keep defaults until you add completion later)
|
||||
})
|
||||
-- That's it. Keymaps are added in c/keybindings.lua on LspAttach.
|
||||
end,
|
||||
},
|
||||
|
||||
-- Formatting with clang-format (runs on save for C/C++)
|
||||
-- If you later move Conform to core, keep this block here: it will just *extend* its opts.
|
||||
{
|
||||
"stevearc/conform.nvim",
|
||||
ft = { "c", "cpp" },
|
||||
opts = function(_, opts)
|
||||
opts.formatters_by_ft = opts.formatters_by_ft or {}
|
||||
opts.formatters_by_ft.c = { "clang-format" }
|
||||
opts.formatters_by_ft.cpp = { "clang-format" }
|
||||
return opts
|
||||
end,
|
||||
config = function(_, opts)
|
||||
local conform = require("conform")
|
||||
conform.setup(opts)
|
||||
|
||||
-- Format C/C++ buffers on save. If clang-format isn't found,
|
||||
-- Conform will fall back to LSP formatting (safe).
|
||||
vim.api.nvim_create_autocmd("BufWritePre", {
|
||||
pattern = { "*.c", "*.h", "*.cpp", "*.hpp" },
|
||||
callback = function(args)
|
||||
conform.format({ bufnr = args.buf, lsp_fallback = true })
|
||||
end,
|
||||
})
|
||||
end,
|
||||
},
|
||||
{
|
||||
"neovim/nvim-lspconfig",
|
||||
ft = { "c", "cpp" },
|
||||
config = function()
|
||||
local lsp = require("lspconfig")
|
||||
|
||||
-- Advertise cmp’s capabilities to clangd (safe even if cmp isn’t loaded yet)
|
||||
local capabilities = vim.lsp.protocol.make_client_capabilities()
|
||||
local ok, cmp_lsp = pcall(require, "cmp_nvim_lsp")
|
||||
if ok then
|
||||
capabilities = cmp_lsp.default_capabilities(capabilities)
|
||||
end
|
||||
|
||||
lsp.clangd.setup({
|
||||
capabilities = capabilities,
|
||||
cmd = { "clangd", "--background-index", "--pch-storage=memory"},
|
||||
-- (keep any flags you had here)
|
||||
})
|
||||
end,
|
||||
},
|
||||
}
|
||||
15
.config/nvim/lua/c/snippets.lua
Normal file
15
.config/nvim/lua/c/snippets.lua
Normal file
@@ -0,0 +1,15 @@
|
||||
local ls = require('luasnip')
|
||||
local s, t, i, f = ls.snippet, ls.text_node, ls.insert_node, ls.function_node
|
||||
|
||||
ls.add_snippets("c", {
|
||||
|
||||
s({ trig = "guard", dscr = "C include guard" }, {
|
||||
|
||||
t("#ifndef "), i(1, "MY_HEADER_H"),
|
||||
t({ "", "#define "}), f(function(args) return args[1][1] end, { 1 }),
|
||||
t({ "", "", "" }),
|
||||
i(0),
|
||||
t({ "", "", "#endif /* " }), f(function(args) return args[1][1] end, {1}), t(" */")
|
||||
})
|
||||
|
||||
})
|
||||
87
.config/nvim/lua/c/utils.lua
Normal file
87
.config/nvim/lua/c/utils.lua
Normal file
@@ -0,0 +1,87 @@
|
||||
-- lua/c/utils.lua
|
||||
local M = {}
|
||||
|
||||
-- Find the project root (same markers clangd likes)
|
||||
local function project_root(buf)
|
||||
return vim.fs.root(buf or 0, { "compile_commands.json", ".git", "Makefile", "makefile" })
|
||||
end
|
||||
|
||||
local function relpath(abs, root)
|
||||
if not abs or not root then return nil end
|
||||
if abs:sub(1, #root) ~= root then return nil end
|
||||
local r = abs:sub(#root + 1)
|
||||
if r:sub(1, 1) == "/" then r = r:sub(2) end
|
||||
return r
|
||||
end
|
||||
|
||||
local function dirpart(p) return p:match("^(.*)/") end
|
||||
|
||||
-- Build the alternate path (header <-> source), preserving nested subdirs
|
||||
local function alternate_rel(rel)
|
||||
local first = rel:match("^([^/]+)/")
|
||||
local ext = rel:match("%.([^.]+)$") or ""
|
||||
local rest = rel:gsub("^" .. (first or "") .. "/", "")
|
||||
|
||||
if first == "include" and ext == "h" then
|
||||
return "src/" .. rest:gsub("%.h$", ".c")
|
||||
elseif first == "src" and ext == "c" then
|
||||
return "include/" .. rest:gsub("%.c$", ".h")
|
||||
end
|
||||
|
||||
-- Fallbacks if file isn’t directly under include/ or src/
|
||||
if ext == "h" then
|
||||
return ("src/" .. rel):gsub("^src/include/", "src/"):gsub("%.h$", ".c")
|
||||
elseif ext == "c" then
|
||||
return ("include/" .. rel):gsub("^include/src/", "include/"):gsub("%.c$", ".h")
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
function M.toggle_header_source()
|
||||
local buf = 0
|
||||
local abs = vim.api.nvim_buf_get_name(buf)
|
||||
if abs == "" then
|
||||
vim.notify("No file name (unsaved buffer).", vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
|
||||
local root = project_root(buf)
|
||||
if not root then
|
||||
vim.notify("Couldn’t locate project root (no .git/Makefile/compile_commands.json).", vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
|
||||
local rel = relpath(abs, root)
|
||||
if not rel then
|
||||
vim.notify("File not under project root: " .. abs, vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
|
||||
local alt_rel = alternate_rel(rel)
|
||||
if not alt_rel then
|
||||
vim.notify("Not a .c/.h under include/ or src/: " .. rel, vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
|
||||
local alt_abs = root .. "/" .. alt_rel
|
||||
if vim.loop.fs_stat(alt_abs) then
|
||||
vim.cmd.edit(vim.fn.fnameescape(alt_abs))
|
||||
return
|
||||
end
|
||||
|
||||
-- Prompt to create
|
||||
local choice = vim.fn.confirm(("Create %s?\n\n%s"):format(alt_rel, alt_abs), "&Yes\n&No", 2)
|
||||
if choice ~= 1 then return end
|
||||
|
||||
-- Ensure dirs exist, then open the new file (unsaved until you write)
|
||||
local dir = dirpart(alt_abs)
|
||||
if dir and not vim.loop.fs_stat(dir) then
|
||||
vim.fn.mkdir(dir, "p")
|
||||
end
|
||||
vim.cmd.edit(vim.fn.fnameescape(alt_abs))
|
||||
-- Optional: write an empty file immediately. Comment out if you prefer unsaved buffer.
|
||||
-- vim.cmd.write()
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user