From 30f47f2f87a4ea37cd0029122204e8d671a82926 Mon Sep 17 00:00:00 2001 From: cap153 <67049883+cap153@users.noreply.github.com> Date: Wed, 12 Feb 2025 14:07:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E8=BE=93=E5=85=A5=E5=BF=AB=E6=8D=B7?= =?UTF-8?q?=E9=94=AE=E6=A0=BC=E5=BC=8F=E5=8C=96=E6=97=B6=E8=A7=A6=E5=8F=91?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E6=98=AF=E5=90=A6=E5=AE=89=E8=A3=85=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=8C=96=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/lazy/plugins/formatter.lua | 61 ++++++++++++++-------------------- 1 file changed, 25 insertions(+), 36 deletions(-) diff --git a/lua/lazy/plugins/formatter.lua b/lua/lazy/plugins/formatter.lua index c48c92c..c885837 100644 --- a/lua/lazy/plugins/formatter.lua +++ b/lua/lazy/plugins/formatter.lua @@ -3,42 +3,31 @@ return { dependencies = { "williamboman/mason.nvim", }, - event = { "BufWritePre" }, - -- Customize or remove this keymap to your liking - keys = { - { - "f", - function() - require("conform").format({ async = true }) - end, - mode = "", - desc = "格式化代码", - }, - }, opts = { formatters_by_ft = { lua = { "stylua" }, python = { "isort", "black" }, - rust = { "rustfmt", lsp_format = "fallback" }, + rust = { "rust-analyzer", lsp_format = "fallback" }, + toml = { "templ" }, }, }, config = function(_, opts) - -- 初始化 mason.nvim + -- 初始化 mason.nvim 和 conform.nvim 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 = {} - for _, cfg in pairs(ft_table) do - if type(cfg) == "table" then - for _, item in ipairs(cfg) do - if type(item) == "string" then - tools[item] = true - end + local cfg = ft_table[ft] + if type(cfg) == "table" then + for _, item in ipairs(cfg) do + if type(item) == "string" then + tools[item] = true end - elseif type(cfg) == "string" then - tools[cfg] = true end + elseif type(cfg) == "string" then + tools[cfg] = true end local list = {} for tool, _ in pairs(tools) do @@ -47,18 +36,18 @@ return { return list end - local ensure_installed = get_ensure_installed(opts.formatters_by_ft) - - -- 利用 mason 的注册中心自动安装缺失的工具 - local registry = require("mason-registry") - for _, tool in ipairs(ensure_installed) do - if not registry.is_installed(tool) then - vim.notify("Installing formatter: " .. tool, vim.log.levels.INFO) - registry.get_package(tool):install() + -- 设置 f 键映射,在按下时自动检测并安装缺失的工具后格式化代码 + vim.keymap.set({ "n", "v" }, "f", function() + local ft = vim.bo.filetype + local tools = get_ensure_installed_for_ft(ft, opts.formatters_by_ft) + local registry = require("mason-registry") + for _, tool in ipairs(tools) do + if not registry.is_installed(tool) then + vim.notify("Installing formatter: " .. tool, vim.log.levels.INFO) + registry.get_package(tool):install() + end end - end - - -- 最后调用 conform.nvim 的 setup,使用传入的 opts - require("conform").setup(opts) + require("conform").format({ async = true, lsp_fallback = true }) + end, { desc = "格式化代码(检测安装缺失工具)" }) end, }