Akmal Soliev
Akmal Soliev

Reputation: 742

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

Upvotes: 3

Views: 7949

Answers (3)

holesx_x
holesx_x

Reputation: 601

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

Upvotes: 10

Akmal Soliev
Akmal Soliev

Reputation: 742

After researching further, I came to realisation that this is achievable using the following plugin: https://github.com/simrat39/rust-tools.nvim#configuration and following configuration:

local opts = {
  tools = {
    inlay_hints = {
      -- automatically set inlay hints (type hints)
      -- default: true
      auto = true,

      -- Only show inlay hints for the current line
      only_current_line = false,

      -- whether to show parameter hints with the inlay hints or not
      -- default: true
      show_parameter_hints = true,

      -- prefix for parameter hints
      -- default: "<-"
      parameter_hints_prefix = "<- ",

      -- prefix for all the other hints (type, chaining)
      -- default: "=>"
      other_hints_prefix = "=> ",

      -- whether to align to the length of the longest line in the file
      max_len_align = false,

      -- padding from the left if max_len_align is true
      max_len_align_padding = 1,

      -- whether to align to the extreme right or not
      right_align = false,

      -- padding from the right if right_align is true
      right_align_padding = 7,

      -- The color of the hints
      highlight = "Comment",
    },
  },
  server = {
    -- standalone file support
    -- setting it to false may improve startup time
    standalone = true,
  }, -- rust-analyzer options
}

require('rust-tools').setup(opts)

Upvotes: 3

rcasia
rcasia

Reputation: 263

Those are diagnostics rather than inlayHints.

You need to add diagnostics like this:

require'lspconfig'.rust_analyzer.setup{
  settings = {
    ['rust-analyzer'] = {
      diagnostics = {
        enable = true;
      }
    }
  }
}

Here there are some more settings you can tweak: https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#rust_analyzer

I hope this works!

Upvotes: 2

Related Questions