use conform.nvim as universal formatting handler

This commit is contained in:
Bogdan Buduroiu 2024-10-22 18:40:01 +08:00
parent d8ccea5412
commit d3cc1e4ec3
Signed by: bruvduroiu
GPG key ID: A8722B2334DE9499
2 changed files with 10 additions and 33 deletions

View file

@ -1,31 +0,0 @@
local autocmd = vim.api.nvim_create_autocmd
autocmd('LspAttach', {
callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id)
if client.supports_method('textDocument/formatting') then
-- Format the current buffer on save
vim.api.nvim_create_autocmd('BufWritePre', {
buffer = args.buf,
callback = function()
vim.lsp.buf.format({bufnr = args.buf, id = client.id})
end,
})
end
end,
})
autocmd('LspDetach', {
callback = function(args)
-- Get the detaching client
local client = vim.lsp.get_client_by_id(args.data.client_id)
-- Remove the autocommand to format the buffer on save, if it exists
if client.supports_method('textDocument/formatting') then
vim.api.nvim_clear_autocmds({
event = 'BufWritePre',
buffer = args.buf,
})
end
end,
})

View file

@ -3,14 +3,22 @@ local options = {
lua = { "stylua" }, lua = { "stylua" },
css = { "prettier" }, css = { "prettier" },
html = { "prettier" }, html = { "prettier" },
python = { "ruff", "pyright" } python = { "ruff_format" },
go = { "goimports", "gofmt" },
json = { "jq" },
}, },
format_on_save = { format_on_save = {
-- These options will be passed to conform.format() -- These options will be passed to conform.format()
timeout_ms = 500, timeout_ms = 500,
lsp_fallback = true, lsp_format = "fallback",
}, },
-- Set the log level. Use `:ConformInfo` to see the location of the log file.
log_level = vim.log.levels.ERROR,
-- Conform will notify you when a formatter errors
notify_on_error = true,
-- Conform will notify you when no formatters are available for the buffer
notify_no_formatters = true,
} }
return options return options