4

I am currently using neovim nightly and though I am getting InlayHints for rust it is only parameter types I am not getting lifetime elision hints on trying to change the configuration I saw on the rust_analyzer docs.. I have tried multiple approaches like using setup_handlers of mason-lspconfig. Trying to setup the lsp outside of the mason-lspconfig.

vim.api.nvim_create_autocmd('LspAttach', {
    group = vim.api.nvim_create_augroup('user_lsp_attach', { clear = true }),
    callback = function(event)
        local opts = { buffer = event.buf }

        vim.keymap.set("n", '<C-i>', function() vim.lsp.inlay_hint.enable(0, not vim.lsp.inlay_hint.is_enabled(0)) end,
            opts)
    end,
})


local lsp_capabilities = require('cmp_nvim_lsp').default_capabilities()

require('mason').setup({})
require('mason-lspconfig').setup({
    ensure_installed = { 'tsserver', 'rust_analyzer', 'clangd' },
    handlers = {
        function(server_name)
            require('lspconfig')[server_name].setup({
                capabilities = lsp_capabilities,
            })
        end,
        rust_analyzer = function()
            require('lspconfig').rust_analyzer.setup({
                capabilities = lsp_capabilities,
                settings = {
                    ['rust_analyzer'] = {
                        inlayHints = {
                                lifetimeElisionHints = {
                                    enable = "always",
                                    useParameterNames = true
                                }

                        },
                    }
                }
            })
        end
   }
})


2 Answers 2

1

Try to add vim.lsp.inlay_hint.enable in the on_attach method like here;

local lspconfig = require 'lspconfig'

lspconfig.rust_analyzer.setup({
  filetypes = {"rust"},
  on_attach = function(client, bufnr)
    vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })
  end,
  settings = {
    ['rust_analyzer'] = {
      cargo = {
        allFeatures = true
      },
      checkOnSave = {
        command = "clippy"
      },
    },
  },
})

Upd: source. You need neovim version 0.10 or later for this.

Sign up to request clarification or add additional context in comments.

1 Comment

Also some LSP servesr requires additional config to have inlay hint turned on. Be sure to check the LSP server documentation.
1

It started working fine for me, I disabled/commented Mason rust_analyzer, added new plugin , below is my init.lua (lazy)

  • Focus on

vim.lsp.inlay_hint.enable(true)

{
  'mrcjkb/rustaceanvim',
  version = '^4', -- Recommended
  lazy = false, -- This plugin is already lazy
  config = function()
      vim.g.rustaceanvim = {
      -- Plugin configuration
      tools = {
      },
      -- LSP configuration
      server = {
        on_attach = function(client, bufnr)
          vim.lsp.inlay_hint.enable(true)
        end,
        default_settings = {
          -- rust-analyzer language server configuration
          ['rust-analyzer'] = {
          },
        },
      },
      -- DAP configuration
      dap = {
      },
    }
    end,
  },

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.