From 4be3a42ffa9af1dddcabf784d19c8d2c2c2d3c2e Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Mon, 12 Aug 2024 11:29:33 +0200 Subject: [PATCH] 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. --- nvim/.config/nvim/lua/plugins/formatting.lua | 64 +++++++------------- 1 file changed, 23 insertions(+), 41 deletions(-) diff --git a/nvim/.config/nvim/lua/plugins/formatting.lua b/nvim/.config/nvim/lua/plugins/formatting.lua index 77f610d..7f0d92b 100644 --- a/nvim/.config/nvim/lua/plugins/formatting.lua +++ b/nvim/.config/nvim/lua/plugins/formatting.lua @@ -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 = { - angular = { "prettierd", "prettier", stop_after_first = true }, - astro = { "prettierd", "prettier", stop_after_first = true }, + angular = { "prettier" }, + astro = { "prettier" }, bash = { "shfmt" }, bib = { "bibtex-tidy" }, - css = function(bufnr) - return { first(bufnr, "prettierd", "prettier"), "rustywind" } - end, - graphql = { "prettierd", "prettier", stop_after_first = true }, - html = function(bufnr) - return { first(bufnr, "prettierd", "prettier"), "rustywind" } - end, - javascript = { "prettierd", "prettier", stop_after_first = true }, - javascriptreact = { "prettierd", "prettier", stop_after_first = true }, + css = { "prettier", "rustywind" }, + graphql = { "prettier" }, + html = { "prettier", "rustywind" }, + javascript = { "prettier" }, + javascriptreact = { "prettier" }, json = { "jq" }, - liquid = { "prettierd", "prettier", stop_after_first = true }, + liquid = { "prettier" }, lua = { "stylua" }, - markdown = function(bufnr) - return { first(bufnr, "prettierd", "prettier"), "injected" } - end, + markdown = { "prettier", "injected" }, python = { "ruff_fix", "ruff_format", "ruff_organize_imports" }, - quarto = function(bufnr) - return { first(bufnr, "prettierd", "prettier"), "injected" } - end, + quarto = { "prettier", "injected" }, sh = { "shfmt" }, sql = { "sleek" }, - svelte = { "prettierd", "prettier", stop_after_first = true }, - typescript = { "prettierd", "prettier", stop_after_first = true }, - typescriptreact = { "prettierd", "prettier", stop_after_first = true }, - vue = function(bufnr) - return { first(bufnr, "prettierd", "prettier"), "rustywind" } - end, - yaml = { "prettierd", "prettier", stop_after_first = true }, + svelte = { "prettier" }, + typescript = { "prettier" }, + typescriptreact = { "prettier" }, + vue = { "prettier", "rustywind" }, + yaml = { "prettier" }, zsh = { "shfmt" }, } @@ -57,17 +34,22 @@ return { "stevearc/conform.nvim", config = function() require("conform").setup({ - + formatters_by_ft = formatters, lsp_format = "fallback", - format_after_save = function(bufnr) if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then return end return { lsp_fallback = true } end, - formatters_by_ft = formatters, }) + require("conform").formatters.prettier = { + options = { + ext_parsers = { + qmd = "markdown", + }, + }, + } vim.api.nvim_create_user_command("FormatDisable", function(args) if args.bang then vim.g.disable_autoformat = true