Auto install formatter used by conform

This commit is contained in:
flea 2024-02-19 18:48:54 +08:00
parent 3603e9ce35
commit cfe2cb9ac9
1 changed files with 22 additions and 7 deletions

View File

@ -1,20 +1,35 @@
return { return {
'stevearc/conform.nvim', 'stevearc/conform.nvim',
dependencies = {
'williamboman/mason.nvim',
},
event = { 'BufReadPre', 'BufNewFile' }, event = { 'BufReadPre', 'BufNewFile' },
opts = { opts = {
formatters_by_ft = { formatters_by_ft = {
lua = { 'stylua' }, lua = { 'stylua' },
python = function(bufnr) python = { 'ruff_format' },
if require('conform').get_formatter_info('ruff_format', bufnr).available then
return { 'ruff_format' }
else
return { 'isort', 'black' }
end
end,
}, },
format_on_save = { format_on_save = {
timeout_ms = 500, timeout_ms = 500,
lsp_fallback = true, lsp_fallback = true,
}, },
}, },
config = function(_, opts)
local registry = require 'mason-registry'
registry.refresh(function()
for _, tbl in pairs(opts.formatters_by_ft) do
for _, pkg_name in ipairs(tbl) do
-- TODO the name formatters_by_ft needed may be different from the name mason needed
if pkg_name == 'ruff_format' then
pkg_name = 'ruff'
end
local pkg = registry.get_package(pkg_name)
if not pkg:is_installed() then
pkg:install()
end
end
end
end)
require('conform').setup(opts)
end,
} }