config/.config/nvim/lua/lazyvim/plugins/lsp/format.lua
2023-11-07 21:31:44 +10:30

40 lines
789 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 it it has a formatter and default
-- to vim.lsp.buf.format()
vim.lsp.buf.format()
-- local ok, conform = pcall(require, "conform")
--
-- if ok then
-- conform.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