35 lines
643 B
Lua
35 lines
643 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()
|
|
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
|