go-language-server / protocol

Package protocol implements Language Server Protocol specification in Go
https://pkg.go.dev/go.lsp.dev/protocol
BSD 3-Clause "New" or "Revised" License
97 stars 15 forks source link

Can't access client from server methods #37

Open dan1994 opened 2 years ago

dan1994 commented 2 years ago

I've just started fiddling around with this package, and this is also my first venture into LSPs. If I understand correctly, in order to send diagnostics about a file in response to a message (e.g. textDocument/didOpen), I need to access the client created by protocol.NewServer. I see that the client is embedded into the context, but the key used to embed it is private and there appears to be no method to extract it from the context (as opposed to the logger which has LoggerFromContext in context.go). Am I missing anything?

a-h commented 2 months ago

I think you're right, just an oversight.

Adding a function to context.go is all that's required to make it usable from the server.

// ClientFromContext extracts Client from context.
func ClientFromContext(ctx context.Context) Client {
    client, ok := ctx.Value(ctxClient).(Client)
    if !ok {
        return nil
    }
    return client
}