47 lines
1.6 KiB
Lua
47 lines
1.6 KiB
Lua
-- 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",
|
|
},
|
|
})
|