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

What is the purpose of publish_diagnostic_on? #315

Open
magnusriga opened this issue Nov 25, 2024 · 4 comments
Open

What is the purpose of publish_diagnostic_on? #315

magnusriga opened this issue Nov 25, 2024 · 4 comments

Comments

@magnusriga
Copy link

Thank you for a fantastic tool.

Question:

In the Language Server Protocol, it is mandatory for LS clients to support on textDocument/didOpen and textDocument/didChange.

I was therefore under the impression that the client always has to let the server know whenever any text changes, by calling the textDocument/didChange method.

Additionally, according to the spec the server is responsible for pushing diagnositcs to the client.

If that assumption is right, and the client always lets the server know about changes, and the server needs to push diagnostics back, then what is the purpose of the setting publish_diagnostic_on?

By default, it is set to publish_diagnostic_on = "insert_leave", instead of publish_diagnostic_on = "change".

@KostkaBrukowa
Copy link
Collaborator

As far as I know this publish_diagnostic_on is setting publishing diagnostics by typescript-tools not tsserver.
In this implementation there is communication tsserver -> typescript-tools -> nvim and yes, tsserver pushes the diagnostics back whenever it wants, but it's typescript-tools job to get those diagnostics and show it inside the nvim, therefore setting publish_diagnostic_on controls when the typescript-tools will show the diagnostics inside the editor.
Like here:

proto_utils.publish_diagnostics(dispatchers, vim.uri_from_bufnr(e.buf), {})
typescript tools based on this flag it changes the timing when it pushes diagnostics

@magnusriga
Copy link
Author

magnusriga commented Nov 25, 2024

@KostkaBrukowa Interesting, thank you, I shall admit I do not have deep enough knowledge on this topic to properly understand what is going on. For instance, I was trying to figure out where in my client (Neovim), vim.lsp.start is called, but could not find it.

Anyways, the odd thing here is that, even though publish_diagnostics is set to insert_leave, my editor is actually immediately showing the TS error diagnostics when I make changes to the code that introduces a TS error. It is not necessary to open/close the file. This should probably be better documented in the readme, so people with less knowhow on this, like myself, do not assume that change is needed to make diagnostics show when changing text.

Do you see the same thing?

EDIT:

Actually, I see from the code you linked that insert_leave does not in fact mean that diagnostics are only pushed on open/close of file:

  if plugin_config.publish_diagnostic_on == publish_diagnostic_mode.insert_leave then
    common.create_lsp_attach_augcmd(function()
      request_diagnostics_debounced()

      api.nvim_create_autocmd("InsertEnter", {
        pattern = common.extensions_pattern,
        callback = function(e)
          proto_utils.publish_diagnostics(dispatchers, vim.uri_from_bufnr(e.buf), {})
        end,
        group = augroup,
      })

      api.nvim_create_autocmd({ "BufEnter", "InsertLeave", "TextChanged" }, {
        pattern = common.extensions_pattern,
        callback = request_diagnostics_debounced,
        group = augroup,
      })
    end, augroup)
  end
end

It seems to use the InsertEnter autocommand event, which fires "Just before starting Insert mode." (from :h InsertEnter). It also uses BufEnter, InsertLeave, TextChanged.

Any idea what the two different autocommands do? Meaning, what is the difference between request_diagnostics_debounced and ... proto_utils.publish_diagnostics(dispatchers, vim.uri_from_bufnr(e.buf), {}) ...?

@KostkaBrukowa
Copy link
Collaborator

KostkaBrukowa commented Nov 26, 2024

proto_utils.publish_diagnostics(dispatchers, vim.uri_from_bufnr(e.buf), {}) actually removes all diagnostics (see last parameter {}) request_diagnostics_debounced requests and pushes diagnostics to nvim instance.
So in this case when we enter insert mode all of the diagnostics dissapear and when we leave insert mode (or enter new file etc.) we show diagnostics again. As in the video below

Screen.Recording.2024-11-26.at.09.52.05.mov

@magnusriga
Copy link
Author

That is extremely helpful, thank you so much. I will leave the setting alone (I initially thought I had to set it to "change" to ensure it picked up changes as I changed text).

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

No branches or pull requests

2 participants