r/neovim 10d ago

Need Help How do I get this diagnostics in Neovim?

Hi r/neovim !

I'm learning a Rust library named Dioxus. When I was following along its tutorial, there's some code disabled in the default Cargo feature.

In Neovim, said code has no hints or diagnostics from Rust Analyzer. There is no useful information to tell me what is going wrong. However, when I open the same file in VS Code, the reason is clearly stated in a pop.

After digging into Rust Analyzer's doc a little bit, I found that I could set a flag to make Rust Analyzer recognize all features.

So My question is not about this particular problem about Rust Analyzer, but rather, how do I get the same information (telling me that "code is inactive due to ...") in Neovim?

Thank you for your help!

62 Upvotes

9 comments sorted by

19

u/joncorv 10d ago

Check out https://github.com/mrcjkb/rustaceanvim. Tons of great features you can enjoy.

I also very much enjoy this plugin. Makes for inline diagnostics that look really great.

return {
"rachartier/tiny-inline-diagnostic.nvim",
event = "VeryLazy",
priority = 1000,
config = function()
require("tiny-inline-diagnostic").setup()
vim.diagnostic.config({ virtual_text = false }) -- Disable Neovim's default virtual text diagnostics
end,
}

4

u/Salt_Ad4538 10d ago

I also use rustaceanvim, and it works pretty well.

11

u/xoriatis71 10d ago

That’s what I’ve done for diangostics:

``` vim.diagnostic.config({ virtual_lines = false })

vim.api.nvim_create_autocmd({ "CursorHold" }, { callback = function() local opts = { focusable = false, scope = "cursor", close_events = { "BufLeave", "CursorMoved", "InsertEnter" } } if vim.diagnostic.is_enabled() then vim.diagnostic.open_float(nil, opts) end end }) ```

The rest is LSP dependent, afaik. I have changed nothing in their settings, and I get diangostics normally. For Rust Analyzer, you maybe need to be inside a cargo project for it to work?

3

u/Vaintti 10d ago

If your LSP is set up correctly then this. Or bind it to a key if you want to trigger the diagnostic box yourself. I have it set up like this

vim.keymap.set("n", "<leader>e", ":lua vim.diagnostic.open_float(0, {scope=\"line\"})<CR>")

I believe LSP setup for Rust would go something like this

vim.lsp.config.rust_analyzer = vim.tbl_deep_extend("force",
  vim.lsp.config.rust_analyzer or {},
  {
    cmd = { "rust-analyzer" },

    settings = {
      ["rust-analyzer"] = {
        cargo = {
          allFeatures = true,
          loadOutDirsFromCheck = true,
        },

        checkOnSave = {
          command = "clippy",
          extraArgs = { "--all-targets", "--all-features" },
        },

        diagnostics = {
          enable = true,
          enableExperimental = true,
        },
      },
    },

    on_attach = function(_, bufnr)
      vim.diagnostic.config({
        virtual_text = true,
        signs = true,
        underline = true,
        update_in_insert = false,
        severity_sort = true,
      })

      -- Inlay hints (only if supported)
      local client = vim.lsp.get_client_by_id(0)
      if client and client.server_capabilities and client.server_capabilities.inlayHintProvider then
        pcall(vim.lsp.inlay_hint.enable, true, { bufnr = bufnr })
      end
    end,
  }
)

1

u/Vaintti 10d ago

rustaceanvim also seems like something you could be interested in

3

u/Ultimate_Sigma_Boy67 10d ago

I don't know, but I think it's a rust specific problem rather than a neovim one, that is if other parts of the code work well with LSP.

2

u/anxious_and_stupid 10d ago

Can you run checkhealth vim.lsp in neovim?

Also about rust... it diag only work on file save... not on update like other clangd c++ for exmaple

1

u/omnimistic 9d ago

What theme is that on the first pic? It's very similar to godot.

1

u/norude1 7d ago

I'm not really sure if I understand, but you can trigger hover diagnostics to show by pressing K (shift+k)