Disable diagnostics by default

The LSP makes me feel extremely under siege, especially since seemingly every LSP server insists on throwing hundreds of totally useless style warnings.

I would really like the diagnostics to be toggle-able, defaulting to OFF, but for the LSP server to start running in the background when I start neovim whether I use the diagnostics or not.

The current API doesn’t seem to provide particularly great ways of doing this… I can probably hobble something together with the documented functions, but it won’t be pretty… and in my early experiments with this I have utterly failed to get neovim to start with diagnostics off.

Any suggestions?

Thanks!

1 Like

autocmd BufEnter * lua vim.lsp.diagnostic.disable() will probably do what you want. You can map a key to lua vim.lsp.diagnostic.enable()

Note, there is no turning diagnostics on or off, only turning the display of diagnostics on/off. Most servers send diagnostics regardless of if we report the diagnostic capability. You should also look into configuring your language server to remove style warnings you do not want.

2 Likes

Thanks for your response, that’s really helpful.

For others reading along wondering why vim.lsp.diagnostic.disable() doesn’t work in NVIM 0.5.0, it’s because it was only added 12 days ago:

In case of noisy lsp diagnostics in your vim config you might want to create some ignore patterns.

For instance:

local luadev =
  require("lua-dev").setup(
  {
    lspconfig = {
      on_attach = on_attach,
      cmd = {sumneko_binary, "-E", sumneko_root_path .. "/main.lua"}
    },
    settings = {
      Lua = {
        diagnostics = {
          globals = {"vim", "use"}
        }
      }
    }
  }
)

or even better, make the lsp aware of stuff like “use” as been described here: https://www.reddit.com/r/neovim/comments/kv0tce/sumneko_lua_lsp_says_packers_use_is_an_undefined/

Alternatively, is there a way to change the color of the inline diagnostics? Right now they use the same color as un-highlighted code.

Yep, see the lspconfig wiki under ui customization

The more modern (non-deprecated) way of doing this seems to be lua vim.diagnostic.hide(). If I do that manually, it works great. However, if I do that with the vimrc setting above:

autocmd BufEnter * lua vim.diagnostic.hide()

it does not work! Do I need to use a different trigger-event now? Did I do something wrong?

vim.diagnostic.disable() will disable diagnostics globally. Likewise, enable() enables them globally. Pass a buffer number as the first argument to either function to disable/enable diagnostics only for the given buffer (0 means “current buffer”).

All of this (and more) is mentioned in the docs. :h vim.diagnostic

Edit: Never mind, I read :h deprecated wrong. You can disregard my message below. Thanks a lot again, everything works great now.

Thanks, using vim.diagnostic.disable() works perfectly. What set me on the wrong path was that (I think) the :h deprecated help is wrong:

*vim.lsp.diagnostic.clear()*		Use |vim.diagnostic.hide()| instead.
*vim.lsp.diagnostic.disable()*
*vim.lsp.diagnostic.display()*		Use |vim.diagnostic.show()| instead.
*vim.lsp.diagnostic.enable()*

Should I make a PR to fix it?