Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix highlight errors when lsp crash or stop #864

Merged
merged 4 commits into from
Apr 22, 2024

Conversation

sudo-tee
Copy link
Contributor

It adds a check wether the client is still available before highlighting.

If the client is not there anymore it returns true to unregister the autocommand

This fix the
method textDocument/documentHighlight is not supported by any of the servers registered for the current buffer errors when doing a LspRestart or the server crashes

It adds a check wether the client is still available before
highlighting.

If the client is not there anymore it returns `true` to unregister the
autocommand

This fix the
`method textDocument/documentHighlight is not supported by any of the servers registered for the current buffer`
errors when doing a LspRestart or the server crashes
@feoh
Copy link
Collaborator

feoh commented Apr 20, 2024

Please forgive my ignorance here but will this fix negatively affect people with different LSP configurations etc?

@dam9000
Copy link
Contributor

dam9000 commented Apr 20, 2024

How do you test or reproduce this error?
I tested :LspRestart and :LspStop and :LspStart and have not seen any errors and the behaviour worked as expected. My observed behaviour:

  • on LspRestart the highlights blinked for a moment and came back
  • on LspStop the highlights disappear on cursor move
  • on LspStart the highlights came back

These cases seem to work normally.
But I only tested with lua, perhaps some other LSP behaves differently.
I don't know what happens in case of LSP crash or how to reproduce it, but I suspect in such a case the highlights will not be the only issue? Perhaps if LSP crashes the best is to just restart nvim, since it will not behave as expected?
In any case I think we need to understand more details about this.

@sudo-tee
Copy link
Contributor Author

sudo-tee commented Apr 21, 2024

I have a pretty unstable LSP (tsserver) on a couple projects. That requires me to frequently restart it. When the server crash I receive a load of errors method textDocument/documentHighlight is not supported by any of the servers registered for the current buffer

Theses errors are probably not an issue by themselves because they are silent, but I'm using Noice and nvim-notify so they are pretty noisy.

The issue is that the more I restart the LSP the more errors notifications I get, tsserver sometimes require 3 restart in a row.
They only go away if a use the :e command or close nvim.

This led me to believe that there might be a small memory leak as the more you restart the lsp the more autocmds are created.

This might be an edge case but doing this trick completely resolved my issues and I can restart the LSP and continue working. I thought I'd share through this pull request.

@dam9000
Copy link
Contributor

dam9000 commented Apr 21, 2024

Hmm I wonder if a more proper approach would be to listen to the LspDetach event and then call vim.lsp.buf.clear_references and unregister all Cursor* autocmds?
Does the LspDetach event get triggered when the LSP crashes?
Also, have you submitted a bug report on tsserver crash or perhaps it's already a known issue?

@sudo-tee
Copy link
Contributor Author

It's is a good idea and it seems to work properly by removing the command in the LspDetatch. As per the crash I have to wait until it happens to see if the error is still there, but with this fix LspRestart will clear the commands the error will automatically be cleared as well. I would say it is not really an issue anymore.

I updated the PR to remove the the commands in the LspDetatch event like you suggested.

@dam9000
Copy link
Contributor

dam9000 commented Apr 22, 2024

@sudo-tee It's looking much better now.
I think it can be simplified further by using nvim_clear_autocmds like this:

diff --git a/init.lua b/init.lua
index f587aec..50e56d5 100644
--- a/init.lua
+++ b/init.lua
@@ -544,10 +544,7 @@ require('lazy').setup({
         group = vim.api.nvim_create_augroup('kickstart-lsp-detach', { clear = true }),
         callback = function(event)
           vim.lsp.buf.clear_references()
-          local cmds = vim.api.nvim_get_autocmds { group = 'kickstart-lsp-highlight', buffer = event.buf }
-          for _, cmd in ipairs(cmds) do
-            vim.api.nvim_del_autocmd(cmd.id)
-          end
+          vim.api.nvim_clear_autocmds { group = 'kickstart-lsp-highlight', buffer = event.buf }
         end,
       })

If you agree that this works and is cleaner go ahead and integrate it and I think we have a final solution.

@sudo-tee
Copy link
Contributor Author

@dam9000

I confirm that it works with the vim.api.nvim_clear_autocmds. Thanks a lot

I updated the PR to use this method instead.

Thanks lot for your help on the issue :)

Copy link
Contributor

@dam9000 dam9000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@feoh
Copy link
Collaborator

feoh commented Apr 22, 2024

Thanks for working towards the best solution on this everyone!

@feoh feoh merged commit 81f270a into nvim-lua:master Apr 22, 2024
1 check passed
MeVsTheVoices pushed a commit to MeVsTheVoices/kickstart.nvim that referenced this pull request Apr 23, 2024
* Fix highlight errors when lsp crash or stop

It adds a check wether the client is still available before
highlighting.

If the client is not there anymore it returns `true` to unregister the
autocommand

This fix the
`method textDocument/documentHighlight is not supported by any of the servers registered for the current buffer`
errors when doing a LspRestart or the server crashes

* Delete the highlight autocommands in the LspDetatch event

* Only delete autocmds for the current buffer with the group name

* Simplify clearing the autocommands

---------

Co-authored-by: Francis Belanger <francis.belanger@ubisoft.com>
real-mielofon pushed a commit to real-mielofon/kickstart.nvim that referenced this pull request Apr 26, 2024
* Fix highlight errors when lsp crash or stop

It adds a check wether the client is still available before
highlighting.

If the client is not there anymore it returns `true` to unregister the
autocommand

This fix the
`method textDocument/documentHighlight is not supported by any of the servers registered for the current buffer`
errors when doing a LspRestart or the server crashes

* Delete the highlight autocommands in the LspDetatch event

* Only delete autocmds for the current buffer with the group name

* Simplify clearing the autocommands

---------

Co-authored-by: Francis Belanger <francis.belanger@ubisoft.com>
rodolfolabiapari pushed a commit to rodolfolabiapari/kickstart.nvim that referenced this pull request Apr 27, 2024
* Fix highlight errors when lsp crash or stop

It adds a check wether the client is still available before
highlighting.

If the client is not there anymore it returns `true` to unregister the
autocommand

This fix the
`method textDocument/documentHighlight is not supported by any of the servers registered for the current buffer`
errors when doing a LspRestart or the server crashes

* Delete the highlight autocommands in the LspDetatch event

* Only delete autocmds for the current buffer with the group name

* Simplify clearing the autocommands

---------

Co-authored-by: Francis Belanger <francis.belanger@ubisoft.com>
orhosko pushed a commit to orhosko/kickstart.nvim that referenced this pull request Apr 28, 2024
* Fix highlight errors when lsp crash or stop

It adds a check wether the client is still available before
highlighting.

If the client is not there anymore it returns `true` to unregister the
autocommand

This fix the
`method textDocument/documentHighlight is not supported by any of the servers registered for the current buffer`
errors when doing a LspRestart or the server crashes

* Delete the highlight autocommands in the LspDetatch event

* Only delete autocmds for the current buffer with the group name

* Simplify clearing the autocommands

---------

Co-authored-by: Francis Belanger <francis.belanger@ubisoft.com>
cannahum pushed a commit to cannahum/kickstart.nvim that referenced this pull request Apr 28, 2024
* Fix highlight errors when lsp crash or stop

It adds a check wether the client is still available before
highlighting.

If the client is not there anymore it returns `true` to unregister the
autocommand

This fix the
`method textDocument/documentHighlight is not supported by any of the servers registered for the current buffer`
errors when doing a LspRestart or the server crashes

* Delete the highlight autocommands in the LspDetatch event

* Only delete autocmds for the current buffer with the group name

* Simplify clearing the autocommands

---------

Co-authored-by: Francis Belanger <francis.belanger@ubisoft.com>
William-Wolke pushed a commit to William-Wolke/kickstart.nvim that referenced this pull request May 3, 2024
* Fix highlight errors when lsp crash or stop

It adds a check wether the client is still available before
highlighting.

If the client is not there anymore it returns `true` to unregister the
autocommand

This fix the
`method textDocument/documentHighlight is not supported by any of the servers registered for the current buffer`
errors when doing a LspRestart or the server crashes

* Delete the highlight autocommands in the LspDetatch event

* Only delete autocmds for the current buffer with the group name

* Simplify clearing the autocommands

---------

Co-authored-by: Francis Belanger <francis.belanger@ubisoft.com>
gcaufield added a commit to gcaufield/kickstart.nvim that referenced this pull request May 16, 2024
* upstream/master: (46 commits)
  Automatically set detached state as needed. (nvim-lua#925)
  fix: debug.lua (nvim-lua#918)
  Add diff to treesitter's ensure_installed languages (nvim-lua#908)
  Move LspDetach handler near kickstart-lsp-highlight group (nvim-lua#900)
  README: add clipboard tool dependency (nvim-lua#886)
  Update README.md (nvim-lua#875)
  fix: highlight group clear on each attach (nvim-lua#874)
  Fix highlight errors when lsp crash or stop (nvim-lua#864)
  Fix deprecation notice of inlay hints (nvim-lua#873)
  Add a commented out example of the classic complete keymaps. Fixes nvim-lua#866 (nvim-lua#868)
  Minor improvements of debian install instructions. Fixes nvim-lua#859 (nvim-lua#869)
  Update README.md (nvim-lua#860)
  fix: restore Mason config timing for DAP startup (again) (nvim-lua#865)
  Add gitsigns recommended keymaps as an optional plugin (nvim-lua#858)
  Move plugin examples from README to optional plugin files (nvim-lua#831)
  Enable inlay hints for the supporting servers (nvim-lua#843)
  Fix: nvim-lua#847 - add prefer_git to treesitter config (nvim-lua#856)
  Comment about nerd font selection. Fixes nvim-lua#853 (nvim-lua#854)
  Arch, btw (nvim-lua#852)
  Update README (nvim-lua#832)
  Add a pull request template (nvim-lua#825)
  README: move backup and paths from external deps to install section (nvim-lua#819)
  Don't lazy load conform plugin (nvim-lua#818)
  Add a keymap space-f to format buffer using conform (nvim-lua#817)
  Add instructions to quit :lazy. Fixes nvim-lua#761
  fix nvim-lua#799 (nvim-lua#800)
  README: wrap long lines (nvim-lua#784)
  Update README.md (nvim-lua#781)
  Add nvim-nio as dependency for nvim-dap-ui (nvim-lua#774)
  Some suggestions and capitalised a few words (nvim-lua#771)
  ...

 > **NOTE**
li6in9muyou pushed a commit to li6in9muyou/nvim--dotfiles that referenced this pull request May 17, 2024
* Fix highlight errors when lsp crash or stop

It adds a check wether the client is still available before
highlighting.

If the client is not there anymore it returns `true` to unregister the
autocommand

This fix the
`method textDocument/documentHighlight is not supported by any of the servers registered for the current buffer`
errors when doing a LspRestart or the server crashes

* Delete the highlight autocommands in the LspDetatch event

* Only delete autocmds for the current buffer with the group name

* Simplify clearing the autocommands

---------

Co-authored-by: Francis Belanger <francis.belanger@ubisoft.com>
dmaspataud pushed a commit to dmaspataud/kickstart.nvim that referenced this pull request May 18, 2024
* Fix highlight errors when lsp crash or stop

It adds a check wether the client is still available before
highlighting.

If the client is not there anymore it returns `true` to unregister the
autocommand

This fix the
`method textDocument/documentHighlight is not supported by any of the servers registered for the current buffer`
errors when doing a LspRestart or the server crashes

* Delete the highlight autocommands in the LspDetatch event

* Only delete autocmds for the current buffer with the group name

* Simplify clearing the autocommands

---------

Co-authored-by: Francis Belanger <francis.belanger@ubisoft.com>
EdwinStep pushed a commit to EdwinStep/nvim that referenced this pull request May 20, 2024
* Fix highlight errors when lsp crash or stop

It adds a check wether the client is still available before
highlighting.

If the client is not there anymore it returns `true` to unregister the
autocommand

This fix the
`method textDocument/documentHighlight is not supported by any of the servers registered for the current buffer`
errors when doing a LspRestart or the server crashes

* Delete the highlight autocommands in the LspDetatch event

* Only delete autocmds for the current buffer with the group name

* Simplify clearing the autocommands

---------

Co-authored-by: Francis Belanger <francis.belanger@ubisoft.com>
dvessel pushed a commit to dvessel/kickstart.nvim that referenced this pull request May 21, 2024
* Fix highlight errors when lsp crash or stop

It adds a check wether the client is still available before
highlighting.

If the client is not there anymore it returns `true` to unregister the
autocommand

This fix the
`method textDocument/documentHighlight is not supported by any of the servers registered for the current buffer`
errors when doing a LspRestart or the server crashes

* Delete the highlight autocommands in the LspDetatch event

* Only delete autocmds for the current buffer with the group name

* Simplify clearing the autocommands

---------

Co-authored-by: Francis Belanger <francis.belanger@ubisoft.com>
darren-omori pushed a commit to darren-omori/kickstart.nvim that referenced this pull request May 24, 2024
* Fix highlight errors when lsp crash or stop

It adds a check wether the client is still available before
highlighting.

If the client is not there anymore it returns `true` to unregister the
autocommand

This fix the
`method textDocument/documentHighlight is not supported by any of the servers registered for the current buffer`
errors when doing a LspRestart or the server crashes

* Delete the highlight autocommands in the LspDetatch event

* Only delete autocmds for the current buffer with the group name

* Simplify clearing the autocommands

---------

Co-authored-by: Francis Belanger <francis.belanger@ubisoft.com>
eunmann pushed a commit to eunmann/kickstart.nvim that referenced this pull request Jun 19, 2024
* Fix highlight errors when lsp crash or stop

It adds a check wether the client is still available before
highlighting.

If the client is not there anymore it returns `true` to unregister the
autocommand

This fix the
`method textDocument/documentHighlight is not supported by any of the servers registered for the current buffer`
errors when doing a LspRestart or the server crashes

* Delete the highlight autocommands in the LspDetatch event

* Only delete autocmds for the current buffer with the group name

* Simplify clearing the autocommands

---------

Co-authored-by: Francis Belanger <francis.belanger@ubisoft.com>
johnlonganecker pushed a commit to johnlonganecker/kickstart.nvim that referenced this pull request Jun 25, 2024
* Fix highlight errors when lsp crash or stop

It adds a check wether the client is still available before
highlighting.

If the client is not there anymore it returns `true` to unregister the
autocommand

This fix the
`method textDocument/documentHighlight is not supported by any of the servers registered for the current buffer`
errors when doing a LspRestart or the server crashes

* Delete the highlight autocommands in the LspDetatch event

* Only delete autocmds for the current buffer with the group name

* Simplify clearing the autocommands

---------

Co-authored-by: Francis Belanger <francis.belanger@ubisoft.com>
halyD pushed a commit to halyD/kickstart.nvim that referenced this pull request Jul 8, 2024
* Fix highlight errors when lsp crash or stop

It adds a check wether the client is still available before
highlighting.

If the client is not there anymore it returns `true` to unregister the
autocommand

This fix the
`method textDocument/documentHighlight is not supported by any of the servers registered for the current buffer`
errors when doing a LspRestart or the server crashes

* Delete the highlight autocommands in the LspDetatch event

* Only delete autocmds for the current buffer with the group name

* Simplify clearing the autocommands

---------

Co-authored-by: Francis Belanger <francis.belanger@ubisoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants