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

在Neovim中通过lsp-zero启用rust-analyzer嵌入提示失败求助

在Neovim中通过lsp-zero配置rust-analyzer的Inlay Hints问题

问题描述

我尝试在Neovim中借助lsp-zero配置rust-analyzer的Inlay Hints,但嵌入提示并未正常显示。希望保留rustc调试提示的同时,添加变量声明后的类型提示,实现类似RustRover的效果。

当前使用的配置代码:

lsp = require('lsp-zero')

lsp.preset('recommended')

lsp.ensure_installed({
  'rust_analyzer',
})


-- Rust
local on_attach = {
  function(client)
    require'completion'.on_attach(client)
  end
}

lsp.configure('rust_analyzer', {
  on_attach=on_attach,
  settings = {
    ["rust-analyzer"] = {
      imports = {
        granularity = {
          group = "module",
        },
        prefix = "self",
      },
      cargo = {
        buildScripts = {
          enable = true,
        },
      },
      procMacro = {
        enable = true
      },
      add_return_type = {
        enable = true
      },
      inlayHints = {
        enable = true,
        showParameterNames = true,
        parameterHintsPrefix = "<- ",
        otherHintsPrefix = "=> ",
      },
    }
  }
})

lsp.setup()

vim.diagnostic.config({
    virtual_text = true
})

效果说明

  • 当前效果:仅显示rustc的调试提示,无变量类型等Inlay Hints
  • 期望效果:保留rustc调试提示的同时,在变量声明后显示对应类型提示,与RustRover的展示效果一致

解决方案

关键问题分析

  1. on_attach被错误定义为表结构,应为单个函数
  2. rust-analyzer的Inlay Hints配置不完整,缺少变量类型提示的启用项
  3. 部分版本的rust-analyzer需要显式启用客户端的Inlay Hints功能

修改后的完整配置

local lsp = require('lsp-zero')

lsp.preset('recommended')

lsp.ensure_installed({
  'rust_analyzer',
})

-- 修正on_attach为函数,显式启用rust-analyzer的Inlay Hints
local on_attach = function(client, bufnr)
  if client.name == 'rust_analyzer' then
    -- Neovim 0.10+ 内置支持inlay hint,旧版本需用第三方插件(如lvimuser/lsp-inlayhints.nvim)
    vim.lsp.inlay_hint(bufnr, true)
  end
end

lsp.configure('rust_analyzer', {
  on_attach = on_attach,
  settings = {
    ["rust-analyzer"] = {
      imports = {
        granularity = {
          group = "module",
        },
        prefix = "self",
      },
      cargo = {
        buildScripts = {
          enable = true,
        },
      },
      procMacro = {
        enable = true
      },
      inlayHints = {
        enable = true,
        -- 启用变量类型提示
        typeHints = {
          enable = true,
          hideClosureInitialization = false,
          hideNamedConstructor = false,
        },
        showParameterNames = true,
        parameterHintsPrefix = "<- ",
        otherHintsPrefix = "=> ",
        -- 配置生命周期与返回类型提示
        lifetimeElisionHints = {
          enable = "always",
          useParameterNames = true,
        },
        closureReturnTypeHints = {
          enable = "always"
        },
      },
    }
  }
})

lsp.setup()

-- 保留rustc调试提示
vim.diagnostic.config({
    virtual_text = true
})

配置要点解释

  • 修正on_attach:改为标准函数格式,针对rust-analyzer客户端显式启用Inlay Hints(Neovim 0.10及以上版本内置支持,旧版本需安装lvimuser/lsp-inlayhints.nvim并调整对应逻辑)
  • 完善Inlay Hints配置:添加typeHints.enable = true确保变量类型提示开启,同时可按需配置生命周期、返回类型等其他提示项
  • 保留rustc调试提示:维持virtual_text = true,确保rustc的诊断提示正常显示

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 04:30:58