config/.config/nvim/lua/lazyvim/plugins/lsp/format.lua

38 lines
923 B
Lua

local M = {}
M.autoformat = true
function M.toggle()
M.autoformat = not M.autoformat
vim.notify(M.autoformat and "Enabled format on save" or "Disabled format on save")
end
function M.format()
-- TODO: work out how to call conform if it has a formatter and default
-- to vim.lsp.buf.format()
vim.lsp.buf.format()
if require("lazy.core.config").plugins["conform.nvim"]._.loaded then
require("conform.nvim").format()
else
vim.lsp.buf.format()
end
end
function M.on_attach(client, buf)
if client.supports_method("textDocument/formatting") then
vim.api.nvim_create_autocmd("BufWritePre", {
group = vim.api.nvim_create_augroup("LspFormat." .. buf, {}),
buffer = buf,
callback = function()
if M.autoformat then
M.format()
end
end,
})
end
end
return M