Skip to content

Commit

Permalink
Use lsprotocol's converter for (de)serialization
Browse files Browse the repository at this point in the history
This simplifies much of the (de)serialization code by relying on the
converter provided by `lsprotocol`.

We use the `METHOD_TO_TYPES` mapping to determine which type definition
to use for any given message. If a method is not known (as in the case
of custom lsp commands) we fall back to pygls's existing generic RPC
message classes.

The following changes to the base `JsonRPCProtocol` class have also been
made

- server and client futures have been unified into a single
  `_request_futures` dict.
- upon sending a request, the corresponding result type is looked up and
  stored in an internal `_result_types` dict.
- (de)serialization code has been moved to a method on the
  `JsonRPCProtocol` class itself so that it has access to the required
  internal state.
- subclasses (such as the `LanguageServerProtocol` class) are now
  required to implement the `get_message_type` and `get_result_type`
  methods to provide the type definitions corresponding with the given
  RPC method name.
  • Loading branch information
alcarney committed Oct 13, 2022
1 parent 474bb38 commit 04875c5
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 161 deletions.
9 changes: 5 additions & 4 deletions pygls/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ def __hash__(self):
return hash((self.code, self.message))

@staticmethod
def from_dict(error):
def from_error(error):
for exc_class in _EXCEPTIONS:
if exc_class.supports_code(error['code']):
return exc_class(**error)
return JsonRpcException(**error)
if exc_class.supports_code(error.code):
return exc_class(code=error.code, message=error.message, data=error.data)

return JsonRpcException(code=error.code, message=error.message, data=error.data)

@classmethod
def supports_code(cls, code):
Expand Down
26 changes: 0 additions & 26 deletions pygls/lsp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,6 @@
TEXT_DOCUMENT_SEMANTIC_TOKENS_RANGE: Union[SemanticTokensLegend, SemanticTokensRegistrationOptions],
}

@attrs.define
class JsonRPCNotification:
"""A class that represents json rpc notification message."""

jsonrpc: str
method: str
params: Any

@attrs.define
class JsonRPCRequestMessage:
"""A class that represents json rpc request message."""

jsonrpc: str
id: Union[int, str]
method: str
params: Any


@attrs.define
class JsonRPCResponseMessage:
"""A class that represents json rpc response message."""

jsonrpc: str
id: Union[int, str]
result: Union[Any, None] = attrs.field(default=None)


def get_method_registration_options_type(
method_name, lsp_methods_map=METHOD_TO_TYPES
Expand Down
Loading

0 comments on commit 04875c5

Please sign in to comment.