From 5fe2e90be452a5d7c7e0929831c01960565153d0 Mon Sep 17 00:00:00 2001 From: "Gustavo L de Mello (Guz)" Date: Sat, 7 Dec 2024 13:22:32 -0300 Subject: [PATCH] refactor(formatting): use conform.nvim in all filetypes --- lua/dot013/plugins/formatting.lua | 83 +++++++++++++++---------------- 1 file changed, 40 insertions(+), 43 deletions(-) diff --git a/lua/dot013/plugins/formatting.lua b/lua/dot013/plugins/formatting.lua index 8cf50d4..e45049b 100644 --- a/lua/dot013/plugins/formatting.lua +++ b/lua/dot013/plugins/formatting.lua @@ -18,54 +18,51 @@ local function js_fmt(bufnr) return f end -local formatters_by_ft = { - ["lua"] = { "stylua" }, - ["nix"] = { "alejandra" }, - ["rust"] = { "rustfmt", lsp_format = "fallback" }, - - ["go"] = function(bufnr) - local f = {} - - if require("conform").get_formatter_info("gofumpt", bufnr).available then - table.insert(f, "gofumpt") - else - table.insert(f, "gofmt") - end - - if require("conform").get_formatter_info("golines", bufnr).available then - table.insert(f, "golines") - elseif require("conform").get_formatter_info("goimports", bufnr).available then - table.insert(f, "goimports") - end - - return f - end, - - ["javascript"] = js_fmt, - ["javascriptreact"] = js_fmt, - ["typescript"] = js_fmt, - ["typescriptreact"] = js_fmt, - ["json"] = function(bufnr) - return { table.unpack(js_fmt(bufnr)), "jq" } - end, - - ["*"] = { "codespell" }, - ["_"] = { "trim_whitespace" }, -} - return { + -- Formatters support { "conform.nvim", - ft = (function() - local fts = {} - for k, _ in pairs(formatters_by_ft) do - table.insert(fts, k) - end - return fts - end)(), + event = { "InsertLeave", "TextChanged" }, after = function() require("conform").setup({ - formatters_by_ft = formatters_by_ft, + formatters_by_ft = { + -- Simple formatters + lua = { "stylua" }, + nix = { "alejandra" }, + rust = { "rustfmt", lsp_format = "fallback" }, + + -- Golang's formatters used by priority + go = function(bufnr) + local f = {} + + if require("conform").get_formatter_info("gofumpt", bufnr).available then + table.insert(f, "gofumpt") + else + table.insert(f, "gofmt") + end + + if require("conform").get_formatter_info("golines", bufnr).available then + table.insert(f, "golines") + elseif require("conform").get_formatter_info("goimports", bufnr).available then + table.insert(f, "goimports") + end + + return f + end, + + -- JavaScript's ecosystem + javascript = js_fmt, + javascriptreact = js_fmt, + typescript = js_fmt, + typescriptreact = js_fmt, + json = function(bufnr) + return { table.unpack(js_fmt(bufnr)), "jq" } + end, + + -- Fallback for any filetype + ["*"] = { "codespell" }, + ["_"] = { "trim_whitespace" }, + }, format_on_save = { timeout_ms = 500, lsp_format = "fallback",