Use mason-tool-installer to install packages for conform

This commit is contained in:
Dreaded_X 2025-05-26 15:45:54 +02:00
parent e795252784
commit 39d01c52ad
Signed by: Dreaded_X
GPG Key ID: 5A0CBFE3C3377FAA
3 changed files with 82 additions and 34 deletions

View File

@ -2,9 +2,6 @@
local slow_format_filetypes = {}
return {
"stevearc/conform.nvim",
dependencies = {
"williamboman/mason.nvim",
},
event = { "BufWritePre" },
cmd = { "ConformInfo" },
keys = {

View File

@ -4,27 +4,85 @@ return {
dependencies = {
{ "mason-org/mason.nvim", opts = {} },
"mason-org/mason-lspconfig.nvim",
"zapling/mason-conform.nvim",
},
config = function()
local lsp = require("tools.lsp")
local ensure_installed = vim.tbl_values(vim.tbl_map(function(tool)
-- Convert lsp entries to consistent format
local tools = {}
for _, tool in pairs(lsp) do
if type(tool) == "table" then
if not tool.system then
return {
tool[1],
condition = tool.condition,
}
-- Strip out lsp config
tool[2] = nil
tools[tool[1]] = tool
else
tools[tool] = { tool }
end
end
-- Convert formatters to same format and merge on top of the lsps
-- TODO: Clean up this function
local formatters_by_ft = require("conform").formatters_by_ft
local mapping = require("mason-conform.mapping")
for _, formatter in pairs(formatters_by_ft) do
local entry = {}
if type(formatter) == "table" then
for _, tool in ipairs(formatter) do
local sub_entry = {}
local name = nil
if type(tool) == "table" then
tool[1] = mapping.conform_to_package[tool[1]]
name = tool[1]
sub_entry = tool
else
name = mapping.conform_to_package[tool]
name = name
sub_entry = { name }
end
if name ~= nil then
if entry[name] ~= nil then
entry[name] = vim.tbl_extend("keep", entry[name], sub_entry)
else
entry[name] = sub_entry
end
end
end
else
return tool
local name = mapping.conform_to_package[formatter]
if name ~= nil then
if entry[name] == nil then
entry[name] = { name }
end
end
end
return nil
end, lsp))
for key, value in pairs(entry) do
if tools[key] ~= nil then
tools[key] = vim.tbl_extend("keep", tools[key], value)
else
tools[key] = value
end
end
end
local ensure_installed = vim.iter(tools)
:map(function(_, tool)
if type(tool) == "table" then
if tool.system then
return nil
end
return { tool[1], condition = tool.condition }
end
return tool
end)
:totable()
require("mason-tool-installer").setup({
ensure_installed = ensure_installed,
auto_update = true,
debounde_hours = 24,
})
end,
},
@ -36,12 +94,4 @@ return {
"neovim/nvim-lspconfig",
},
},
{
"zapling/mason-conform.nvim",
opts = {},
dependencies = {
{ "mason-org/mason.nvim", opts = {} },
"stevearc/conform.nvim",
},
},
}

View File

@ -1,19 +1,20 @@
return {
c = { "clang-format" },
cpp = { "clang-format" },
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" },
rust = { { "rustfmt", system = true } },
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" },
["_"] = "trim_whitespace",
"trim_newlines",
}