Files
dotfiles/config/nvim/lua/plugins/conform.lua
2025-07-18 17:10:38 +03:00

77 lines
2.0 KiB
Lua

return {
{
'stevearc/conform.nvim',
event = { 'BufReadPre', 'BufNewFile' },
config = function()
local conform = require 'conform'
conform.setup {
formatters_by_ft = {
bash = { 'shfmt' },
go = { 'golangci-lint' },
lua = { 'stylua' },
sh = { 'shfmt' },
ansible = { 'ansible-lint' },
docker = { 'hadolint' },
},
format_on_save = function(bufnr)
-- Disable autoformat for files in a certain paths
local bufname = vim.api.nvim_buf_get_name(bufnr)
if bufname:match '/dist|node_modules|vendor/' then return end
local disable_lsp = {
c = true,
cpp = true,
}
return {
lsp_fallback = not disable_lsp[vim.bo[bufnr].filetype],
async = false,
timeout_ms = 500,
}
end,
notify_on_error = true,
}
vim.o.formatexpr = "v:lua.require'conform'.formatexpr()"
-- Autoformat toggle keybinding
local autoformat = true
vim.g.autoformat_enabled = autoformat
vim.api.nvim_create_user_command('ToggleFormat', function()
autoformat = not autoformat
vim.g.autoformat_enabled = autoformat
vim.notify('Autoformat on save: ' .. (autoformat and 'enabled' or 'disabled'))
end, {})
vim.keymap.set(
'n',
'<leader>tf',
':ToggleFormat<CR>',
{ desc = 'Toggle autoformat on save' }
)
vim.api.nvim_create_autocmd('BufWritePre', {
callback = function(args)
if autoformat then
conform.format {
bufnr = args.buf,
async = true,
lsp_format = 'fallback',
}
end
end,
})
-- Global statusline helper function
function _G.autoformat_status()
return vim.g.autoformat_enabled and '[ fmt:on]' or '[ fmt:off]'
end
end,
},
{
'gpanders/editorconfig.nvim',
lazy = false,
},
}