diff --git a/nvim/lua/config/lsp.lua b/nvim/lua/config/lsp.lua index 8027f76..2252305 100644 --- a/nvim/lua/config/lsp.lua +++ b/nvim/lua/config/lsp.lua @@ -82,9 +82,20 @@ vim.api.nvim_create_autocmd("BufWritePre", { local cpp_exts = { cpp = true, cxx = true, hpp = true, ixx = true } if cpp_exts[ext] then - local filepath = vim.fn.expand("%:p") - vim.fn.system({ "clang-format", "-i", filepath }) - vim.cmd("edit!") -- reload the buffer after in-place formatting + local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false) + local input = table.concat(lines, "\n") + + -- pass buffer content via stdin, get formatted output back + local result = vim.fn.systemlist( + { "clang-format", "--assume-filename=" .. vim.fn.expand("%:t") }, + input + ) + + if vim.v.shell_error == 0 then + vim.api.nvim_buf_set_lines(0, 0, -1, false, result) + else + vim.notify("clang-format failed: " .. table.concat(result, "\n"), vim.log.levels.WARN) + end return end