tliron / glsp

Language Server Protocol SDK for Go
Apache License 2.0
183 stars 24 forks source link

How do you publish diagnostics with this library? #11

Closed tonyalaribe closed 2 years ago

tonyalaribe commented 2 years ago

Hi, first of all, thanks for the tremendous amount of work you put into this library.

I'm implementing an LSP for a doctest library which I wrote (https://github.com/apitoolkit/doctests), and the way it should work is such that a user can add golang expressions into their comments, and they can execute those expressions like an inline repl. But the documentation of LSPs has been tough so far. Including on the official lsp spec website. So maybe you can help me better.

I've been able to implement this functionality in 2 ways already, but each way doesn't completely give me what I would like or expect.

  1. Via codelens image

I implement the TextDocumentCodeLens which triggers code commands. But the problem is that in the WorkspaceExecuteCommand handler, the return type is just an interface, so I'm completely clueless about how to update the document i'm in like with code actions. So I end up editing the file on disk in the execute command function, but then the user has to reload the file from disk to see the changes i made, or risk making more changes and overwritting the files on disk.

Is there a type/data that i can return from executeCommand, to make it edit the current text document instead of by passing the editor to directly edit the file on disk?

  1. I've also been able to implement the functionality via code actions, and with code actions I can return the protocol.Edit values, where i define a range in the document and it's new text content. This works very smoothly with no issues. The problem is that, unlike with code lenses, I don't get any visual cue telling me that the comment line can be evaluated or refreshed. So I figured maybe we need to manually publish the diagnostics, but I have been unable to figure out how to trigger publishing diagnostics with this library as well. Could you help? Or just point me to resources?
tliron commented 2 years ago

The documentation simply says that you can return a result and an error, the latter being "code and message", which is a little too thin. I interpret this to imply that different implementations might handle the "error" differently...

tonyalaribe commented 2 years ago

But if you had to implement such a functionality, how would you do it?

The ability to perform an action a line of code and edit the current document to include the result of the action into the page.

On Sun, Jun 26, 2022, 00:06 Tal Liron @.***> wrote:

The documentation https://microsoft.github.io/language-server-protocol/specifications/specification-3-16/#workspace_executeCommand simply says that you can return a result and an error, the latter being "code and message", which is a little too thin. I interpret this to imply that different implementations might handle the "error" differently...

— Reply to this email directly, view it on GitHub https://github.com/tliron/glsp/issues/11#issuecomment-1166369183, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABSCVASKLDSH6T5IW2I4GEDVQ57H5ANCNFSM5Z23QXGA . You are receiving this because you authored the thread.Message ID: @.***>

tliron commented 2 years ago

I have never done so myself. Perhaps other users can chime in with advice?

mickael-menu commented 2 years ago

I implemented diagnostics with glsp, you can take a look here: https://github.com/mickael-menu/zk/blob/68e6b70eaefdf8344065fcec39d5419dc80d6a02/internal/adapter/lsp/server.go#L556

I refresh them when receiving TextDocumentDidOpen and TextDocumentDidChange, with a custom throttle of one second with TextDocumentDidChange, to wait for the user to be idle before refreshing the diagnostics (this is the delay bool parameter with NeedsRefreshDiagnostics).

It looks like this in Neovim:

image

I didn't use the code parameter but the message and severity ones.