fix order of operations
This commit is contained in:
+14
-3
@@ -82,9 +82,20 @@ vim.api.nvim_create_autocmd("BufWritePre", {
|
|||||||
local cpp_exts = { cpp = true, cxx = true, hpp = true, ixx = true }
|
local cpp_exts = { cpp = true, cxx = true, hpp = true, ixx = true }
|
||||||
|
|
||||||
if cpp_exts[ext] then
|
if cpp_exts[ext] then
|
||||||
local filepath = vim.fn.expand("%:p")
|
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
|
||||||
vim.fn.system({ "clang-format", "-i", filepath })
|
local input = table.concat(lines, "\n")
|
||||||
vim.cmd("edit!") -- reload the buffer after in-place formatting
|
|
||||||
|
-- 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
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user