Files
dotfiles/private_dot_config/nvim/lua/plugins/conform.lua
2026-03-08 09:01:19 +00:00

63 lines
1.6 KiB
Lua

return {
"stevearc/conform.nvim",
event = { "BufWritePre" },
cmd = { "ConformInfo" },
dependencies = {
"williamboman/mason.nvim",
},
config = function()
require("conform").setup({
formatters_by_ft = {
lua = { "stylua" },
cpp = { "clang-format" },
c = { "clang-format" },
go = { "goimports", "gofmt" },
json = { "prettier" },
},
format_on_save = {
timeout_ms = 500,
lsp_fallback = true,
},
formatters = {
["clang-format"] = {
prepend_args = function(self, ctx)
local local_config =
vim.fs.find({ ".clang-format", "_clang-format" }, { path = ctx.dirname, upward = true })[1]
if not local_config then
return { "--style=file:" .. vim.fn.expand("~/.clang-format") }
end
return {}
end,
},
stylua = {
prepend_args = function(self, ctx)
local local_config =
vim.fs.find({ "stylua.toml", ".stylua.toml" }, { path = ctx.dirname, upward = true })[1]
if not local_config then
return { "--config-path", vim.fn.expand("~/.config/stylua/stylua.toml") }
end
return {}
end,
},
prettier = {
prepend_args = function(self, ctx)
local local_config =
vim.fs.find({ ".prettierrc", ".prettierrc.json" }, { path = ctx.dirname, upward = true })[1]
if not local_config then
return { "--config", vim.fn.expand("~/.prettierrc") }
end
return {}
end,
},
},
})
vim.keymap.set({ "n", "v" }, "<leader>gf", function()
require("conform").format({
lsp_fallback = true,
timeout_ms = 500,
})
end, { silent = true })
end,
}