50 lines
1.1 KiB
Lua
50 lines
1.1 KiB
Lua
local M = {}
|
|
|
|
M.autoformat = true
|
|
M.ignore_files = {}
|
|
|
|
function M.setup(opts)
|
|
M.autoformat = opts.autoformat or true
|
|
M.ignore_files = opts.ignore_files or {}
|
|
end
|
|
|
|
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").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
|
|
for _, file in pairs(M.ignore_files) do
|
|
print(vim.api.nvim_buf_get_name(0))
|
|
if string.find(vim.api.nvim_buf_get_name(0), file) then
|
|
return
|
|
end
|
|
end
|
|
M.format()
|
|
end
|
|
end,
|
|
})
|
|
end
|
|
end
|
|
|
|
return M
|