dotfiles/nvim/dot-config/nvim/lua/plugins/conform.lua

84 lines
2.2 KiB
Lua

-- https://github.com/stevearc/conform.nvim
local slow_format_filetypes = {}
return {
"stevearc/conform.nvim",
event = { "BufWritePre" },
cmd = { "ConformInfo" },
keys = {
{
-- Customize or remove this keymap to your liking
"<leader>f",
function()
require("conform").format({ async = true, lsp_fallback = true })
end,
mode = "",
desc = "[F]ormat buffer",
},
},
opts = {
formatters_by_ft = (function()
local formatters = require("tools.format")
local formatters_by_ft = {}
for lang, formatter in pairs(formatters) do
formatters_by_ft[lang] = {}
if type(formatter) == "table" then
for _, tool in ipairs(formatter) do
if type(tool) == "table" then
table.insert(formatters_by_ft[lang], tool[1])
else
table.insert(formatters_by_ft[lang], tool)
end
end
end
end
return formatters_by_ft
end)(),
notify_on_error = false,
format_on_save = function(bufnr)
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
if slow_format_filetypes[vim.bo[bufnr].filetype] then
return
end
local function on_format(err)
if err and err:match("timeout$") then
slow_format_filetypes[vim.bo[bufnr].filetype] = true
end
end
return { timeout_ms = 200, lsp_fallback = true }, on_format
end,
format_after_save = function(bufnr)
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
if not slow_format_filetypes[vim.bo[bufnr].filetype] then
return
end
return { lsp_fallback = true }
end,
-- log_level = vim.log.levels.DEBUG,
},
init = function()
vim.api.nvim_create_user_command("FormatDisable", function(args)
if args.bang then
-- FormatDisable! will disable formatting just for this buffer
vim.b.disable_autoformat = true
else
vim.g.disable_autoformat = true
end
end, {
desc = "Disable autoformat-on-save",
bang = true,
})
vim.api.nvim_create_user_command("FormatEnable", function()
vim.b.disable_autoformat = false
vim.g.disable_autoformat = false
end, {
desc = "Re-enable autoformat-on-save",
})
vim.o.formatexpr = "v:lua.require'conform'.formatexpr()"
end,
}