2024-06-26 14:05:25 +07:00
|
|
|
return {
|
|
|
|
"stevearc/conform.nvim",
|
|
|
|
enabled = pcode.disable_null_ls or false,
|
|
|
|
event = { "BufReadPre", "BufNewFile" },
|
2024-06-26 19:34:33 +07:00
|
|
|
opts = function(_, opts)
|
2024-06-26 14:05:25 +07:00
|
|
|
local mason_reg = require("mason-registry")
|
|
|
|
|
2024-06-26 19:34:33 +07:00
|
|
|
opts.formatters = opts.formatters or {}
|
|
|
|
opts.formatters_by_ft = opts.formatters_by_ft or {}
|
2024-06-26 14:05:25 +07:00
|
|
|
|
|
|
|
-- add diff langue vs filetype
|
|
|
|
local keymap = {
|
|
|
|
["c++"] = "cpp",
|
|
|
|
["c#"] = "cs",
|
2024-06-26 19:34:33 +07:00
|
|
|
["jsx"] = "javascriptreact",
|
2024-06-26 14:05:25 +07:00
|
|
|
}
|
|
|
|
|
2024-06-26 20:01:10 +07:00
|
|
|
-- add diff conform vs mason
|
2024-06-26 14:05:25 +07:00
|
|
|
local name_map = {
|
|
|
|
["cmakelang"] = "cmake_format",
|
|
|
|
["deno"] = "deno_fmt",
|
|
|
|
["elm-format"] = "elm_format",
|
|
|
|
["gdtoolkit"] = "gdformat",
|
|
|
|
["nixpkgs-fmt"] = "nixpkgs_fmt",
|
|
|
|
["opa"] = "opa_fmt",
|
|
|
|
["php-cs-fixer"] = "php_cs_fixer",
|
|
|
|
["ruff"] = "ruff_format",
|
|
|
|
["sql-formatter"] = "sql_formatter",
|
|
|
|
["xmlformatter"] = "xmlformat",
|
|
|
|
}
|
|
|
|
|
2024-06-26 19:34:33 +07:00
|
|
|
-- add new mapping filetype
|
|
|
|
local addnew = {
|
|
|
|
["jsonc"] = "prettier",
|
|
|
|
["json"] = "prettier",
|
|
|
|
["typescriptreact"] = "prettier",
|
|
|
|
}
|
|
|
|
|
|
|
|
-- add ignore filetype
|
2024-06-26 17:15:35 +07:00
|
|
|
local ignore = {
|
|
|
|
["php"] = "tlint",
|
|
|
|
}
|
|
|
|
|
2024-06-26 14:05:25 +07:00
|
|
|
for _, pkg in pairs(mason_reg.get_installed_packages()) do
|
|
|
|
for _, type in pairs(pkg.spec.categories) do
|
|
|
|
-- only act upon a formatter
|
|
|
|
if type == "Formatter" then
|
|
|
|
-- if formatter doesn't have a builtin config, create our own from a generic template
|
|
|
|
if not require("conform").get_formatter_config(pkg.spec.name) then
|
|
|
|
-- the key of the entry to this table
|
|
|
|
-- is the name of the bare executable
|
|
|
|
-- the actual value may not be the absolute path
|
|
|
|
-- in some cases
|
|
|
|
local bin = next(pkg.spec.bin)
|
|
|
|
-- this should be replaced by a function
|
|
|
|
-- that quieries the configured mason install path
|
|
|
|
local prefix = vim.fn.stdpath("data") .. "/mason/bin/"
|
|
|
|
|
2024-06-26 19:34:33 +07:00
|
|
|
opts.formatters[pkg.spec.name] = {
|
2024-06-26 14:05:25 +07:00
|
|
|
command = prefix .. bin,
|
|
|
|
args = { "$FILENAME" },
|
|
|
|
stdin = true,
|
|
|
|
require_cwd = false,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-06-26 19:34:33 +07:00
|
|
|
-- local listtest = {}
|
2024-06-26 14:05:25 +07:00
|
|
|
-- finally add the formatter to it's compatible filetype(s)
|
|
|
|
for _, ft in pairs(pkg.spec.languages) do
|
|
|
|
local ftl = string.lower(ft)
|
2024-06-26 19:34:33 +07:00
|
|
|
-- register only ready installed package
|
2024-06-26 14:05:25 +07:00
|
|
|
local ready = mason_reg.get_package(pkg.spec.name):is_installed()
|
|
|
|
if ready then
|
|
|
|
if keymap[ftl] ~= nil then
|
|
|
|
ftl = keymap[ftl]
|
|
|
|
end
|
|
|
|
if name_map[pkg.spec.name] ~= nil then
|
|
|
|
pkg.spec.name = name_map[pkg.spec.name]
|
|
|
|
end
|
2024-06-26 19:34:33 +07:00
|
|
|
|
|
|
|
-- if substring(pkg.spec.name, "prettier") then
|
|
|
|
-- table.insert(listtest, ftl)
|
|
|
|
-- end
|
|
|
|
|
|
|
|
-- add new mapping language
|
|
|
|
for key, value in pairs(addnew) do
|
|
|
|
if value == pkg.spec.name then
|
|
|
|
opts.formatters_by_ft[key] = opts.formatters_by_ft[key] or {}
|
|
|
|
table.insert(opts.formatters_by_ft[key], pkg.spec.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-26 17:15:35 +07:00
|
|
|
if ignore[ftl] ~= pkg.spec.name then
|
2024-06-26 19:34:33 +07:00
|
|
|
opts.formatters_by_ft[ftl] = opts.formatters_by_ft[ftl] or {}
|
|
|
|
table.insert(opts.formatters_by_ft[ftl], pkg.spec.name)
|
2024-06-26 17:15:35 +07:00
|
|
|
end
|
2024-06-26 14:05:25 +07:00
|
|
|
end
|
|
|
|
end
|
2024-06-26 19:34:33 +07:00
|
|
|
-- print(table.concat(listtest, ","))
|
2024-06-26 14:05:25 +07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-26 17:54:14 +07:00
|
|
|
local onsave = pcode.format_on_save or false
|
|
|
|
if onsave then
|
|
|
|
return {
|
|
|
|
format_on_save = {
|
|
|
|
lsp_fallback = true,
|
|
|
|
timeout_ms = pcode.format_timeout_ms or 5000,
|
|
|
|
},
|
2024-06-26 19:34:33 +07:00
|
|
|
formatters = opts.formatters,
|
|
|
|
formatters_by_ft = opts.formatters_by_ft,
|
2024-06-26 17:54:14 +07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return {
|
2024-06-26 19:34:33 +07:00
|
|
|
formatters = opts.formatters,
|
|
|
|
formatters_by_ft = opts.formatters_by_ft,
|
2024-06-26 17:54:14 +07:00
|
|
|
}
|
|
|
|
end
|
2024-06-26 14:05:25 +07:00
|
|
|
end,
|
|
|
|
config = function(_, opts)
|
|
|
|
local conform = require("conform")
|
|
|
|
conform.setup(opts)
|
|
|
|
vim.keymap.set({ "n", "v" }, "<leader>lF", function()
|
|
|
|
conform.format({
|
|
|
|
lsp_fallback = true,
|
|
|
|
async = false,
|
|
|
|
timeout_ms = pcode.format_timeout_ms or 5000,
|
|
|
|
})
|
|
|
|
end, { desc = "Format file or range (in visual mode)" })
|
|
|
|
end,
|
|
|
|
}
|