ray-x / go.nvim

G'day Nvimer, Joyful Gopher: Discover the Feature-Rich Go Plugin for Neovim
MIT License
2.12k stars 123 forks source link

Default gopls settings prevents diagnostics from being refreshed on InsertLeave #515

Open antoineco opened 3 weeks ago

antoineco commented 3 weeks ago

Without go.nvim and using gopls defaults, LSP diagnostics are refreshed on InsertLeave (ref. :h vim.diagnostic.Opts).

When using go.nvim with either lsp_cfg = true or lsp_cfg = { settigns = { gopls = { ... } } }, LSP diagnostics disappear upon entering Insert mode, and are only shown again upon saving the buffer.

antoineco commented 3 weeks ago

I found the culprit.

When update_in_insert is false (the default in both Neovim and go.nvim), diagTrigger defaults to Save:

https://github.com/ray-x/go.nvim/blob/fb612d13c34d3d1d2caa4d5785733abe70dc22f0/lua/go/gopls.lua#L312-L315

This differs from gopls defaults, which have this set to Edit: https://github.com/golang/tools/blob/v0.26.0/gopls/doc/settings.md#diagnosticstrigger-enum

ray-x commented 2 weeks ago

There is a gap between nvim and gopls setup. Edit means update_in_insert=true Save does not mean LeaveInsert/update_in_insert=false, it should map to BufWritePost So If you want to update the diagnostic in normal without save, you will need to set update_in_insert=true, which will be very annoying.

I am open to suggestions.

antoineco commented 2 weeks ago

I agree that update_in_insert is annoying and not something I want to enable.

The reason why I think 'Save' isn't great as a default trigger is that I often want to tackle multiple diagnostics inside a file (e.g. after a refactoring which broke multiple things). In that case, I'll typically jump to the next diagnostic, address it, and return to Normal mode. At that point, all my other diagnostics have now disappeared and I have to save the buffer to make them come back.

The alternative is to send diagnostics to the location list with vim.diagnostic.setloclist() prior to performing the first edit, but this has the disadvantage of not carrying the context of a true LSP diagnostic at the marked location.

I got the behavior I wanted by setting diagnosticsTrigger and diagnosticsDelay to their default values (Edit, 1s) in lsp_cfg.