rust 在Neovim中使用lsp-zero启用inlayHints

elcex8rz  于 2023-10-20  发布在  其他
关注(0)|答案(2)|浏览(133)

我曾尝试用rust-analyzer设置inlayHints,但是,它不显示inlayHints。我想保留调试提示,但也可能在变量声明后添加类型提示,例如在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调试提示。目前我所看到的:

我想看的是:

4smxwvx5

4smxwvx51#

这些是诊断而不是inlayHints。
你需要像这样添加diagnostics

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

这里有一些你可以调整的设置:https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#rust_analyzer
我希望这个工作!

hgqdbh6s

hgqdbh6s2#

经过进一步研究,我意识到这是可以使用以下插件实现的:https://github.com/simrat39/rust-tools.nvim#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)

相关问题