-
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
|
Beta Was this translation helpful? Give feedback.
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! :) |
Beta Was this translation helpful? Give feedback.
In the current version of pygls there is the
LanguageServerProtocol
class, which among other things, provides the type definition that corresponds to a given message. (See here)Subclassing
LanguageServerProtocol
allows you to add support for your own messages