rustneovimrust-analyzer

Enable inlayHints in Neovim with lsp-zero


I have attempted to setup inlayHints with rust-analyzer, however, it doesn't show me the inlayHints. I would like to maintain the debugging hints, but also possibly add type hints after the variable declaration, such as done in RustRover.

config:

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
})

NOTE: I would like to keep rustc debugging hints. Currently what I'm seeing: enter image description here

What I want to see: enter image description here


Solution

  • Neovim v0.10 is now the stable version and has native support for LSP inlay hints.

    Command for enable hints in current buffer: :lua vim.lsp.inlay_hint.enable(true, { 0 })

    Init.lua LSP on_attach function:

    if vim.lsp.inlay_hint then
      vim.lsp.inlay_hint.enable(true, { 0 })
    end
    

    More complex configuration for init.lua

    if vim.lsp.inlay_hint then
      nmap("<leader>L",
      function() if vim.lsp.inlay_hint.is_enabled() then vim.lsp.inlay_hint.enable(false, { bufnr }) else vim.lsp.inlay_hint.enable(true, { bufnr }) end end, "Toggle Inlay Hints")
    end