Auto install conform formatters and adjusted how formatters are configured

This commit is contained in:
Dreaded_X 2025-05-26 03:30:30 +02:00
parent a077ab060f
commit 624b32792c
Signed by: Dreaded_X
GPG Key ID: 5A0CBFE3C3377FAA
6 changed files with 29 additions and 182 deletions

View File

@ -21,6 +21,7 @@
"lspkind-nvim": { "branch": "master", "commit": "d79a1c3299ad0ef94e255d045bed9fa26025dab6" }, "lspkind-nvim": { "branch": "master", "commit": "d79a1c3299ad0ef94e255d045bed9fa26025dab6" },
"lualine.nvim": { "branch": "master", "commit": "0c6cca9f2c63dadeb9225c45bc92bb95a151d4af" }, "lualine.nvim": { "branch": "master", "commit": "0c6cca9f2c63dadeb9225c45bc92bb95a151d4af" },
"luvit-meta": { "branch": "main", "commit": "1df30b60b1b4aecfebc785aa98943db6c6989716" }, "luvit-meta": { "branch": "main", "commit": "1df30b60b1b4aecfebc785aa98943db6c6989716" },
"mason-conform.nvim": { "branch": "main", "commit": "f3b96fa2217fcb1513301eefbe10ea0e765e33eb" },
"mason-lspconfig.nvim": { "branch": "main", "commit": "d24b3f1612e53f9d54d866b16bedab51813f2bf1" }, "mason-lspconfig.nvim": { "branch": "main", "commit": "d24b3f1612e53f9d54d866b16bedab51813f2bf1" },
"mason-tool-installer.nvim": { "branch": "main", "commit": "75d60a8f928decd8b38897f80849768b7c540a5b" }, "mason-tool-installer.nvim": { "branch": "main", "commit": "75d60a8f928decd8b38897f80849768b7c540a5b" },
"mason.nvim": { "branch": "main", "commit": "8024d64e1330b86044fed4c8494ef3dcd483a67c" }, "mason.nvim": { "branch": "main", "commit": "8024d64e1330b86044fed4c8494ef3dcd483a67c" },

View File

@ -1,5 +1,4 @@
-- https://github.com/stevearc/conform.nvim -- https://github.com/stevearc/conform.nvim
local slow_format_filetypes = {} local slow_format_filetypes = {}
return { return {
"stevearc/conform.nvim", "stevearc/conform.nvim",
@ -20,7 +19,7 @@ return {
}, },
}, },
opts = { opts = {
formatters_by_ft = require("tools").formatters, formatters_by_ft = require("tools.format"),
notify_on_error = false, notify_on_error = false,
format_on_save = function(bufnr) format_on_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

View File

@ -32,4 +32,12 @@ return {
}) })
end, end,
}, },
{
"zapling/mason-conform.nvim",
opts = {},
dependencies = {
{ "mason-org/mason.nvim", opts = {} },
"stevearc/conform.nvim",
},
},
} }

View File

@ -0,0 +1,19 @@
return {
c = { "clang-format" },
cpp = { "clang-format" },
go = { "goimports", "gofmt" },
python = { "ruff_organize_imports", "ruff_format" },
rust = { "rustfmt" },
javascript = { "prettierd" },
javascriptreact = { "prettierd" },
typescript = { "prettierd" },
typescriptreact = { "prettierd" },
css = { "prettierd" },
markdown = { "prettierd" },
yaml = { "prettierd" },
lua = { "stylua" },
json = { "jq" },
toml = { "taplo" },
-- ["*"] = { "injected" },
["_"] = { "trim_whitespace", "trim_newlines" },
}

View File

@ -1,18 +0,0 @@
-- Helper function for assigning formatters to multiple languages at the same time
local M = {}
function M.assign_formatters(input)
local table = {}
for k, v in pairs(input) do
if type(k) == "number" then
for _, lang in pairs(v[1]) do
table[lang] = v[2]
end
else
table[k] = v
end
end
return table
end
return M

View File

@ -1,162 +0,0 @@
-- Based on: https://github.com/zapling/mason-conform.nvim
-- conform formatter to mason package mapping
-- https://mason-registry.dev/registry/list
local conform_to_mason = {
-- alejandra
["asmfmt"] = "asmfmt",
["ast-grep"] = "ast-grep",
-- astyle
-- auto_optional
-- autocorrect
["autoflake"] = "autoflake",
["autopep8"] = "autopep8",
-- awk
-- bean-format
["beautysh"] = "beautysh",
["bibtex-tidy"] = "bibtex-tidy",
-- biome-check
["biome"] = "biome",
["black"] = "black",
["blade-formatter"] = "blade-formatter",
["blue"] = "blue",
["buf"] = "buf",
["buildifier"] = "buildifier",
["cbfmt"] = "cbfmt",
["clang-format"] = "clang-format",
-- cljstyle
["cmake_format"] = "cmakelang",
["codespell"] = "codespell",
["csharpier"] = "csharpier",
-- cue_fmt
["darker"] = "darker",
-- dart_format
["deno_fmt"] = "deno",
-- dfmt
["djlint"] = "djlint",
["dprint"] = "dprint",
["easy-coding-standard"] = "easy-coding-standard",
["elm_format"] = "elm-format",
-- erb_format
["eslint_d"] = "eslint_d",
["fantomas"] = "fantomas",
-- fish_indent
["fixjson"] = "fixjson",
-- fnlfmt
["fourmolu"] = "fourmolu",
["gci"] = "gci",
["gdformat"] = "gdtoolkit",
["gersemi"] = "gersemi",
-- gn
-- gofmt
["gofumpt"] = "gofumpt",
["goimports-reviser"] = "goimports-reviser",
["goimports"] = "goimports",
["golines"] = "golines",
["google-java-format"] = "google-java-format",
["htmlbeautifier"] = "htmlbeautifier",
-- indent
-- init
-- injected
["isort"] = "isort",
["joker"] = "joker",
["jq"] = "jq",
["jsonnetfmt"] = "jsonnetfmt",
-- just
["ktlint"] = "ktlint",
["latexindent"] = "latexindent",
["markdown-toc"] = "markdown-toc",
["markdownlint-cli2"] = "markdownlint-cli2",
["markdownlint"] = "markdownlint",
["mdformat"] = "mdformat",
["mdslw"] = "mdslw",
-- mix
-- nixfmt
["nixpkgs_fmt"] = "nixpkgs-fmt",
["ocamlformat"] = "ocamlformat",
-- opa_fmt
-- packer_fmt
-- pangu
-- perlimports
-- perltidy
-- pg_format
["php_cs_fixer"] = "php-cs-fixer",
["phpcbf"] = "phpcbf",
-- phpinsights
["pint"] = "pint",
["prettier"] = "prettier",
["prettierd"] = "prettierd",
["pretty-php"] = "pretty-php",
-- puppet-lint
["reorder-python-imports"] = "reorder-python-imports",
-- rescript-format
["rubocop"] = "rubocop",
["rubyfmt"] = "rubyfmt",
-- ruff
["ruff_fix"] = "ruff",
["ruff_format"] = "ruff",
["rufo"] = "rufo",
-- NOTE: Should install through rustup instead
-- ["rustfmt"] = "rustfmt",
["rustywind"] = "rustywind",
-- scalafmt
["shellcheck"] = "shellcheck",
["shellharden"] = "shellharden",
["shfmt"] = "shfmt",
["sql_formatter"] = "sql-formatter",
["sqlfluff"] = "sqlfluff",
["sqlfmt"] = "sqlfmt",
-- squeeze_blanks
["standardjs"] = "standardjs",
["standardrb"] = "standardrb",
["stylelint"] = "stylelint",
-- styler
["stylua"] = "stylua",
-- swift_format
-- swiftformat
["taplo"] = "taplo",
["templ"] = "templ",
-- terraform_fmt
-- terragrunt_hclfmt
["tlint"] = "tlint",
-- trim_newlines
-- trim_whitespace
-- twig-cs-fixer
["typos"] = "typos",
-- typstfmt
-- uncrustify
["usort"] = "usort",
["xmlformat"] = "xmlformatter",
-- xmllint
["yamlfix"] = "yamlfix",
["yamlfmt"] = "yamlfmt",
["yapf"] = "yapf",
["yq"] = "yq",
-- zigfmt
["zprint"] = "zprint",
}
local M = {}
function M.process_formatters(formatters_by_ft)
local formatters_to_install = {}
for _, formatters in pairs(formatters_by_ft) do
for _, formatter in pairs(formatters) do
if type(formatter) == "table" then
for _, f in pairs(formatter) do
local package = conform_to_mason[f]
if package ~= nil then
formatters_to_install[package] = 1
end
end
end
local package = conform_to_mason[formatter]
if package ~= nil then
formatters_to_install[package] = 1
end
end
end
return vim.tbl_keys(formatters_to_install)
end
return M