Run treefmt on nvim buffer save

This commit is contained in:
Pim Kunis 2024-10-28 13:02:12 +01:00
parent 0d6ad4b9c1
commit 4cb90679a2

View file

@ -45,14 +45,21 @@ require("lspconfig").terraformls.setup({
capabilities = capabilities,
})
-- require'lspconfig'.efm.setup {
-- on_attach = require("lsp-format").on_attach,
-- init_options = {documentFormatting = true},
-- settings = {
-- languages = {
-- lua = {{formatCommand = "lua-format -i", formatStdin = true}},
-- nix = {{formatCommand = "nixfmt", formatStdin = true}}
-- }
-- },
-- filetypes = {"lua", "nix"}
-- }
local function has_treefmt()
local git_root = vim.fn.systemlist("git rev-parse --show-toplevel")[1]
if vim.v.shell_error ~= 0 then
return false
end
local treefmt_path = git_root .. "/treefmt.nix"
return vim.fn.filereadable(treefmt_path) == 1
end
vim.api.nvim_create_autocmd("BufWritePost", {
pattern = "*",
callback = function()
if has_treefmt() then
vim.cmd("silent !treefmt > /dev/null 2>&1")
end
end,
group = vim.api.nvim_create_augroup("TreefmtAutoformat", { clear = true }),
})