From 5ce78f6a6944a0ca5e1afa4d93828988610e5182 Mon Sep 17 00:00:00 2001 From: "RingOfStorms (Josh)" Date: Thu, 3 Oct 2024 21:55:47 -0500 Subject: [PATCH] make formatting config work again --- lua/plugins/formatter.lua | 53 ++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/lua/plugins/formatter.lua b/lua/plugins/formatter.lua index 6aa02ce..f69344a 100644 --- a/lua/plugins/formatter.lua +++ b/lua/plugins/formatter.lua @@ -15,32 +15,67 @@ local function formatCurrent(retry) end) end +---@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 + +-- Make function that converts { { "prettierd", "prettier" }, "rustywind" } to +--function (bufnr) +-- return { first(bufnr, "prettierd", "prettier"), "rustywind" } +-- end, +local function expandFormatters(formatters) + return function(bufnr) + local result = {} + for i = 1, #formatters do + local formatter = formatters[i] + if type(formatter) == "table" then + result[i] = first(bufnr, unpack(formatter)) + else + result[i] = formatter + end + end + return result + end +end + return { "stevearc/conform.nvim", opts = { -- https://github.com/stevearc/conform.nvim?tab=readme-ov-file#setup notify_on_error = true, formatters = { - v_fmt = { - command = "v", - args = { "fmt" }, - }, + -- v_fmt = { + -- command = "v", + -- args = { "fmt" }, + -- }, }, -- Note that all these need to be available at runtime, add them to flake.nix#runtimeDependencies formatters_by_ft = { lua = { "stylua" }, nix = { "nixfmt" }, - vlang = { "v_fmt" }, - typescript = { { "prettierd", "prettier" }, "rustywind" }, - typescriptreact = { { "prettierd", "prettier" }, "rustywind" }, - javascript = { { "prettierd", "prettier" }, "rustywind" }, - javascriptreact = { { "prettierd", "prettier" }, "rustywind" }, + -- vlang = { "v_fmt" }, + typescript = expandFormatters({ { "prettierd", "prettier" }, "rustywind" }), + typescriptreact = expandFormatters({ { "prettierd", "prettier" }, "rustywind" }), + javascript = expandFormatters({ { "prettierd", "prettier" }, "rustywind" }), + javascriptreact = expandFormatters({ { "prettierd", "prettier" }, "rustywind" }), -- TODO revisit these I'd like to use them but they are not in nixpkgs yet -- https://nixos.org/guides/nix-pills/ -- markdown = { "mdslw", "mdsf"}, markdown = { "markdownlint-cli2" }, -- rust = { "rustfmt" }, + + ["*"] = { lsp_format = "fallback" }, }, }, keys = {