|
How do I define a custom feature in my server, and make it so that the request params are deserialized correctly? Currently I have something like the following, but |
Answered by
alcarney
Mar 19, 2024
Replies: 1 comment
|
In the current version of pygls there is the Subclassing from pygls.protocol import LanguageServerProtocol
@attrs.define
def FooParams:
foo_bar: str = attrs.field()
# NOTE: This assumes that `$/custom/feature` is a notification message - rather than a request.
@attrs.define
class FooNotification:
params: FooParams = attrs.field()
method: str = "$/custom/feature"
jsonrpc: str = attrs.field(default="2.0")
class MyProtocol(LanguageServerProtocol):
def get_message_type(self, method: str) -> Optional[Type]:
if method == FooNotification.method:
return FooNotification
return super().get_message_type(method)
# NOTE: You might not need this method.
# This only applies if `$/custom/feature` is a request and that you want
# to be able to `return MyCustomResult()` from the handler.
def get_result_type(self, method: str) -> Optional[Type]:
return super().get_result_type(method)To enable it, you need to then pass it as an argument to the server = LanguageServer("my-server", "v1", protocol_cls=MyProtocol)See also this guide if you need to extend pygls' default converter object to help it (de)serialize your types Hope that helps! :) |
0 replies
Answer selected by
frabert
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the current version of pygls there is the
LanguageServerProtocolclass, which among other things, provides the type definition that corresponds to a given message. (See here)Subclassing
LanguageServerProtocolallows you to add support for your own messages