protocol icon indicating copy to clipboard operation
protocol copied to clipboard

Can't access client from server methods

Open dan1994 opened this issue 3 years ago • 1 comments

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?

dan1994 avatar Sep 01 '22 20:09 dan1994

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
}

a-h avatar Jul 04 '24 14:07 a-h