mirror of
https://github.com/cap153/nvim.git
synced 2026-05-11 17:35:59 +08:00
在输入快捷键格式化时触发检测是否安装格式化工具
This commit is contained in:
parent
f3d14dd626
commit
30f47f2f87
1 changed files with 25 additions and 36 deletions
|
|
@ -3,42 +3,31 @@ return {
|
||||||
dependencies = {
|
dependencies = {
|
||||||
"williamboman/mason.nvim",
|
"williamboman/mason.nvim",
|
||||||
},
|
},
|
||||||
event = { "BufWritePre" },
|
|
||||||
-- Customize or remove this keymap to your liking
|
|
||||||
keys = {
|
|
||||||
{
|
|
||||||
"<leader>f",
|
|
||||||
function()
|
|
||||||
require("conform").format({ async = true })
|
|
||||||
end,
|
|
||||||
mode = "",
|
|
||||||
desc = "格式化代码",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
opts = {
|
opts = {
|
||||||
formatters_by_ft = {
|
formatters_by_ft = {
|
||||||
lua = { "stylua" },
|
lua = { "stylua" },
|
||||||
python = { "isort", "black" },
|
python = { "isort", "black" },
|
||||||
rust = { "rustfmt", lsp_format = "fallback" },
|
rust = { "rust-analyzer", lsp_format = "fallback" },
|
||||||
|
toml = { "templ" },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
config = function(_, opts)
|
config = function(_, opts)
|
||||||
-- 初始化 mason.nvim
|
-- 初始化 mason.nvim 和 conform.nvim
|
||||||
require("mason").setup()
|
require("mason").setup()
|
||||||
|
require("conform").setup(opts)
|
||||||
|
|
||||||
-- 辅助函数:从 formatters_by_ft 中提取所有工具名称(去重)
|
-- 辅助函数:从指定文件类型的配置中提取所有工具名称(去重)
|
||||||
local function get_ensure_installed(ft_table)
|
local function get_ensure_installed_for_ft(ft, ft_table)
|
||||||
local tools = {}
|
local tools = {}
|
||||||
for _, cfg in pairs(ft_table) do
|
local cfg = ft_table[ft]
|
||||||
if type(cfg) == "table" then
|
if type(cfg) == "table" then
|
||||||
for _, item in ipairs(cfg) do
|
for _, item in ipairs(cfg) do
|
||||||
if type(item) == "string" then
|
if type(item) == "string" then
|
||||||
tools[item] = true
|
tools[item] = true
|
||||||
end
|
|
||||||
end
|
end
|
||||||
elseif type(cfg) == "string" then
|
|
||||||
tools[cfg] = true
|
|
||||||
end
|
end
|
||||||
|
elseif type(cfg) == "string" then
|
||||||
|
tools[cfg] = true
|
||||||
end
|
end
|
||||||
local list = {}
|
local list = {}
|
||||||
for tool, _ in pairs(tools) do
|
for tool, _ in pairs(tools) do
|
||||||
|
|
@ -47,18 +36,18 @@ return {
|
||||||
return list
|
return list
|
||||||
end
|
end
|
||||||
|
|
||||||
local ensure_installed = get_ensure_installed(opts.formatters_by_ft)
|
-- 设置 <leader>f 键映射,在按下时自动检测并安装缺失的工具后格式化代码
|
||||||
|
vim.keymap.set({ "n", "v" }, "<leader>f", function()
|
||||||
-- 利用 mason 的注册中心自动安装缺失的工具
|
local ft = vim.bo.filetype
|
||||||
local registry = require("mason-registry")
|
local tools = get_ensure_installed_for_ft(ft, opts.formatters_by_ft)
|
||||||
for _, tool in ipairs(ensure_installed) do
|
local registry = require("mason-registry")
|
||||||
if not registry.is_installed(tool) then
|
for _, tool in ipairs(tools) do
|
||||||
vim.notify("Installing formatter: " .. tool, vim.log.levels.INFO)
|
if not registry.is_installed(tool) then
|
||||||
registry.get_package(tool):install()
|
vim.notify("Installing formatter: " .. tool, vim.log.levels.INFO)
|
||||||
|
registry.get_package(tool):install()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
require("conform").format({ async = true, lsp_fallback = true })
|
||||||
|
end, { desc = "格式化代码(检测安装缺失工具)" })
|
||||||
-- 最后调用 conform.nvim 的 setup,使用传入的 opts
|
|
||||||
require("conform").setup(opts)
|
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue