nvim: Simplify formatter loading

Since we use Mason to automatically install any formatter we need, there
is (currently) no need for the more complicated logic of deciding
between multiple formatters (since the one we prefer should always be
available). This also fixes an issue that we can set 'prettier' specific
options in conform.nvim but those do not apply to 'prettierd' the same
way - we now just use prettier and ignore prettierd.
This commit is contained in:
Marty Oehme 2024-08-12 11:29:33 +02:00
parent be842ce622
commit 4be3a42ffa
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A

View file

@ -1,49 +1,26 @@
---@param bufnr integer
---@param ... string
---@return string
local function first(bufnr, ...)
local conform = require("conform")
for i = 1, select("#", ...) do
local formatter = select(i, ...)
if conform.get_formatter_info(formatter, bufnr).available then
return formatter
end
end
return select(1, ...)
end
local formatters = { local formatters = {
angular = { "prettierd", "prettier", stop_after_first = true }, angular = { "prettier" },
astro = { "prettierd", "prettier", stop_after_first = true }, astro = { "prettier" },
bash = { "shfmt" }, bash = { "shfmt" },
bib = { "bibtex-tidy" }, bib = { "bibtex-tidy" },
css = function(bufnr) css = { "prettier", "rustywind" },
return { first(bufnr, "prettierd", "prettier"), "rustywind" } graphql = { "prettier" },
end, html = { "prettier", "rustywind" },
graphql = { "prettierd", "prettier", stop_after_first = true }, javascript = { "prettier" },
html = function(bufnr) javascriptreact = { "prettier" },
return { first(bufnr, "prettierd", "prettier"), "rustywind" }
end,
javascript = { "prettierd", "prettier", stop_after_first = true },
javascriptreact = { "prettierd", "prettier", stop_after_first = true },
json = { "jq" }, json = { "jq" },
liquid = { "prettierd", "prettier", stop_after_first = true }, liquid = { "prettier" },
lua = { "stylua" }, lua = { "stylua" },
markdown = function(bufnr) markdown = { "prettier", "injected" },
return { first(bufnr, "prettierd", "prettier"), "injected" }
end,
python = { "ruff_fix", "ruff_format", "ruff_organize_imports" }, python = { "ruff_fix", "ruff_format", "ruff_organize_imports" },
quarto = function(bufnr) quarto = { "prettier", "injected" },
return { first(bufnr, "prettierd", "prettier"), "injected" }
end,
sh = { "shfmt" }, sh = { "shfmt" },
sql = { "sleek" }, sql = { "sleek" },
svelte = { "prettierd", "prettier", stop_after_first = true }, svelte = { "prettier" },
typescript = { "prettierd", "prettier", stop_after_first = true }, typescript = { "prettier" },
typescriptreact = { "prettierd", "prettier", stop_after_first = true }, typescriptreact = { "prettier" },
vue = function(bufnr) vue = { "prettier", "rustywind" },
return { first(bufnr, "prettierd", "prettier"), "rustywind" } yaml = { "prettier" },
end,
yaml = { "prettierd", "prettier", stop_after_first = true },
zsh = { "shfmt" }, zsh = { "shfmt" },
} }
@ -57,17 +34,22 @@ return {
"stevearc/conform.nvim", "stevearc/conform.nvim",
config = function() config = function()
require("conform").setup({ require("conform").setup({
formatters_by_ft = formatters,
lsp_format = "fallback", lsp_format = "fallback",
format_after_save = function(bufnr) format_after_save = function(bufnr)
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return return
end end
return { lsp_fallback = true } return { lsp_fallback = true }
end, end,
formatters_by_ft = formatters,
}) })
require("conform").formatters.prettier = {
options = {
ext_parsers = {
qmd = "markdown",
},
},
}
vim.api.nvim_create_user_command("FormatDisable", function(args) vim.api.nvim_create_user_command("FormatDisable", function(args)
if args.bang then if args.bang then
vim.g.disable_autoformat = true vim.g.disable_autoformat = true