You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何正确配置leptosfmt以适配Lunarvim?

配置Leptosfmt在LunarVim中自动格式化view!宏的问题

我正尝试配置leptosfmt以适配LunarVim,实现保存时格式化view!宏,但尚未找到简洁可行的方案。以下是我的探索与尝试:

尝试通过rust-analyzer集成配置

根据leptosfmt仓库的README及相关PR内容,我尝试在LunarVim的LSP配置中覆盖rust-analyzer的rustfmt命令:

require("lvim.lsp.manager").setup("rust_analyzer", {
  settings = {
    ["rust_analyzer"] = {
      rustfmt = {
        overrideCommand = { "leptosfmt", "--stdin", "--rustfmt" },
      }
    }
  }
})

但该配置无效,view!宏并未如预期在保存时被格式化。

我同时使用Rust Tools插件,也在其配置中加入了相同的rustfmt覆盖设置:

server = {
  on_init = require("lvim.lsp").common_on_init,
  on_attach = function(client, bufnr)
    require("lvim.lsp").common_on_attach(client, bufnr)
    local rt = require("rust-tools")
    -- Hover actions
    vim.keymap.set("n", "<C-space>", rt.hover_actions.hover_actions, { buffer = bufnr })
    -- Code action groups
    vim.keymap.set("n", "<leader>lA", rt.code_action_group.code_action_group, { buffer = bufnr })
  end,
  settings = {
    ["rust_analyzer"] = {
      rustfmt = {
        overrideCommand = { "leptosfmt", "--stdin", "--rustfmt" },
      }
    }
  }
},

同样没有效果,我甚至卸载了rust-tools以排除冲突,问题依然存在。

临时解决方案

我编写了一个自动命令可以实现需求,但这并非合理的处理方式,且可能存在性能问题:

vim.api.nvim_create_autocmd("BufWritePost", {
  pattern = "*.rs",
  callback = function()
    local found = vim.fn.search("view!", "nw")
    if found > 0 then
      local filepath = vim.fn.expand('%:p')
      vim.fn.system('leptosfmt ' .. filepath)
      vim.cmd('edit!')
    end
  end
})

寻求建议

请问还有其他更合适的配置方案吗?


内容的提问来源于stack exchange,提问作者WVAviator

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 12:25:03